ADTF
builds/digitalwerk/solutions/adtf_content/adtf_base/adtf_core/src/libraries/a_utils/include/a_utils/std/log.h
Go to the documentation of this file.
1 
7 #ifndef _LOG_FUNCTIONS_HEADER_
8 #define _LOG_FUNCTIONS_HEADER_
9 
10 namespace A_UTILS_NS
11 {
15 namespace log
16 {
17 
22 {
23  None = 0,
24  Error = 10,
25  Warning = 20,
26  Info = 30,
27  Detail = 35,
28  Dump = 40,
29  Debug = Dump,
30  All = 0xFF
31 };
32 
36 struct tLogEntry
37 {
40  const tChar* strMessage;
41  const tChar* strSource;
42 };
43 
48 tVoid add_entry(const tLogEntry& sEntry);
49 
57 inline tVoid add_entry(tUInt8 nLogLevel,
58  const tChar* strMessage = nullptr,
59  const tChar* strSource = nullptr)
60 {
61  add_entry({cDateTime::GetCurrentDateTime().ToTimeStamp(), nLogLevel, strMessage, strSource});
62 }
63 
67 typedef std::function<tVoid(const tLogEntry& sEntry)> logger;
68 
74 
80 
87 
93 
99 inline tVoid set_filtered_logging(tUInt8 nMaxLogLevel, logger oLogger = default_logger)
100 {
101  set_logger([=](const tLogEntry& sEntry)
102  {
103  if (sEntry.nLogLevel <= nMaxLogLevel)
104  {
105  oLogger(sEntry);
106  }
107  });
108 }
109 
111 #define _A_UTILS_STRINGIFY(__number) #__number
113 #define _A_UTILS_TO_STRING(__number) _A_UTILS_STRINGIFY(__number)
115 #define LOG_ADD_ENTRY(__level, ...)\
116  A_UTILS_NS::log::add_entry(__level, \
117  A_UTILS_NS::cString::Format(__VA_ARGS__).GetPtr(),\
118  A_UTILS_NS::cString::Format("%s:%d(%s)", __FILE__, __LINE__, __FUNC__).GetPtr())
119 
120 #ifdef _DEBUG
122 #define LOG_DUMP(...) LOG_ADD_ENTRY(A_UTILS_NS::log::tLogLevel::Dump, __VA_ARGS__)
123 #else
125 #define LOG_DUMP(...)
126 #endif
128 #define LOG_DETAIL(...) LOG_ADD_ENTRY(A_UTILS_NS::log::tLogLevel::Detail, __VA_ARGS__)
130 #define LOG_INFO(...) LOG_ADD_ENTRY(A_UTILS_NS::log::tLogLevel::Info, __VA_ARGS__)
132 #define LOG_WARNING(...) LOG_ADD_ENTRY(A_UTILS_NS::log::tLogLevel::Warning, __VA_ARGS__)
134 #define LOG_ERROR(...) LOG_ADD_ENTRY(A_UTILS_NS::log::tLogLevel::Error, __VA_ARGS__)
135 
136 }
137 
138 } // namespace A_UTILS_NS
139 
140 
141 #endif // _LOG_FUNCTIONS_HEADER_
uint8_t tUInt8
type definition for unsigned integer values (8bit) (platform and compiler independent type).
char tChar
The tChar defines the type for platform character set (platform and compiler dependent type).
void tVoid
The tVoid is always the definition for the void (non-type).
static cDateTime GetCurrentDateTime()
Get the current date and time (hardware clock with respect to timezone).
tTimeStamp ToTimeStamp() const
Convert stored date and time to a timestamp.
tVoid set_filtered_logging(tUInt8 nMaxLogLevel, logger oLogger=default_logger)
Convenience method to filter log messages.
tVoid set_logger(logger oLogger)
Sets the currently used logger.
cString default_format(const tLogEntry &sEntry)
Returns the default string representation of a log entry.
std::function< tVoid(const tLogEntry &sEntry)> logger
Logger interface definition.
tVoid default_logger(const tLogEntry &sEntry)
Default logging method, that writes log messages to stdout.
logger get_logger()
Returns the currently used logger.
tVoid add_entry(const tLogEntry &sEntry)
Adds a new log entry to the current logger.
ADTF A_UTIL Namespace - Within adtf this is used as adtf::util or adtf_util.
Definition: d_ptr.h:11
A log entry.
const tChar * strMessage
the message text, optionally nullptr.
tUInt8 nLogLevel
the log level, see tLogLevel.
tTimeStamp nTimeStamp
time stamp of the log message.
const tChar * strSource
the origin of the entry, optionally nullptr.