diff options
Diffstat (limited to 'common/log.cpp')
-rw-r--r-- | common/log.cpp | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/common/log.cpp b/common/log.cpp index 1d93404..5d35f40 100644 --- a/common/log.cpp +++ b/common/log.cpp | |||
@@ -33,6 +33,46 @@ private: | |||
33 | Q_DISABLE_COPY(DebugStream) | 33 | Q_DISABLE_COPY(DebugStream) |
34 | }; | 34 | }; |
35 | 35 | ||
36 | /* | ||
37 | * ANSI color codes: | ||
38 | * 0: reset colors/style | ||
39 | * 1: bold | ||
40 | * 4: underline | ||
41 | * 30 - 37: black, red, green, yellow, blue, magenta, cyan, and white text | ||
42 | * 40 - 47: black, red, green, yellow, blue, magenta, cyan, and white background | ||
43 | */ | ||
44 | enum ANSI_Colors { | ||
45 | DoNothing = -1, | ||
46 | Reset = 0, | ||
47 | Bold = 1, | ||
48 | Underline = 4, | ||
49 | Black = 30, | ||
50 | Red = 31, | ||
51 | Green = 32, | ||
52 | Yellow = 33, | ||
53 | Blue = 34 | ||
54 | }; | ||
55 | |||
56 | static QString colorCommand(int colorCode) | ||
57 | { | ||
58 | return QString("\x1b[%1m").arg(colorCode); | ||
59 | } | ||
60 | |||
61 | static QString colorCommand(QList<int> colorCodes) | ||
62 | { | ||
63 | colorCodes.removeAll(ANSI_Colors::DoNothing); | ||
64 | if (colorCodes.isEmpty()) { | ||
65 | return QString(); | ||
66 | } | ||
67 | QString string("\x1b["); | ||
68 | for (int code : colorCodes) { | ||
69 | string += QString("%1;").arg(code); | ||
70 | } | ||
71 | string.chop(1); | ||
72 | string += "m"; | ||
73 | return string; | ||
74 | } | ||
75 | |||
36 | QDebug debugStream(DebugLevel debugLevel, int line, const char* file, const char* function) | 76 | QDebug debugStream(DebugLevel debugLevel, int line, const char* file, const char* function) |
37 | { | 77 | { |
38 | static DebugStream stream; | 78 | static DebugStream stream; |
@@ -47,6 +87,7 @@ QDebug debugStream(DebugLevel debugLevel, int line, const char* file, const char | |||
47 | } | 87 | } |
48 | 88 | ||
49 | QString prefix; | 89 | QString prefix; |
90 | int prefixColorCode = ANSI_Colors::DoNothing; | ||
50 | switch (debugLevel) { | 91 | switch (debugLevel) { |
51 | case DebugLevel::Trace: | 92 | case DebugLevel::Trace: |
52 | prefix = "Trace:"; | 93 | prefix = "Trace:"; |
@@ -56,15 +97,18 @@ QDebug debugStream(DebugLevel debugLevel, int line, const char* file, const char | |||
56 | break; | 97 | break; |
57 | case DebugLevel::Warning: | 98 | case DebugLevel::Warning: |
58 | prefix = "Warning:"; | 99 | prefix = "Warning:"; |
100 | prefixColorCode = ANSI_Colors::Red; | ||
59 | break; | 101 | break; |
60 | case DebugLevel::Error: | 102 | case DebugLevel::Error: |
61 | prefix = "Error:"; | 103 | prefix = "Error:"; |
104 | prefixColorCode = ANSI_Colors::Red; | ||
62 | break; | 105 | break; |
63 | default: | 106 | default: |
64 | break; | 107 | break; |
65 | }; | 108 | }; |
66 | 109 | ||
67 | debug << prefix + QString(" %1(%2) %3:").arg(QString::fromLatin1(programName)).arg(unsigned(getpid())).arg(function) /* << file << ":" << line */; | 110 | const QString resetColor = colorCommand(ANSI_Colors::Reset); |
111 | debug << colorCommand(QList<int>() << ANSI_Colors::Bold << prefixColorCode) + prefix + resetColor + QString(" %1(%2) %3:").arg(QString::fromLatin1(programName)).arg(unsigned(getpid())).arg(function) + resetColor/* << file << ":" << line */; | ||
68 | 112 | ||
69 | return debug; | 113 | return debug; |
70 | } | 114 | } |