Commit afb5652b authored by atraczyk's avatar atraczyk Committed by Andreas Traczyk
Browse files

logger: strip paths correctly

- adds wlogger which takes a the filename of the file calling the
  logger as a parameter, then strips the path

- changes colors so that when debugging the daemon as a win32
  application, the console text is brighter and easier to see

Change-Id: I62bec1af71f9aae0ff259c7b1ac0d9a7462a9de5
Tuleap: #790
parent f0bad69f
...@@ -71,10 +71,18 @@ ...@@ -71,10 +71,18 @@
#define YELLOW "\033[01;33m" #define YELLOW "\033[01;33m"
#define CYAN "\033[22;36m" #define CYAN "\033[22;36m"
#else #else
#ifdef RING_UWP
#define FOREGROUND_WHITE 0x000f
#define RED FOREGROUND_RED + 0x0008
#define YELLOW FOREGROUND_RED + FOREGROUND_GREEN + 0x0008
#define CYAN FOREGROUND_BLUE + FOREGROUND_GREEN + 0x0008
#define LIGHT_GREEN FOREGROUND_GREEN + 0x0008
#else
#define RED FOREGROUND_RED #define RED FOREGROUND_RED
#define YELLOW FOREGROUND_RED + FOREGROUND_GREEN #define YELLOW FOREGROUND_RED + FOREGROUND_GREEN
#define CYAN FOREGROUND_BLUE + FOREGROUND_GREEN #define CYAN FOREGROUND_BLUE + FOREGROUND_GREEN
#endif #endif
#endif
static int consoleLog; static int consoleLog;
static int debugMode; static int debugMode;
...@@ -116,6 +124,23 @@ getHeader(const char* ctx) ...@@ -116,6 +124,23 @@ getHeader(const char* ctx)
return out.str(); return out.str();
} }
#ifdef RING_UWP
void
wlogger(const int level, const char* file, const char* format, ...)
{
if (!debugMode && level == LOG_DEBUG)
return;
const char* file_name_only = FILE_NAME_ONLY(file);
std::string buffer(file_name_only);
buffer.append(format);
va_list ap;
va_start(ap, format);
vlogger(level, buffer.c_str(), ap);
va_end(ap);
}
#else
void void
logger(const int level, const char* format, ...) logger(const int level, const char* format, ...)
{ {
...@@ -127,6 +152,7 @@ logger(const int level, const char* format, ...) ...@@ -127,6 +152,7 @@ logger(const int level, const char* format, ...)
vlogger(level, format, ap); vlogger(level, format, ap);
va_end(ap); va_end(ap);
} }
#endif
void void
vlogger(const int level, const char *format, va_list ap) vlogger(const int level, const char *format, va_list ap)
...@@ -143,12 +169,16 @@ vlogger(const int level, const char *format, va_list ap) ...@@ -143,12 +169,16 @@ vlogger(const int level, const char *format, va_list ap)
const char* color_header = CYAN; const char* color_header = CYAN;
const char* color_prefix = ""; const char* color_prefix = "";
#else #else
WORD color_header = CYAN; #ifdef RING_UWP
WORD color_prefix = LIGHT_GREEN;
#else
WORD color_prefix = FOREGROUND_GREEN; WORD color_prefix = FOREGROUND_GREEN;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO consoleInfo; CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
WORD saved_attributes; WORD saved_attributes;
#endif #endif
WORD color_header = CYAN;
#endif
switch (level) { switch (level) {
case LOG_ERR: case LOG_ERR:
...@@ -160,8 +190,10 @@ vlogger(const int level, const char *format, va_list ap) ...@@ -160,8 +190,10 @@ vlogger(const int level, const char *format, va_list ap)
} }
#ifdef _WIN32 #ifdef _WIN32
#if !defined(RING_UWP)
GetConsoleScreenBufferInfo(hConsole, &consoleInfo); GetConsoleScreenBufferInfo(hConsole, &consoleInfo);
saved_attributes = consoleInfo.wAttributes; saved_attributes = consoleInfo.wAttributes;
#endif
#endif #endif
// must exist, check LOG_FORMAT // must exist, check LOG_FORMAT
...@@ -170,21 +202,31 @@ vlogger(const int level, const char *format, va_list ap) ...@@ -170,21 +202,31 @@ vlogger(const int level, const char *format, va_list ap)
#ifndef _WIN32 #ifndef _WIN32
fputs(color_header, stderr); fputs(color_header, stderr);
#else #else
#if !defined(RING_UWP)
SetConsoleTextAttribute(hConsole, color_header); SetConsoleTextAttribute(hConsole, color_header);
#endif
#endif #endif
std::string ctx(format, sep - format); std::string ctx(format, sep - format);
format = sep + 2; format = sep + 2;
fputs(getHeader(ctx.c_str()).c_str(), stderr); fputs(getHeader(ctx.c_str()).c_str(), stderr);
#ifdef RING_UWP
char tmp[2048];
vsprintf(tmp, format, ap);
#endif
#ifndef _WIN32 #ifndef _WIN32
fputs(END_COLOR, stderr); fputs(END_COLOR, stderr);
#else #else
#if !defined(RING_UWP)
SetConsoleTextAttribute(hConsole, saved_attributes); SetConsoleTextAttribute(hConsole, saved_attributes);
#endif
#endif #endif
} }
#ifndef _WIN32 #ifndef _WIN32
fputs(color_prefix, stderr); fputs(color_prefix, stderr);
#else #else
#if !defined(RING_UWP)
SetConsoleTextAttribute(hConsole, color_prefix); SetConsoleTextAttribute(hConsole, color_prefix);
#endif
#endif #endif
vfprintf(stderr, format, ap); vfprintf(stderr, format, ap);
...@@ -194,7 +236,9 @@ vlogger(const int level, const char *format, va_list ap) ...@@ -194,7 +236,9 @@ vlogger(const int level, const char *format, va_list ap)
#ifndef _WIN32 #ifndef _WIN32
fputs(END_COLOR, stderr); fputs(END_COLOR, stderr);
#else #else
#if !defined(RING_UWP)
SetConsoleTextAttribute(hConsole, saved_attributes); SetConsoleTextAttribute(hConsole, saved_attributes);
#endif
#endif #endif
} else { } else {
......
...@@ -34,8 +34,13 @@ extern "C" { ...@@ -34,8 +34,13 @@ extern "C" {
/** /**
* Print something, coloring it depending on the level * Print something, coloring it depending on the level
*/ */
void logger(const int level, const char* format, ...)
#ifdef _WIN32 #ifdef RING_UWP
void wlogger(const int level, const char* file, const char* format, ...);
#else
void logger(const int level, const char* format, ...)
#endif
#if defined(_WIN32) && !defined(RING_UWP)
__attribute__((format(gnu_printf, 2, 3))) __attribute__((format(gnu_printf, 2, 3)))
#elif defined(__GNUC__) #elif defined(__GNUC__)
__attribute__((format(printf, 2, 3))) __attribute__((format(printf, 2, 3)))
...@@ -67,10 +72,19 @@ void strErr(); ...@@ -67,10 +72,19 @@ void strErr();
#define XSTR(X) STR(X) #define XSTR(X) STR(X)
// Line return char in a string // Line return char in a string
#ifdef RING_UWP
#define ENDL " "
#else
#define ENDL "\n" #define ENDL "\n"
#endif
// Do not remove the "| " in following without modifying vlogger() code // Do not remove the "| " in following without modifying vlogger() code
#ifndef RING_UWP
#define LOG_FORMAT(M, ...) FILE_NAME ":" XSTR(__LINE__) "| " M, ##__VA_ARGS__ #define LOG_FORMAT(M, ...) FILE_NAME ":" XSTR(__LINE__) "| " M, ##__VA_ARGS__
#else
#define FILE_NAME_ONLY(X) (strrchr(X, '\\') ? strrchr(X, '\\') + 1 : X)
#define LOG_FORMAT(M, ...) ":" XSTR(__LINE__) "| " M, ##__VA_ARGS__
#endif
#ifdef __ANDROID__ #ifdef __ANDROID__
...@@ -105,7 +119,11 @@ void strErr(); ...@@ -105,7 +119,11 @@ void strErr();
#define FILE_NAME __FILE__ #define FILE_NAME __FILE__
#ifndef RING_UWP
#define LOGGER(M, LEVEL, ...) logger(LEVEL, LOG_FORMAT(M, ##__VA_ARGS__)) #define LOGGER(M, LEVEL, ...) logger(LEVEL, LOG_FORMAT(M, ##__VA_ARGS__))
#else
#define LOGGER(M, LEVEL, ...) wlogger(LEVEL, FILE_NAME, LOG_FORMAT(M, ##__VA_ARGS__))
#endif
#else #else
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment