diff options
Diffstat (limited to 'other/bootstrap_daemon/src/log.c')
-rw-r--r-- | other/bootstrap_daemon/src/log.c | 117 |
1 files changed, 117 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..d441b98e --- /dev/null +++ b/other/bootstrap_daemon/src/log.c | |||
@@ -0,0 +1,117 @@ | |||
1 | /* log.c | ||
2 | * | ||
3 | * Tox DHT bootstrap daemon. | ||
4 | * Logging utility with support of multipel logging backends. | ||
5 | * | ||
6 | * Copyright (C) 2015-2016 Tox project All Rights Reserved. | ||
7 | * | ||
8 | * This file is part of Tox. | ||
9 | * | ||
10 | * Tox is free software: you can redistribute it and/or modify | ||
11 | * it under the terms of the GNU General Public License as published by | ||
12 | * the Free Software Foundation, either version 3 of the License, or | ||
13 | * (at your option) any later version. | ||
14 | * | ||
15 | * Tox is distributed in the hope that it will be useful, | ||
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
18 | * GNU General Public License for more details. | ||
19 | * | ||
20 | * You should have received a copy of the GNU General Public License | ||
21 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
22 | * | ||
23 | */ | ||
24 | |||
25 | #include "log.h" | ||
26 | |||
27 | #include "global.h" | ||
28 | |||
29 | #include <syslog.h> | ||
30 | |||
31 | #include <stdarg.h> | ||
32 | #include <stdio.h> | ||
33 | |||
34 | LOG_BACKEND current_backend = -1; | ||
35 | |||
36 | bool open_log(LOG_BACKEND backend) | ||
37 | { | ||
38 | if (current_backend != -1) { | ||
39 | return false; | ||
40 | } | ||
41 | |||
42 | if (backend == LOG_BACKEND_SYSLOG) { | ||
43 | openlog(DAEMON_NAME, LOG_NOWAIT | LOG_PID, LOG_DAEMON); | ||
44 | } | ||
45 | |||
46 | current_backend = backend; | ||
47 | |||
48 | return true; | ||
49 | } | ||
50 | |||
51 | bool close_log() | ||
52 | { | ||
53 | if (current_backend == -1) { | ||
54 | return false; | ||
55 | } | ||
56 | |||
57 | if (current_backend == LOG_BACKEND_SYSLOG) { | ||
58 | closelog(); | ||
59 | } | ||
60 | |||
61 | current_backend = -1; | ||
62 | |||
63 | return true; | ||
64 | } | ||
65 | |||
66 | int level_syslog(LOG_LEVEL level) | ||
67 | { | ||
68 | switch (level) { | ||
69 | case LOG_LEVEL_INFO: | ||
70 | return LOG_INFO; | ||
71 | case LOG_LEVEL_WARNING: | ||
72 | return LOG_WARNING; | ||
73 | case LOG_LEVEL_ERROR: | ||
74 | return LOG_ERR; | ||
75 | } | ||
76 | } | ||
77 | |||
78 | void log_syslog(LOG_LEVEL level, const char *format, va_list args) | ||
79 | { | ||
80 | vsyslog(level_syslog(level), format, args); | ||
81 | } | ||
82 | |||
83 | FILE* level_stdout(LOG_LEVEL level) | ||
84 | { | ||
85 | switch (level) { | ||
86 | case LOG_LEVEL_INFO: | ||
87 | return stdout; | ||
88 | case LOG_LEVEL_WARNING: // intentional fallthrough | ||
89 | case LOG_LEVEL_ERROR: | ||
90 | return stderr; | ||
91 | } | ||
92 | } | ||
93 | |||
94 | void log_stdout(LOG_LEVEL level, const char *format, va_list args) | ||
95 | { | ||
96 | vfprintf(level_stdout(level), format, args); | ||
97 | fflush(level_stdout(level)); | ||
98 | } | ||
99 | |||
100 | bool write_log(LOG_LEVEL level, const char *format, ...) | ||
101 | { | ||
102 | va_list args; | ||
103 | va_start(args, format); | ||
104 | |||
105 | switch (current_backend) { | ||
106 | case LOG_BACKEND_SYSLOG: | ||
107 | log_syslog(level, format, args); | ||
108 | break; | ||
109 | case LOG_BACKEND_STDOUT: | ||
110 | log_stdout(level, format, args); | ||
111 | break; | ||
112 | } | ||
113 | |||
114 | va_end(args); | ||
115 | |||
116 | return current_backend != -1; | ||
117 | } | ||