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) endif (NOT WIN32)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DDEBUG") 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: # PROGRAMS:
add_executable(breakhack add_executable(breakhack

View File

@ -84,64 +84,40 @@ m_vsprintf(char *dest, size_t sz, const char *fmt, va_list args)
} }
void 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; va_list args;
char tstamp[10]; char tstamp[10];
timestamp(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); va_start(args, fmt);
vprintf(fmt, args); vfprintf(out, fmt, args);
va_end(args); va_end(args);
printf("\n"); 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 void

View File

@ -19,20 +19,36 @@
#ifndef UTIL_H_ #ifndef UTIL_H_
#define UTIL_H_ #define UTIL_H_
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
void #ifndef __FNAME__
fatal(const char *fmt, ...); #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 void
error(const char *fmt, ...); log_print(FILE *out,
const char *prefix,
void const char *file,
debug(const char *fmt, ...); int line,
const char *func,
void const char *fmt,
info(const char *fmt, ...); ...);
void * void *
ec_malloc(unsigned long size); ec_malloc(unsigned long size);