summaryrefslogtreecommitdiff
path: root/src/common/logging
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/logging')
-rw-r--r--src/common/logging/backend.cpp19
-rw-r--r--src/common/logging/backend.h1
2 files changed, 8 insertions, 12 deletions
diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp
index a5e031189..b369f199f 100644
--- a/src/common/logging/backend.cpp
+++ b/src/common/logging/backend.cpp
@@ -40,9 +40,7 @@ public:
40 const Impl& operator=(Impl const&) = delete; 40 const Impl& operator=(Impl const&) = delete;
41 41
42 void PushEntry(Entry e) { 42 void PushEntry(Entry e) {
43 std::lock_guard<std::mutex> lock(message_mutex);
44 message_queue.Push(std::move(e)); 43 message_queue.Push(std::move(e));
45 message_cv.notify_one();
46 } 44 }
47 45
48 void AddBackend(std::unique_ptr<Backend> backend) { 46 void AddBackend(std::unique_ptr<Backend> backend) {
@@ -86,15 +84,13 @@ private:
86 } 84 }
87 }; 85 };
88 while (true) { 86 while (true) {
89 { 87 entry = message_queue.PopWait();
90 std::unique_lock<std::mutex> lock(message_mutex); 88 if (entry.final_entry) {
91 message_cv.wait(lock, [&] { return !running || message_queue.Pop(entry); });
92 }
93 if (!running) {
94 break; 89 break;
95 } 90 }
96 write_logs(entry); 91 write_logs(entry);
97 } 92 }
93
98 // Drain the logging queue. Only writes out up to MAX_LOGS_TO_WRITE to prevent a case 94 // Drain the logging queue. Only writes out up to MAX_LOGS_TO_WRITE to prevent a case
99 // where a system is repeatedly spamming logs even on close. 95 // where a system is repeatedly spamming logs even on close.
100 const int MAX_LOGS_TO_WRITE = filter.IsDebug() ? INT_MAX : 100; 96 const int MAX_LOGS_TO_WRITE = filter.IsDebug() ? INT_MAX : 100;
@@ -106,14 +102,13 @@ private:
106 } 102 }
107 103
108 ~Impl() { 104 ~Impl() {
109 running = false; 105 Entry entry;
110 message_cv.notify_one(); 106 entry.final_entry = true;
107 message_queue.Push(entry);
111 backend_thread.join(); 108 backend_thread.join();
112 } 109 }
113 110
114 std::atomic_bool running{true}; 111 std::mutex writing_mutex;
115 std::mutex message_mutex, writing_mutex;
116 std::condition_variable message_cv;
117 std::thread backend_thread; 112 std::thread backend_thread;
118 std::vector<std::unique_ptr<Backend>> backends; 113 std::vector<std::unique_ptr<Backend>> backends;
119 Common::MPSCQueue<Log::Entry> message_queue; 114 Common::MPSCQueue<Log::Entry> message_queue;
diff --git a/src/common/logging/backend.h b/src/common/logging/backend.h
index 91bb0c309..a31ee6968 100644
--- a/src/common/logging/backend.h
+++ b/src/common/logging/backend.h
@@ -27,6 +27,7 @@ struct Entry {
27 unsigned int line_num; 27 unsigned int line_num;
28 std::string function; 28 std::string function;
29 std::string message; 29 std::string message;
30 bool final_entry = false;
30 31
31 Entry() = default; 32 Entry() = default;
32 Entry(Entry&& o) = default; 33 Entry(Entry&& o) = default;