summaryrefslogtreecommitdiff
path: root/src/common/logging/backend.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/logging/backend.cpp')
-rw-r--r--src/common/logging/backend.cpp42
1 files changed, 34 insertions, 8 deletions
diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp
index 2a3bded40..6e8e8eb36 100644
--- a/src/common/logging/backend.cpp
+++ b/src/common/logging/backend.cpp
@@ -28,7 +28,7 @@
28#ifdef _WIN32 28#ifdef _WIN32
29#include "common/string_util.h" 29#include "common/string_util.h"
30#endif 30#endif
31#include "common/threadsafe_queue.h" 31#include "common/bounded_threadsafe_queue.h"
32 32
33namespace Common::Log { 33namespace Common::Log {
34 34
@@ -155,6 +155,26 @@ public:
155 void EnableForStacktrace() override {} 155 void EnableForStacktrace() override {}
156}; 156};
157 157
158#ifdef ANDROID
159/**
160 * Backend that writes to the Android logcat
161 */
162class LogcatBackend : public Backend {
163public:
164 explicit LogcatBackend() = default;
165
166 ~LogcatBackend() override = default;
167
168 void Write(const Entry& entry) override {
169 PrintMessageToLogcat(entry);
170 }
171
172 void Flush() override {}
173
174 void EnableForStacktrace() override {}
175};
176#endif
177
158bool initialization_in_progress_suppress_logging = true; 178bool initialization_in_progress_suppress_logging = true;
159 179
160/** 180/**
@@ -204,11 +224,11 @@ public:
204 224
205 void PushEntry(Class log_class, Level log_level, const char* filename, unsigned int line_num, 225 void PushEntry(Class log_class, Level log_level, const char* filename, unsigned int line_num,
206 const char* function, std::string&& message) { 226 const char* function, std::string&& message) {
207 if (!filter.CheckMessage(log_class, log_level)) 227 if (!filter.CheckMessage(log_class, log_level)) {
208 return; 228 return;
209 const Entry& entry = 229 }
210 CreateEntry(log_class, log_level, filename, line_num, function, std::move(message)); 230 message_queue.EmplaceWait(
211 message_queue.Push(entry); 231 CreateEntry(log_class, log_level, filename, line_num, function, std::move(message)));
212 } 232 }
213 233
214private: 234private:
@@ -225,7 +245,7 @@ private:
225 ForEachBackend([&entry](Backend& backend) { backend.Write(entry); }); 245 ForEachBackend([&entry](Backend& backend) { backend.Write(entry); });
226 }; 246 };
227 while (!stop_token.stop_requested()) { 247 while (!stop_token.stop_requested()) {
228 entry = message_queue.PopWait(stop_token); 248 message_queue.PopWait(entry, stop_token);
229 if (entry.filename != nullptr) { 249 if (entry.filename != nullptr) {
230 write_logs(); 250 write_logs();
231 } 251 }
@@ -233,7 +253,7 @@ private:
233 // Drain the logging queue. Only writes out up to MAX_LOGS_TO_WRITE to prevent a 253 // Drain the logging queue. Only writes out up to MAX_LOGS_TO_WRITE to prevent a
234 // case where a system is repeatedly spamming logs even on close. 254 // case where a system is repeatedly spamming logs even on close.
235 int max_logs_to_write = filter.IsDebug() ? INT_MAX : 100; 255 int max_logs_to_write = filter.IsDebug() ? INT_MAX : 100;
236 while (max_logs_to_write-- && message_queue.Pop(entry)) { 256 while (max_logs_to_write-- && message_queue.TryPop(entry)) {
237 write_logs(); 257 write_logs();
238 } 258 }
239 }); 259 });
@@ -260,6 +280,9 @@ private:
260 lambda(static_cast<Backend&>(debugger_backend)); 280 lambda(static_cast<Backend&>(debugger_backend));
261 lambda(static_cast<Backend&>(color_console_backend)); 281 lambda(static_cast<Backend&>(color_console_backend));
262 lambda(static_cast<Backend&>(file_backend)); 282 lambda(static_cast<Backend&>(file_backend));
283#ifdef ANDROID
284 lambda(static_cast<Backend&>(lc_backend));
285#endif
263 } 286 }
264 287
265 static void Deleter(Impl* ptr) { 288 static void Deleter(Impl* ptr) {
@@ -272,8 +295,11 @@ private:
272 DebuggerBackend debugger_backend{}; 295 DebuggerBackend debugger_backend{};
273 ColorConsoleBackend color_console_backend{}; 296 ColorConsoleBackend color_console_backend{};
274 FileBackend file_backend; 297 FileBackend file_backend;
298#ifdef ANDROID
299 LogcatBackend lc_backend{};
300#endif
275 301
276 MPSCQueue<Entry, true> message_queue{}; 302 MPSCQueue<Entry> message_queue{};
277 std::chrono::steady_clock::time_point time_origin{std::chrono::steady_clock::now()}; 303 std::chrono::steady_clock::time_point time_origin{std::chrono::steady_clock::now()};
278 std::jthread backend_thread; 304 std::jthread backend_thread;
279}; 305};