-
Notifications
You must be signed in to change notification settings - Fork 0
/
microLog_test.cpp
executable file
·167 lines (116 loc) · 4.29 KB
/
microLog_test.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
/// microLog_test.cpp
// clang++ -std=c++11 ./microLog_test.cpp
// clang++ -std=c++11 -stdlib=libc++ ./microLog_test.cpp
// These macros can also be defined in the makefile, or in microLog_config.hpp:
/*
#define MICRO_LOG_ACTIVE 1
#define MICRO_LOG_MIN_LEVEL 4
*/
#ifdef MICRO_LOG_TEST
//#define uLOG_TEST_NO_INIT
#ifdef uLOG_TEST_NO_INIT // Test without logger initialization
#define MICRO_LOG_DLL
#endif
#include "microLog.hpp"
#include <cmath>
#include <iostream>
#include <string>
#ifdef uLOG_TEST_NO_INIT // Test without logger initialization
int Test_microLog(std::string logPath, int nTestCases = 1)
{
for(size_t n = 0; n < nTestCases; ++n)
{
uLOGF(logPath, warning, info, "Test n. " << n + 1 << " without logger initialization.");
}
return 0;
}
#else // uLOG_TEST_NO_INIT
uLOG_INIT; // microLog initialization
int Test_microLog(std::string logPath, size_t nTestCases = 1)
{
/// Tests:
// - Plain tests
// - Multithreading tests - TODO
// - Complex tests - TODO
// - Border line tests - TODO
// - Performace tests - TODO
using std::sin;
///--- TEST INIT ---
uLOG_START(logPath, uLog::backup_append);
// Test with custom log file
std::ofstream custom_ofs("/Volumes/ramdisk/custom.log");
uLOG_DATE; // date
uLog::LogLevels();
uLog::minLogLevel = nolog;
uLog::MinLogLevel();
///--- TEST CODE ---
for(size_t n = 0; n < nTestCases; ++n)
{
uLog::LogFields::SetSystem();
uLOG_TITLES(info); // columns' titles
uLog::minLogLevel = nolog;
for(int l = nolog; l <= fatal; ++l)
{
uLOG(l) << "Test log message with level " << l + 1 << "." << uLOGE;
}
uLOG(info) << "Test insertion operator: " << char((n + 65)%255) << " " << n << " " << sin(n + 1.0) << uLOGE;
uLog::minLogLevel = warning;
uLOG(detail) << "Log not generated, since below the minimum log level." << uLOGE;
uLOG(warning) << "Previous log not generated, since below the minimum log level." << uLOGE;
uLOG(warning) << "Log generated, since above the minimum log level." << uLOGE;
uLog::minLogLevel = warning;
uLOG_(detail, MICRO_LOG_LEVEL1) << "Test minimum log levels for specific code areas with macros: not generated." << uLOGE;
uLOG_(detail, uLog::logConstLevel1) << "Test minimum log levels for specific code areas with constants: not generated." << uLOGE;
uLog::minLogLevel = warning;
uLOG_(detail, MICRO_LOG_LEVEL2) << "Test minimum log levels for specific code areas with macros." << uLOGE;
uLOG_(detail, uLog::logConstLevel2) << "Test minimum log levels for specific code areas with constants." << uLOGE;
/*
//+TODO
uLOG(warning) << "Log made of separate tokens... ";
uLOGT(warning) << "first token, ";
uLOGT(warning) << "last token" << uLOGE;
*/
// Test with custom log file
uLOG_TITLES_S(custom_ofs, warning);
uLOGS(custom_ofs, warning) << "Test log on a different file." << uLOGE;
// Test without logger initialization
uLOGF(logPath, warning, info, "Test without logger initialization.");
}
return 0;
}
#endif // uLOG_TEST_NO_INIT
int main()
{
int testResult = 0;
int nErrors = 0;
int nWarnings = 0;
std::cout << "\n--- microLog test ---\n" << std::endl;
std::string logPath;
std::string ramDiskPath = "/Volumes/ramdisk/";
char pathOpt = '2';
if(pathOpt == '0')
{
std::cout << "Select log file path:\n"
<< "1. Local directory.\n"
<< "2. Ram disk (" << ramDiskPath << ").\n"
<< " Note: check you have a ram disk on your system, a set its path in the source code (microLog_test.cpp).\n" << std::endl;
std::cin >> pathOpt;
}
if(pathOpt == '2')
logPath.append("/Volumes/ramdisk/");
logPath.append("myProg.log");
std::cout << "Test version: " << VERSION << "\n";
std::cout << "microLog version: " << MICRO_LOG_VERSION << "\n";
std::cout << "Log file path: " << logPath << std::endl;
testResult = Test_microLog(logPath);
#ifndef uLOG_TEST_NO_INIT // Test without logger initialization
uLog::Statistics::Log();
#endif
std::cout << "\nTest completed." << std::endl;
if(testResult == 0)
std::cout << "\nTest passed." << std::endl;
else
std::cout << "\nTest FAILED." << std::endl;
return testResult;
}
#endif // MICRO_LOG_TEST