MCPcopy Create free account
hub / github.com/Snapchat/Valdi / logToExternalLogger

Function logToExternalLogger

libs/utils/src/utils/logging/Logger.cpp:33–84  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

31} // namespace
32
33void logToExternalLogger(LogLevel logLevel, LogContext logContext, const std::string& tag, const std::string& message) {
34 if (auto* logger = gExternalLogger.load()) {
35 try {
36 logger->log(logLevel, logContext, tag, message);
37 } catch (std::range_error&) {
38 logger->log(logLevel, logContext, tag, "[INVALID LOG MESSAGE]");
39 }
40 } else {
41 if (logLevel < gMaxInternalLogLevel) {
42 return;
43 }
44
45#if defined(SC_DESKTOP) || defined(EMSCRIPTEN)
46
47#if defined(EMSCRIPTEN)
48
49 fmt::print("[{}] {} {} {}\n", toString(logLevel), toString(logContext), tag, message);
50
51#else // defined(EMSCRIPTEN)
52
53 timeval curTime;
54 gettimeofday(&curTime, nullptr);
55 int64_t milliseconds = curTime.tv_sec * 1000 + curTime.tv_usec / 1000;
56 auto getStyle = [&] {
57 static bool useColor = getenv("TERM") != nullptr;
58 if (!useColor) {
59 return fmt::text_style();
60 }
61 switch (logLevel) {
62 case LogLevel::Warn:
63 return fmt::fg(fmt::terminal_color::yellow);
64 case LogLevel::Error:
65 return fmt::fg(fmt::terminal_color::red);
66 default:
67 return fmt::fg(fmt::terminal_color::white);
68 }
69 };
70 fmt::print(getStyle(),
71 "[{}][{:<14}] [{}] {} {} {}",
72 toString(logLevel),
73 std::this_thread::get_id(),
74 millisecondsToString(milliseconds),
75 toString(logContext),
76 tag,
77 message);
78 fmt::print("\n");
79
80#endif // defined(EMSCRIPTEN)
81
82#endif // defined(SC_DESKTOP) || defined(EMSCRIPTEN)
83 }
84}
85
86void setExternalLogger(Logger* logger) {
87 gExternalLogger.store(logger);

Callers 1

logFunction · 0.85

Calls 6

printFunction · 0.85
getStyleFunction · 0.85
millisecondsToStringFunction · 0.85
toStringFunction · 0.70
loadMethod · 0.65
logMethod · 0.65

Tested by

no test coverage detected