ADTF
Loading...
Searching...
No Matches
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#include <a_utils/core/result.h>
11#include <chrono>
12#include <functional>
13
15#define LOG_RESULT(code) \
16do\
17{ \
18 tResult _result = code; \
19 if (IS_OK(_result))\
20 {\
21 LOG_INFO("%s", A_UTILS_NS::detail::to_string(_result).c_str());\
22 } \
23 else \
24 {\
25 LOG_ERROR("%s", A_UTILS_NS::detail::to_string(_result).c_str());\
26 } \
27} \
28while(tFalse)
29
32#define RETURN_AND_LOG_ERROR(code) \
33do\
34{ \
35 tResult _result = code; \
36 LOG_RESULT(_result); \
37 return (_result); \
38}\
39while(tFalse)
40
43#define RETURN_AND_LOG_ERROR_STR(code, ...) \
44do\
45{ \
46 LOG_ERROR(__VA_ARGS__); \
47 return (code); \
48}\
49while(tFalse)
50
53#define RETURN_IF_FAILED_AND_LOG_ERROR_STR(code, ...) \
54do\
55{ \
56 tResult _outer_result = code; \
57 if (IS_FAILED(_outer_result)) \
58 {\
59 LOG_ERROR(__VA_ARGS__); \
60 LOG_RESULT(_outer_result);\
61 return _outer_result;\
62 } \
63}\
64while(tFalse)
65
66
67namespace A_UTILS_NS
68{
69
73namespace log
74{
75
80{
81 None = 0,
82 Error = 10,
83 Warning = 20,
84 Info = 30,
85 Detail = 35,
86 Dump = 40,
87 Debug = Dump,
88 All = 0xFF
89};
90
101
106tVoid add_entry(const tLogEntry& sEntry);
107
112
120inline tVoid add_entry(tUInt8 nLogLevel,
121 const tChar* strMessage = nullptr,
122 const tChar* strSource = nullptr)
123{
124 add_entry({log_get_current_date_time(), nLogLevel, strMessage, strSource});
125}
126
130typedef std::function<tVoid(const tLogEntry& sEntry)> logger;
131
137
143
149
156{
157 set_logger([=](const tLogEntry& sEntry)
158 {
159 if (sEntry.nLogLevel <= nMaxLogLevel)
160 {
161 oLogger(sEntry);
162 }
163 });
164}
165
167#define _A_UTILS_STRINGIFY(__number) #__number
169#define _A_UTILS_TO_STRING(__number) _A_UTILS_STRINGIFY(__number)
171#define LOG_ADD_ENTRY(__level, ...)\
172 A_UTILS_NS::log::add_entry(__level, \
173 A_UTILS_NS::detail::format(__VA_ARGS__).c_str(),\
174 A_UTILS_NS::detail::format("%s:%d(%s)", __FILE__, __LINE__, __FUNC__).c_str())
175
176#ifdef _DEBUG
178#define LOG_DUMP(...) LOG_ADD_ENTRY(A_UTILS_NS::log::tLogLevel::Dump, __VA_ARGS__)
179#else
181#define LOG_DUMP(...)
182#endif
184#define LOG_DETAIL(...) LOG_ADD_ENTRY(A_UTILS_NS::log::tLogLevel::Detail, __VA_ARGS__)
186#define LOG_INFO(...) LOG_ADD_ENTRY(A_UTILS_NS::log::tLogLevel::Info, __VA_ARGS__)
188#define LOG_WARNING(...) LOG_ADD_ENTRY(A_UTILS_NS::log::tLogLevel::Warning, __VA_ARGS__)
190#define LOG_ERROR(...) LOG_ADD_ENTRY(A_UTILS_NS::log::tLogLevel::Error, __VA_ARGS__)
191
192}
193
194} // namespace A_UTILS_NS
195
196
197#endif // _LOG_FUNCTIONS_HEADER_
unsigned char 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).
Copyright © Audi Electronics Venture GmbH.
tVoid set_logger(logger oLogger)
Sets the currently used logger.
tVoid default_logger(const tLogEntry &sEntry)
Default logging method, that writes log messages to stdout.
tTimeStamp log_get_current_date_time()
Internal helper to hide cDateTime::GetCurrentDateTime.
std::function< tVoid(const tLogEntry &sEntry)> logger
Logger interface definition.
logger get_logger()
Returns the currently used logger.
tVoid add_entry(const tLogEntry &sEntry)
Adds a new log entry to the current logger.
tVoid set_filtered_logging(tUInt8 nMaxLogLevel, logger oLogger=default_logger)
Convenience method to filter log messages.
ADTF A_UTIL Namespace - Within adtf this is used as 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.