diff options
Diffstat (limited to 'other/bootstrap_daemon/src')
-rw-r--r-- | other/bootstrap_daemon/src/log.c (renamed from other/bootstrap_daemon/src/logger.c) | 41 | ||||
-rw-r--r-- | other/bootstrap_daemon/src/log.h (renamed from other/bootstrap_daemon/src/logger.h) | 28 |
2 files changed, 35 insertions, 34 deletions
diff --git a/other/bootstrap_daemon/src/logger.c b/other/bootstrap_daemon/src/log.c index 3cf18fd6..dff2dbad 100644 --- a/other/bootstrap_daemon/src/logger.c +++ b/other/bootstrap_daemon/src/log.c | |||
@@ -21,7 +21,7 @@ | |||
21 | * | 21 | * |
22 | */ | 22 | */ |
23 | 23 | ||
24 | #include "logger.h" | 24 | #include "log.h" |
25 | 25 | ||
26 | #include "global.h" | 26 | #include "global.h" |
27 | 27 | ||
@@ -30,34 +30,36 @@ | |||
30 | #include <stdarg.h> | 30 | #include <stdarg.h> |
31 | #include <stdio.h> | 31 | #include <stdio.h> |
32 | 32 | ||
33 | typedef struct Logger { | 33 | LOGGER_BACKEND current_backend = -1; |
34 | LOGGER_BACKEND backend; | ||
35 | } Logger; | ||
36 | 34 | ||
37 | Logger* new_logger(LOGGER_BACKEND backend) | 35 | bool open_log(LOGGER_BACKEND backend) |
38 | { | 36 | { |
39 | if (backend == LOGGER_BACKEND_SYSLOG) { | 37 | if (current_backend != -1) { |
40 | openlog(DAEMON_NAME, LOG_NOWAIT | LOG_PID, LOG_DAEMON); | 38 | return false; |
41 | } | 39 | } |
42 | 40 | ||
43 | Logger* logger = malloc(sizeof(Logger)); | 41 | if (backend == LOGGER_BACKEND_SYSLOG) { |
44 | 42 | openlog(DAEMON_NAME, LOG_NOWAIT | LOG_PID, LOG_DAEMON); | |
45 | if (logger == NULL) { | ||
46 | return NULL; | ||
47 | } | 43 | } |
48 | 44 | ||
49 | logger->backend = backend; | 45 | current_backend = backend; |
50 | 46 | ||
51 | return logger; | 47 | return true; |
52 | } | 48 | } |
53 | 49 | ||
54 | void kill_logger(Logger* logger) | 50 | bool close_log() |
55 | { | 51 | { |
56 | if (backend == LOGGER_BACKEND_SYSLOG) { | 52 | if (current_backend == -1) { |
53 | return false; | ||
54 | } | ||
55 | |||
56 | if (current_backend == LOGGER_BACKEND_SYSLOG) { | ||
57 | closelog(); | 57 | closelog(); |
58 | } | 58 | } |
59 | 59 | ||
60 | free(logger); | 60 | current_backend = -1; |
61 | |||
62 | return true; | ||
61 | } | 63 | } |
62 | 64 | ||
63 | int level_syslog(LOG_LEVEL level) | 65 | int level_syslog(LOG_LEVEL level) |
@@ -85,7 +87,6 @@ FILE* level_stdout(LOG_LEVEL level) | |||
85 | case LOG_LEVEL_WARNING: // intentional fallthrough | 87 | case LOG_LEVEL_WARNING: // intentional fallthrough |
86 | case LOG_LEVEL_ERROR: | 88 | case LOG_LEVEL_ERROR: |
87 | return stderr; | 89 | return stderr; |
88 | |||
89 | } | 90 | } |
90 | } | 91 | } |
91 | 92 | ||
@@ -94,12 +95,12 @@ void log_stdout(LOG_LEVEL level, const char *format, va_list args) | |||
94 | vfprintf(level_stdout(level), format, args); | 95 | vfprintf(level_stdout(level), format, args); |
95 | } | 96 | } |
96 | 97 | ||
97 | void log(Logger* logger, LOG_LEVEL level, const char *format, ...) | 98 | bool log(LOG_LEVEL level, const char *format, ...) |
98 | { | 99 | { |
99 | va_list args; | 100 | va_list args; |
100 | va_start(args, format); | 101 | va_start(args, format); |
101 | 102 | ||
102 | switch (logger->backend) { | 103 | switch (current_backend) { |
103 | case LOGGER_BACKEND_SYSLOG: | 104 | case LOGGER_BACKEND_SYSLOG: |
104 | log_syslog(level, format, args); | 105 | log_syslog(level, format, args); |
105 | break; | 106 | break; |
@@ -109,4 +110,6 @@ void log(Logger* logger, LOG_LEVEL level, const char *format, ...) | |||
109 | } | 110 | } |
110 | 111 | ||
111 | va_end(args); | 112 | va_end(args); |
113 | |||
114 | return current_backend != -1; | ||
112 | } | 115 | } |
diff --git a/other/bootstrap_daemon/src/logger.h b/other/bootstrap_daemon/src/log.h index 174d3445..7e5dce99 100644 --- a/other/bootstrap_daemon/src/logger.h +++ b/other/bootstrap_daemon/src/log.h | |||
@@ -1,4 +1,4 @@ | |||
1 | /* logger.h | 1 | /* log.h |
2 | * | 2 | * |
3 | * Tox DHT bootstrap daemon. | 3 | * Tox DHT bootstrap daemon. |
4 | * | 4 | * |
@@ -21,8 +21,8 @@ | |||
21 | * | 21 | * |
22 | */ | 22 | */ |
23 | 23 | ||
24 | #ifndef LOGGER_H | 24 | #ifndef LOG_H |
25 | #define LOGGER_H | 25 | #define LOG_H |
26 | 26 | ||
27 | typedef enum LOGGER_BACKEND { | 27 | typedef enum LOGGER_BACKEND { |
28 | LOGGER_BACKEND_SYSLOG, | 28 | LOGGER_BACKEND_SYSLOG, |
@@ -35,29 +35,27 @@ typedef enum LOG_LEVEL { | |||
35 | LOG_LEVEL_ERROR | 35 | LOG_LEVEL_ERROR |
36 | } LOG_LEVEL; | 36 | } LOG_LEVEL; |
37 | 37 | ||
38 | typedef struct Logger Logger; | ||
39 | |||
40 | /** | 38 | /** |
41 | * Creates new logger. | 39 | * Initializes logger. |
42 | * @param backend Specifies which backend the logger should use. | 40 | * @param backend Specifies which backend to use. |
43 | * @return Logger object on success, NULL on failure. | 41 | * @return true on success, flase if log is already opened. |
44 | */ | 42 | */ |
45 | Logger* new_logger(LOGGER_BACKEND backend); | 43 | bool open_log(LOGGER_BACKEND backend); |
46 | 44 | ||
47 | /** | 45 | /** |
48 | * Destroys a logger object, releasing all used resources. | 46 | * Releases all used resources by the logger. |
49 | * @param logger Logger object to destroy. | 47 | * @return true on success, flase if log is already closed. |
50 | */ | 48 | */ |
51 | void kill_logger(Logger* logger); | 49 | bool close_log(); |
52 | 50 | ||
53 | /** | 51 | /** |
54 | * Logs a message. | 52 | * Logs a message. |
55 | * @param logger Logger object to use. | ||
56 | * @param level Log level to use. | 53 | * @param level Log level to use. |
57 | * @param format printf-like format string. | 54 | * @param format printf-like format string. |
58 | * @param ... Zero or more arguments, similar to printf function. | 55 | * @param ... Zero or more arguments, similar to printf function. |
56 | * @return true on success, flase if log is closed. | ||
59 | */ | 57 | */ |
60 | void log(Logger* logger, LOG_LEVEL level, const char *format, ...); | 58 | bool log(LOG_LEVEL level, const char *format, ...); |
61 | 59 | ||
62 | 60 | ||
63 | #endif // LOGGER_H | 61 | #endif // LOG_H |