Merge pull request #2 from lioncash/char

logvisor: Use std::fputs/std::fputc where applicable
This commit is contained in:
Phillip Stephens 2019-08-26 15:54:58 -07:00 committed by GitHub
commit 81c72ad220
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 88 additions and 74 deletions

View File

@ -134,7 +134,7 @@ void logvisorAbort() {
for (i = 0; i < frames; i++) { for (i = 0; i < frames; i++) {
SymFromAddr(process, (DWORD64)(stack[i]), 0, symbol); SymFromAddr(process, (DWORD64)(stack[i]), 0, symbol);
fprintf(stderr, "%i: %s - 0x%0llX", frames - i - 1, symbol->Name, symbol->Address); std::fprintf(stderr, "%i: %s - 0x%0llX", frames - i - 1, symbol->Name, symbol->Address);
DWORD dwDisplacement; DWORD dwDisplacement;
IMAGEHLP_LINE64 line; IMAGEHLP_LINE64 line;
@ -143,14 +143,14 @@ void logvisorAbort() {
line.SizeOfStruct = sizeof(IMAGEHLP_LINE64); line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
if (SymGetLineFromAddr64(process, (DWORD64)(stack[i]), &dwDisplacement, &line)) { if (SymGetLineFromAddr64(process, (DWORD64)(stack[i]), &dwDisplacement, &line)) {
// SymGetLineFromAddr64 returned success // SymGetLineFromAddr64 returned success
fprintf(stderr, " LINE %d\n", int(line.LineNumber)); std::fprintf(stderr, " LINE %d\n", int(line.LineNumber));
} else { } else {
fprintf(stderr, "\n"); std::fputc('\n', stderr);
} }
} }
fflush(stderr); std::fflush(stderr);
free(symbol); std::free(symbol);
#endif #endif
@ -320,26 +320,29 @@ struct ConsoleLogger : public ILogger {
static void _reportHead(const char* modName, const char* sourceInfo, Level severity) { static void _reportHead(const char* modName, const char* sourceInfo, Level severity) {
/* Clear current line out */ /* Clear current line out */
int width = ConsoleWidth(); const int width = ConsoleWidth();
std::fputs("\r", stderr); std::fputc('\r', stderr);
for (int w = 0; w < width; ++w) for (int w = 0; w < width; ++w) {
std::fputs(" ", stderr); std::fputc(' ', stderr);
std::fputs("\r", stderr); }
std::fputc('\r', stderr);
std::chrono::steady_clock::duration tm = CurrentUptime(); const std::chrono::steady_clock::duration tm = CurrentUptime();
double tmd = tm.count() * std::chrono::steady_clock::duration::period::num / const double tmd = tm.count() * std::chrono::steady_clock::duration::period::num /
(double)std::chrono::steady_clock::duration::period::den; static_cast<double>(std::chrono::steady_clock::duration::period::den);
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 "{:5.4} "), tmd);
uint_fast64_t fIdx = FrameIndex.load(); const uint_fast64_t fIdx = FrameIndex.load();
if (fIdx) 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);
@ -357,55 +360,60 @@ 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
#if !WINDOWS_STORE #if !WINDOWS_STORE
SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_WHITE); SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_WHITE);
fprintf(stderr, "["); std::fputc('[', stderr);
SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_GREEN); SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
fprintf(stderr, "%5.4f ", tmd); std::fprintf(stderr, "%5.4f ", tmd);
uint64_t fi = FrameIndex.load(); const uint64_t fi = FrameIndex.load();
if (fi) if (fi != 0) {
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);
fprintf(stderr, "INFO"); std::fputs("INFO", stderr);
break; break;
case Warning: case Warning:
SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN); SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
fprintf(stderr, "WARNING"); std::fputs("WARNING", stderr);
break; break;
case Error: case Error:
SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_RED); SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_RED);
fprintf(stderr, "ERROR"); std::fputs("ERROR", stderr);
break; break;
case Fatal: case Fatal:
SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_RED); SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_RED);
fprintf(stderr, "FATAL ERROR"); std::fputs("FATAL ERROR", stderr);
break; break;
default: default:
break; break;
}; }
SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_WHITE); SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_WHITE);
fprintf(stderr, " %s", modName); std::fprintf(stderr, " %s", modName);
SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN); SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
if (sourceInfo) if (sourceInfo) {
fprintf(stderr, " {%s}", sourceInfo); std::fprintf(stderr, " {%s}", sourceInfo);
}
SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE); SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
if (thrName) if (thrName) {
fprintf(stderr, " (%s)", thrName); std::fprintf(stderr, " (%s)", thrName);
}
SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_WHITE); SetConsoleTextAttribute(Term, FOREGROUND_INTENSITY | FOREGROUND_WHITE);
fprintf(stderr, "] "); std::fputs("] ", stderr);
SetConsoleTextAttribute(Term, FOREGROUND_WHITE); SetConsoleTextAttribute(Term, FOREGROUND_WHITE);
#endif #endif
#else #else
std::fputs("[", stderr); std::fputc('[', stderr);
fmt::print(stderr, fmt("{:5.4} "), tmd); fmt::print(stderr, fmt("{:5.4} "), tmd);
uint_fast64_t fIdx = FrameIndex.load(); uint_fast64_t fIdx = FrameIndex.load();
if (fIdx) if (fIdx)
@ -425,44 +433,46 @@ struct ConsoleLogger : public ILogger {
break; break;
default: default:
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
} }
} }
void report(const char* modName, Level severity, fmt::string_view format, fmt::format_args args) { void report(const char* modName, Level severity, fmt::string_view format, fmt::format_args args) override {
_reportHead(modName, nullptr, severity); _reportHead(modName, nullptr, severity);
fmt::vprint(stderr, format, args); fmt::vprint(stderr, format, args);
std::fputs("\n", stderr); std::fputc('\n', stderr);
std::fflush(stderr); std::fflush(stderr);
} }
void report(const char* modName, Level severity, fmt::wstring_view format, fmt::wformat_args args) { void report(const char* modName, Level severity, fmt::wstring_view format, fmt::wformat_args args) override {
_reportHead(modName, nullptr, severity); _reportHead(modName, nullptr, severity);
fmt::vprint(stderr, format, args); fmt::vprint(stderr, format, args);
std::fputs("\n", stderr); std::fputc('\n', stderr);
std::fflush(stderr); std::fflush(stderr);
} }
void reportSource(const char* modName, Level severity, const char* file, unsigned linenum, void reportSource(const char* modName, Level severity, const char* file, unsigned linenum, fmt::string_view format,
fmt::string_view format, fmt::format_args args) { fmt::format_args args) override {
_reportHead(modName, fmt::format(fmt("{}:{}"), file, linenum).c_str(), severity); _reportHead(modName, fmt::format(fmt("{}:{}"), file, linenum).c_str(), severity);
fmt::vprint(stderr, format, args); fmt::vprint(stderr, format, args);
std::fputs("\n", stderr); std::fputc('\n', stderr);
std::fflush(stderr); std::fflush(stderr);
} }
void reportSource(const char* modName, Level severity, const char* file, unsigned linenum, void reportSource(const char* modName, Level severity, const char* file, unsigned linenum, fmt::wstring_view format,
fmt::wstring_view format, fmt::wformat_args args) { fmt::wformat_args args) override {
_reportHead(modName, fmt::format(fmt("{}:{}"), file, linenum).c_str(), severity); _reportHead(modName, fmt::format(fmt("{}:{}"), file, linenum).c_str(), severity);
fmt::vprint(stderr, format, args); fmt::vprint(stderr, format, args);
std::fputs("\n", stderr); std::fputc('\n', stderr);
std::fflush(stderr); std::fflush(stderr);
} }
}; };
@ -478,9 +488,9 @@ void CreateWin32Console() {
/* Debug console */ /* Debug console */
AllocConsole(); AllocConsole();
freopen("CONIN$", "r", stdin); std::freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout); std::freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr); std::freopen("CONOUT$", "w", stderr);
#endif #endif
} }
#endif #endif
@ -498,19 +508,21 @@ struct FileLogger : public ILogger {
virtual void closeFile() { std::fclose(fp); } virtual void closeFile() { std::fclose(fp); }
void _reportHead(const char* modName, const char* sourceInfo, Level severity) { void _reportHead(const char* modName, const char* sourceInfo, Level severity) {
std::chrono::steady_clock::duration tm = CurrentUptime(); const std::chrono::steady_clock::duration tm = CurrentUptime();
double tmd = tm.count() * std::chrono::steady_clock::duration::period::num / const double tmd = tm.count() * std::chrono::steady_clock::duration::period::num /
(double)std::chrono::steady_clock::duration::period::den; static_cast<double>(std::chrono::steady_clock::duration::period::den);
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];
}
std::fputs("[", fp); std::fputc('[', fp);
std::fprintf(fp, "%5.4f ", tmd); std::fprintf(fp, "%5.4f ", tmd);
uint_fast64_t fIdx = FrameIndex.load(); const uint_fast64_t fIdx = FrameIndex.load();
if (fIdx) if (fIdx != 0) {
std::fprintf(fp, "(%" PRIu64 ") ", fIdx); std::fprintf(fp, "(%" PRIu64 ") ", fIdx);
}
switch (severity) { switch (severity) {
case Info: case Info:
std::fputs("INFO", fp); std::fputs("INFO", fp);
@ -528,44 +540,46 @@ struct FileLogger : public ILogger {
break; break;
}; };
std::fprintf(fp, " %s", modName); std::fprintf(fp, " %s", modName);
if (sourceInfo) if (sourceInfo) {
std::fprintf(fp, " {%s}", sourceInfo); std::fprintf(fp, " {%s}", sourceInfo);
if (thrName) }
if (thrName) {
std::fprintf(fp, " (%s)", thrName); std::fprintf(fp, " (%s)", thrName);
}
std::fputs("] ", fp); std::fputs("] ", fp);
} }
void report(const char* modName, Level severity, fmt::string_view format, fmt::format_args args) { void report(const char* modName, Level severity, fmt::string_view format, fmt::format_args args) override {
openFile(); openFile();
_reportHead(modName, nullptr, severity); _reportHead(modName, nullptr, severity);
fmt::vprint(fp, format, args); fmt::vprint(fp, format, args);
std::fputs("\n", fp); std::fputc('\n', fp);
closeFile(); closeFile();
} }
void report(const char* modName, Level severity, fmt::wstring_view format, fmt::wformat_args args) { void report(const char* modName, Level severity, fmt::wstring_view format, fmt::wformat_args args) override {
openFile(); openFile();
_reportHead(modName, nullptr, severity); _reportHead(modName, nullptr, severity);
fmt::vprint(fp, format, args); fmt::vprint(fp, format, args);
std::fputs("\n", fp); std::fputc('\n', fp);
closeFile(); closeFile();
} }
void reportSource(const char* modName, Level severity, const char* file, unsigned linenum, void reportSource(const char* modName, Level severity, const char* file, unsigned linenum, fmt::string_view format,
fmt::string_view format, fmt::format_args args) { fmt::format_args args) override {
openFile(); openFile();
_reportHead(modName, fmt::format(fmt("{}:{}"), file, linenum).c_str(), severity); _reportHead(modName, fmt::format(fmt("{}:{}"), file, linenum).c_str(), severity);
fmt::vprint(fp, format, args); fmt::vprint(fp, format, args);
std::fputs("\n", fp); std::fputc('\n', fp);
closeFile(); closeFile();
} }
void reportSource(const char* modName, Level severity, const char* file, unsigned linenum, void reportSource(const char* modName, Level severity, const char* file, unsigned linenum, fmt::wstring_view format,
fmt::wstring_view format, fmt::wformat_args args) { fmt::wformat_args args) override {
openFile(); openFile();
_reportHead(modName, fmt::format(fmt("{}:{}"), file, linenum).c_str(), severity); _reportHead(modName, fmt::format(fmt("{}:{}"), file, linenum).c_str(), severity);
fmt::vprint(fp, format, args); fmt::vprint(fp, format, args);
std::fputs("\n", fp); std::fputc('\n', fp);
closeFile(); closeFile();
} }
}; };
@ -573,7 +587,7 @@ struct FileLogger : public ILogger {
struct FileLogger8 : public FileLogger { struct FileLogger8 : public FileLogger {
const char* m_filepath; const char* m_filepath;
FileLogger8(const char* filepath) : m_filepath(filepath) {} FileLogger8(const char* filepath) : m_filepath(filepath) {}
void openFile() { fp = std::fopen(m_filepath, "a"); } void openFile() override { fp = std::fopen(m_filepath, "a"); }
}; };
void RegisterFileLogger(const char* filepath) { void RegisterFileLogger(const char* filepath) {
@ -586,7 +600,7 @@ void RegisterFileLogger(const char* filepath) {
struct FileLogger16 : public FileLogger { struct FileLogger16 : public FileLogger {
const wchar_t* m_filepath; const wchar_t* m_filepath;
FileLogger16(const wchar_t* filepath) : m_filepath(filepath) {} FileLogger16(const wchar_t* filepath) : m_filepath(filepath) {}
void openFile() { fp = _wfopen(m_filepath, L"a"); } void openFile() override { fp = _wfopen(m_filepath, L"a"); }
}; };
void RegisterFileLogger(const wchar_t* filepath) { void RegisterFileLogger(const wchar_t* filepath) {