Browse Source

* Added sender type property to log entry

master
bergmann 4 years ago
parent
commit
1fe1c16185
5 changed files with 59 additions and 20 deletions
  1. +11
    -0
      include/cpplogging/interface/logger.h
  2. +19
    -0
      include/cpplogging/interface/logger.inl
  3. +1
    -1
      include/cpplogging/manager/consumer/consumer.inl
  4. +8
    -7
      include/cpplogging/types.h
  5. +20
    -12
      src/cpplogging/manager/consumer/formatting.cpp

+ 11
- 0
include/cpplogging/interface/logger.h View File

@@ -70,6 +70,17 @@ namespace cpplogging
*/
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.
*/


+ 19
- 0
include/cpplogging/interface/logger.inl View File

@@ -1,5 +1,6 @@
#pragma once

#include <cxxabi.h>
#include "logger.h"

namespace cpplogging
@@ -43,6 +44,24 @@ namespace cpplogging
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
::file(const char * file)
{


+ 1
- 1
include/cpplogging/manager/consumer/consumer.inl View File

@@ -13,7 +13,7 @@ namespace cpplogging
static const std::string value(
"[${runtime:-016.6f}] "
"${level:5S} "
"${sender:016x}@${thread:016x} "
"${sender_type}(${sender:016x})@${thread:016x} "
"${filename:-25}:${line:5d}$n"
"${message}");
return value;


+ 8
- 7
include/cpplogging/types.h View File

@@ -27,13 +27,14 @@ namespace cpplogging
using clock_type = std::chrono::steady_clock;
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 message;
};


+ 20
- 12
src/cpplogging/manager/consumer/formatting.cpp View File

@@ -29,6 +29,7 @@ enum class value_token
systime, //!< unix timestamp
runtime, //!< runtime of the application
sender, //!< sender of the log message
sender_type, //!< type of sender
thread, //!< thread id
filename, //!< filename only
filepath, //!< whole filepath
@@ -106,18 +107,19 @@ struct invariant_string_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)
@@ -576,6 +578,12 @@ void consumer::write_formatted(const log_entry& e, const format_type& f, std::os
}
break;

/* sender_type */
case value_token::sender_type:
has_line_end = false;
os << e.sender_type;
break;

/* thread */
case value_token::thread:
has_line_end = false;


Loading…
Cancel
Save