| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 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 |
|
| 59 |
inline JournalLogLevel logLevel() const { return m_level; } |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 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 |
|
| 70 |
inline JournalLogLevel debugLevel() const { return m_debugLevel; } |
| 71 |
|
| 72 |
void setFlushLevel(JournalLogLevel level, bool writeLogLine = true); |
| 73 |
|
| 74 |
|
| 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 |
|
| 95 |
inline static bool hasAdditionalLogger() { return m_additionalLogger != nullptr; } |
| 96 |
|
| 97 |
|
| 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 |
|
| 121 |
Journaller& operator = (Journaller const&) = delete; |
| 122 |
Journaller(Journaller const&) = delete; |
| 123 |
}; |
| 124 |
|
| 125 |
#if 1 && (defined(MSC_VER) || 1) |
| 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 { \ |
| 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 |
|
| 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 |
|
| 236 |
|
| 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 |
|
| 251 |
|
| 252 |
|
| 253 |
template <typename T> |
| 254 |
class JlHexLogger { |
| 255 |
public: |
| 256 |
T m_value; |
| 257 |
|
| 258 |
|
| 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, ...) 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 |
|
| 300 |
|
| 301 |
#define JLENUMEXPANDER(E, items) JLENUMEXPHDR(E) items JLENUMEXPFTR() |
| 302 |
|
| 303 |
|
| 304 |
|
| 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 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 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; |
| 363 |
|
| 364 |
|
| 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 |
|
| 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 |