Commit 0eb462a8 authored by Olivier SOLDANO's avatar Olivier SOLDANO Committed by Andreas Traczyk

change win32 logger for better readability

higten the colors, and reduce the size of the file paths
to fit in the fixed size window of win32 debug

Change-Id: I9b0d167b5f6b8ebdc74937129c31c7a828320bcf
Reviewed-by: Andreas Traczyk's avatarAndreas Traczyk <andreas.traczyk@savoirfairelinux.com>
parent 16133a06
......@@ -29,9 +29,9 @@
#include "client/ring_signal.h"
#ifdef RING_UWP
#include <sys_time.h>
# include <sys_time.h>
#else
#include <sys/time.h>
# include <sys/time.h>
#endif
#include <string>
......@@ -73,18 +73,12 @@
#define YELLOW "\033[01;33m"
#define CYAN "\033[22;36m"
#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 YELLOW FOREGROUND_RED + FOREGROUND_GREEN
#define CYAN FOREGROUND_BLUE + FOREGROUND_GREEN
#endif
#endif
#endif // _WIN32
static int consoleLog;
static int debugMode;
......@@ -118,7 +112,7 @@ getHeader(const char* ctx)
out.fill(prev_fill);
// Context
if (ctx) {
if (ctx){
#ifdef RING_UWP
out << "|" << std::setw(32) << ctx;
#else
......@@ -131,34 +125,38 @@ getHeader(const char* ctx)
return out.str();
}
#ifdef RING_UWP
#ifndef _WIN32
void
wlogger(const int level, const char* file, const char* format, ...)
logger(const int level, 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);
vlogger(level, format, ap);
va_end(ap);
}
#else
void
logger(const int level, const char* format, ...)
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, format, ap);
vlogger(level, buffer.c_str(), ap);
va_end(ap);
}
#endif
void
......@@ -175,17 +173,16 @@ vlogger(const int level, const char *format, va_list ap)
#ifndef _WIN32
const char* color_header = CYAN;
const char* color_prefix = "";
#else
#ifdef RING_UWP
WORD color_prefix = LIGHT_GREEN;
#else
WORD color_prefix = FOREGROUND_GREEN;
WORD color_header = CYAN;
#endif
#if defined(_WIN32) && !defined(RING_UWP)
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
WORD saved_attributes;
#endif
WORD color_header = CYAN;
#endif
switch (level) {
case LOG_ERR:
......@@ -196,57 +193,37 @@ vlogger(const int level, const char *format, va_list ap)
break;
}
#ifdef _WIN32
#if !defined(RING_UWP)
#ifndef _WIN32
fputs(color_header, stderr);
#elif !defined(RING_UWP)
GetConsoleScreenBufferInfo(hConsole, &consoleInfo);
saved_attributes = consoleInfo.wAttributes;
#endif
SetConsoleTextAttribute(hConsole, color_header);
#endif
// must exist, check LOG_FORMAT
auto sep = strchr(format, '|');
if (sep) {
#ifndef _WIN32
fputs(color_header, stderr);
#else
#if !defined(RING_UWP)
SetConsoleTextAttribute(hConsole, color_header);
#endif
#endif
std::string ctx(format, sep - format);
format = sep + 2;
fputs(getHeader(ctx.c_str()).c_str(), stderr);
auto sep = strchr(format, '|'); // must exist, check LOG_FORMAT
std::string ctx(format, sep - format);
format = sep + 2;
fputs(getHeader(ctx.c_str()).c_str(), stderr);
#ifdef RING_UWP
char tmp[4096];
vsprintf(tmp, format, ap);
ring::emitSignal<DRing::DebugSignal::MessageSend>(getHeader(ctx.c_str()).c_str() + std::string(tmp));
#endif
#ifndef _WIN32
fputs(END_COLOR, stderr);
#else
#if !defined(RING_UWP)
SetConsoleTextAttribute(hConsole, saved_attributes);
#endif
char tmp[4096];
vsprintf(tmp, format, ap);
ring::emitSignal<DRing::DebugSignal::MessageSend>(getHeader(ctx.c_str()).c_str() + std::string(tmp));
#endif
}
#ifndef _WIN32
fputs(END_COLOR, stderr);
fputs(color_prefix, stderr);
#else
#if !defined(RING_UWP)
#elif !defined(RING_UWP)
SetConsoleTextAttribute(hConsole, saved_attributes);
SetConsoleTextAttribute(hConsole, color_prefix);
#endif
#endif
vfprintf(stderr, format, ap);
if (not sep)
fputs(ENDL, stderr);
#ifndef _WIN32
fputs(END_COLOR, stderr);
#else
#if !defined(RING_UWP)
#elif !defined(RING_UWP)
SetConsoleTextAttribute(hConsole, saved_attributes);
#endif
#endif
} else {
......
......@@ -34,14 +34,13 @@ extern "C" {
/**
* Print something, coloring it depending on the level
*/
#ifdef RING_UWP
void wlogger(const int level, const char* file, const char* format, ...);
#ifdef _WIN32
void wlogger(const int level, const char* file, const char* format, ...)
#else
void logger(const int level, const char* format, ...)
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, 3, 4)))
#elif defined(__GNUC__)
__attribute__((format(printf, 2, 3)))
#endif
......@@ -71,6 +70,12 @@ void strErr();
#define STR(EXP) #EXP
#define XSTR(X) STR(X)
#ifdef RING_UWP
#define FILE_NAME_ONLY(X) (strrchr(X, '\\') ? strrchr(X, '\\') + 1 : X)
#elif defined(_WIN32)
#define FILE_NAME_ONLY(X) (strrchr(X, '/') ? strrchr(X, '/') + 1 : X)
#endif
// Line return char in a string
#ifdef RING_UWP
#define ENDL " "
......@@ -79,10 +84,9 @@ void strErr();
#endif
// Do not remove the "| " in following without modifying vlogger() code
#ifndef RING_UWP
#ifndef _WIN32
#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
......@@ -118,12 +122,7 @@ void strErr();
#define LOG_DEBUG EVENTLOG_SUCCESS
#define FILE_NAME __FILE__
#ifndef RING_UWP
#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
#define LOGGER(M, LEVEL, ...) wlogger(LEVEL, FILE_NAME,LOG_FORMAT(M, ##__VA_ARGS__))
#else
......
Markdown is supported
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