uatools 1.0.0-pre2
 
Loading...
Searching...
No Matches
logging.hpp
Go to the documentation of this file.
1
14
15#ifndef ESO_UATOOLS_PROTOCOL_OPEN62541_LOGGING_H_
16#define ESO_UATOOLS_PROTOCOL_OPEN62541_LOGGING_H_
17
18#include <cstdarg>
19#include <cstdio>
20#include <iostream>
21
23
24#ifdef UA_ENABLE_AMALGAMATION
25# include <open62541.h>
26#else
27# include <open62541/plugin/log.h>
28# include <open62541/config.h>
29#endif
30
32 static constexpr const char* LOGGER_NAME = "open62541";
33}
34
35static void CustomOpen62541Log(void* /*log_context*/,
36 UA_LogLevel level,
37 UA_LogCategory /*category*/,
38 const char* msg,
39 va_list args) {
40 char buffer[1024] = {};
41 auto buffer_size = sizeof(buffer);
42 auto ret_val = vsnprintf(buffer, buffer_size, msg, args);
43 if (ret_val < 0) {
44 std::cerr << "Logging message too big for the allocated buffer\n";
45 return;
46 }
47
48 // Route through the installed uatools logger. Until a logger is
49 // installed (HasLogger() == false), drop the message silently —
50 // open62541 may emit during early init before main() has had a
51 // chance to install a logger, and we don't want that to throw.
53 return;
54 }
55
56 switch (level) {
57 case UA_LOGLEVEL_TRACE: UATTRACE("[open62541] {}", buffer); break;
58 case UA_LOGLEVEL_DEBUG: UATDEBUG("[open62541] {}", buffer); break;
59 case UA_LOGLEVEL_INFO: UATINFO("[open62541] {}", buffer); break;
60 case UA_LOGLEVEL_WARNING: UATWARNING("[open62541] {}", buffer); break;
61 case UA_LOGLEVEL_ERROR: UATERROR("[open62541] {}", buffer); break;
62 case UA_LOGLEVEL_FATAL: UATERROR("[open62541][FATAL] {}", buffer); break;
63 default: UATINFO("[open62541] {}", buffer); break;
64 }
65}
66
67#if ((UA_OPEN62541_VER_MAJOR * 100 + UA_OPEN62541_VER_MINOR) * 100 + UA_OPEN62541_VER_PATCH) >= 10400
68// TODO this should probably be moved to an unnamed namespace inside
69// protocol/open62541/src/open62541.cpp on the next major release (API
70// breakage)
71[[maybe_unused]] static void CustomOpen62541LogClear(UA_Logger* /*logContext*/) {}
72
73[[deprecated("use CustomOpen62541LogClear(UA_Logger*)")]]
74#endif
75[[maybe_unused]] static void CustomOpen62541LogClear(void* /*logContext*/) {}
76
77// TODO this should probably be moved to an unnamed namespace inside
78// protocol/open62541/src/open62541.cpp on the next major release (API
79// breakage)
80[[maybe_unused]] static UA_Logger CustomLogger =
81{ CustomOpen62541Log, nullptr, CustomOpen62541LogClear };
82
83#endif // ESO_UATOOLS_PROTOCOL_OPEN62541_LOGGING_H_
uatools logging abstraction.
#define UATERROR(fmt_str,...)
Definition logger.hpp:220
#define UATWARNING(fmt_str,...)
Definition logger.hpp:217
#define UATTRACE(...)
Trace macro.
Definition logger.hpp:201
#define UATDEBUG(fmt_str,...)
Definition logger.hpp:211
#define UATINFO(fmt_str,...)
Definition logger.hpp:214
bool HasLogger() noexcept
true iff a logger has been installed.
Definition logger.cpp:73
Definition iDataChangeHandler.hpp:27