Merge pull request #37 from KjellKod/filter

preparation for g3sinks: Filter
This commit is contained in:
Kjell Hedstrom 2015-08-19 08:51:18 -06:00
commit bc4459d779
5 changed files with 210 additions and 129 deletions

View File

@ -23,6 +23,7 @@
#endif #endif
#include <string> #include <string>
#include <algorithm>
// Levels for logging, made so that it would be easy to change, remove, add levels -- KjellKod // Levels for logging, made so that it would be easy to change, remove, add levels -- KjellKod
@ -33,12 +34,29 @@ struct LEVELS {
LEVELS(const LEVELS& other): value(other.value), text(other.text.c_str()) {} LEVELS(const LEVELS& other): value(other.value), text(other.text.c_str()) {}
LEVELS(int id, const char* idtext) : value(id), text(idtext) {} LEVELS(int id, const char* idtext) : value(id), text(idtext) {}
friend bool operator==(const LEVELS &lhs, const LEVELS &rhs) { bool operator==(const LEVELS& rhs) const {
return (lhs.value == rhs.value && lhs.text == rhs.text); return (value == rhs.value && text == rhs.text);
} }
const int value; bool operator!=(const LEVELS& rhs) const {
const std::string text; return (value != rhs.value || text != rhs.text);
}
friend void swap(LEVELS& first, LEVELS& second) {
using std::swap;
swap(first.value, second.value);
swap(first.text, second.text);
}
LEVELS& operator=(LEVELS other) {
swap(*this, other);
return *this;
}
int value;
std::string text;
}; };

View File

@ -74,10 +74,13 @@ namespace g3 {
} }
LogMessage(const std::string &file, const int line, const std::string &function, const LEVELS &level); LogMessage& operator=(LogMessage other);
explicit LogMessage(const std::string &fatalOsSignalCrashMessage);
LogMessage(const LogMessage &);
LogMessage(const std::string& file, const int line, const std::string& function, const LEVELS& level);
explicit LogMessage(const std::string& fatalOsSignalCrashMessage);
LogMessage(const LogMessage& other);
LogMessage(LogMessage&& other); LogMessage(LogMessage&& other);
virtual ~LogMessage() {} virtual ~LogMessage() {}
@ -94,6 +97,23 @@ namespace g3 {
LEVELS _level; LEVELS _level;
std::string _expression; // only with content for CHECK(...) calls std::string _expression; // only with content for CHECK(...) calls
mutable std::string _message; mutable std::string _message;
friend void swap(LogMessage& first, LogMessage& second) {
// enable ADL (not necessary in our case, but good practice)
using std::swap;
swap(first._timestamp, second._timestamp);
swap(first._call_thread_id, second._call_thread_id);
swap(first._microseconds, second._microseconds);
swap(first._file, second._file);
swap(first._line, second._line);
swap(first._function, second._function);
swap(first._level, second._level);
swap(first._expression, second._expression);
swap(first._message, second._message);
}
}; };

View File

@ -10,7 +10,7 @@
#include "g3log/crashhandler.hpp" #include "g3log/crashhandler.hpp"
#include "g3log/time.hpp" #include "g3log/time.hpp"
#include "g3log/std2_make_unique.hpp" #include "g3log/std2_make_unique.hpp"
#include <algorithm>
#include <mutex> #include <mutex>
namespace { namespace {
@ -128,6 +128,12 @@ namespace g3 {
} }
// By copy, not by reference. See this explanation for details:
// http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom
LogMessage& LogMessage::operator=(LogMessage other) {
swap(*this, other);
return *this;
}
LogMessage::LogMessage(const std::string& file, const int line, LogMessage::LogMessage(const std::string& file, const int line,
@ -156,11 +162,9 @@ namespace g3 {
, _function(other._function) , _function(other._function)
, _level(other._level) , _level(other._level)
, _expression(other._expression) , _expression(other._expression)
, _message(other._message) , _message(other._message) {
{
} }
LogMessage::LogMessage(LogMessage &&other) LogMessage::LogMessage(LogMessage &&other)
: _timestamp(other._timestamp) : _timestamp(other._timestamp)
, _call_thread_id(other._call_thread_id) , _call_thread_id(other._call_thread_id)
@ -174,6 +178,7 @@ namespace g3 {
} }
std::string LogMessage::threadID() const { std::string LogMessage::threadID() const {
std::ostringstream oss; std::ostringstream oss;
oss << _call_thread_id; oss << _call_thread_id;

View File

@ -18,6 +18,8 @@
#include <thread> #include <thread>
#include <chrono> #include <chrono>
#include <exception> #include <exception>
#include <algorithm>
namespace { namespace {
const std::string log_directory = "./"; const std::string log_directory = "./";
const std::string t_info = "test INFO "; const std::string t_info = "test INFO ";
@ -112,6 +114,41 @@ TEST(Initialization, No_Logger_Initialized___Expecting_LOG_calls_to_be_Still_OKi
} }
#endif // #ifdef G3_DYNAMIC_LOGGING #endif // #ifdef G3_DYNAMIC_LOGGING
TEST(Basics, Levels_StdFind) {
std::vector<LEVELS> levels = {INFO, WARNING, FATAL};
auto info = INFO;
auto warning = WARNING;
auto debug = DEBUG;
auto found_info = std::find(levels.begin(), levels.end(), info);
EXPECT_TRUE(found_info != levels.end());
bool wasFound = (levels.end() != std::find(levels.begin(), levels.end(), info));
EXPECT_TRUE(wasFound);
auto wasNotFound = (levels.end() == std::find(levels.begin(), levels.end(), debug));
EXPECT_TRUE(wasNotFound);
auto foundWarningIterator = std::find(levels.begin(), levels.end(), WARNING);
EXPECT_TRUE(foundWarningIterator != levels.end());
foundWarningIterator = std::find(levels.begin(), levels.end(), warning);
EXPECT_TRUE(foundWarningIterator != levels.end());
auto wasNotFoundIterator = std::find(levels.begin(), levels.end(), DEBUG);
EXPECT_FALSE(wasNotFoundIterator != levels.end());
}
TEST(Basics, Levels_Operator) {
auto info = INFO;
auto warning = WARNING;
EXPECT_NE(INFO, WARNING);
EXPECT_EQ(info, INFO);
EXPECT_TRUE(INFO == INFO);
EXPECT_FALSE(info == WARNING);
EXPECT_TRUE(info != WARNING);
EXPECT_FALSE(info != INFO);
}
TEST(Basics, Shutdown) { TEST(Basics, Shutdown) {
std::string file_content; std::string file_content;
@ -285,7 +322,8 @@ TEST(LogTest, LOG_preFatalLogging_hook) {
logger.reset(); logger.reset();
EXPECT_EQ(g_fatal_counter.load(), size_t{1}); EXPECT_EQ(g_fatal_counter.load(), size_t{1});
} }
{ // Now with no fatal pre-logging-hook {
// Now with no fatal pre-logging-hook
RestoreFileLogger logger(log_directory); RestoreFileLogger logger(log_directory);
ASSERT_FALSE(mockFatalWasCalled()); ASSERT_FALSE(mockFatalWasCalled());
g_fatal_counter.store(0); g_fatal_counter.store(0);