summaryrefslogtreecommitdiff
path: root/other/bootstrap_daemon/src/log.c
diff options
context:
space:
mode:
authorMaxim Biro <nurupo.contributions@gmail.com>2015-12-31 01:24:31 -0500
committerMaxim Biro <nurupo.contributions@gmail.com>2015-12-31 01:24:31 -0500
commitc50781a2b7d0ab56acb31b8f79df2e69f9f74b8b (patch)
tree6d120de273d14850689eda796c694f6e4753cfb1 /other/bootstrap_daemon/src/log.c
parentbfc3c4eaac4eec6a2e714b7483b646d8ee21efc9 (diff)
Make logger a global object instead of an instance
Passing Logger object into every function isn't fun. See for yourself: something as simple as a public key printing function turns from >void print_public_key(const uint8_t *public_key) to >void print_public_key(Logger *logger, const uint8_t *public_key)
Diffstat (limited to 'other/bootstrap_daemon/src/log.c')
-rw-r--r--other/bootstrap_daemon/src/log.c115
1 files changed, 115 insertions, 0 deletions
diff --git a/other/bootstrap_daemon/src/log.c b/other/bootstrap_daemon/src/log.c
new file mode 100644
index 00000000..dff2dbad
--- /dev/null
+++ b/other/bootstrap_daemon/src/log.c
@@ -0,0 +1,115 @@
1/* logger.c
2 *
3 * Tox DHT bootstrap daemon.
4 *
5 * Copyright (C) 2015 Tox project All Rights Reserved.
6 *
7 * This file is part of Tox.
8 *
9 * Tox is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * Tox is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include "log.h"
25
26#include "global.h"
27
28#include <syslog.h>
29
30#include <stdarg.h>
31#include <stdio.h>
32
33LOGGER_BACKEND current_backend = -1;
34
35bool open_log(LOGGER_BACKEND backend)
36{
37 if (current_backend != -1) {
38 return false;
39 }
40
41 if (backend == LOGGER_BACKEND_SYSLOG) {
42 openlog(DAEMON_NAME, LOG_NOWAIT | LOG_PID, LOG_DAEMON);
43 }
44
45 current_backend = backend;
46
47 return true;
48}
49
50bool close_log()
51{
52 if (current_backend == -1) {
53 return false;
54 }
55
56 if (current_backend == LOGGER_BACKEND_SYSLOG) {
57 closelog();
58 }
59
60 current_backend = -1;
61
62 return true;
63}
64
65int level_syslog(LOG_LEVEL level)
66{
67 switch (level) {
68 case LOG_LEVEL_INFO:
69 return LOG_INFO;
70 case LOG_LEVEL_WARNING:
71 return LOG_WARNING;
72 case LOG_LEVEL_ERROR:
73 return LOG_ERR;
74 }
75}
76
77void log_syslog(LOG_LEVEL level, const char *format, va_list args)
78{
79 vsyslog(level_syslog(level), format, args);
80}
81
82FILE* level_stdout(LOG_LEVEL level)
83{
84 switch (level) {
85 case LOG_LEVEL_INFO:
86 return stdout;
87 case LOG_LEVEL_WARNING: // intentional fallthrough
88 case LOG_LEVEL_ERROR:
89 return stderr;
90 }
91}
92
93void log_stdout(LOG_LEVEL level, const char *format, va_list args)
94{
95 vfprintf(level_stdout(level), format, args);
96}
97
98bool log(LOG_LEVEL level, const char *format, ...)
99{
100 va_list args;
101 va_start(args, format);
102
103 switch (current_backend) {
104 case LOGGER_BACKEND_SYSLOG:
105 log_syslog(level, format, args);
106 break;
107 case LOGGER_BACKEND_STDOUT:
108 log_stdout(level, format, args);
109 break;
110 }
111
112 va_end(args);
113
114 return current_backend != -1;
115}