Coloured, file, function and line referenced debug output.

This commit is contained in:
Linus Probert 2018-04-21 22:56:48 +02:00
parent 0cb14e1827
commit 8647eaa7a4
3 changed files with 52 additions and 59 deletions

View File

@ -85,6 +85,7 @@ if (NOT WIN32)
endif (NOT WIN32)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DDEBUG")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D__FNAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'")
# PROGRAMS:
add_executable(breakhack

View File

@ -84,64 +84,40 @@ m_vsprintf(char *dest, size_t sz, const char *fmt, va_list args)
}
void
debug(const char *fmt, ...)
log_print(FILE *out,
const char *prefix,
const char *file,
int line,
const char *function,
const char * fmt,
...)
{
#ifdef DEBUG
va_list args;
char tstamp[10];
timestamp(tstamp, 10);
printf("[%s][--] ", tstamp);
#ifndef _WIN32
if (out == stdout || out == stderr) {
fprintf(out, "\033[34m[%s]", tstamp);
if (strcmp(prefix, "DEBUG") == 0)
fprintf(out, "\033[33m");
else if (strcmp(prefix, "FATAL") == 0 || strcmp(prefix, "ERROR") == 0)
fprintf(out, "\033[31m");
else
fprintf(out, "\033[32m");
fprintf(out, "[%5s]", prefix);
fprintf(out, "\033[36m[%20s:%-3d]\033[37m[%20s()]\033[0m ",
file, line, function);
} else {
fprintf(out, "[%s][%5s][%20s:%-3d][%20s()] ", tstamp, prefix, file, line, function);
}
#else
fprintf(out, "[%s][%5s][%20s:%-3d][%20s()] ", tstamp, prefix, file, line, function);
#endif
va_start(args, fmt);
vprintf(fmt, args);
vfprintf(out, fmt, args);
va_end(args);
printf("\n");
#else // DEBUG
UNUSED (fmt);
#endif // DEBUG
}
void
info(const char * fmt, ...)
{
va_list args;
char tstamp[10];
timestamp(tstamp, 10);
printf("[%s][**] ", tstamp);
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
printf("\n");
}
void
error(const char *fmt, ...)
{
va_list args;
char tstamp[10];
timestamp(tstamp, 10);
fprintf(stderr, "[%s][!*] ", tstamp);
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
}
void
fatal(const char *fmt, ...)
{
va_list args;
char tstamp[10];
timestamp(tstamp, 10);
fprintf(stderr, "[%s][!!] ", tstamp);
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
exit(-1);
}
void

View File

@ -19,20 +19,36 @@
#ifndef UTIL_H_
#define UTIL_H_
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
void
fatal(const char *fmt, ...);
#ifndef __FNAME__
#define __FNAME__ __FILE__
#endif // __FNAME__
#ifdef DEBUG
#define debug(...) log_print(stdout, "DEBUG", __FNAME__, __LINE__, __func__, __VA_ARGS__)
#define info(...) log_print(stdout, "INFO", __FNAME__, __LINE__, __func__, __VA_ARGS__)
#else // DEBUG
#define debug(...) do {} while(0)
#define info(...) do {} while(0)
#endif // DEBUG
#define error(...) log_print(stderr, "ERROR", __FNAME__, __LINE__, __func__, __VA_ARGS__)
#define fatal(...) \
{ \
log_print(stderr, "FATAL", __FNAME__, __LINE__, __func__, __VA_ARGS__); \
exit(-1); \
}
void
error(const char *fmt, ...);
void
debug(const char *fmt, ...);
void
info(const char *fmt, ...);
log_print(FILE *out,
const char *prefix,
const char *file,
int line,
const char *func,
const char *fmt,
...);
void *
ec_malloc(unsigned long size);