9
9
10
10
void Logger::start () {
11
11
stop = false ;
12
+ empty = true ;
12
13
processingThread = std::thread (&Logger::threadFunc, this );
13
14
#ifdef logtofile
14
15
logFile.open (" log.txt" );
@@ -23,26 +24,34 @@ void Logger::end() {
23
24
#endif
24
25
}
25
26
26
- void Logger::log (const std::string message) const {
27
+ void Logger::log (const std::string& message) const {
27
28
auto * newNode = new MessageNode ();
28
29
newNode->type = LOG;
29
30
newNode->msg = message;
30
31
tail->next = newNode;
31
- tail = newNode;
32
+ tail = newNode; {
33
+ std::lock_guard lk (m);
34
+ empty = false ;
35
+ }
36
+ cv.notify_one ();
32
37
}
33
38
34
- void Logger::logToFile (std::string message) const {
39
+ void Logger::logToFile (const std::string& message) const {
35
40
// Since the function doesn't do anything with the flag disabled the compiler should technically remove it and all the calls to it
36
41
#ifdef logtofile
37
42
auto * newNode = new MessageNode ();
38
43
newNode->type = TOFILE;
39
44
newNode->msg = message;
40
45
tail->next = newNode;
41
- tail = newNode;
46
+ tail = newNode; {
47
+ std::lock_guard lk (m);
48
+ empty = false ;
49
+ }
50
+ cv.notify_one ();
42
51
#endif
43
52
}
44
53
45
- void Logger::sendInt (const std::string name, const int value) const {
54
+ void Logger::sendInt (const std::string& name, const int value) const {
46
55
// Since the function doesn't do anything with the flag disabled the compiler should technically remove it and all the calls to it
47
56
#ifdef wasm
48
57
auto * newNode = new IntNode ();
@@ -51,10 +60,15 @@ void Logger::sendInt(const std::string name, const int value) const {
51
60
newNode->value = value;
52
61
tail->next = newNode;
53
62
tail = newNode;
63
+ {
64
+ std::lock_guard lk (m);
65
+ empty = false ;
66
+ }
67
+ cv.notify_one ();
54
68
#endif
55
69
}
56
70
57
- void Logger::sendString (const std::string name, const std::string value) const {
71
+ void Logger::sendString (const std::string& name, const std::string& value) const {
58
72
// Since the function doesn't do anything with the flag disabled the compiler should technically remove it and all the calls to it
59
73
#ifdef wasm
60
74
auto * newNode = new StringNode ();
@@ -63,6 +77,11 @@ void Logger::sendString(const std::string name, const std::string value) const {
63
77
newNode->value = value;
64
78
tail->next = newNode;
65
79
tail = newNode;
80
+ {
81
+ std::lock_guard lk (m);
82
+ empty = false ;
83
+ }
84
+ cv.notify_one ();
66
85
#endif
67
86
}
68
87
@@ -71,23 +90,29 @@ Logger::Logger() {
71
90
this ->head = new MessageNode ();
72
91
this ->tail = this ->head ;
73
92
stop = false ;
93
+ empty = true ;
74
94
}
75
95
76
96
Logger::~Logger () {
77
97
delete this ->head ;
78
98
}
79
99
80
- void Logger::threadFunc () const {
100
+ void Logger::threadFunc () {
81
101
while (!stop) {
82
102
// Keep processing the buffer, wait a bit when it's empty
83
- if (!processNode ()) std::this_thread::sleep_for (std::chrono::milliseconds (1 ));
103
+ if (!processNode ()) {
104
+ std::unique_lock lk (m);
105
+ empty = true ;
106
+ cv.wait (lk, [this ] { return !empty || stop; });
107
+ lk.unlock ();
108
+ }
84
109
}
85
110
// Empty the buffer
86
111
while (processNode ()) {
87
112
}
88
113
}
89
114
90
- bool Logger::processNode () const {
115
+ bool Logger::processNode () {
91
116
if (head->next != nullptr ) {
92
117
Node* newHead = head->next ;
93
118
0 commit comments