diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-02-27 05:27:13 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-03-16 02:25:06 +0000 |
commit | 2f39bd33c32ffcb6624bb3ccd0cff5af4bc2bf8a (patch) | |
tree | 02d41f3f050aa5a47fbd7da4f1c18821db86b3c9 /toxcore | |
parent | fa8927aa0f90be1b15ef1d059c6f2ce81e2be8f5 (diff) |
Add default stderr logger for logging to nullptr.
This is useful for debugging a function that doesn't have a logger
available. It should not be used in production code, since it outputs to
stderr.
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/ccompat.h | 4 | ||||
-rw-r--r-- | toxcore/logger.c | 46 | ||||
-rw-r--r-- | toxcore/logger.h | 13 |
3 files changed, 56 insertions, 7 deletions
diff --git a/toxcore/ccompat.h b/toxcore/ccompat.h index 1ceb5a5a..b70850d2 100644 --- a/toxcore/ccompat.h +++ b/toxcore/ccompat.h | |||
@@ -43,9 +43,9 @@ | |||
43 | #endif | 43 | #endif |
44 | 44 | ||
45 | #ifdef __GNUC__ | 45 | #ifdef __GNUC__ |
46 | #define GNU_PRINTF __attribute__((__format__(__printf__, 6, 7))) | 46 | #define GNU_PRINTF(f, a) __attribute__((__format__(__printf__, f, a))) |
47 | #else | 47 | #else |
48 | #define GNU_PRINTF | 48 | #define GNU_PRINTF(f, a) |
49 | #endif | 49 | #endif |
50 | 50 | ||
51 | #endif /* CCOMPAT_H */ | 51 | #endif /* CCOMPAT_H */ |
diff --git a/toxcore/logger.c b/toxcore/logger.c index 18b765a3..80195e93 100644 --- a/toxcore/logger.c +++ b/toxcore/logger.c | |||
@@ -39,6 +39,42 @@ struct Logger { | |||
39 | }; | 39 | }; |
40 | 40 | ||
41 | 41 | ||
42 | static const char *logger_level_name(LOGGER_LEVEL level) | ||
43 | { | ||
44 | switch (level) { | ||
45 | case LOG_TRACE: | ||
46 | return "TRACE"; | ||
47 | |||
48 | case LOG_DEBUG: | ||
49 | return "DEBUG"; | ||
50 | |||
51 | case LOG_INFO: | ||
52 | return "INFO"; | ||
53 | |||
54 | case LOG_WARNING: | ||
55 | return "WARNING"; | ||
56 | |||
57 | case LOG_ERROR: | ||
58 | return "ERROR"; | ||
59 | } | ||
60 | |||
61 | return "<unknown>"; | ||
62 | } | ||
63 | |||
64 | static void logger_stderr_handler(void *context, LOGGER_LEVEL level, const char *file, int line, const char *func, | ||
65 | const char *message, void *userdata) | ||
66 | { | ||
67 | // GL stands for "global logger". | ||
68 | fprintf(stderr, "[GL] %s %s:%d(%s): %s\n", logger_level_name(level), file, line, func, message); | ||
69 | } | ||
70 | |||
71 | static const Logger logger_stderr = { | ||
72 | logger_stderr_handler, | ||
73 | nullptr, | ||
74 | nullptr, | ||
75 | }; | ||
76 | |||
77 | |||
42 | /** | 78 | /** |
43 | * Public Functions | 79 | * Public Functions |
44 | */ | 80 | */ |
@@ -59,10 +95,14 @@ void logger_callback_log(Logger *log, logger_cb *function, void *context, void * | |||
59 | log->userdata = userdata; | 95 | log->userdata = userdata; |
60 | } | 96 | } |
61 | 97 | ||
62 | void logger_write(Logger *log, LOGGER_LEVEL level, const char *file, int line, const char *func, const char *format, | 98 | void logger_write(const Logger *log, LOGGER_LEVEL level, const char *file, int line, const char *func, |
63 | ...) | 99 | const char *format, ...) |
64 | { | 100 | { |
65 | if (!log || !log->callback) { | 101 | if (!log) { |
102 | log = &logger_stderr; | ||
103 | } | ||
104 | |||
105 | if (!log->callback) { | ||
66 | return; | 106 | return; |
67 | } | 107 | } |
68 | 108 | ||
diff --git a/toxcore/logger.h b/toxcore/logger.h index b3a8f7dc..2a6ee2ed 100644 --- a/toxcore/logger.h +++ b/toxcore/logger.h | |||
@@ -50,6 +50,9 @@ typedef void logger_cb(void *context, LOGGER_LEVEL level, const char *file, int | |||
50 | */ | 50 | */ |
51 | Logger *logger_new(void); | 51 | Logger *logger_new(void); |
52 | 52 | ||
53 | /** | ||
54 | * Frees all resources associated with the logger. | ||
55 | */ | ||
53 | void logger_kill(Logger *log); | 56 | void logger_kill(Logger *log); |
54 | 57 | ||
55 | /** | 58 | /** |
@@ -59,10 +62,16 @@ void logger_kill(Logger *log); | |||
59 | void logger_callback_log(Logger *log, logger_cb *function, void *context, void *userdata); | 62 | void logger_callback_log(Logger *log, logger_cb *function, void *context, void *userdata); |
60 | 63 | ||
61 | /** | 64 | /** |
62 | * Main write function. If logging disabled does nothing. | 65 | * Main write function. If logging is disabled, this does nothing. |
66 | * | ||
67 | * If the logger is NULL, this writes to stderr. This behaviour should not be | ||
68 | * used in production code, but can be useful for temporarily debugging a | ||
69 | * function that does not have a logger available. It's essentially | ||
70 | * fprintf(stderr, ...), but with timestamps and source location. | ||
63 | */ | 71 | */ |
64 | void logger_write( | 72 | void logger_write( |
65 | Logger *log, LOGGER_LEVEL level, const char *file, int line, const char *func, const char *format, ...) GNU_PRINTF; | 73 | const Logger *log, LOGGER_LEVEL level, const char *file, int line, const char *func, |
74 | const char *format, ...) GNU_PRINTF(6, 7); | ||
66 | 75 | ||
67 | 76 | ||
68 | #define LOGGER_WRITE(log, level, ...) \ | 77 | #define LOGGER_WRITE(log, level, ...) \ |