Skip to content

Commit

Permalink
logger: strip paths correctly
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
atraczyk committed Dec 12, 2016
1 parent f0bad69 commit afb5652
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
46 changes: 45 additions & 1 deletion src/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,18 @@
#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

static int consoleLog;
static int debugMode;
Expand Down Expand Up @@ -116,6 +124,23 @@ getHeader(const char* ctx)
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
logger(const int level, const char* format, ...)
{
Expand All @@ -127,6 +152,7 @@ logger(const int level, const char* format, ...)
vlogger(level, format, ap);
va_end(ap);
}
#endif

void
vlogger(const int level, const char *format, va_list ap)
Expand All @@ -143,12 +169,16 @@ vlogger(const int level, const char *format, va_list ap)
const char* color_header = CYAN;
const char* color_prefix = "";
#else
WORD color_header = CYAN;
#ifdef RING_UWP
WORD color_prefix = LIGHT_GREEN;
#else
WORD color_prefix = FOREGROUND_GREEN;
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:
Expand All @@ -160,8 +190,10 @@ vlogger(const int level, const char *format, va_list ap)
}

#ifdef _WIN32
#if !defined(RING_UWP)
GetConsoleScreenBufferInfo(hConsole, &consoleInfo);
saved_attributes = consoleInfo.wAttributes;
#endif
#endif

// must exist, check LOG_FORMAT
Expand All @@ -170,21 +202,31 @@ vlogger(const int level, const char *format, va_list ap)
#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);
#ifdef RING_UWP
char tmp[2048];
vsprintf(tmp, format, ap);
#endif
#ifndef _WIN32
fputs(END_COLOR, stderr);
#else
#if !defined(RING_UWP)
SetConsoleTextAttribute(hConsole, saved_attributes);
#endif
#endif
}
#ifndef _WIN32
fputs(color_prefix, stderr);
#else
#if !defined(RING_UWP)
SetConsoleTextAttribute(hConsole, color_prefix);
#endif
#endif

vfprintf(stderr, format, ap);
Expand All @@ -194,7 +236,9 @@ vlogger(const int level, const char *format, va_list ap)
#ifndef _WIN32
fputs(END_COLOR, stderr);
#else
#if !defined(RING_UWP)
SetConsoleTextAttribute(hConsole, saved_attributes);
#endif
#endif

} else {
Expand Down
22 changes: 20 additions & 2 deletions src/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ extern "C" {
/**
* 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)))
#elif defined(__GNUC__)
__attribute__((format(printf, 2, 3)))
Expand Down Expand Up @@ -67,10 +72,19 @@ void strErr();
#define XSTR(X) STR(X)

// Line return char in a string
#ifdef RING_UWP
#define ENDL " "
#else
#define ENDL "\n"
#endif

// Do not remove the "| " in following without modifying vlogger() code
#ifndef RING_UWP
#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__

Expand Down Expand Up @@ -105,7 +119,11 @@ void strErr();

#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

#else

Expand Down

0 comments on commit afb5652

Please sign in to comment.