diff --git a/include/cpplogging/interface/global.h b/include/cpplogging/interface/global.h index b3fade8..777c495 100644 --- a/include/cpplogging/interface/global.h +++ b/include/cpplogging/interface/global.h @@ -5,6 +5,6 @@ #define cpplogging_global_log(p_level) \ if (::cpplogging::logger::get().is_enabled(::cpplogging::log_level::p_level)) \ ::cpplogging::logger::get().log() \ - .level(::cpplogging::log_level::p_level) \ - .file(__FILE__) \ - .line(__LINE__) + << ::cpplogging::level(::cpplogging::log_level::p_level) \ + << ::cpplogging::file(__FILE__) \ + << ::cpplogging::line(__LINE__) diff --git a/include/cpplogging/interface/iomanip.h b/include/cpplogging/interface/iomanip.h index da389ea..5c27a24 100644 --- a/include/cpplogging/interface/iomanip.h +++ b/include/cpplogging/interface/iomanip.h @@ -32,7 +32,7 @@ namespace cpplogging * @brief Set the sender of a log stream. */ template - inline auto sender(const T_sender * sender); + inline auto sender(const T_sender* sender); /** * @brief Set the sender type of a log stream. diff --git a/include/cpplogging/interface/iomanip.inl b/include/cpplogging/interface/iomanip.inl index f314469..de0f442 100644 --- a/include/cpplogging/interface/iomanip.inl +++ b/include/cpplogging/interface/iomanip.inl @@ -33,7 +33,7 @@ namespace cpplogging } template - auto sender(const T_sender * sender) + auto sender(const T_sender* sender) { return __impl::make_stream_setter([=](log_entry& e){ int status; diff --git a/include/cpplogging/interface/logger.h b/include/cpplogging/interface/logger.h index 2f06bf7..94da4f7 100644 --- a/include/cpplogging/interface/logger.h +++ b/include/cpplogging/interface/logger.h @@ -6,9 +6,9 @@ #define cpplogging_log(p_logger, p_level) \ if (p_logger.is_enabled(::cpplogging::log_level::p_level)) \ p_logger.log() \ - .level(::cpplogging::log_level::p_level) \ - .file(__FILE__) \ - .line(__LINE__) + << ::cpplogging::level(::cpplogging::log_level::p_level) \ + << ::cpplogging::file(__FILE__) \ + << ::cpplogging::line(__LINE__) namespace cpplogging { @@ -59,72 +59,6 @@ namespace cpplogging * @brief Destructor. Will finally write the log entry. */ inline ~log_helper(); - - /** - * @brief Set the log level of the entry, - */ - inline log_helper& level(log_level level); - - /** - * @brief Set the sender of the log entry. - */ - inline log_helper& sender(const void * sender); - - /** - * @brief Set the sender and the sender type of the log entry. - */ - template - inline log_helper& sender(const T_sender * sender); - - /** - * @brief Set the sender type of the log entry. - */ - inline log_helper& sender_type(const std::string& sender_type); - - /** - * @brief Set the filename of the log entry. - */ - inline log_helper& file(const char * file); - - /** - * @brief Set the line number of the log entry. - */ - inline log_helper& line(int line); - - /** - * @brief Set the message of the log entry. - */ - inline log_helper& message(const std::string& msg); - - /** - * @brief Add the passed string to the log message. - */ - inline log_helper& add_message(const std::string& msg); - - /** - * @brief Set the message of the entry using format string. - */ - template - inline log_helper& format(const std::string& fmt, T_args&&... args); - - /** - * @brief Add a message to the entry using format string. - */ - template - inline log_helper& add_format(const std::string& fmt, T_args&&... args); - - private: - /** - * @brief Format a string. - * - * @tparam T_args Arguments of the format string. - * - * @param[in] fmt Format string. - * @param[in] sz Size of the buffer to use for the formatting. - * @param[in] args Arguments of the format. - */ - template - static inline std::string format(const std::string& fmt, size_t sz, T_args&&... args); }; } diff --git a/include/cpplogging/interface/logger.inl b/include/cpplogging/interface/logger.inl index 9ee311d..c6c8932 100644 --- a/include/cpplogging/interface/logger.inl +++ b/include/cpplogging/interface/logger.inl @@ -27,96 +27,9 @@ namespace cpplogging ::~log_helper() { entry->message += str(); - logger.write_entry(entry); + if (entry.use_count() == 1) + logger.write_entry(entry); } - - log_helper& log_helper - ::level(log_level level) - { - entry->level = level; - return *this; - } - - log_helper& log_helper - ::sender(const void * sender) - { - entry->sender = sender; - return *this; - } - - template - log_helper& log_helper - ::sender(const T_sender * sender) - { - int status; - auto name = abi::__cxa_demangle(typeid(T_sender).name(), 0, 0, &status); - entry->sender_type = std::string(name ? name : typeid(T_sender).name()); - entry->sender = sender; - return *this; - } - - log_helper& log_helper - ::sender_type(const std::string& sender_type) - { - entry->sender_type = sender_type; - return *this; - } - - log_helper& log_helper - ::file(const char * file) - { - entry->file = file; - return *this; - } - - log_helper& log_helper - ::line(int line) - { - entry->line = line; - return *this; - } - - log_helper& log_helper - ::message(const std::string& msg) - { - entry->message = msg; - return *this; - } - - log_helper& log_helper - ::add_message(const std::string& msg) - { - entry->message += msg; - return *this; - } - - template - log_helper& log_helper - ::format(const std::string& fmt, T_args&&... args) - { - entry->message = format(fmt, 0x8000, std::forward(args)...); - return *this; - } - - template - log_helper& log_helper - ::add_format(const std::string& fmt, T_args&&... args) - { - entry->message += format(fmt, 0x8000, std::forward(args)...); - return *this; - } - - template - std::string log_helper - ::format(const std::string& fmt, size_t sz, T_args&&... args) - { - std::unique_ptr buff(static_cast(malloc(sz)), &free); - auto len = snprintf(buff.get(), sz, fmt.c_str(), std::forward(args)...); - if (len < 0) - throw std::runtime_error("unable to format string"); - return std::string(buff.get(), len); - } - } /* logger */ diff --git a/src/cpplogging/manager/matcher/matcher_regex.cpp b/src/cpplogging/manager/matcher/matcher_regex.cpp index ec6c86a..95f442c 100644 --- a/src/cpplogging/manager/matcher/matcher_regex.cpp +++ b/src/cpplogging/manager/matcher/matcher_regex.cpp @@ -8,7 +8,7 @@ matcher_regex::matcher_regex(const std::string regex, bool invert) { } bool matcher_regex::match(const logger& logger) const - { return !logger.name().empty() && std::regex_match(logger.name(), _regex) != _invert; } + { return std::regex_match(logger.name(), _regex) != _invert; } bool matcher_regex::match(const consumer& consumer) const - { return !consumer.name().empty() && std::regex_match(consumer.name(), _regex) != _invert; } + { return std::regex_match(consumer.name(), _regex) != _invert; } diff --git a/test/cpplogging/cpplogging_tests.cpp b/test/cpplogging/cpplogging_tests.cpp index 56a027a..4115f11 100644 --- a/test/cpplogging/cpplogging_tests.cpp +++ b/test/cpplogging/cpplogging_tests.cpp @@ -128,6 +128,33 @@ TEST(LoggingTests, log_base) cpplogging_log(l1, error).sender((void*)0x24).message("test2") << " error"; } +TEST(LoggingTests, log_inverted_regex) +{ + LoggingReset loggingReset; + StrictMock c0("consumer0"); + + EXPECT_CALL(c0, write_entry(MatchLogData( + log_level::debug, + (void*)0x12, + std::this_thread::get_id(), + std::string("logger2"), + std::string("test2")))); + + manager::define_rule( + std::make_unique("^logger[0-1]+$", true), + std::make_unique("consumer0"), + log_level::debug, + log_level::error); + + auto& l0 = logger::get("logger0"); + auto& l1 = logger::get("logger1"); + auto& l2 = logger::get("logger2"); + + cpplogging_log(l0, debug).sender((void*)0x10) << "test0"; + cpplogging_log(l1, debug).sender((void*)0x11) << "test1"; + cpplogging_log(l2, debug).sender((void*)0x12) << "test2"; +} + #ifdef CPPLOGGING_HAS_LOAD_CONFIG TEST(LoggingTests, load) {