diff options
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | pn_log.c | 52 | ||||
-rw-r--r-- | pn_log.h | 25 |
3 files changed, 81 insertions, 1 deletions
@@ -3,6 +3,8 @@ CC := gcc CFLAGS := -O2 -ggdb -Wall -Wextra -Wno-unused-parameter -ansi -std=c99 LDFLAGS := -Wl,--no-undefined +override CFLAGS += -D_GNU_SOURCE + GIO_CFLAGS := $(shell pkg-config --cflags gio-2.0) GIO_LIBS := -lgio-2.0 -lgobject-2.0 -lglib-2.0 @@ -15,7 +17,8 @@ QUIET_LINK = @echo ' LINK '$@; QUIET_CLEAN = @echo ' CLEAN '$@; endif -test: test.o pn_core.o pn_session.o pn_node.o +test: test.o pn_core.o pn_session.o pn_node.o \ + pn_log.o test: override CFLAGS += $(GIO_CFLAGS) test: override LIBS += $(GIO_LIBS) diff --git a/pn_log.c b/pn_log.c new file mode 100644 index 0000000..1463efc --- /dev/null +++ b/pn_log.c @@ -0,0 +1,52 @@ +#include "pn_log.h" + +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> + +#include <glib.h> + +static inline char log_level_to_char(unsigned level) +{ + switch (level) { + case 0: return 'E'; + case 1: return 'W'; + case 2: return 'T'; + case 3: return 'I'; + case 4: return 'D'; + default: return 'O'; + } +} + +void _pn_helper(unsigned level, + void *object, + const char *file, + const char *function, + unsigned line, + const char *fmt, + ...) +{ + char *tmp; + va_list args; + char l = log_level_to_char(level); + + va_start(args, fmt); + + vasprintf(&tmp, fmt, args); + + if (level <= 1) + fprintf(stdout, "%c: %s: %s\n", l, function, tmp); + else if (level == 2) + fprintf(stdout, "%c: %s:%s(%u): %s\n", l, file, function, line, tmp); + else if (level == 3) + fprintf(stdout, "%c: %s: %s\n", l, function, tmp); +#if defined(DEBUG) + else if (level == 4) + fprintf(stdout, "%c: %s:%s(%u): %s\n", l, file, function, line, tmp); +#endif + fflush(stdout); + + free(tmp); + + va_end(args); +} diff --git a/pn_log.h b/pn_log.h new file mode 100644 index 0000000..7315b29 --- /dev/null +++ b/pn_log.h @@ -0,0 +1,25 @@ +#ifndef PN_LOG_H +#define PN_LOG_H + +void _pn_helper(unsigned level, + void *object, + const char *file, + const char *function, + unsigned line, + const char *fmt, + ...) __attribute__((format(printf, 6, 7))); + +#define pn_base(level, object, ...) _pn_helper(level, object, __FILE__, __func__, __LINE__, __VA_ARGS__) + +#define pn_err(object, ...) pn_base(0, object, __VA_ARGS__) +#define pn_warn(object, ...) pn_base(1, object, __VA_ARGS__) +#define pn_test(object, ...) pn_base(2, object, __VA_ARGS__) +#define pn_info(object, ...) pn_base(3, object, __VA_ARGS__) + +#if defined(DEBUG) +#define pn_debug(object, ...) pn_base(4, object, __VA_ARGS__) +#else +#define pn_debug(object, ...) ({ if (0) pn_base(4, object, __VA_ARGS__); }) +#endif + +#endif /* PN_LOG_H */ |