added colored output for win32 cmd in tracelog
This commit is contained in:
parent
915fd081f3
commit
d80773bb00
1 changed files with 36 additions and 12 deletions
48
tracelog.c
48
tracelog.c
|
|
@ -7,16 +7,16 @@
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#pragma warning(disable:4996) // _CRT_SECURE_NO_WARNINGS.
|
#pragma warning(disable:4996) // _CRT_SECURE_NO_WARNINGS.
|
||||||
#include "win32_slim.h"
|
#include "win32_slim.h"
|
||||||
#endif
|
#ifndef TLOG_VS
|
||||||
|
#define TLOG_WIN32_NO_VS
|
||||||
#ifdef TLOG_VS
|
|
||||||
#ifdef _WIN32
|
|
||||||
#ifndef TLOG_NO_COLOURS
|
#ifndef TLOG_NO_COLOURS
|
||||||
#define TLOG_NO_COLOURS
|
#define TLOG_NO_COLOURS
|
||||||
#endif
|
#endif
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "win32_slim.h"
|
#ifdef TLOG_VS
|
||||||
#else
|
#ifndef _WIN32
|
||||||
#error "can't use TLOG_VS if not on windows"
|
#error "can't use TLOG_VS if not on windows"
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -49,6 +49,22 @@
|
||||||
|
|
||||||
bool use_newline = true;
|
bool use_newline = true;
|
||||||
|
|
||||||
|
#ifdef TLOG_WIN32_NO_VS
|
||||||
|
static void setLevelColour(int level) {
|
||||||
|
WORD attribute = 15;
|
||||||
|
switch (level) {
|
||||||
|
case LogDebug: attribute = 1; break;
|
||||||
|
case LogInfo: attribute = 2; break;
|
||||||
|
case LogWarning: attribute = 6; break;
|
||||||
|
case LogError: attribute = 4; break;
|
||||||
|
case LogFatal: attribute = 4; break;
|
||||||
|
}
|
||||||
|
|
||||||
|
HANDLE hc = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
|
SetConsoleTextAttribute(hc, attribute);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void traceLog(int level, const char *fmt, ...) {
|
void traceLog(int level, const char *fmt, ...) {
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, fmt);
|
va_start(args, fmt);
|
||||||
|
|
@ -71,18 +87,26 @@ void traceLogVaList(int level, const char *fmt, va_list args) {
|
||||||
default: beg = ""; break;
|
default: beg = ""; break;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t offset = strlen(beg);
|
size_t offset = 0;
|
||||||
|
|
||||||
|
#ifndef TLOG_WIN32_NO_VS
|
||||||
|
offset = strlen(beg);
|
||||||
strncpy(buffer, beg, sizeof(buffer));
|
strncpy(buffer, beg, sizeof(buffer));
|
||||||
|
#endif
|
||||||
|
|
||||||
vsnprintf(buffer + offset, sizeof(buffer) - offset, fmt, args);
|
vsnprintf(buffer + offset, sizeof(buffer) - offset, fmt, args);
|
||||||
|
|
||||||
#ifdef _WIN32
|
#if defined(TLOG_VS)
|
||||||
SetConsoleOutputCP(CP_UTF8);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef TLOG_VS
|
|
||||||
OutputDebugStringA(buffer);
|
OutputDebugStringA(buffer);
|
||||||
if(use_newline) OutputDebugStringA("\n");
|
if(use_newline) OutputDebugStringA("\n");
|
||||||
|
#elif defined(TLOG_WIN32_NO_VS)
|
||||||
|
SetConsoleOutputCP(CP_UTF8);
|
||||||
|
setLevelColour(level);
|
||||||
|
printf("%s", beg);
|
||||||
|
// set back to white
|
||||||
|
setLevelColour(LogTrace);
|
||||||
|
printf("%s", buffer);
|
||||||
|
if(use_newline) puts("");
|
||||||
#else
|
#else
|
||||||
printf("%s", buffer);
|
printf("%s", buffer);
|
||||||
if(use_newline) puts("");
|
if(use_newline) puts("");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue