Skip to content

Commit 95c7367

Browse files
condition variable in logger
1 parent d2bad9c commit 95c7367

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

src/Logger.cpp

+34-9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
void Logger::start() {
1111
stop = false;
12+
empty = true;
1213
processingThread = std::thread(&Logger::threadFunc, this);
1314
#ifdef logtofile
1415
logFile.open("log.txt");
@@ -23,26 +24,34 @@ void Logger::end() {
2324
#endif
2425
}
2526

26-
void Logger::log(const std::string message) const {
27+
void Logger::log(const std::string&message) const {
2728
auto* newNode = new MessageNode();
2829
newNode->type = LOG;
2930
newNode->msg = message;
3031
tail->next = newNode;
31-
tail = newNode;
32+
tail = newNode; {
33+
std::lock_guard lk(m);
34+
empty = false;
35+
}
36+
cv.notify_one();
3237
}
3338

34-
void Logger::logToFile(std::string message) const {
39+
void Logger::logToFile(const std::string&message) const {
3540
// Since the function doesn't do anything with the flag disabled the compiler should technically remove it and all the calls to it
3641
#ifdef logtofile
3742
auto* newNode = new MessageNode();
3843
newNode->type = TOFILE;
3944
newNode->msg = message;
4045
tail->next = newNode;
41-
tail = newNode;
46+
tail = newNode; {
47+
std::lock_guard lk(m);
48+
empty = false;
49+
}
50+
cv.notify_one();
4251
#endif
4352
}
4453

45-
void Logger::sendInt(const std::string name, const int value) const {
54+
void Logger::sendInt(const std::string&name, const int value) const {
4655
// Since the function doesn't do anything with the flag disabled the compiler should technically remove it and all the calls to it
4756
#ifdef wasm
4857
auto* newNode = new IntNode();
@@ -51,10 +60,15 @@ void Logger::sendInt(const std::string name, const int value) const {
5160
newNode->value = value;
5261
tail->next = newNode;
5362
tail = newNode;
63+
{
64+
std::lock_guard lk(m);
65+
empty = false;
66+
}
67+
cv.notify_one();
5468
#endif
5569
}
5670

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 {
5872
// Since the function doesn't do anything with the flag disabled the compiler should technically remove it and all the calls to it
5973
#ifdef wasm
6074
auto* newNode = new StringNode();
@@ -63,6 +77,11 @@ void Logger::sendString(const std::string name, const std::string value) const {
6377
newNode->value = value;
6478
tail->next = newNode;
6579
tail = newNode;
80+
{
81+
std::lock_guard lk(m);
82+
empty = false;
83+
}
84+
cv.notify_one();
6685
#endif
6786
}
6887

@@ -71,23 +90,29 @@ Logger::Logger() {
7190
this->head = new MessageNode();
7291
this->tail = this->head;
7392
stop = false;
93+
empty = true;
7494
}
7595

7696
Logger::~Logger() {
7797
delete this->head;
7898
}
7999

80-
void Logger::threadFunc() const {
100+
void Logger::threadFunc() {
81101
while (!stop) {
82102
// 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+
}
84109
}
85110
// Empty the buffer
86111
while (processNode()) {
87112
}
88113
}
89114

90-
bool Logger::processNode() const {
115+
bool Logger::processNode() {
91116
if (head->next != nullptr) {
92117
Node* newHead = head->next;
93118

src/Logger.h

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#ifndef LOGGER_H
22
#define LOGGER_H
3+
#include <condition_variable>
34
#include <string>
45
#include <thread>
56
#include <fstream>
7+
#include <semaphore>
68

79
enum MessageType {
810
LOG = 0,
@@ -36,13 +38,13 @@ class Logger {
3638

3739
void end();
3840

39-
void log(std::string message) const;
41+
void log(const std::string& message) const;
4042

41-
void logToFile(std::string message) const;
43+
void logToFile(const std::string& message) const;
4244

43-
void sendInt(std::string name, int value) const;
45+
void sendInt(const std::string& name, int value) const;
4446

45-
void sendString(std::string name, std::string value) const;
47+
void sendString(const std::string& name, const std::string& value) const;
4648

4749
Logger();
4850

@@ -51,14 +53,18 @@ class Logger {
5153
private:
5254
mutable Node* head;
5355
mutable Node* tail;
54-
mutable std::ofstream logFile;
56+
std::ofstream logFile;
5557

56-
void threadFunc() const;
58+
void threadFunc();
5759

58-
bool processNode() const;
60+
bool processNode();
5961

6062
std::thread processingThread;
61-
bool stop;
63+
64+
mutable std::mutex m;
65+
mutable std::condition_variable cv;
66+
mutable bool empty{};
67+
mutable bool stop;
6268
};
6369

6470
#endif //LOGGER_H

0 commit comments

Comments
 (0)