-
Notifications
You must be signed in to change notification settings - Fork 7
/
Logger.cpp
192 lines (171 loc) · 4.89 KB
/
Logger.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/////////////////////////////////////////////////////////////////////
// Logger.cpp - log text messages to std::ostream //
// ver 1.2 //
//-----------------------------------------------------------------//
// Jim Fawcett (c) copyright 2015 //
// All rights granted provided this copyright notice is retained //
//-----------------------------------------------------------------//
// Language: C++, Visual Studio 2015 //
// Application: Several Projects, CSE687 - Object Oriented Design //
// Author: Jim Fawcett, Syracuse University, CST 4-187 //
// jfawcett@twcny.rr.com //
/////////////////////////////////////////////////////////////////////
#include <functional>
#include <fstream>
#include <windows.h>
#include "Logger.h"
#include "../Utilities/Utilities.h"
using namespace Logging;
//----< send text message to std::ostream >--------------------------
void Logger::write(const std::string& msg)
{
if(_ThreadRunning)
_queue.enQ(msg);
}
void Logger::title(const std::string& msg, char underline)
{
std::string temp = "\n " + msg + "\n " + std::string(msg.size() + 2, underline);
write(temp);
}
//----< attach logger to existing std::ostream >---------------------
void Logger::attach(std::ostream* pOut)
{
streams_.push_back(pOut);
//_pOut = pOut;
}
//----< start logging >----------------------------------------------
/*
* log to all the attached streams
*/
void Logger::start()
{
if (_ThreadRunning)
return;
_ThreadRunning = true;
std::function<void()> tp = [=]() {
while (true)
{
std::string msg = _queue.deQ();
if (msg == "quit")
{
_ThreadRunning = false;
break;
}
for (auto pStrm : streams_)
{
*pStrm << msg;
}
}
};
_pThr = new std::thread (tp);
//thr.detach();
}
//----< has logger been started? >-----------------------------------
bool Logger::running()
{
return _ThreadRunning;
}
//----< suspend logger >---------------------------------------------
void Logger::pause(bool doPause)
{
if (doPause)
{
_Paused = true;
::SuspendThread(_pThr->native_handle());
}
else
{
_Paused = false;
::ResumeThread(_pThr->native_handle());
}
}
//----< is logger currently paused? >--------------------------------
bool Logger::paused()
{
return _Paused;
}
//----< wait for logging to empty input queue >----------------------
void Logger::flush()
{
if (_ThreadRunning && !_Paused)
{
while (_queue.size() > 0)
;
for (auto pStream : streams_)
pStream->flush();
}
}
//----< stop logging >-----------------------------------------------
void Logger::stop(const std::string& msg)
{
if (_ThreadRunning)
{
if(msg != "")
write(msg);
write("quit"); // request thread to stop
if (_pThr->joinable())
_pThr->join(); // wait for queue to empty
_ThreadRunning = false;
}
}
//----< wait for logger to finish >----------------------------------
void Logger::wait()
{
if (_ThreadRunning && _pThr->joinable())
_pThr->join();
}
//----< stop logging thread >----------------------------------------
Logger::~Logger()
{
stop();
}
struct Cosmetic
{
~Cosmetic() { std::cout << "\n\n"; }
} cosmetic;
#ifdef TEST_LOGGER
using Util = Utilities::StringHelper;
int main()
{
//Util::Title("Testing Logger Class");
Logger log;
log.attach(&std::cout);
std::ofstream out("logFile.txt");
if (out.good())
log.attach(&out);
else
std::cout << "\n couldn't open logFile for writing";
log.write("\n won't get logged - not started yet");
log.start();
log.title("Testing Logger Class", '=');
log.write("\n one");
log.write("\n two");
log.write("\n fini");
log.stop();
log.write("\n won't get logged - stopped");
log.start();
log.write("\n starting again");
log.write("\n and stopping again");
log.stop("\n log terminating now");
log.wait();
StaticLogger<1>::attach(&std::cout);
StaticLogger<1>::attach(&out);
StaticLogger<1>::start();
StaticLogger<1>::write("\n");
StaticLogger<1>::title("Testing StaticLogger class");
StaticLogger<1>::write("\n static logger at work");
Logger& logger = StaticLogger<1>::instance();
logger.write("\n static logger still at work");
for(size_t i=0; i<5; ++i)
logger.write("\n a log msg");
logger.write("\n suspending logger");
logger.pause(true);
for (size_t i = 0; i<5; ++i)
logger.write("\n a log msg written while log suspended");
logger.pause(false);
logger.write("\n a log msg written after log resumed");
logger.stop("\n stopping static logger");
logger.wait();
out.close();
}
#endif