1#include "StructuredLogger.h"
10 void writeStrWithoutNewlines(
const char* str, FILE* stream) {
11 const char* start = str;
12 const char* end = str;
15 if (*end ==
'\n' || *end ==
'\r') {
17 std::fwrite(start, 1, end - start, stream);
19 std::fputc(
' ', stream);
26 std::fwrite(start, 1, end - start, stream);
37void Cogs::Logging::StructuredLogger::consumeMessage(
const char* source,
Category category, uint32_t ,
const char* message,
const char* filename,
int lineNumber)
39 thread_local static char buffer[500] = {};
40 constexpr const char categoryNames[] = {
'T',
'D',
'I',
'W',
'E',
'F' };
42 const char level = categoryNames[
static_cast<int>(category)];
45 auto now = std::chrono::system_clock::now();
46 auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
47 auto now_c = std::chrono::system_clock::to_time_t(now);
48 std::tm now_tm = *std::gmtime(&now_c);
51 std::strftime(timestamp,
sizeof(timestamp),
"%Y-%m-%dT%H:%M:%S", &now_tm);
52 std::snprintf(timestamp + 19, 6,
".%03dZ",
static_cast<int>(now_ms.count()));
55 if(outputFilenames && filename && filename[0] !=
'\0') {
56 n = std::snprintf(buffer,
sizeof(buffer) - 1,
"%c %s (%s:%d) [%s] ", level, timestamp, filename, lineNumber, source);
59 n = std::snprintf(buffer,
sizeof(buffer) - 1,
"%c %s [%s] ", level, timestamp, source);
64 n = std::min(n,
int(
sizeof(buffer)));
66 LockGuard lock(mutex);
68 std::fwrite(buffer, 1, n, stderr);
69 writeStrWithoutNewlines(message, stderr);
70 std::fputc(
'\n', stderr);
72 if(category >= Category::Error) {
static StructuredLogger & instance()
Retrieve the global StructuredLogger instance.
Category
Logging categories used to filter log messages.