Skip to content

Commit 6b0e7bc

Browse files
committed
Log handling fix
Move syncstream out of log.h to avoid conflict with Qt Use thread local osyncstream instead of re-creating.
1 parent 63951bc commit 6b0e7bc

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

src/lib/fcitx-utils/log.cpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <utility>
2222
#include <vector>
2323
#include <format>
24+
#include <syncstream>
2425
#include "macros.h"
2526
#include "stringutils.h"
2627

@@ -33,9 +34,14 @@ FCITX_DEFINE_LOG_CATEGORY(defaultCategory, "default");
3334
using LogRule = std::pair<std::string, LogLevel>;
3435

3536
struct LogConfig {
36-
std::ostream *defaultLogStream = &std::cerr;
37-
bool showTimeDate = true;
38-
} globalLogConfig;
37+
static std::ostream *defaultLogStream;
38+
static thread_local std::osyncstream localLogStream;
39+
static bool showTimeDate;
40+
};
41+
42+
std::ostream *LogConfig::defaultLogStream = &std::cerr;
43+
thread_local std::osyncstream LogConfig::localLogStream{*defaultLogStream};
44+
bool LogConfig::showTimeDate = true;
3945

4046
bool validateLogLevel(std::underlying_type_t<LogLevel> l) {
4147
return (l >= 0 &&
@@ -163,7 +169,7 @@ void Log::setLogRule(const std::string &ruleString) {
163169
auto rules = stringutils::split(ruleString, ",");
164170
for (const auto &rule : rules) {
165171
if (rule == "notimedate") {
166-
globalLogConfig.showTimeDate = false;
172+
LogConfig::showTimeDate = false;
167173
continue;
168174
}
169175

@@ -185,14 +191,21 @@ void Log::setLogRule(const std::string &ruleString) {
185191
}
186192

187193
void Log::setLogStream(std::ostream &stream) {
188-
globalLogConfig.defaultLogStream = &stream;
194+
LogConfig::defaultLogStream = &stream;
189195
}
190196

191-
std::ostream &Log::logStream() { return *globalLogConfig.defaultLogStream; }
197+
std::ostream &Log::logStream() {
198+
auto *buf = LogConfig::defaultLogStream->rdbuf();
199+
if (LogConfig::localLogStream.get_wrapped() != buf) {
200+
LogConfig::localLogStream = std::osyncstream(buf);
201+
}
202+
return LogConfig::localLogStream;
203+
}
192204

193205
LogMessageBuilder::LogMessageBuilder(std::ostream &out, LogLevel l,
194206
const char *filename, int lineNumber)
195207
: out_(out) {
208+
out << std::noemit_on_flush;
196209
switch (l) {
197210
case LogLevel::Fatal:
198211
out_ << "F";
@@ -213,7 +226,7 @@ LogMessageBuilder::LogMessageBuilder(std::ostream &out, LogLevel l,
213226
break;
214227
}
215228

216-
if (globalLogConfig.showTimeDate) {
229+
if (LogConfig::showTimeDate) {
217230
try {
218231
auto now = std::chrono::time_point_cast<std::chrono::microseconds>(
219232
std::chrono::system_clock::now());
@@ -233,6 +246,9 @@ LogMessageBuilder::LogMessageBuilder(std::ostream &out, LogLevel l,
233246
out_ << filename << ":" << lineNumber << "] ";
234247
}
235248

236-
LogMessageBuilder::~LogMessageBuilder() { out_ << '\n'; }
249+
LogMessageBuilder::~LogMessageBuilder() {
250+
out_ << '\n' << std::emit_on_flush;
251+
out_.flush();
252+
}
237253

238254
} // namespace fcitx

src/lib/fcitx-utils/log.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include <fcitx-utils/tuplehelpers.h>
3535
#include <source_location> // IWYU pragma: keep
3636
#include <span>
37-
#include <syncstream>
3837

3938
namespace fcitx {
4039

@@ -293,13 +292,11 @@ template <typename MetaStringFileName, int N>
293292
class LogMessageBuilderWrapper {
294293
public:
295294
LogMessageBuilderWrapper(LogLevel l)
296-
: out_(Log::logStream()),
297-
builder_(out_, l, MetaStringFileName::data(), N) {}
295+
: builder_(Log::logStream(), l, MetaStringFileName::data(), N) {}
298296

299297
LogMessageBuilder &self() { return builder_; }
300298

301299
private:
302-
std::osyncstream out_;
303300
LogMessageBuilder builder_;
304301
};
305302

test/testlog.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
* SPDX-License-Identifier: LGPL-2.1-or-later
55
*
66
*/
7+
#include <iostream>
78
#include <sstream>
9+
#include <string>
10+
#include <tuple>
11+
#include <unordered_map>
12+
#include <vector>
813
#include "fcitx-utils/log.h"
9-
#include "fcitx-utils/metastring.h"
1014

1115
int main() {
1216
int a = 0;

0 commit comments

Comments
 (0)