| 1 |
/* |
| 2 |
Copyright (c) 2010 Boris Moiseev (cyberbobs at gmail dot com) |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU Lesser General Public License version 2.1 |
| 6 |
as published by the Free Software Foundation and appearing in the file |
| 7 |
LICENSE.LGPL included in the packaging of this file. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU Lesser General Public License for more details. |
| 13 |
*/ |
| 14 |
|
| 15 |
#include "AbstractStringAppender.h" |
| 16 |
|
| 17 |
#include <QRegularExpression> |
| 18 |
#include <QCoreApplication> |
| 19 |
#include <QThread> |
| 20 |
|
| 21 |
DLOG_CORE_BEGIN_NAMESPACE |
| 22 |
|
| 23 |
inline static QString formattedLevelWithColor(Logger::LogLevel level, QString &msg) |
| 24 |
{ |
| 25 |
switch (level) |
| 26 |
{ |
| 27 |
case Logger::Trace: |
| 28 |
return QString("\x1b[94m%1\x1b[0m").arg(msg); |
| 29 |
case Logger::Debug: |
| 30 |
return QString("\x1b[36m%1\x1b[0m").arg(msg); |
| 31 |
case Logger::Info: |
| 32 |
return QString("\x1b[32m%1\x1b[0m").arg(msg); |
| 33 |
case Logger::Warning: |
| 34 |
return QString("\x1b[33m%1\x1b[0m").arg(msg); |
| 35 |
case Logger::Error: |
| 36 |
return QString("\x1b[31m%1\x1b[0m").arg(msg); |
| 37 |
case Logger::Fatal: |
| 38 |
return QString("\x1b[35m%1\x1b[0m").arg(msg); |
| 39 |
default: |
| 40 |
break; |
| 41 |
} |
| 42 |
return msg; |
| 43 |
} |
| 44 |
|
| 45 |
/*! |
| 46 |
@~english |
| 47 |
\class Dtk::Core::AbstractStringAppender |
| 48 |
\inmodule dtkcore |
| 49 |
|
| 50 |
\brief The AbstractStringAppender class provides a convenient base for appenders working with plain text formatted |
| 51 |
logs. |
| 52 |
|
| 53 |
AbstractSringAppender is the simple extension of the AbstractAppender class providing the convenient way to create |
| 54 |
custom log appenders working with a plain text formatted log targets. |
| 55 |
|
| 56 |
It have the formattedString() protected function that formats the logging arguments according to a format set with |
| 57 |
setFormat(). |
| 58 |
|
| 59 |
This class can not be directly instantiated because it contains pure virtual function inherited from AbstractAppender |
| 60 |
class. |
| 61 |
|
| 62 |
For more detailed description of customizing the log output format see the documentation on the setFormat() function. |
| 63 |
*/ |
| 64 |
|
| 65 |
const char formattingMarker = '%'; |
| 66 |
|
| 67 |
/*! |
| 68 |
@~english |
| 69 |
\brief Constructs a new string appender object. |
| 70 |
*/ |
| 71 |
AbstractStringAppender::AbstractStringAppender() |
| 72 |
: m_format(QLatin1String("%{time}{yyyy-MM-ddTHH:mm:ss.zzz} [%{type:-7}] <%{function}> %{message}\n")) |
| 73 |
{ |
| 74 |
} |
| 75 |
|
| 76 |
/*! |
| 77 |
@~english |
| 78 |
\brief Returns the current log format string. |
| 79 |
|
| 80 |
The default format is set to "%{time}{yyyy-MM-ddTHH:mm:ss.zzz} [%{type:-7}] <%{function}> %{message}\n". You can set a different log record |
| 81 |
format using the setFormat() function. |
| 82 |
|
| 83 |
\sa setFormat(const QString&) |
| 84 |
*/ |
| 85 |
QString AbstractStringAppender::format() const |
| 86 |
{ |
| 87 |
QReadLocker locker(&m_formatLock); |
| 88 |
return m_format; |
| 89 |
} |
| 90 |
|
| 91 |
/*! |
| 92 |
@~english |
| 93 |
\brief Sets the logging format for writing strings to the log target with this appender. |
| 94 |
|
| 95 |
The string format seems to be very common to those developers who have used a standard sprintf function. |
| 96 |
|
| 97 |
Log output format is a simple QString with the special markers (starting with % sign) which will be replaced with |
| 98 |
it's internal meaning when writing a log record. |
| 99 |
|
| 100 |
Controlling marker begins with the percent sign (%) which is followed by the command inside {} brackets |
| 101 |
(the command describes, what will be put to log record instead of marker). |
| 102 |
Optional field width argument may be specified right after the command (through the colon symbol before the closing bracket) |
| 103 |
Some commands requires an additional formatting argument (in the second {} brackets). |
| 104 |
|
| 105 |
Field width argument works almost identically to the QString::arg() fieldWidth argument (and uses it |
| 106 |
internally). For example, "%{type:-7}" will be replaced with the left padded debug level of the message |
| 107 |
("Debug ") or something. For the more detailed description of it you may consider to look to the Qt |
| 108 |
Reference Documentation. |
| 109 |
|
| 110 |
Supported marker commands are: |
| 111 |
\list |
| 112 |
\li %{time} - timestamp. You may specify your custom timestamp \a format using the second {} brackets after the marker, |
| 113 |
\li timestamp \a format here will be similar to those used in QDateTime::toString() function. For example, |
| 114 |
\li "%{time}{dd-MM-yyyy, HH:mm}" may be replaced with "17-12-2010, 20:17" depending on current date and time. |
| 115 |
\li The default \a format used here is "HH:mm:ss.zzz". |
| 116 |
\li %{type} - Log level. Possible log levels are shown in the Logger::LogLevel enumerator. |
| 117 |
\li %{Type} - Uppercased log level. |
| 118 |
\li %{typeOne} - One letter log level. |
| 119 |
\li %{TypeOne} - One uppercase letter log level. |
| 120 |
\li %{File} - Full source file name (with path) of the file that requested log recording. Uses the __FILE__ preprocessor macro. |
| 121 |
\li %{file} - Short file name (with stripped path). |
| 122 |
\li %{line} - Line number in the source file. Uses the __LINE__ preprocessor macro. |
| 123 |
\li %{Function} - Name of function that called on of the LOG_* macros. Uses the Q_FUNC_INFO macro provided with Qt. |
| 124 |
\li %{function} - Similar to the %{Function}, but the function name is stripped using stripFunctionName |
| 125 |
\li %{message} - The log message sent by the caller. |
| 126 |
\li %{category} - The log category. |
| 127 |
\li %{appname} - Application name (returned by QCoreApplication::applicationName() function). |
| 128 |
\li %{pid} - Application pid (returned by QCoreApplication::applicationPid() function). |
| 129 |
\li %{threadid} - ID of current thread. |
| 130 |
\li %% - Convinient marker that is replaced with the single % mark. |
| 131 |
\endlist |
| 132 |
|
| 133 |
\note Format doesn't add '\\n' to the end of the \a format line. Please consider adding it manually. |
| 134 |
|
| 135 |
\sa format() |
| 136 |
\sa stripFunctionName() |
| 137 |
\sa Logger::LogLevel |
| 138 |
*/ |
| 139 |
void AbstractStringAppender::setFormat(const QString &format) |
| 140 |
{ |
| 141 |
QWriteLocker locker(&m_formatLock); |
| 142 |
m_format = format; |
| 143 |
} |
| 144 |
|
| 145 |
/*! |
| 146 |
@~english |
| 147 |
\brief Strips the long function signature (as added by Q_FUNC_INFO macro). |
| 148 |
|
| 149 |
The string processing drops the returning type, arguments and template parameters of function. It is definitely |
| 150 |
useful for enchancing the log output readability. |
| 151 |
|
| 152 |
The \a name parameter is the function name. |
| 153 |
|
| 154 |
\return stripped function name |
| 155 |
*/ |
| 156 |
QString AbstractStringAppender::stripFunctionName(const char *name) |
| 157 |
{ |
| 158 |
return QString::fromLatin1(qCleanupFuncinfo(name)); |
| 159 |
} |
| 160 |
|
| 161 |
// The function was backported from Qt5 sources (qlogging.h) |
| 162 |
QByteArray AbstractStringAppender::qCleanupFuncinfo(const char *name) |
| 163 |
{ |
| 164 |
QByteArray info(name); |
| 165 |
|
| 166 |
// Strip the function info down to the base function name |
| 167 |
// note that this throws away the template definitions, |
| 168 |
// the parameter types (overloads) and any const/volatile qualifiers. |
| 169 |
if (info.isEmpty()) |
| 170 |
return info; |
| 171 |
|
| 172 |
int pos; |
| 173 |
|
| 174 |
// skip trailing [with XXX] for templates (gcc) |
| 175 |
pos = info.size() - 1; |
| 176 |
if (info.endsWith(']')) |
| 177 |
{ |
| 178 |
while (--pos) |
| 179 |
{ |
| 180 |
if (info.at(pos) == '[') |
| 181 |
info.truncate(pos); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
bool hasLambda = false; |
| 186 |
QRegularExpression lambdaRegex("::<lambda\\(.*\\)>"); |
| 187 |
QRegularExpressionMatch match = lambdaRegex.match(QString::fromLatin1(info)); |
| 188 |
if (match.hasMatch()) |
| 189 |
{ |
| 190 |
hasLambda = true; |
| 191 |
info.remove(match.capturedStart(), match.capturedLength()); |
| 192 |
} |
| 193 |
|
| 194 |
// operator names with '(', ')', '<', '>' in it |
| 195 |
static const char operator_call[] = "operator()"; |
| 196 |
static const char operator_lessThan[] = "operator<"; |
| 197 |
static const char operator_greaterThan[] = "operator>"; |
| 198 |
static const char operator_lessThanEqual[] = "operator<="; |
| 199 |
static const char operator_greaterThanEqual[] = "operator>="; |
| 200 |
|
| 201 |
// canonize operator names |
| 202 |
info.replace("operator ", "operator"); |
| 203 |
|
| 204 |
// remove argument list |
| 205 |
Q_FOREVER |
| 206 |
{ |
| 207 |
int parencount = 0; |
| 208 |
pos = info.lastIndexOf(')'); |
| 209 |
if (pos == -1) |
| 210 |
{ |
| 211 |
// Don't know how to parse this function name |
| 212 |
return info; |
| 213 |
} |
| 214 |
|
| 215 |
// find the beginning of the argument list |
| 216 |
--pos; |
| 217 |
++parencount; |
| 218 |
while (pos && parencount) |
| 219 |
{ |
| 220 |
if (info.at(pos) == ')') |
| 221 |
++parencount; |
| 222 |
else if (info.at(pos) == '(') |
| 223 |
--parencount; |
| 224 |
--pos; |
| 225 |
} |
| 226 |
if (parencount != 0) |
| 227 |
return info; |
| 228 |
|
| 229 |
info.truncate(++pos); |
| 230 |
|
| 231 |
if (info.at(pos - 1) == ')') |
| 232 |
{ |
| 233 |
if (info.indexOf(operator_call) == pos - (int)strlen(operator_call)) |
| 234 |
break; |
| 235 |
|
| 236 |
// this function returns a pointer to a function |
| 237 |
// and we matched the arguments of the return type's parameter list |
| 238 |
// try again |
| 239 |
info.remove(0, info.indexOf('(')); |
| 240 |
info.chop(1); |
| 241 |
continue; |
| 242 |
} |
| 243 |
else |
| 244 |
{ |
| 245 |
break; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
if (hasLambda) |
| 250 |
info.append("::lambda"); |
| 251 |
|
| 252 |
// find the beginning of the function name |
| 253 |
int parencount = 0; |
| 254 |
int templatecount = 0; |
| 255 |
--pos; |
| 256 |
|
| 257 |
// make sure special characters in operator names are kept |
| 258 |
if (pos > -1) |
| 259 |
{ |
| 260 |
switch (info.at(pos)) |
| 261 |
{ |
| 262 |
case ')': |
| 263 |
if (info.indexOf(operator_call) == pos - (int)strlen(operator_call) + 1) |
| 264 |
pos -= 2; |
| 265 |
break; |
| 266 |
case '<': |
| 267 |
if (info.indexOf(operator_lessThan) == pos - (int)strlen(operator_lessThan) + 1) |
| 268 |
--pos; |
| 269 |
break; |
| 270 |
case '>': |
| 271 |
if (info.indexOf(operator_greaterThan) == pos - (int)strlen(operator_greaterThan) + 1) |
| 272 |
--pos; |
| 273 |
break; |
| 274 |
case '=': |
| 275 |
{ |
| 276 |
int operatorLength = (int)strlen(operator_lessThanEqual); |
| 277 |
if (info.indexOf(operator_lessThanEqual) == pos - operatorLength + 1) |
| 278 |
pos -= 2; |
| 279 |
else if (info.indexOf(operator_greaterThanEqual) == pos - operatorLength + 1) |
| 280 |
pos -= 2; |
| 281 |
break; |
| 282 |
} |
| 283 |
default: |
| 284 |
break; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
while (pos > -1) |
| 289 |
{ |
| 290 |
if (parencount < 0 || templatecount < 0) |
| 291 |
return info; |
| 292 |
|
| 293 |
char c = info.at(pos); |
| 294 |
if (c == ')') |
| 295 |
++parencount; |
| 296 |
else if (c == '(') |
| 297 |
--parencount; |
| 298 |
else if (c == '>') |
| 299 |
++templatecount; |
| 300 |
else if (c == '<') |
| 301 |
--templatecount; |
| 302 |
else if (c == ' ' && templatecount == 0 && parencount == 0) |
| 303 |
break; |
| 304 |
|
| 305 |
--pos; |
| 306 |
} |
| 307 |
info = info.mid(pos + 1); |
| 308 |
|
| 309 |
// remove trailing '*', '&' that are part of the return argument |
| 310 |
while ((info.at(0) == '*') || (info.at(0) == '&')) |
| 311 |
info = info.mid(1); |
| 312 |
|
| 313 |
// we have the full function name now. |
| 314 |
// clean up the templates |
| 315 |
while ((pos = info.lastIndexOf('>')) != -1) |
| 316 |
{ |
| 317 |
if (!info.contains('<')) |
| 318 |
break; |
| 319 |
|
| 320 |
// find the matching close |
| 321 |
int end = pos; |
| 322 |
templatecount = 1; |
| 323 |
--pos; |
| 324 |
while (pos && templatecount) |
| 325 |
{ |
| 326 |
const char c = info.at(pos); |
| 327 |
if (c == '>') |
| 328 |
++templatecount; |
| 329 |
else if (c == '<') |
| 330 |
--templatecount; |
| 331 |
--pos; |
| 332 |
} |
| 333 |
++pos; |
| 334 |
info.remove(pos, end - pos + 1); |
| 335 |
} |
| 336 |
|
| 337 |
return info; |
| 338 |
} |
| 339 |
|
| 340 |
QString AbstractStringAppender::formattedString(const QDateTime &time, Logger::LogLevel level, |
| 341 |
const char *file, int line, const char *func, |
| 342 |
const QString &category, const QString &msg) const |
| 343 |
{ |
| 344 |
return formattedString(time, level, file, line, func, category, msg, false); |
| 345 |
} |
| 346 |
|
| 347 |
/*! |
| 348 |
@~english |
| 349 |
\brief Returns the string to record to the logging target, formatted according to the format(). |
| 350 |
|
| 351 |
\a time The time stamp. |
| 352 |
The \a level parameter describes the LogLevel, and the \a file parameter is the current file name, |
| 353 |
and the \a line parameter indicates the number of lines to output. |
| 354 |
The \a func parameter indicates the function name to output. |
| 355 |
The \a category parameter indicates the log category. |
| 356 |
The \a msg parameter indicates the output message. |
| 357 |
The \a withcolor parameter indicates wether to add color to output |
| 358 |
|
| 359 |
\sa format() |
| 360 |
\sa setFormat(const QString&) |
| 361 |
*/ |
| 362 |
QString AbstractStringAppender::formattedString(const QDateTime &time, Logger::LogLevel level, |
| 363 |
const char *file, int line, const char *func, |
| 364 |
const QString &category, const QString &msg, |
| 365 |
bool withcolor) const |
| 366 |
{ |
| 367 |
QString f = format(); |
| 368 |
|
| 369 |
// dtkcore无法正确解析Qt的日志格式,dtk默认的日志格式并未和Qt统一,解析方式需要兼容两种不同的格式。 |
| 370 |
if (f.contains(QLatin1String("time "))) |
| 371 |
f.replace(f.indexOf(' ', f.indexOf(QLatin1String("time")) + QLatin1String("time").size()), 1, QLatin1String("}{")); |
| 372 |
|
| 373 |
const int size = f.size(); |
| 374 |
|
| 375 |
QString result; |
| 376 |
|
| 377 |
int i = 0; |
| 378 |
while (i < f.size()) |
| 379 |
{ |
| 380 |
QChar c = f.at(i); |
| 381 |
|
| 382 |
// We will silently ignore the broken % marker at the end of string |
| 383 |
if (c != QLatin1Char(formattingMarker) || (i + 2) >= size) |
| 384 |
{ |
| 385 |
result.append(c); |
| 386 |
} |
| 387 |
else |
| 388 |
{ |
| 389 |
i += 2; |
| 390 |
QChar currentChar = f.at(i); |
| 391 |
QString command; |
| 392 |
int fieldWidth = 0; |
| 393 |
|
| 394 |
if (currentChar.isLetter()) |
| 395 |
{ |
| 396 |
command.append(currentChar); |
| 397 |
int j = 1; |
| 398 |
while ((i + j) < size && f.at(i + j).isLetter()) |
| 399 |
{ |
| 400 |
command.append(f.at(i + j)); |
| 401 |
j++; |
| 402 |
} |
| 403 |
|
| 404 |
i += j; |
| 405 |
currentChar = f.at(i); |
| 406 |
|
| 407 |
// Check for the padding instruction |
| 408 |
if (currentChar == QLatin1Char(':')) |
| 409 |
{ |
| 410 |
currentChar = f.at(++i); |
| 411 |
if (currentChar.isDigit() || currentChar.category() == QChar::Punctuation_Dash) |
| 412 |
{ |
| 413 |
int j = 1; |
| 414 |
while ((i + j) < size && f.at(i + j).isDigit()) |
| 415 |
j++; |
| 416 |
|
| 417 |
fieldWidth = f.mid(i, j).toInt(); |
| 418 |
i += j; |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
// Log record chunk to insert instead of formatting instruction |
| 424 |
QString chunk; |
| 425 |
|
| 426 |
// Time stamp |
| 427 |
if (command == QLatin1String("time")) |
| 428 |
{ |
| 429 |
if (f.at(i + 1) == QLatin1Char('{')) |
| 430 |
{ |
| 431 |
int j = 1; |
| 432 |
while ((i + 2 + j) < size && f.at(i + 2 + j) != QLatin1Char('}')) |
| 433 |
j++; |
| 434 |
|
| 435 |
if ((i + 2 + j) < size) |
| 436 |
{ |
| 437 |
chunk = time.toString(f.mid(i + 2, j)); |
| 438 |
|
| 439 |
i += j; |
| 440 |
i += 2; |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
if (chunk.isNull()) |
| 445 |
chunk = time.toString(QLatin1String("HH:mm:ss.zzz")); |
| 446 |
} |
| 447 |
else if (command == QLatin1String("type")) |
| 448 |
{ |
| 449 |
// Log level |
| 450 |
chunk = Logger::levelToString(level); |
| 451 |
if (withcolor) |
| 452 |
{ |
| 453 |
chunk = formattedLevelWithColor(level, chunk); |
| 454 |
} |
| 455 |
} |
| 456 |
else if (command == QLatin1String("Type")) |
| 457 |
{ |
| 458 |
// Uppercased log level |
| 459 |
chunk = Logger::levelToString(level).toUpper(); |
| 460 |
if (withcolor) |
| 461 |
{ |
| 462 |
chunk = formattedLevelWithColor(level, chunk); |
| 463 |
} |
| 464 |
} |
| 465 |
else if (command == QLatin1String("typeOne")) |
| 466 |
{ |
| 467 |
// One letter log level |
| 468 |
chunk = Logger::levelToString(level).left(1).toLower(); |
| 469 |
if (withcolor) |
| 470 |
{ |
| 471 |
chunk = formattedLevelWithColor(level, chunk); |
| 472 |
} |
| 473 |
} |
| 474 |
else if (command == QLatin1String("TypeOne")) |
| 475 |
{ |
| 476 |
// One uppercase letter log level |
| 477 |
chunk = Logger::levelToString(level).left(1).toUpper(); |
| 478 |
if (withcolor) |
| 479 |
{ |
| 480 |
chunk = formattedLevelWithColor(level, chunk); |
| 481 |
} |
| 482 |
} |
| 483 |
else if (command == QLatin1String("File")) |
| 484 |
{ |
| 485 |
// Filename |
| 486 |
chunk = QLatin1String(file); |
| 487 |
} |
| 488 |
else if (command == QLatin1String("file")) |
| 489 |
{ |
| 490 |
// Filename without a path |
| 491 |
chunk = QString(QLatin1String(file)).section('/', -1); |
| 492 |
} |
| 493 |
else if (command == QLatin1String("line")) |
| 494 |
{ |
| 495 |
// Source line number |
| 496 |
chunk = QString::number(line); |
| 497 |
} |
| 498 |
else if (command == QLatin1String("Function")) |
| 499 |
{ |
| 500 |
// Function name, as returned by Q_FUNC_INFO |
| 501 |
chunk = QString::fromLatin1(func); |
| 502 |
} |
| 503 |
else if (command == QLatin1String("function")) |
| 504 |
{ |
| 505 |
// Stripped function name |
| 506 |
chunk = stripFunctionName(func); |
| 507 |
} |
| 508 |
else if (command == QLatin1String("message")) |
| 509 |
{ |
| 510 |
// Log message |
| 511 |
chunk = msg; |
| 512 |
} |
| 513 |
else if (command == QLatin1String("category")) |
| 514 |
{ |
| 515 |
// Log message |
| 516 |
chunk = category; |
| 517 |
} |
| 518 |
else if (command == QLatin1String("pid")) |
| 519 |
{ |
| 520 |
// Application pid |
| 521 |
chunk = QString::number(QCoreApplication::applicationPid()); |
| 522 |
} |
| 523 |
else if (command == QLatin1String("appname")) |
| 524 |
{ |
| 525 |
// Application name |
| 526 |
chunk = QCoreApplication::applicationName(); |
| 527 |
} |
| 528 |
else if (command == QLatin1String("threadid")) |
| 529 |
{ |
| 530 |
// Thread ID (duplicates Qt5 threadid debbuging way) |
| 531 |
chunk = QLatin1String("0x") + QString::number(qlonglong(QThread::currentThread()->currentThread()), 16); |
| 532 |
} |
| 533 |
else if (command == QString(formattingMarker)) |
| 534 |
{ |
| 535 |
// We simply replace the double formatting marker (%) with one |
| 536 |
chunk = QLatin1Char(formattingMarker); |
| 537 |
} |
| 538 |
else |
| 539 |
{ |
| 540 |
// Do not process any unknown commands |
| 541 |
chunk = QString(formattingMarker); |
| 542 |
chunk.append(command); |
| 543 |
} |
| 544 |
|
| 545 |
if (!chunk.isEmpty() && chunk != "0") |
| 546 |
{ |
| 547 |
result.append(QString(QLatin1String("%1")).arg(chunk, fieldWidth)); |
| 548 |
} |
| 549 |
} |
| 550 |
++i; |
| 551 |
} |
| 552 |
|
| 553 |
return result; |
| 554 |
} |
| 555 |
|
| 556 |
DLOG_CORE_END_NAMESPACE |