Ensure only one console logger is able to be registered

This commit is contained in:
Jack Andersen 2019-09-30 21:20:45 -10:00
parent f623ace3b4
commit 187c35f3c4
1 changed files with 22 additions and 27 deletions

View File

@ -322,9 +322,8 @@ struct ConsoleLogger : public ILogger {
/* Clear current line out */ /* Clear current line out */
const int width = ConsoleWidth(); const int width = ConsoleWidth();
std::fputc('\r', stderr); std::fputc('\r', stderr);
for (int w = 0; w < width; ++w) { for (int w = 0; w < width; ++w)
std::fputc(' ', stderr); std::fputc(' ', stderr);
}
std::fputc('\r', stderr); std::fputc('\r', stderr);
const std::chrono::steady_clock::duration tm = CurrentUptime(); const std::chrono::steady_clock::duration tm = CurrentUptime();
@ -332,17 +331,15 @@ struct ConsoleLogger : public ILogger {
static_cast<double>(std::chrono::steady_clock::duration::period::den); static_cast<double>(std::chrono::steady_clock::duration::period::den);
const std::thread::id thrId = std::this_thread::get_id(); const std::thread::id thrId = std::this_thread::get_id();
const char* thrName = nullptr; const char* thrName = nullptr;
if (ThreadMap.find(thrId) != ThreadMap.end()) { if (ThreadMap.find(thrId) != ThreadMap.end())
thrName = ThreadMap[thrId]; thrName = ThreadMap[thrId];
}
if (XtermColor) { if (XtermColor) {
std::fputs(BOLD "[", stderr); std::fputs(BOLD "[", stderr);
fmt::print(stderr, fmt(GREEN "{:5.4} "), tmd); fmt::print(stderr, fmt(GREEN "{:.4f} "), tmd);
const uint_fast64_t fIdx = FrameIndex.load(); const uint_fast64_t fIdx = FrameIndex.load();
if (fIdx != 0) { if (fIdx != 0)
fmt::print(stderr, fmt("({}) "), fIdx); fmt::print(stderr, fmt("({}) "), fIdx);
}
switch (severity) { switch (severity) {
case Info: case Info:
std::fputs(BOLD CYAN "INFO", stderr); std::fputs(BOLD CYAN "INFO", stderr);
@ -360,12 +357,10 @@ struct ConsoleLogger : public ILogger {
break; break;
}; };
fmt::print(stderr, fmt(NORMAL BOLD " {}"), modName); fmt::print(stderr, fmt(NORMAL BOLD " {}"), modName);
if (sourceInfo) { if (sourceInfo)
fmt::print(stderr, fmt(BOLD YELLOW " {{}}"), sourceInfo); fmt::print(stderr, fmt(BOLD YELLOW " {{}}"), sourceInfo);
} if (thrName)
if (thrName) {
fmt::print(stderr, fmt(BOLD MAGENTA " ({})"), thrName); fmt::print(stderr, fmt(BOLD MAGENTA " ({})"), thrName);
}
std::fputs(NORMAL BOLD "] " NORMAL, stderr); std::fputs(NORMAL BOLD "] " NORMAL, stderr);
} else { } else {
#if _WIN32 #if _WIN32
@ -373,11 +368,10 @@ struct ConsoleLogger : public ILogger {
SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_WHITE); SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_WHITE);
std::fputc('[', stderr); std::fputc('[', stderr);
SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_GREEN); SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
std::fprintf(stderr, "%5.4f ", tmd); fmt::print(stderr, fmt("{:.4f} "), tmd);
const uint64_t fi = FrameIndex.load(); const uint64_t fi = FrameIndex.load();
if (fi != 0) { if (fi != 0)
std::fprintf(stderr, "(%" PRIu64 ") ", fi); std::fprintf(stderr, "(%" PRIu64 ") ", fi);
}
switch (severity) { switch (severity) {
case Info: case Info:
SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE); SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
@ -399,22 +393,20 @@ struct ConsoleLogger : public ILogger {
break; break;
} }
SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_WHITE); SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_WHITE);
std::fprintf(stderr, " %s", modName); fmt::print(stderr, fmt(" {}"), modName);
SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN); SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
if (sourceInfo) { if (sourceInfo)
std::fprintf(stderr, " {%s}", sourceInfo); fmt::print(stderr, fmt(" {{}}"), sourceInfo);
}
SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE); SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
if (thrName) { if (thrName)
std::fprintf(stderr, " (%s)", thrName); fmt::print(stderr, fmt(" ({})"), thrName);
}
SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_WHITE); SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_WHITE);
std::fputs("] ", stderr); std::fputs("] ", stderr);
SetConsoleTextAttribute(Term, FOREGROUND_WHITE); SetConsoleTextAttribute(Term, FOREGROUND_WHITE);
#endif #endif
#else #else
std::fputc('[', stderr); std::fputc('[', stderr);
fmt::print(stderr, fmt("{:5.4} "), tmd); fmt::print(stderr, fmt("{:.4f} "), tmd);
uint_fast64_t fIdx = FrameIndex.load(); uint_fast64_t fIdx = FrameIndex.load();
if (fIdx) if (fIdx)
fmt::print(stderr, fmt("({}) "), fIdx); fmt::print(stderr, fmt("({}) "), fIdx);
@ -435,12 +427,10 @@ struct ConsoleLogger : public ILogger {
break; break;
} }
fmt::print(stderr, fmt(" {}"), modName); fmt::print(stderr, fmt(" {}"), modName);
if (sourceInfo) { if (sourceInfo)
fmt::print(stderr, fmt(" {{}}"), sourceInfo); fmt::print(stderr, fmt(" {{}}"), sourceInfo);
} if (thrName)
if (thrName) {
fmt::print(stderr, fmt(" ({})"), thrName); fmt::print(stderr, fmt(" ({})"), thrName);
}
std::fputs("] ", stderr); std::fputs("] ", stderr);
#endif #endif
} }
@ -477,9 +467,14 @@ struct ConsoleLogger : public ILogger {
} }
}; };
static bool ConsoleLoggerRegistered = false;
void RegisterConsoleLogger() { void RegisterConsoleLogger() {
/* Otherwise construct new console logger */ /* Otherwise construct new console logger */
MainLoggers.emplace_back(new ConsoleLogger); if (!ConsoleLoggerRegistered) {
MainLoggers.emplace_back(new ConsoleLogger);
ConsoleLoggerRegistered = true;
}
} }
#if _WIN32 #if _WIN32