diff options
author | Maxim Biro <nurupo.contributions@gmail.com> | 2017-03-02 02:38:57 -0500 |
---|---|---|
committer | Maxim Biro <nurupo.contributions@gmail.com> | 2017-06-04 16:07:03 -0400 |
commit | b0aec02225b642b2e420e634dce919beee0cd0f2 (patch) | |
tree | df290f8e2f76efda437f1455411f576b3f9f7766 /other/bootstrap_daemon/src/log_backend_syslog.c | |
parent | 1e8fa85aadf602bdca3a540de09a8184f7139a6c (diff) |
Split daemon's logging backends in separate modules
Diffstat (limited to 'other/bootstrap_daemon/src/log_backend_syslog.c')
-rw-r--r-- | other/bootstrap_daemon/src/log_backend_syslog.c | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/other/bootstrap_daemon/src/log_backend_syslog.c b/other/bootstrap_daemon/src/log_backend_syslog.c new file mode 100644 index 00000000..88911a21 --- /dev/null +++ b/other/bootstrap_daemon/src/log_backend_syslog.c | |||
@@ -0,0 +1,79 @@ | |||
1 | /* | ||
2 | * Tox DHT bootstrap daemon. | ||
3 | * Syslog logging backend. | ||
4 | */ | ||
5 | |||
6 | /* | ||
7 | * Copyright © 2016-2017 The TokTok team. | ||
8 | * Copyright © 2015-2016 Tox project. | ||
9 | * | ||
10 | * This file is part of Tox, the free peer to peer instant messenger. | ||
11 | * | ||
12 | * Tox is free software: you can redistribute it and/or modify | ||
13 | * it under the terms of the GNU General Public License as published by | ||
14 | * the Free Software Foundation, either version 3 of the License, or | ||
15 | * (at your option) any later version. | ||
16 | * | ||
17 | * Tox is distributed in the hope that it will be useful, | ||
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
20 | * GNU General Public License for more details. | ||
21 | * | ||
22 | * You should have received a copy of the GNU General Public License | ||
23 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
24 | */ | ||
25 | #include "log_backend_syslog.h" | ||
26 | |||
27 | #include "global.h" | ||
28 | |||
29 | #include "../../../toxcore/ccompat.h" | ||
30 | |||
31 | #include <assert.h> | ||
32 | #include <stdio.h> | ||
33 | #include <syslog.h> | ||
34 | |||
35 | void log_backend_syslog_open(void) | ||
36 | { | ||
37 | openlog(DAEMON_NAME, LOG_NOWAIT | LOG_PID, LOG_DAEMON); | ||
38 | } | ||
39 | |||
40 | void log_backend_syslog_close(void) | ||
41 | { | ||
42 | closelog(); | ||
43 | } | ||
44 | |||
45 | static int log_backend_syslog_level(LOG_LEVEL level) | ||
46 | { | ||
47 | switch (level) { | ||
48 | case LOG_LEVEL_INFO: | ||
49 | return LOG_INFO; | ||
50 | |||
51 | case LOG_LEVEL_WARNING: | ||
52 | return LOG_WARNING; | ||
53 | |||
54 | case LOG_LEVEL_ERROR: | ||
55 | return LOG_ERR; | ||
56 | } | ||
57 | |||
58 | return LOG_INFO; | ||
59 | } | ||
60 | |||
61 | void log_backend_syslog_write(LOG_LEVEL level, const char *format, va_list args) | ||
62 | { | ||
63 | va_list args2; | ||
64 | |||
65 | va_copy(args2, args); | ||
66 | int size = vsnprintf(NULL, 0, format, args2); | ||
67 | va_end(args2); | ||
68 | |||
69 | assert(size >= 0); | ||
70 | |||
71 | if (size < 0) { | ||
72 | return; | ||
73 | } | ||
74 | |||
75 | VLA(char, buf, size + 1); | ||
76 | vsnprintf(buf, size + 1, format, args); | ||
77 | |||
78 | syslog(log_backend_syslog_level(level), "%s", buf); | ||
79 | } | ||