forked from aspone/OrderBook
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLogger.h
109 lines (97 loc) · 2.82 KB
/
Logger.h
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
#ifndef __JLOGGER__
#define __JLOGGER__
#include <atomic>
#include <chrono>
#include <functional>
#include <iostream>
#include <mutex>
#include <queue>
#include <string>
#include <thread>
#define TRUE 1
class Logger {
public:
//------------------------------------------------------------
Logger()
: exit_(false)
, messages_available_(false)
, thread_(0)
, messages_()
, mutex_()
{}
//------------------------------------------------------------
~Logger() {
// Guard against it never being used or being stopped elsewhere
if (thread_ == 0) return;
exit_ = true;
thread_->join();
delete thread_;
thread_ = 0;
}
//------------------------------------------------------------
// provide option to manually block and let all logs flush on exit
void stopLogger() {
exit_ = true;
thread_->join();
delete thread_;
thread_ = 0;
}
//------------------------------------------------------------
void print(std::string msg) {
// lazy init creation
// Optimizeable. For now, keep interface clean and hide temporal coupling
// from users.
if (thread_ == 0) {
init();
}
// mutex protects against messing with messages_ while logging thread
// is copying out to it's own queue
std::lock_guard<std::mutex> lock(mutex_);
messages_.push(msg);
messages_available_ = true;
}
//------------------------------------------------------------
void runLogger() {
while (TRUE) {
if (messages_available_ == true) {
copyMessages();
printMessages();
}
if (exit_ == true) {
copyMessages();
printMessages();
return;
}
}
}
private:
//------------------------------------------------------------
void copyMessages() {
// Just copy them all out. Deal with strcpy costs here in bulk.
// Could optimize later if it becomes a problem and blocks enqueue
std::lock_guard<std::mutex> lock(mutex_);
while (messages_.size() != 0) {
messages_to_print_.push(messages_.front());
messages_.pop();
}
messages_available_ = false;
}
//------------------------------------------------------------
void init() {
thread_ = new std::thread(std::bind(&Logger::runLogger, this));
}
//------------------------------------------------------------
void printMessages() {
while (messages_to_print_.size() != 0) {
fprintf(stderr, "%s", messages_to_print_.front().c_str());
messages_to_print_.pop();
}
}
std::atomic<bool> exit_;
std::atomic<bool> messages_available_;
std::thread *thread_;
std::queue<std::string> messages_;
std::queue<std::string> messages_to_print_;
std::mutex mutex_;
};
#endif