-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
111 lines (97 loc) · 3.51 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
/*
* Author: doe300
*
* See the file "LICENSE" for the full license governing this code.
*/
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <thread>
#include "log.h"
#include "logger.h"
using namespace std;
static void logThread()
{
const int threadID = std::rand();
while(true)
{
cpplog::debug() << "Test from: " << threadID << cpplog::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
cpplog::error() << "Test 2 from : " << threadID << cpplog::endl;
CPPLOG_LAZY(cpplog::Level::INFO, log << "Lazy logging!" << cpplog::endl);
CPPLOG_LAZY_BLOCK(cpplog::Level::INFO, cpplog::info() << "More lazy logging" << cpplog::endl;
cpplog::debug() << "Even more lazy logging" << cpplog::endl);
}
}
struct NonCopyMoveableLogCallback
{
explicit NonCopyMoveableLogCallback() = default;
NonCopyMoveableLogCallback(const NonCopyMoveableLogCallback&) = delete;
NonCopyMoveableLogCallback(NonCopyMoveableLogCallback&&) noexcept = delete;
~NonCopyMoveableLogCallback() noexcept = default;
NonCopyMoveableLogCallback& operator=(const NonCopyMoveableLogCallback&) = delete;
NonCopyMoveableLogCallback& operator=(NonCopyMoveableLogCallback&&) noexcept = delete;
void operator()(std::wostream& out) const
{
out << "SNAFU" << cpplog::endl;
}
void operator()() const
{
cpplog::warn() << "This is also fine!" << cpplog::endl;
}
};
/*
*
*/
int main(int argc, char** argv)
{
(void) argc;
(void) argv;
cpplog::DEFAULT_LOGGER.reset(new cpplog::ColoredLogger(std::wcout));
cpplog::info() << "Dummy" << cpplog::endl;
for(int i = 0; i < 10; i++)
{
new std::thread(&logThread);
}
unsigned counter = 0;
cpplog::setThreadLogger(cpplog::DEFAULT_LOGGER.get());
auto start = std::chrono::steady_clock::now();
auto end = start + std::chrono::seconds{10};
if(argc > 1 && argv[1] == std::string{"--short-test"})
{
end = start + std::chrono::seconds{2};
}
while(std::chrono::steady_clock::now() < end)
{
cpplog::debug() << std::string("Test") << cpplog::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
cpplog::error() << "Test2" << cpplog::endl;
cpplog::logLazy(cpplog::Level::INFO,
[](std::wostream& out)
{
NonCopyMoveableLogCallback logCallback;
logCallback(out);
});
cpplog::logLazy(cpplog::Level::WARNING,
[]()
{
NonCopyMoveableLogCallback logCallback;
logCallback();
});
CPPLOG_LAZY(cpplog::Level::DEBUG, log << "FOOBAR" << cpplog::endl);
CPPLOG_LAZY_BLOCK(cpplog::Level::WARNING, cpplog::warn() << "This is fine!" << cpplog::endl);
++counter;
if(counter > 100)
{
// tests the ability to reset the logger while using it
cpplog::warn() << "Changing logger..." << cpplog::endl;
// TODO this might still randomly crash if the other threads are accessing the global logger instance while
// it is being reset, but this behavior is discouraged anyway...
cpplog::DEFAULT_LOGGER.reset(new cpplog::ColoredLogger(std::wcout));
// test manually changing per-thread logger. This line would crash if the logger is not updated
cpplog::setThreadLogger(cpplog::DEFAULT_LOGGER.get());
counter = 0;
}
}
return EXIT_SUCCESS;
}