|
|
@@ -1,5 +1,45 @@ |
|
|
|
#include "IZ_log.h" |
|
|
|
|
|
|
|
bool IZ_LogIsSupportedTerminal() { |
|
|
|
bool result; |
|
|
|
const char *term = getenv("TERM"); |
|
|
|
result = term && strcmp(term, "") != 0; |
|
|
|
#ifndef _WIN32 |
|
|
|
return result; |
|
|
|
#else |
|
|
|
if (result) { |
|
|
|
return 1; |
|
|
|
} |
|
|
|
|
|
|
|
// Attempt to enable virtual terminal processing on Windows. |
|
|
|
// See: https://msdn.microsoft.com/en-us/library/windows/desktop/mt638032(v=vs.85).aspx |
|
|
|
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); |
|
|
|
if (hOut == INVALID_HANDLE_VALUE) { |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
DWORD dwMode = 0; |
|
|
|
if (!GetConsoleMode(hOut, &dwMode)) { |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
dwMode |= 0x4; // ENABLE_VIRTUAL_TERMINAL_PROCESSING |
|
|
|
if (!SetConsoleMode(hOut, dwMode)) { |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
return 1; |
|
|
|
#endif |
|
|
|
} |
|
|
|
|
|
|
|
bool IZ_LogIsSupportedColor() { |
|
|
|
#ifdef IZ_WIN64 |
|
|
|
return _isatty(_fileno(stdout)) && IZ_LogIsSupportedTerminal(); |
|
|
|
#else |
|
|
|
return isatty(fileno(stdout)) && IZ_LogIsSupportedTerminal(); |
|
|
|
#endif |
|
|
|
} |
|
|
|
|
|
|
|
void IZ_LogError(const char* fmt, ...) { |
|
|
|
#ifdef IZ_LOG_LEVEL_FLAG_ERROR |
|
|
|
char buffer[4096]; |
|
|
@@ -7,7 +47,13 @@ void IZ_LogError(const char* fmt, ...) { |
|
|
|
va_start(args, fmt); |
|
|
|
vsnprintf(buffer, sizeof(buffer), fmt, args); |
|
|
|
va_end(args); |
|
|
|
fprintf(stderr, RED "%12s" RESET " %s\n", IZ_LOG_DATE_FUNCTION(), buffer); |
|
|
|
|
|
|
|
if (IZ_LogIsSupportedColor()) { |
|
|
|
fprintf(stdout, RED "%12s" RESET " %s\n", IZ_LOG_DATE_FUNCTION(), buffer); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
fprintf(stderr, "%12s" " %s\n", IZ_LOG_DATE_FUNCTION(), buffer); |
|
|
|
#endif |
|
|
|
} |
|
|
|
|
|
|
@@ -19,18 +65,24 @@ void IZ_LogInfo(IZ_LogCategory category, const char* fmt, ...) { |
|
|
|
vsnprintf(buffer, sizeof(buffer), fmt, args); |
|
|
|
va_end(args); |
|
|
|
|
|
|
|
switch (category) { |
|
|
|
default: |
|
|
|
case IZ_LOG_CATEGORY_GENERIC: |
|
|
|
fprintf(stdout, CYN "%12s" RESET " %s\n", IZ_LOG_DATE_FUNCTION(), buffer); |
|
|
|
break; |
|
|
|
case IZ_LOG_CATEGORY_GLOBAL: |
|
|
|
fprintf(stdout, MAG "%12s" RESET " %s\n", IZ_LOG_DATE_FUNCTION(), buffer); |
|
|
|
break; |
|
|
|
case IZ_LOG_CATEGORY_INPUT: |
|
|
|
fprintf(stdout, GRN "%12s" RESET " %s\n", IZ_LOG_DATE_FUNCTION(), buffer); |
|
|
|
break; |
|
|
|
if (IZ_LogIsSupportedColor()) { |
|
|
|
switch (category) { |
|
|
|
default: |
|
|
|
case IZ_LOG_CATEGORY_GENERIC: |
|
|
|
fprintf(stdout, CYN "%12s" RESET " %s\n", IZ_LOG_DATE_FUNCTION(), buffer); |
|
|
|
break; |
|
|
|
case IZ_LOG_CATEGORY_GLOBAL: |
|
|
|
fprintf(stdout, MAG "%12s" RESET " %s\n", IZ_LOG_DATE_FUNCTION(), buffer); |
|
|
|
break; |
|
|
|
case IZ_LOG_CATEGORY_INPUT: |
|
|
|
fprintf(stdout, GRN "%12s" RESET " %s\n", IZ_LOG_DATE_FUNCTION(), buffer); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
fprintf(stdout, "%12s" " %s\n", IZ_LOG_DATE_FUNCTION(), buffer); |
|
|
|
#endif |
|
|
|
} |
|
|
|
|
|
|
@@ -42,11 +94,16 @@ void IZ_LogWarn(bool is_critical, const char* fmt, ...) { |
|
|
|
vsnprintf(buffer, sizeof(buffer), fmt, args); |
|
|
|
va_end(args); |
|
|
|
|
|
|
|
if (is_critical) { |
|
|
|
fprintf(stdout, WHT "%24s" RESET " %s\n", IZ_LOG_DATE_FUNCTION(), buffer); |
|
|
|
} else { |
|
|
|
fprintf(stdout, YEL "%24s" RESET " %s\n", IZ_LOG_DATE_FUNCTION(), buffer); |
|
|
|
if (IZ_LogIsSupportedColor()) { |
|
|
|
if (is_critical) { |
|
|
|
fprintf(stdout, WHT "%24s" RESET " %s\n", IZ_LOG_DATE_FUNCTION(), buffer); |
|
|
|
} else { |
|
|
|
fprintf(stdout, YEL "%24s" RESET " %s\n", IZ_LOG_DATE_FUNCTION(), buffer); |
|
|
|
} |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
fprintf(stdout, "%24s" " %s\n", IZ_LOG_DATE_FUNCTION(), buffer); |
|
|
|
#endif |
|
|
|
} |
|
|
|
|
|
|
@@ -57,7 +114,11 @@ void IZ_Log(const char* fmt, ...) { |
|
|
|
va_start(args, fmt); |
|
|
|
vsnprintf(buffer, sizeof(buffer), fmt, args); |
|
|
|
va_end(args); |
|
|
|
fprintf(stdout, BLU "%12s" RESET " %s\n", IZ_LOG_DATE_FUNCTION(), buffer); |
|
|
|
if (IZ_LogIsSupportedColor()) { |
|
|
|
fprintf(stdout, BLU "%12s" RESET " %s\n", IZ_LOG_DATE_FUNCTION(), buffer); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
fprintf(stdout, "%12s" " %s\n", IZ_LOG_DATE_FUNCTION(), buffer); |
|
|
|
#endif |
|
|
|
} |