Debian Codegraph @

3rdparty/xspublic/xscommon/journaller.h

raw symbols SCIP
1
2 // Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions, and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions, and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the names of the copyright holders nor the names of their contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
26 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
28 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
29 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
30 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
31 //
32
33 #ifndef JOURNALLERBASE_H
34 #define JOURNALLERBASE_H
35
36 #include "journalloglevel.h"
37 #include "abstractadditionallogger.h"
38 #include <sstream>
39 #ifndef JL_NOTEMPLATE
40 #include <iomanip>
41 #endif
42 #include <xstypes/xsstring.h>
43 #include <memory>
44
45 class JournalFile;
46 class JournalThreader;
47 class Journaller
48 {
49 public:
50 Journaller(const XsString& pathfile, bool purge = true, JournalLogLevel initialLogLevel = JLL_Alert);
51 ~Journaller();
52
53 void log(JournalLogLevel level, const std::string& msg);
54 void writeCallstack(JournalLogLevel level);
55
56 void setLogLevel(JournalLogLevel level, bool writeLogLine = true);
57
58 //! \returns The log level for logging to file
59 inline JournalLogLevel logLevel() const { return m_level; }
60
61 /*! \brief Compares a log/debug level with a set one
62 \param level The log level to compare with
63 \returns True if a set log/debug level is equal or higher than the compared one
64 */
65 inline bool logLevel(JournalLogLevel level) const { return level >= m_level || level >= m_debugLevel; }
66
67 void setDebugLevel(JournalLogLevel level, bool writeLogLine = true);
68
69 //! \returns The log level for logging to debug output
70 inline JournalLogLevel debugLevel() const { return m_debugLevel; }
71
72 void setFlushLevel(JournalLogLevel level, bool writeLogLine = true);
73
74 //! \returns The flush level
75 inline JournalLogLevel flushLevel() const { return m_flushLevel; }
76
77 void writeFileHeader(const std::string& appName);
78 void setUseDateTime(bool yes);
79
80 void writeTime();
81 void writeThread();
82 void writeTag();
83 void writeLevel(JournalLogLevel level);
84 void writeMessage(const std::string& msg);
85 void flush();
86
87 const XsString filename() const;
88
89 void setTag(const std::string &tag);
90 std::string tag() const;
91
92 static void setAdditionalLogger(AbstractAdditionalLogger* additionalLogger);
93
94 //! \returns True if it has an additional logger
95 inline static bool hasAdditionalLogger() { return m_additionalLogger != nullptr; }
96
97 //! \returns The additional logger
98 inline static AbstractAdditionalLogger* additionalLogger() { return m_additionalLogger; }
99
100 static std::string tagFromFilename(const std::string &fn);
101 void moveLogFile(const XsString& pathfile, bool purge = true, bool eraseOld = true);
102 void moveLogs(Journaller* target, bool eraseOld = true);
103
104 private:
105 void init(XsString const& pathfile, bool purge);
106 void flushLine();
107
108 std::shared_ptr<JournalFile> m_file;
109 std::string m_tag;
110 std::string m_appName;
111 JournalLogLevel m_level;
112 JournalLogLevel m_debugLevel;
113 JournalLogLevel m_flushLevel;
114 std::shared_ptr<JournalThreader> m_threader;
115
116 bool m_useDateTime;
117
118 static AbstractAdditionalLogger* m_additionalLogger;
119
120 // no copying allowed
121 Journaller& operator = (Journaller const&) = delete;
122 Journaller(Journaller const&) = delete;
123 };
124
125 #if 1 && (defined(MSC_VER) || 1) // add exceptions to compilers here that do not (yet) support constexpr. These will fall back to the full path. If the first 1 is set to 0, no path stripping will be done in any case.
126 inline static constexpr char const* jlStrippedPath(char const* a, char const* b)
127 {
128 return (a[0] ? (a[0] == '/' || a[0] == '\\' ? jlStrippedPath(a+1, a+1) : jlStrippedPath(a+1, b) ) : b);
129 }
130 inline static constexpr char const* jlStrippedPathFile(char const* a)
131 {
132 return jlStrippedPath(a,a);
133 }
134 #define STRIPPEDFILE jlStrippedPathFile(__FILE__)
135 #else
136 #define STRIPPEDFILE __FILE__
137 #endif
138
139 #if !defined(JLNOLINEINFO)
140 #define JLGENERIC_LINEINFO STRIPPEDFILE << "(" << __LINE__ << ") "
141 #else
142 #define JLGENERIC_LINEINFO ""
143 #endif
144
145 #define JLGENERIC(journal, level, msg)\
146 do { /*lint --e{506}*/ \
147 if (journal && journal->logLevel(level)) \
148 { \
149 std::ostringstream os; \
150 os << JLGENERIC_LINEINFO << __FUNCTION__ << " " << msg; \
151 journal->log(level, os.str()); \
152 } \
153 if (Journaller::hasAdditionalLogger() && Journaller::additionalLogger()->logLevel(level)) \
154 { \
155 std::ostringstream os; \
156 os << msg; \
157 Journaller::additionalLogger()->log(level, STRIPPEDFILE, __LINE__, __FUNCTION__, os.str()); \
158 } \
159 } while(0)
160
161 #define JLGENERIC_NODEC(journal, level, msg)\
162 do { \
163 if (journal && journal->logLevel(level)) \
164 { \
165 std::ostringstream os; \
166 os << msg << '\n'; \
167 journal->writeMessage(os.str()); \
168 } \
169 if (Journaller::hasAdditionalLogger() && Journaller::additionalLogger()->logLevel(level)) \
170 { \
171 std::ostringstream os; \
172 os << msg << '\n'; \
173 Journaller::additionalLogger()->logNoDecoration(level, STRIPPEDFILE, __LINE__, __FUNCTION__, os.str()); \
174 } \
175 } while(0)
176
177 #if !defined(JLDEBUG)
178
179 #if JLDEF_BUILD > JLL_TRACE
180 #define JLTRACE(...) ((void)0)
181 #define JLTRACE_NODEC(...) ((void)0)
182 #else
183 #define JLTRACE(journal, msg) JLGENERIC(journal, JLL_Trace, msg)
184 #define JLTRACE_NODEC(journal, msg) JLGENERIC_NODEC(journal, JLL_Trace, msg)
185 #endif
186
187 #if JLDEF_BUILD > JLL_DEBUG
188 #define JLDEBUG(...) ((void)0)
189 #define JLDEBUG_NODEC(...) ((void)0)
190 #else
191 #define JLDEBUG(journal, msg) JLGENERIC(journal, JLL_Debug, msg)
192 #define JLDEBUG_NODEC(journal, msg) JLGENERIC_NODEC(journal, JLL_Debug, msg)
193 #endif
194
195 #if JLDEF_BUILD > JLL_ALERT
196 #define JLALERT(...) ((void)0)
197 #define JLALERT_NODEC(...) ((void)0)
198 #else
199 #define JLALERT(journal, msg) JLGENERIC(journal, JLL_Alert, msg)
200 #define JLALERT_NODEC(journal, msg) JLGENERIC_NODEC(journal, JLL_Alert, msg)
201 #endif
202
203 #if JLDEF_BUILD > JLL_ERROR
204 #define JLERROR(...) ((void)0)
205 #define JLERROR_NODEC(...) ((void)0)
206 #else
207 #define JLERROR(journal, msg) JLGENERIC(journal, JLL_Error, msg)
208 #define JLERROR_NODEC(journal, msg) JLGENERIC_NODEC(journal, JLL_Error, msg)
209 #endif
210
211 #if JLDEF_BUILD > JLL_FATAL
212 #define JLFATAL(...) ((void)0)
213 #define JLFATAL_NODEC(...) ((void)0)
214 #else
215 #define JLFATAL(journal, msg) JLGENERIC(journal, JLL_Fatal, msg)
216 #define JLFATAL_NODEC(journal, msg) JLGENERIC_NODEC(journal, JLL_Fatal, msg)
217 #endif
218
219 #if JLDEF_BUILD > JLL_WRITE
220 #define JLWRITE(...) ((void)0)
221 #define JLWRITE_NODEC(...) ((void)0)
222 #else
223 #define JLWRITE(journal, msg) JLGENERIC(journal, JLL_Write, msg)
224 #define JLWRITE_NODEC(journal, msg) JLGENERIC_NODEC(journal, JLL_Write, msg)
225 #endif
226
227 // some convenience macros, since we almost always use a global gJournal Journaller
228 #define JLTRACEG(msg) JLTRACE(gJournal, msg)
229 #define JLDEBUGG(msg) JLDEBUG(gJournal, msg)
230 #define JLALERTG(msg) JLALERT(gJournal, msg)
231 #define JLERRORG(msg) JLERROR(gJournal, msg)
232 #define JLFATALG(msg) JLFATAL(gJournal, msg)
233 #define JLWRITEG(msg) JLWRITE(gJournal, msg)
234
235 // these can be used to log the final result of a value when leaving the function.
236 // use JLWRITEFINALG(myvar); or JLDEBUGFINALG(myvar); at the start of your function
237 #define JLFINALNAME(a) #a ": "
238 #define JLFINALVALUE(journal, level, a) JournalValueJanitor<decltype(a)> jlFinalValue ## a(journal, a, [](char const* fi, char const* fu, char const* va) { std::stringstream os; os << fi << " " << fu << " exit " << va; return os.str(); }(__FILE__, __FUNCTION__, JLFINALNAME(a)), level, true)
239 #define JLWRITEFINAL(journal, a) JLFINALVALUE(journal, JLL_Write, a)
240 #define JLWRITEFINALG(a) JLWRITEFINAL(gJournal, a)
241 #define JLDEBUGFINAL(journal, a) JLFINALVALUE(journal, JLL_Debug, a)
242 #define JLDEBUGFINALG(a) JLDEBUGFINAL(gJournal, a)
243
244 #endif
245
246 #define JLIF(journal, level, todo) do { if(journal && journal->logLevel(level)) { todo; } } while(0)
247
248 #ifndef JL_NOTEMPLATE
249
250 /*! \class JlHexLogger
251 \brief A support class of journaller that is used for the logging of hex values
252 */
253 template <typename T>
254 class JlHexLogger {
255 public:
256 T m_value; //!< A hex value
257
258 /*! \brief Constructor
259 */
260 explicit JlHexLogger(T i)
261 : m_value(i)
262 {
263 }
264 };
265
266 template <typename T>
267 std::ostream& operator << (std::ostream& os, JlHexLogger<T> const& hex)
268 {
269 std::ios_base::fmtflags f = os.flags();
270 os << std::hex << std::uppercase << hex.m_value << std::setiosflags(f);
271 return os;
272 }
273 template <> std::ostream& operator << (std::ostream& os, JlHexLogger<char> const& hex);
274
275 #define JLHEXLOG_BARE(d) JlHexLogger<decltype(d)>(d) // C++0x11 only!
276 #else
277 #define JLHEXLOG_BARE(d) std::hex << std::uppercase << (int) d << std::nouppercase << std::dec
278 #endif
279 #define JLHEXLOG(d) "0x" << JLHEXLOG_BARE(d)
280 #define JLVARLOG(a) #a ": " << a << " "
281
282 #define JLPRECISE2(msg, prec)\
283 [&](){\
284 std::ostringstream os; \
285 os.precision(prec); \
286 os << msg; \
287 return os.str();\
288 }()
289 #define JLPRECISE(msg) JLPRECISE2(msg, 16)
290
291 #define JLCASE2(s, a, b) case a: s << b << "(" << static_cast<int>(a) << ")"; break;
292 #define JLCASE(s, a) case a: s << #a << "(" << static_cast<int>(a) << ")"; break;
293 #define JLDEFAULTCASE(s) default: s << "Unknown case: " << static_cast<int>(e); break;
294 #define JLENUMEXPPROTO(E) std::ostream& operator << (std::ostream& dbg, E const& e)
295 #define JLENUMEXPHDR(E, ...) /*! \brief Translate \a e into a text representation */ JLENUMEXPPROTO(E) { __VA_ARGS__ switch(e) {
296 #define JLENUMCASE(a) JLCASE(dbg, a)
297 #define JLENUMCASE2(a, b) JLCASE2(dbg, a, b)
298 #define JLENUMEXPFTR(...) JLDEFAULTCASE(dbg) } __VA_ARGS__ return dbg; }
299 /*! Use this macro to define enum expansion to a text stream. supply the type as parameter \a E and all
300 enum values you want to expand as a sequence of JLENUMCASE(item) (no commas) */
301 #define JLENUMEXPANDER(E, items) JLENUMEXPHDR(E) items JLENUMEXPFTR()
302
303 /*! Use this macro to define enum expansion to a text stream. supply the type as parameter \a E and all
304 enum values you want to expand as a sequence of JLENUMCASE(item) (no commas) */
305 #define JLENUMEXPANDERHEX(E, items) JLENUMEXPHDR(E, dbg << std::hex << std::uppercase;) items JLENUMEXPFTR(dbg << std::dec << std::nouppercase;)
306
307 #define JLQTDEBUGHANDLER \
308 QtMessageHandler gOldQtMessageHandler = nullptr; \
309 Journaller* qtJournal = nullptr; \
310 QtMessageHandler qtChecker = nullptr; \
311 void jlQtMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) {\
312 if (qtChecker) qtChecker(type, context, msg); \
313 if (!qtJournal) return;\
314 switch (type) {\
315 case QtDebugMsg:\
316 JLDEBUG(qtJournal, (const char *)msg.toLatin1());\
317 break;\
318 case QtWarningMsg:\
319 JLALERT(qtJournal, (const char *)msg.toLatin1());\
320 break;\
321 case QtCriticalMsg:\
322 JLERROR(qtJournal, (const char *)msg.toLatin1());\
323 break;\
324 case QtFatalMsg:\
325 JLFATAL(qtJournal, (const char *)msg.toLatin1());\
326 abort();\
327 default:\
328 break;\
329 }\
330 if (gOldQtMessageHandler)\
331 gOldQtMessageHandler(type,context,msg);\
332 }
333
334 #define JLINSTALLQTDEBUGHANDLER(journal) \
335 qtJournal = journal;\
336 if (journal) {\
337 if (gOldQtMessageHandler == NULL) gOldQtMessageHandler = qInstallMessageHandler(jlQtMessageHandler);\
338 else qInstallMessageHandler(jlQtMessageHandler);\
339 } else if (gOldQtMessageHandler) {\
340 qInstallMessageHandler(gOldQtMessageHandler);\
341 gOldQtMessageHandler = NULL;\
342 }
343
344 //////////////////////////////////////////////////////////////////////////////////////////
345 /*! \class JournalValueJanitor
346 \brief Class for journalling a referenced value when the janitor leaves scope
347 \details The class stores a reference to the supplied object and uses streaming to output
348 it to the journal. Like all journalling this means that for the supplied type T
349 std::stringstream& std::stringstream::operator << (std::stringstream&, const T& value) must
350 be defined.
351 */
352 template <typename T = int>
353 class JournalValueJanitor {
354 private:
355 const JournalValueJanitor& operator = (const JournalValueJanitor&);
356
357 Journaller* m_journal;
358 const T& m_value;
359 std::string m_msg;
360 JournalLogLevel m_level;
361 public:
362 bool m_enabled; //!< Boolean value if set to true then the janitor is enabled
363
364 /*! \brief Constructor
365 */
366 JournalValueJanitor<T>(Journaller* journal, const T& value, const std::string& msg = std::string(), JournalLogLevel level = JLL_Debug, bool enabled = true)
367 : m_journal(journal)
368 , m_value(value)
369 , m_msg(msg)
370 , m_level(level)
371 , m_enabled(enabled && journal)
372 {}
373
374 /*! \brief Destructor
375 */
376 ~JournalValueJanitor()
377 {
378 if (m_enabled)
379 {
380 std::stringstream os;
381 os << m_msg << m_value;
382 m_journal->log(m_level, os.str());
383 }
384 }
385 };
386
387 void jlTerminate(Journaller** gj);
388
389 #endif