Skip to content

Commit e4264bf

Browse files
authored
Colorized logs (#640)
1 parent bc366b5 commit e4264bf

File tree

3 files changed

+96
-33
lines changed

3 files changed

+96
-33
lines changed

Diff for: include/utils/Logger.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class Logger : public QObject
100100
static QMap<QString, Logger*> _loggerMap;
101101
static QAtomicInteger<int> GLOBAL_MIN_LOG_LEVEL;
102102
static QString _lastError;
103-
103+
static bool _hasConsole;
104104
const QString _name;
105105
const QString _appname;
106106
const bool _syslogEnabled;

Diff for: sources/hyperhdr/main.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#endif
1818

1919
#include <exception>
20+
#include <iostream>
2021

2122
#include <QCoreApplication>
2223
#include <QApplication>
@@ -150,10 +151,6 @@ int main(int argc, char** argv)
150151
{
151152
QStringList params;
152153

153-
// initialize main logger and set global log level
154-
Logger* log = Logger::getInstance("MAIN");
155-
Logger::setLogLevel(Logger::INFO);
156-
157154
// check if we are running already an instance
158155
// TODO Allow one session per user
159156
#ifdef _WIN32
@@ -216,8 +213,8 @@ int main(int argc, char** argv)
216213
{
217214
if (getProcessIdsByProcessName(processName).size() > 1)
218215
{
219-
Error(log, "The HyperHdr Daemon is already running, abort start");
220-
return 0;
216+
std::cerr << "The HyperHDR Daemon is already running, abort start";
217+
return 1;
221218
}
222219
}
223220
else
@@ -235,6 +232,10 @@ int main(int argc, char** argv)
235232
}
236233
#endif
237234

235+
// initialize main logger and set global log level
236+
Logger* log = Logger::getInstance("MAIN");
237+
Logger::setLogLevel(Logger::INFO);
238+
238239
int logLevelCheck = 0;
239240
if (parser.isSet(silentOption))
240241
{

Diff for: sources/utils/Logger.cpp

+88-26
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
#ifndef _WIN32
88
#include <syslog.h>
9+
#include <unistd.h>
910
#elif _WIN32
11+
#include <io.h>
1012
#include <windows.h>
1113
#include <Shlwapi.h>
1214
#pragma comment(lib, "Shlwapi.lib")
@@ -20,8 +22,9 @@
2022
#include <time.h>
2123

2224

23-
24-
QMutex Logger::_mapLock;
25+
QMutex Logger::_mapLock;
26+
QString Logger::_lastError;
27+
bool Logger::_hasConsole;
2528
QMap<QString, Logger*> Logger::_loggerMap;
2629
QAtomicInteger<int> Logger::GLOBAL_MIN_LOG_LEVEL{ static_cast<int>(Logger::UNSET) };
2730

@@ -31,6 +34,8 @@ namespace
3134

3235
#ifndef _WIN32
3336
const int LogLevelSysLog[] = { LOG_DEBUG, LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERR };
37+
#else
38+
HANDLE consoleHandle = nullptr;
3439
#endif
3540

3641
const size_t MAX_IDENTIFICATION_LENGTH = 22;
@@ -56,10 +61,28 @@ Logger* Logger::getInstance(const QString& name, Logger::LogLevel minLevel)
5661
if (log == nullptr)
5762
{
5863
log = new Logger(name, minLevel);
59-
_loggerMap.insert(name, log); // compat version, replace it with following line if we have 100% c++11
60-
//LoggerMap.emplace(name, log); // not compat with older linux distro's e.g. wheezy
64+
_loggerMap.insert(name, log);
6165
connect(log, &Logger::newLogMessage, LoggerManager::getInstance(), &LoggerManager::handleNewLogMessage);
6266
connect(log, &Logger::newState, LoggerManager::getInstance(), &LoggerManager::handleNewState);
67+
68+
if (_loggerMap.size() == 1)
69+
{
70+
// detect if we can output colorized logs
71+
#ifdef _WIN32
72+
consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
73+
_hasConsole = (consoleHandle != nullptr);
74+
#else
75+
_hasConsole = isatty(fileno(stdin));
76+
#endif
77+
78+
T_LOG_MESSAGE t;
79+
t.utime = QDateTime::currentDateTime().toMSecsSinceEpoch();
80+
t.loggerName = name;
81+
t.level = LogLevel::INFO;
82+
t.levelString = LogLevelStrings[t.level];
83+
t.message = (_hasConsole) ? "TTY is attached to the log output" : "TTY is not attached to the log output";
84+
LoggerManager::getInstance()->handleNewLogMessage(t);
85+
}
6386
}
6487

6588
return log;
@@ -154,28 +177,69 @@ Logger::~Logger()
154177

155178
void Logger::write(const Logger::T_LOG_MESSAGE& message)
156179
{
157-
QString location;
158-
if (message.level == Logger::DEBUG)
180+
if (_hasConsole)
159181
{
160-
location = QString("%1:%2:%3() | ")
161-
.arg(message.fileName)
162-
.arg(message.line)
163-
.arg(message.function);
164-
}
165-
166-
QString name = (message.appName + " " + message.loggerName).trimmed();
167-
name.resize(MAX_IDENTIFICATION_LENGTH, ' ');
168-
169-
const QDateTime timestamp = QDateTime::fromMSecsSinceEpoch(message.utime);
182+
static QMutex localMutex;
183+
QString location, prefix, sufix;
184+
185+
if (message.level == Logger::DEBUG)
186+
{
187+
location = QString("%1:%2:%3() | ")
188+
.arg(message.fileName)
189+
.arg(message.line)
190+
.arg(message.function);
191+
}
170192

171-
std::cout << QString("%1 %2 : <%3> %4%5")
172-
.arg(timestamp.toString("yyyy-MM-ddThh:mm:ss.zzz"))
173-
.arg(name)
174-
.arg(LogLevelStrings[message.level])
175-
.arg(location)
176-
.arg(message.message)
177-
.toStdString()
178-
<< std::endl;
193+
QString name = (message.appName + " " + message.loggerName).trimmed();
194+
name.resize(MAX_IDENTIFICATION_LENGTH, ' ');
195+
196+
const QDateTime timestamp = QDateTime::fromMSecsSinceEpoch(message.utime);
197+
198+
localMutex.lock();
199+
200+
#ifndef _WIN32
201+
prefix = "\033[0m";
202+
sufix = "\033[36;1m";
203+
#else
204+
SetConsoleTextAttribute(consoleHandle, 7);
205+
#endif
206+
207+
std::cout << QString("%1%2 %3 : ")
208+
.arg(prefix)
209+
.arg(timestamp.toString("hh:mm:ss.zzz"))
210+
.arg(name)
211+
.toStdString();
212+
213+
#ifndef _WIN32
214+
switch (message.level)
215+
{
216+
case(Logger::INFO): prefix = "\033[32;1m"; break;
217+
case(Logger::WARNING): prefix = "\033[33;1m"; break;
218+
case(Logger::ERRORR): prefix = "\033[31;1m"; break;
219+
default: break;
220+
}
221+
#else
222+
switch (message.level)
223+
{
224+
case(Logger::INFO):SetConsoleTextAttribute(consoleHandle, 10); break;
225+
case(Logger::WARNING):SetConsoleTextAttribute(consoleHandle, 14); break;
226+
case(Logger::ERRORR):SetConsoleTextAttribute(consoleHandle, 12); break;
227+
default: break;
228+
}
229+
#endif
230+
std::cout << QString("%1<%2> %3%4%5")
231+
.arg(prefix)
232+
.arg(LogLevelStrings[message.level])
233+
.arg(location)
234+
.arg(message.message)
235+
.arg(sufix)
236+
.toStdString()
237+
<< std::endl;
238+
#ifdef _WIN32
239+
SetConsoleTextAttribute(consoleHandle, 11);
240+
#endif
241+
localMutex.unlock();
242+
}
179243

180244
newLogMessage(message);
181245
}
@@ -258,8 +322,6 @@ void Logger::Message(LogLevel level, const char* sourceFile, const char* func, u
258322
}
259323
}
260324

261-
QString Logger::_lastError;
262-
263325
QString Logger::getLastError()
264326
{
265327
return _lastError;

0 commit comments

Comments
 (0)