| 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 |
// Local |
| 16 |
|
| 17 |
#include "ConsoleAppender.h" |
| 18 |
|
| 19 |
#include <spdlog/spdlog.h> |
| 20 |
#include <spdlog/spdlog-inl.h> |
| 21 |
#include <spdlog/details/registry.h> |
| 22 |
#include <spdlog/details/registry-inl.h> |
| 23 |
#include <spdlog/sinks/stdout_color_sinks.h> |
| 24 |
|
| 25 |
// STL |
| 26 |
#include <iostream> |
| 27 |
extern "C" |
| 28 |
{ |
| 29 |
#include <unistd.h> |
| 30 |
} |
| 31 |
|
| 32 |
DLOG_CORE_BEGIN_NAMESPACE |
| 33 |
|
| 34 |
/*! |
| 35 |
@~english |
| 36 |
@class Dtk::Core::ConsoleAppender |
| 37 |
@ingroup dlog |
| 38 |
|
| 39 |
@brief ConsoleAppender is the simple appender that writes the log records to the std::cerr output stream. |
| 40 |
|
| 41 |
ConsoleAppender uses "[%{type:-7}] <%{function}> %{message}\n" as a default output format. It is similar to the |
| 42 |
AbstractStringAppender but doesn't show a time. |
| 43 |
|
| 44 |
You can modify ConsoleAppender output format without modifying your code by using \c QT_MESSAGE_PATTERN environment |
| 45 |
variable. If you need your application to ignore this environment variable you can call |
| 46 |
ConsoleAppender::ignoreEnvironmentPattern(true) |
| 47 |
*/ |
| 48 |
|
| 49 |
ConsoleAppender::ConsoleAppender() |
| 50 |
: AbstractStringAppender(), m_ignoreEnvPattern(false) |
| 51 |
{ |
| 52 |
if (!spdlog::get("console")) |
| 53 |
{ |
| 54 |
auto clogger = spdlog::stdout_color_mt("console"); |
| 55 |
clogger->set_level(spdlog::level::level_enum(detailsLevel())); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
QString ConsoleAppender::format() const |
| 60 |
{ |
| 61 |
const QString envPattern = QString::fromLocal8Bit(qgetenv("QT_MESSAGE_PATTERN")); |
| 62 |
return (m_ignoreEnvPattern || envPattern.isEmpty()) ? AbstractStringAppender::format() : (envPattern + "\n"); |
| 63 |
} |
| 64 |
|
| 65 |
void ConsoleAppender::ignoreEnvironmentPattern(bool ignore) |
| 66 |
{ |
| 67 |
m_ignoreEnvPattern = ignore; |
| 68 |
} |
| 69 |
|
| 70 |
/*! |
| 71 |
@~english |
| 72 |
\brief Writes the log record to the std::cerr stream. |
| 73 |
\reimp |
| 74 |
|
| 75 |
The \a time parameter indicates the time stamp. |
| 76 |
The \a level parameter describes the LogLevel. |
| 77 |
The \a file parameter is the current file name. |
| 78 |
The \a line parameter indicates the number of lines to output. |
| 79 |
The \a func parameter indicates the function name to output. |
| 80 |
The \a category parameter indicates the log category. |
| 81 |
The \a msg parameter indicates the output message. |
| 82 |
|
| 83 |
\sa AbstractStringAppender::format() |
| 84 |
*/ |
| 85 |
void ConsoleAppender::append(const QDateTime &time, Logger::LogLevel level, const char *file, int line, |
| 86 |
const char *func, const QString &category, const QString &msg) |
| 87 |
{ |
| 88 |
auto clogger = spdlog::get("console"); |
| 89 |
if (Q_UNLIKELY(!clogger)) |
| 90 |
return; |
| 91 |
|
| 92 |
clogger->set_level(spdlog::level::level_enum(detailsLevel())); |
| 93 |
|
| 94 |
const auto &formatted = formattedString(time, level, file, line, func, category, msg, isatty(STDOUT_FILENO)); |
| 95 |
clogger->log(spdlog::level::level_enum(level), formatted.toStdString()); |
| 96 |
} |
| 97 |
|
| 98 |
DLOG_CORE_END_NAMESPACE |