| @@ -70,6 +70,17 @@ namespace cpplogging | |||||
| */ | */ | ||||
| inline log_helper& sender(const void * sender); | inline log_helper& sender(const void * sender); | ||||
| /** | |||||
| * @brief Set the sender and the sender type of the log entry. | |||||
| */ | |||||
| template<typename T_sender> | |||||
| 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. | * @brief Set the filename of the log entry. | ||||
| */ | */ | ||||
| @@ -1,5 +1,6 @@ | |||||
| #pragma once | #pragma once | ||||
| #include <cxxabi.h> | |||||
| #include "logger.h" | #include "logger.h" | ||||
| namespace cpplogging | namespace cpplogging | ||||
| @@ -43,6 +44,24 @@ namespace cpplogging | |||||
| return *this; | return *this; | ||||
| } | } | ||||
| template<typename T_sender> | |||||
| 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 | log_helper& log_helper | ||||
| ::file(const char * file) | ::file(const char * file) | ||||
| { | { | ||||
| @@ -13,7 +13,7 @@ namespace cpplogging | |||||
| static const std::string value( | static const std::string value( | ||||
| "[${runtime:-016.6f}] " | "[${runtime:-016.6f}] " | ||||
| "${level:5S} " | "${level:5S} " | ||||
| "${sender:016x}@${thread:016x} " | |||||
| "${sender_type}(${sender:016x})@${thread:016x} " | |||||
| "${filename:-25}:${line:5d}$n" | "${filename:-25}:${line:5d}$n" | ||||
| "${message}"); | "${message}"); | ||||
| return value; | return value; | ||||
| @@ -27,13 +27,14 @@ namespace cpplogging | |||||
| using clock_type = std::chrono::steady_clock; | using clock_type = std::chrono::steady_clock; | ||||
| using uptime_type = clock_type::time_point; | using uptime_type = clock_type::time_point; | ||||
| log_level level { log_level::debug }; | |||||
| uptime_type uptime { clock_type::now() }; | |||||
| time_t systime { std::time(nullptr) }; | |||||
| const void* sender { nullptr }; | |||||
| std::thread::id thread { std::this_thread::get_id() }; | |||||
| const char* file { nullptr }; | |||||
| int line { 0 }; | |||||
| log_level level { log_level::debug }; | |||||
| uptime_type uptime { clock_type::now() }; | |||||
| time_t systime { std::time(nullptr) }; | |||||
| const void* sender { nullptr }; | |||||
| std::string sender_type { "void" }; | |||||
| std::thread::id thread { std::this_thread::get_id() }; | |||||
| const char* file { nullptr }; | |||||
| int line { 0 }; | |||||
| std::string name; | std::string name; | ||||
| std::string message; | std::string message; | ||||
| }; | }; | ||||
| @@ -29,6 +29,7 @@ enum class value_token | |||||
| systime, //!< unix timestamp | systime, //!< unix timestamp | ||||
| runtime, //!< runtime of the application | runtime, //!< runtime of the application | ||||
| sender, //!< sender of the log message | sender, //!< sender of the log message | ||||
| sender_type, //!< type of sender | |||||
| thread, //!< thread id | thread, //!< thread id | ||||
| filename, //!< filename only | filename, //!< filename only | ||||
| filepath, //!< whole filepath | filepath, //!< whole filepath | ||||
| @@ -106,18 +107,19 @@ struct invariant_string_map | |||||
| }; | }; | ||||
| static invariant_string_map value_token_map({ | static invariant_string_map value_token_map({ | ||||
| { "level", value_token::level }, | |||||
| { "uptime", value_token::uptime }, | |||||
| { "systime", value_token::systime }, | |||||
| { "runtime", value_token::runtime }, | |||||
| { "sender", value_token::sender }, | |||||
| { "thread", value_token::thread }, | |||||
| { "filename", value_token::filename }, | |||||
| { "filepath", value_token::filepath }, | |||||
| { "line", value_token::line }, | |||||
| { "name", value_token::name }, | |||||
| { "message", value_token::message }, | |||||
| { "newline", value_token::newline }, | |||||
| { "level", value_token::level }, | |||||
| { "uptime", value_token::uptime }, | |||||
| { "systime", value_token::systime }, | |||||
| { "runtime", value_token::runtime }, | |||||
| { "sender", value_token::sender }, | |||||
| { "sender_type", value_token::sender_type }, | |||||
| { "thread", value_token::thread }, | |||||
| { "filename", value_token::filename }, | |||||
| { "filepath", value_token::filepath }, | |||||
| { "line", value_token::line }, | |||||
| { "name", value_token::name }, | |||||
| { "message", value_token::message }, | |||||
| { "newline", value_token::newline }, | |||||
| }); | }); | ||||
| consumer::format_type consumer::parse_format(const std::string& format) | consumer::format_type consumer::parse_format(const std::string& format) | ||||
| @@ -576,6 +578,12 @@ void consumer::write_formatted(const log_entry& e, const format_type& f, std::os | |||||
| } | } | ||||
| break; | break; | ||||
| /* sender_type */ | |||||
| case value_token::sender_type: | |||||
| has_line_end = false; | |||||
| os << e.sender_type; | |||||
| break; | |||||
| /* thread */ | /* thread */ | ||||
| case value_token::thread: | case value_token::thread: | ||||
| has_line_end = false; | has_line_end = false; | ||||