add FILE* output

This commit is contained in:
2023-07-01 20:56:26 +02:00
parent fe5b21aad9
commit 2ff78f2f23
2 changed files with 36 additions and 19 deletions

View File

@@ -14,29 +14,36 @@
#ifndef DEBUG_H #ifndef DEBUG_H
#define DEBUG_H #define DEBUG_H
#include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <br.h>
#include <bits.h> #include <bits.h>
#define _unused __attribute__((__unused__))
#define _printf __attribute__ ((format (printf, 6, 7))) #define _printf __attribute__ ((format (printf, 6, 7)))
#ifdef DEBUG_DEBUG #ifdef DEBUG_DEBUG
void debug_init(u32 level);
void debug_init(u32 level, FILE *stream);
void debug_level_set(u32 level); void debug_level_set(u32 level);
u32 debug_level_get(void); u32 debug_level_get(void);
void debug_stream_set(FILE *stream);
void _printf debug(u32 level, bool timestamp, void _printf debug(u32 level, bool timestamp,
u32 indent, const char *src, u32 indent, const char *src,
u32 line, const char *fmt, ...); u32 line, const char *fmt, ...);
#else /* DEBUG_DEBUG */ #else /* DEBUG_DEBUG */
static inline void debug_init(_unused u32 level) {}
static inline void debug_level_set(_unused u32 level) {} static inline void debug_init(__unused u32 level, __unused FILE *stream) {}
static inline void _printf debug(_unused u32 level, _unused bool timestamp, static inline void debug_level_set(__unused u32 level) {}
_unused u32 indent, _unused const char *src, static inline u32 debug_level_get(void) {return 0;}
_unused u32 line, _unused const char *fmt, ...) {} static inline void debug_stream_set(__unused FILE *stream) {}
static inline void _printf debug(__unused u32 level, __unused bool timestamp,
__unused u32 indent, __unused const char *src,
__unused u32 line, __unused const char *fmt, ...) {}
#endif /* DEBUG_DEBUG */ #endif /* DEBUG_DEBUG */
#undef _unused
#undef _printf #undef _printf
/** /**

View File

@@ -27,18 +27,26 @@
static long long timer_start; /* in nanosecond */ static long long timer_start; /* in nanosecond */
static u32 debug_level = 0; static u32 debug_level = 0;
static FILE *stream = NULL;
void debug_level_set(u32 level) void debug_level_set(u32 level)
{ {
debug_level = level; debug_level = level;
log(0, "debug level set to %u\n", level);
log(1, "debug level set to %u\n", level);
} }
void debug_init(u32 level) void debug_stream_set(FILE *_stream)
{
stream = _stream;
log(0, "stream set to %d\n", stream? fileno(stream): -1);
}
void debug_init(u32 level, FILE *_stream)
{ {
struct timespec timer; struct timespec timer;
printf("stream=%p\n", _stream);
debug_stream_set(_stream);
debug_level_set(level); debug_level_set(level);
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;
@@ -66,28 +74,30 @@ inline static long long timer_elapsed()
void debug(u32 level, bool timestamp, u32 indent, const char *src, void debug(u32 level, bool timestamp, u32 indent, const char *src,
u32 line, const char *fmt, ...) u32 line, const char *fmt, ...)
{ {
if (level > debug_level) printf("str=%p lev=%d dlev=%d\n", stream, level, debug_level);
if (!stream || level > debug_level) {
return; return;
}
va_list ap; va_list ap;
if (indent) if (indent)
printf("%*s", 2*(indent-1), ""); fprintf(stream, "%*s", 2*(indent-1), "");
if (timestamp) { if (timestamp) {
long long diff = timer_elapsed(); long long diff = timer_elapsed();
printf("%lld.%03lld ", diff/NANOSEC, (diff/1000000)%1000); fprintf(stream, "%lld.%03lld ", diff/NANOSEC, (diff/1000000)%1000);
printf("%010lld ", diff); fprintf(stream, "%010lld ", diff);
} }
if (src) { if (src) {
if (line) if (line)
printf("[%s:%u] ", src, line); fprintf(stream, "[%s:%u] ", src, line);
else else
printf("[%s] ", src); fprintf(stream, "[%s] ", src);
} }
va_start(ap, fmt); va_start(ap, fmt);
vprintf(fmt, ap); vfprintf(stream, fmt, ap);
va_end(ap); va_end(ap);
} }