LibLogicalAccess  2.5.0
An Open Source RFID Library
Loading...
Searching...
No Matches
logs.hpp
Go to the documentation of this file.
1
7#ifndef LOGICALACCESS_LOGS_HPP
8#define LOGICALACCESS_LOGS_HPP
9
13#undef EXCEPTION
14#define EXCEPTION(type, ...) \
15 type(/*__FUNCTION__ "," __FILE__ "," __LINE__ " : "*/ __VA_ARGS__)
16
20#define EXCEPTION_ASSERT(condition, type, msg) \
21 if (!(condition)) \
22 { \
23 throw EXCEPTION(type, msg); \
24 }
25
26#include <logicalaccess/plugins/llacommon/lla_common_api.hpp>
28#include <fstream>
29#include <map>
30#include <sstream>
31#include <vector>
32#include <array>
33#include <cstdint>
34
35namespace logicalaccess
36{
38{
39 NONE = 0,
52};
53
57LLA_COMMON_API std::ostream &operator<<(std::ostream &ss, const ByteVector &bytebuff);
58
62LLA_COMMON_API std::ostream &operator<<(std::ostream &ss,
63 const std::vector<bool> &bytebuff);
64
68template <long unsigned int Size>
69std::ostream &operator<<(std::ostream &ss, const std::array<uint8_t, Size> &bytearray)
70{
71 ss << ByteVector(bytearray.begin(), bytearray.end());
72 return ss;
73}
74
82class LLA_COMMON_API LogContext
83{
84 public:
85 explicit LogContext(const std::string &);
87};
88
89class LLA_COMMON_API Logs
90{
91 public:
92 Logs(const char *file, const char *func, int line, enum LogLevel level);
93
94 ~Logs();
95
96 template <class T>
97 Logs &operator<<(const T &arg)
98 {
99 _stream << arg;
100 return (*this);
101 }
102
103 static std::ofstream logfile;
104
109 static bool logToStderr;
110
111 private:
115 static std::string pretty_context_infos();
116
118 std::stringstream _stream;
119 static std::map<LogLevel, std::string> logLevelMsg;
120};
121
129struct LLA_COMMON_API LogDisabler
130{
131 LogDisabler();
132 ~LogDisabler();
133
134 private:
135 bool old_;
136};
137
138#ifdef SWIG
139// Swig seems to be lost wrt variadic template function trace_print_helper()
140// and believe there are multiple definition. Possibly, it does not properly
141// understand SFINAE.
142// Therefore we disable the warning.
143#pragma SWIG nowarn=302
144#endif
145
149LLA_COMMON_API std::ostream &operator<<(std::ostream &ss, const ByteVector &bytebuff);
150
154LLA_COMMON_API std::ostream &operator<<(std::ostream &ss,
155 const std::vector<bool> &bytebuff);
156
157LLA_COMMON_API std::ostream &operator<<(std::ostream &ss,
158 const std::vector<bool> &bytebuff);
159
160#define LOG(x) logicalaccess::Logs(__FILE__, __FUNCTION__, __LINE__, x)
161
162LLA_COMMON_API void trace_print_helper(std::stringstream &ss, const char *param_names,
163 int idx);
164
165LLA_COMMON_API std::string get_nth_param_name(const char *param_names, int idx);
166
170template <typename Current, typename... T>
171typename std::enable_if<!std::is_same<
172 unsigned char, typename std::remove_reference<Current>::type>::value>::type
173trace_print_helper(std::stringstream &ss, const char *param_names, int idx, Current &&c,
174 T &&... args);
175
179template <typename Current, typename... T>
180typename std::enable_if<std::is_same<
181 unsigned char, typename std::remove_reference<Current>::type>::value>::type
182trace_print_helper(std::stringstream &ss, const char *param_names, int idx, Current &&c,
183 T &&... args);
184
185
186template <typename Current, typename... T>
187typename std::enable_if<std::is_same<
188 unsigned char, typename std::remove_reference<Current>::type>::value>::type
189trace_print_helper(std::stringstream &ss, const char *param_names, int idx, Current &&c,
190 T &&... args)
191{
192 ss << " [" << get_nth_param_name(param_names, idx) << " --> " << +c << "]";
193 trace_print_helper(ss, param_names, idx + 1, args...);
194}
195
196template <typename Current, typename... T>
197typename std::enable_if<!std::is_same<
198 unsigned char, typename std::remove_reference<Current>::type>::value>::type
199trace_print_helper(std::stringstream &ss, const char *param_names, int idx, Current &&c,
200 T &&... args)
201{
202 using namespace logicalaccess; // to access some custom ostream overload for vector.
203 ss << " [" << get_nth_param_name(param_names, idx) << " --> " << c << "]";
204 trace_print_helper(ss, param_names, idx + 1, args...);
205}
206
207template <typename... T>
208void trace_print(std::stringstream &ss, const char *param_names, T &&... params)
209{
210 trace_print_helper(ss, param_names, 0, params...);
211}
212
217#define TRACE(...) \
218 std::stringstream trace_stringstream; \
219 trace_print(trace_stringstream, #__VA_ARGS__, ##__VA_ARGS__); \
220 LOG(TRACE) << trace_stringstream.str();
221
222LLA_COMMON_API void trace_print_helper(std::stringstream &ss, const char *param_names,
223 int idx);
224
225LLA_COMMON_API std::string get_nth_param_name(const char *param_names, int idx);
226
227#define LLA_LOG_CTX(param) \
228 LogContext lla_log_ctx([&](void) { \
229 std::stringstream logger_macro_ss__; \
230 logger_macro_ss__ << param; \
231 return logger_macro_ss__.str(); \
232 }())
233
237#define THROW_EXCEPTION_WITH_LOG(type, msg, ...) \
238 { \
239 LOG(logicalaccess::LogLevel::ERRORS) << msg; \
240 throw EXCEPTION(type, msg, ##__VA_ARGS__); \
241 }
242
246#define EXCEPTION_ASSERT_WITH_LOG(condition, type, msg, ...) \
247 if (!(condition)) \
248 { \
249 THROW_EXCEPTION_WITH_LOG(type, msg, ##__VA_ARGS__); \
250 }
251}
252
253#endif
Definition: logs.hpp:83
Definition: logs.hpp:90
std::stringstream _stream
Definition: logs.hpp:118
Logs & operator<<(const T &arg)
Definition: logs.hpp:97
static bool logToStderr
Definition: logs.hpp:109
static std::map< LogLevel, std::string > logLevelMsg
Definition: logs.hpp:119
static std::ofstream logfile
Definition: logs.hpp:103
enum LogLevel d_level
Definition: logs.hpp:117
std::vector< uint8_t > ByteVector
Definition: lla_fwd.hpp:80
Definition: asn1.hpp:9
LogLevel
Definition: logs.hpp:38
@ WARNINGS
Definition: logs.hpp:42
@ ALERTS
Definition: logs.hpp:47
@ DEBUGS
Definition: logs.hpp:48
@ NONE
Definition: logs.hpp:39
@ COMS
Definition: logs.hpp:49
@ EMERGENSYS
Definition: logs.hpp:45
@ CRITICALS
Definition: logs.hpp:46
@ PLUGINS_ERROR
Definition: logs.hpp:51
@ NOTICES
Definition: logs.hpp:43
@ PLUGINS
Definition: logs.hpp:50
@ ERRORS
Definition: logs.hpp:44
@ TRACE
Definition: logs.hpp:40
@ INFOS
Definition: logs.hpp:41
std::string get_nth_param_name(const char *param_names, int idx)
Definition: logs.cpp:162
LLA_CORE_API std::ostream & operator<<(std::ostream &os, const Key &key)
Output to stream operator.
Definition: key.cpp:298
void trace_print(std::stringstream &ss, const char *param_names, T &&... params)
Definition: logs.hpp:208
void trace_print_helper(std::stringstream &ss, const char *param_names, int)
Definition: logs.cpp:186
Definition: logs.hpp:130
bool old_
Definition: logs.hpp:135