debug: Add flush option

This commit is contained in:
2023-07-09 15:33:37 +02:00
parent 120a459206
commit 892bdcd004
2 changed files with 40 additions and 20 deletions

View File

@@ -25,20 +25,24 @@
#ifdef DEBUG_DEBUG #ifdef DEBUG_DEBUG
void debug_init(u32 level, FILE *stream); void debug_init(uint level, FILE *stream, bool flush);
void debug_level_set(u32 level); void debug_level_set(uint level);
void debug_stream_set(FILE *stream); void debug_stream_set(FILE *stream);
void _printf debug(u32 level, bool timestamp, void debug_flush_set(bool flush);
u32 indent, const char *src, void _printf debug(uint level, bool timestamp,
u32 line, const char *fmt, ...); uint indent, const char *src,
uint line, const char *fmt, ...);
#else /* DEBUG_DEBUG */ #else /* DEBUG_DEBUG */
static inline void debug_init(__unused u32 level, __unused FILE *stream) {} static inline void debug_init(__unused uint level,
static inline void debug_level_set(__unused u32 level) {} __unused FILE *stream,
_unused bool flush) {}
static inline void debug_level_set(__unused uint level) {}
static inline void debug_stream_set(__unused FILE *stream) {} static inline void debug_stream_set(__unused FILE *stream) {}
static inline void _printf debug(__unused u32 level, __unused bool timestamp, static inline void debug_flush_set(__unused bool level) {}
__unused u32 indent, __unused const char *src, static inline void _printf debug(__unused uint level, __unused bool timestamp,
__unused u32 line, __unused const char *fmt, ...) {} __unused uint indent, __unused const char *src,
__unused uint line, __unused const char *fmt, ...) {}
#endif /* DEBUG_DEBUG */ #endif /* DEBUG_DEBUG */

View File

@@ -26,10 +26,15 @@
#define MILLISEC 1000000 /* milli sec in sec */ #define MILLISEC 1000000 /* milli sec in sec */
static long long timer_start; /* in nanosecond */ static long long timer_start; /* in nanosecond */
static u32 debug_level = 0; static uint debug_level = 0;
static bool debug_flush = false;
static FILE *stream = NULL; static FILE *stream = NULL;
void debug_level_set(u32 level) /**
* debug_level_set() - set debug level.
* @level: unsigned debug level.
*/
void debug_level_set(uint level)
{ {
debug_level = level; debug_level = level;
log(0, "debug level set to %u\n", level); log(0, "debug level set to %u\n", level);
@@ -41,12 +46,19 @@ void debug_stream_set(FILE *_stream)
log(0, "stream set to %d\n", stream? fileno(stream): -1); log(0, "stream set to %d\n", stream? fileno(stream): -1);
} }
void debug_init(u32 level, FILE *_stream) void debug_flush_set(bool flush)
{
debug_flush = flush;
log(0, "debug flush set to %d\n", flush);
}
void debug_init(uint level, FILE *_stream, bool flush)
{ {
struct timespec timer; struct timespec timer;
debug_stream_set(_stream); debug_stream_set(_stream);
debug_level_set(level); debug_level_set(level);
debug_flush_set(flush);
if (!clock_gettime(CLOCK_MONOTONIC, &timer)) { if (!clock_gettime(CLOCK_MONOTONIC, &timer)) {
timer_start = timer.tv_sec * NANOSEC + timer.tv_nsec; timer_start = timer.tv_sec * NANOSEC + timer.tv_nsec;
} }
@@ -64,14 +76,16 @@ inline static long long timer_elapsed()
return (timer.tv_sec * NANOSEC + timer.tv_nsec) - timer_start; return (timer.tv_sec * NANOSEC + timer.tv_nsec) - timer_start;
} }
/* void debug - log function /**
* @timestamp : boolean * debug() - log function
* @indent : indent level (2 spaces each) * @level: log level
* @src : source file/func name (or NULL) * @timestamp: boolean, print timestamp if true
* @line : line number * @indent: indent level (2 spaces each)
* @src: source file/func name (or NULL)
* @line: line number
*/ */
void debug(u32 level, bool timestamp, u32 indent, const char *src, void debug(uint level, bool timestamp, uint indent, const char *src,
u32 line, const char *fmt, ...) uint line, const char *fmt, ...)
{ {
if (!stream || level > debug_level) if (!stream || level > debug_level)
return; return;
@@ -96,6 +110,8 @@ void debug(u32 level, bool timestamp, u32 indent, const char *src,
va_start(ap, fmt); va_start(ap, fmt);
vfprintf(stream, fmt, ap); vfprintf(stream, fmt, ap);
va_end(ap); va_end(ap);
if (debug_flush)
fflush(stream);
} }
#ifdef BIN_debug #ifdef BIN_debug