|
3 | 3 | #include <iostream>
|
4 | 4 |
|
5 | 5 | #ifdef wasm
|
| 6 | + |
6 | 7 | #include "emscripten.h"
|
7 | 8 | #include "emscripten/threading.h"
|
| 9 | + |
8 | 10 | #endif
|
9 | 11 |
|
10 | 12 | void Logger::start() {
|
11 |
| - stop = false; |
12 |
| - empty = true; |
13 |
| - processingThread = std::thread(&Logger::threadFunc, this); |
| 13 | + stop = false; |
| 14 | + empty = true; |
| 15 | + processingThread = std::thread(&Logger::threadFunc, this); |
14 | 16 | #ifdef logtofile
|
15 |
| - logFile.open("log.txt"); |
| 17 | + logFile.open("log.txt"); |
16 | 18 | #endif
|
17 | 19 | }
|
18 | 20 |
|
19 | 21 | void Logger::end() {
|
20 |
| - stop = true; |
21 |
| - processingThread.join(); |
| 22 | + stop = true; |
| 23 | + processingThread.join(); |
22 | 24 | #ifdef logtofile
|
23 |
| - logFile.close(); |
| 25 | + logFile.close(); |
24 | 26 | #endif
|
25 | 27 | }
|
26 | 28 |
|
27 |
| -void Logger::log(const std::string&message) const { |
28 |
| - auto* newNode = new MessageNode(); |
29 |
| - newNode->type = LOG; |
30 |
| - newNode->msg = message; |
31 |
| - tail->next = newNode; |
32 |
| - tail = newNode; { |
33 |
| - std::lock_guard lk(m); |
34 |
| - empty = false; |
35 |
| - } |
36 |
| - cv.notify_one(); |
| 29 | +void Logger::log(const std::string &message) const { |
| 30 | + auto *newNode = new MessageNode(); |
| 31 | + newNode->type = LOG; |
| 32 | + newNode->msg = message; |
| 33 | + tail->next = newNode; |
| 34 | + tail = newNode; |
| 35 | + { |
| 36 | + std::lock_guard lk(m); |
| 37 | + empty = false; |
| 38 | + } |
| 39 | + cv.notify_one(); |
37 | 40 | }
|
38 | 41 |
|
39 |
| -void Logger::logToFile(const std::string&message) const { |
40 |
| - // Since the function doesn't do anything with the flag disabled the compiler should technically remove it and all the calls to it |
| 42 | +void Logger::logToFile(const std::string &message) const { |
| 43 | + // Since the function doesn't do anything with the flag disabled the compiler should technically remove it and all the calls to it |
41 | 44 | #ifdef logtofile
|
42 |
| - auto* newNode = new MessageNode(); |
43 |
| - newNode->type = TOFILE; |
44 |
| - newNode->msg = message; |
45 |
| - tail->next = newNode; |
46 |
| - tail = newNode; { |
47 |
| - std::lock_guard lk(m); |
48 |
| - empty = false; |
49 |
| - } |
50 |
| - cv.notify_one(); |
| 45 | + auto* newNode = new MessageNode(); |
| 46 | + newNode->type = TOFILE; |
| 47 | + newNode->msg = message; |
| 48 | + tail->next = newNode; |
| 49 | + tail = newNode; { |
| 50 | + std::lock_guard lk(m); |
| 51 | + empty = false; |
| 52 | + } |
| 53 | + cv.notify_one(); |
51 | 54 | #endif
|
52 | 55 | }
|
53 | 56 |
|
54 |
| -void Logger::sendInt(const std::string&name, const int value) const { |
55 |
| - // Since the function doesn't do anything with the flag disabled the compiler should technically remove it and all the calls to it |
| 57 | +void Logger::sendInt(const std::string &name, const int value) const { |
| 58 | + // Since the function doesn't do anything with the flag disabled the compiler should technically remove it and all the calls to it |
56 | 59 | #ifdef wasm
|
57 |
| - auto* newNode = new IntNode(); |
58 |
| - newNode->type = INT; |
59 |
| - newNode->name = name; |
60 |
| - newNode->value = value; |
61 |
| - tail->next = newNode; |
62 |
| - tail = newNode; |
63 |
| - { |
64 |
| - std::lock_guard lk(m); |
65 |
| - empty = false; |
66 |
| - } |
67 |
| - cv.notify_one(); |
| 60 | + auto *newNode = new IntNode(); |
| 61 | + newNode->type = INT; |
| 62 | + newNode->name = name; |
| 63 | + newNode->value = value; |
| 64 | + tail->next = newNode; |
| 65 | + tail = newNode; |
| 66 | + { |
| 67 | + std::lock_guard lk(m); |
| 68 | + empty = false; |
| 69 | + } |
| 70 | + cv.notify_one(); |
68 | 71 | #endif
|
69 | 72 | }
|
70 | 73 |
|
71 |
| -void Logger::sendString(const std::string&name, const std::string&value) const { |
72 |
| - // Since the function doesn't do anything with the flag disabled the compiler should technically remove it and all the calls to it |
| 74 | +void Logger::sendString(const std::string &name, const std::string &value) const { |
| 75 | + // Since the function doesn't do anything with the flag disabled the compiler should technically remove it and all the calls to it |
73 | 76 | #ifdef wasm
|
74 |
| - auto* newNode = new StringNode(); |
75 |
| - newNode->type = STRING; |
76 |
| - newNode->name = name; |
77 |
| - newNode->value = value; |
78 |
| - tail->next = newNode; |
79 |
| - tail = newNode; |
80 |
| - { |
81 |
| - std::lock_guard lk(m); |
82 |
| - empty = false; |
83 |
| - } |
84 |
| - cv.notify_one(); |
| 77 | + auto *newNode = new StringNode(); |
| 78 | + newNode->type = STRING; |
| 79 | + newNode->name = name; |
| 80 | + newNode->value = value; |
| 81 | + tail->next = newNode; |
| 82 | + tail = newNode; |
| 83 | + { |
| 84 | + std::lock_guard lk(m); |
| 85 | + empty = false; |
| 86 | + } |
| 87 | + cv.notify_one(); |
85 | 88 | #endif
|
86 | 89 | }
|
87 | 90 |
|
88 | 91 |
|
89 | 92 | Logger::Logger() {
|
90 |
| - this->head = new MessageNode(); |
91 |
| - this->tail = this->head; |
92 |
| - stop = false; |
93 |
| - empty = true; |
| 93 | + this->head = new MessageNode(); |
| 94 | + this->tail = this->head; |
| 95 | + stop = false; |
| 96 | + empty = true; |
94 | 97 | }
|
95 | 98 |
|
96 | 99 | Logger::~Logger() {
|
97 |
| - delete this->head; |
| 100 | + delete this->head; |
98 | 101 | }
|
99 | 102 |
|
100 | 103 | void Logger::threadFunc() {
|
101 |
| - while (!stop) { |
102 |
| - // Keep processing the buffer, wait a bit when it's empty |
103 |
| - if (!processNode()) { |
104 |
| - std::unique_lock lk(m); |
105 |
| - empty = true; |
106 |
| - cv.wait(lk, [this] { return !empty || stop; }); |
107 |
| - lk.unlock(); |
108 |
| - } |
109 |
| - } |
110 |
| - // Empty the buffer |
111 |
| - while (processNode()) { |
112 |
| - } |
| 104 | + while (!stop) { |
| 105 | + // Keep processing the buffer, wait a bit when it's empty |
| 106 | + if (!processNode()) { |
| 107 | + std::unique_lock lk(m); |
| 108 | + empty = true; |
| 109 | + cv.wait(lk, [this] { return !empty || stop; }); |
| 110 | + lk.unlock(); |
| 111 | + } |
| 112 | + } |
| 113 | + // Empty the buffer |
| 114 | + while (processNode()) { |
| 115 | + } |
113 | 116 | }
|
114 | 117 |
|
115 | 118 | bool Logger::processNode() {
|
116 |
| - if (head->next != nullptr) { |
117 |
| - Node* newHead = head->next; |
| 119 | + if (head->next != nullptr) { |
| 120 | + Node *newHead = head->next; |
118 | 121 |
|
119 | 122 | #ifndef wasm
|
120 |
| - if (newHead->type == LOG) { |
121 |
| - std::cout << reinterpret_cast<MessageNode *>(newHead)->msg; |
122 |
| - } |
| 123 | + if (newHead->type == LOG) { |
| 124 | + std::cout << reinterpret_cast<MessageNode *>(newHead)->msg; |
| 125 | + } |
123 | 126 | #ifdef logtofile
|
124 |
| - else if (newHead->type == TOFILE) { |
125 |
| - logFile << reinterpret_cast<MessageNode *>(newHead)->msg; |
126 |
| - } |
| 127 | + else if (newHead->type == TOFILE) { |
| 128 | + logFile << reinterpret_cast<MessageNode *>(newHead)->msg; |
| 129 | + } |
127 | 130 | #endif
|
128 | 131 | #endif
|
129 | 132 |
|
130 | 133 | #ifdef wasm
|
131 |
| - switch (newHead->type) { |
132 |
| - case LOG: { |
133 |
| - MAIN_THREAD_EM_ASM( |
134 |
| - postMessage({data: [UTF8ToString($0)], name: "log"}) |
135 |
| - , reinterpret_cast<MessageNode *>(newHead)->msg.c_str()); |
136 |
| - break; |
137 |
| - } |
138 |
| - case STRING: { |
139 |
| - MAIN_THREAD_EM_ASM( |
140 |
| - postMessage({data: [UTF8ToString($1)], name: UTF8ToString($0)}) |
141 |
| - , reinterpret_cast<StringNode *>(newHead)->name.c_str(), reinterpret_cast<StringNode *>(newHead)->value.c_str()); |
142 |
| - break; |
143 |
| - } |
144 |
| - case INT: { |
145 |
| - MAIN_THREAD_EM_ASM( |
146 |
| - postMessage({data: [$1], name: UTF8ToString($0)}) |
147 |
| - , reinterpret_cast<IntNode *>(newHead)->name.c_str(), reinterpret_cast<IntNode *>(newHead)->value); |
148 |
| - break; |
149 |
| - } |
150 |
| - default: break; |
151 |
| - } |
| 134 | + switch (newHead->type) { |
| 135 | + case LOG: { |
| 136 | + MAIN_THREAD_EM_ASM( |
| 137 | + postMessage({data:[UTF8ToString($0)], name: "log"}), |
| 138 | + reinterpret_cast<MessageNode *>(newHead)->msg.c_str()); |
| 139 | + break; |
| 140 | + } |
| 141 | + case STRING: { |
| 142 | + MAIN_THREAD_EM_ASM( |
| 143 | + postMessage({data:[UTF8ToString($1)], name: UTF8ToString($0)}), |
| 144 | + reinterpret_cast<StringNode *>(newHead)->name.c_str(), |
| 145 | + reinterpret_cast<StringNode *>(newHead)->value.c_str()); |
| 146 | + break; |
| 147 | + } |
| 148 | + case INT: { |
| 149 | + MAIN_THREAD_EM_ASM( |
| 150 | + postMessage({data:[$1], name: UTF8ToString($0)}), |
| 151 | + reinterpret_cast<IntNode *>(newHead)->name.c_str(), |
| 152 | + reinterpret_cast<IntNode *>(newHead)->value); |
| 153 | + break; |
| 154 | + } |
| 155 | + default: |
| 156 | + break; |
| 157 | + } |
152 | 158 | #endif
|
153 | 159 |
|
154 |
| - delete head; |
155 |
| - head = newHead; |
156 |
| - return true; |
157 |
| - } |
158 |
| - return false; |
| 160 | + delete head; |
| 161 | + head = newHead; |
| 162 | + return true; |
| 163 | + } |
| 164 | + return false; |
159 | 165 | }
|
0 commit comments