-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
176 lines (137 loc) · 5.23 KB
/
main.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <iostream>
#include <thread>
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#ifdef KORS_LOGGER_QT_SUPPORT
#include <QDebug>
#include <QString>
#include <QPoint>
#endif
#include "logger.h"
#include "logstream.h"
#include "log.h"
namespace app {
struct MyType
{
MyType (int v)
: val(v) {}
int val = 0;
};
inline app::logger::Stream& operator<<(app::logger::Stream& s, const MyType& t)
{
s << t.val;
return s;
}
class Example
{
public:
Example() = default;
void example()
{
logger::Logger* logger = logger::Logger::instance();
logger->setupDefault();
//! Default output to console, catch Qt messages (if supported)
LOGE() << "This is error";
LOGW() << "This is warning";
LOGI() << "This is info";
LOGD() << "This is debug"; //! NOTE Default not output
std::thread t1([]() { LOGI() << "Info from thread"; });
t1.join();
#ifdef KORS_LOGGER_QT_SUPPORT
qCritical() << "This is qCritical";
qWarning() << "This is qWarning";
qDebug() << "This is qDebug"; //! NOTE Default not output
LOGI() << QString("This is QString");
#endif
LOGI() << "This is custom type: " << MyType(42);
/*
23:07:43.602 | ERROR | main_thread | Example::example | This is error
23:07:43.602 | WARN | main_thread | Example::example | This is warning
23:07:43.602 | INFO | main_thread | Example::example | This is info
23:07:43.602 | INFO | 140147421083200 | Example::example | Info from thread
23:07:43.602 | ERROR | main_thread | Example::example | This is qCritical
23:07:43.603 | WARN | main_thread | Example::example | This is qWarning
23:07:43.603 | INFO | main_thread | Example::example | "This is QString"
23:07:43.603 | INFO | main_thread | Example::example | This is custom type: 42
*/
//! Using message formatting
LOGD("This is formatted message, arg1: %d, arg2: %d, sum: %d", 40, 2, 42);
/*
23:07:43.603 | INFO | main_thread | Example::example | This is formatted message, arg1: 40, arg2: 2, sum: 42
*/
//! Set tag (default class::func)
#undef LOG_TAG
#define LOG_TAG "MYTAG"
LOGI() << "This is info with custorm tag";
/*
23:07:43.603 | INFO | main_thread | MYTAG | This is info with custorm tag
*/
//! Set log level
logger->setLevel(logger::Level::Debug);
LOGD() << "This is debug";
#ifdef KORS_LOGGER_QT_SUPPORT
qDebug() << "This is qDebug (for Qt always tag is class::func)";
#endif
/*
23:07:43.603 | DEBUG | main_thread | MYTAG | This is debug
23:07:43.603 | DEBUG | main_thread | Example::example | This is qDebug (for Qt always tag is class::func)
*/
//! --- Setup logger ---
LOGI() << "Custom setup... ";
//! Destination and format
logger->clearDests();
//! Console
logger->addDest(new logger::ConsoleLogDest(logger::LogLayout("${time} | ${type|7} | ${thread} | ${tag|20} | ${message}")));
//! File
std::string pwd = fs::current_path();
std::string logsDir = pwd + "/logs";
if (!fs::exists(logsDir)) {
fs::create_directories(logsDir);
}
std::string logPath = logsDir + "/myapp.log";
logger->addDest(new logger::FileLogDest(logPath,
logger::LogLayout("${datetime} | ${type|7} | ${thread} | ${tag|20} | ${message}")));
/** NOTE Layout have a tags
"${datetime}" - yyyy-MM-ddThh:mm:ss.zzz
"${time}" - hh:mm:ss.zzz
"${type}" - type
"${tag}" - tag
"${thread}" - thread, the main thread output as "main_thread" otherwise thread id
"${message}" - message
|N - min field width
*/
LOGI() << "now log fields width is changed";
/*
23:07:43.603 | INFO | main_thread | MYTAG | Custom setup...
23:07:43.603 | INFO | main_thread | MYTAG | now log fields width is changed
*/
//! NOTE Custom log layout can be used - inherits of the LogLayout with overridden method "output"
//! NOTE Any custom destinations can be added - inherits of the LogDest with overridden method "write"
//! Level
logger->setLevel(logger::Level::Debug);
//! Catch Qt message (if supported)
#ifdef KORS_LOGGER_QT_SUPPORT
logger->setIsCatchQtMsg(true);
#endif
//! Custom types
logger->setType("MYTRACE", true);
//! See custom macro in example log.h
MYTRACE() << "This my trace";
/*
23:07:43.603 | MYTRACE | main_thread | MYTAG | This my trace
*/
//! Disable output for type
logger->setType("MYTRACE", false); //! NOTE Type should be a debug level
MYTRACE() << "This my trace"; //! NOTE Not output
}
};
}
int main(int argc, char* argv[])
{
std::cout << "Hello World, I am Logger\n";
app::Example t;
t.example();
#undef LOG_TAG
#define LOG_TAG CLASSFUNC
LOGI() << "Goodbye!";
}