summaryrefslogtreecommitdiff
path: root/other
diff options
context:
space:
mode:
Diffstat (limited to 'other')
-rw-r--r--other/bootstrap_daemon/src/global.h30
-rw-r--r--other/bootstrap_daemon/src/logger.c112
-rw-r--r--other/bootstrap_daemon/src/logger.h63
-rw-r--r--other/bootstrap_daemon/src/tox-bootstrapd.c (renamed from other/bootstrap_daemon/tox-bootstrapd.c)0
4 files changed, 205 insertions, 0 deletions
diff --git a/other/bootstrap_daemon/src/global.h b/other/bootstrap_daemon/src/global.h
new file mode 100644
index 00000000..7aa42780
--- /dev/null
+++ b/other/bootstrap_daemon/src/global.h
@@ -0,0 +1,30 @@
1/* global.h
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#ifndef GLOBAL_H
25#define GLOBAL_H
26
27#define DAEMON_NAME "tox-bootstrapd"
28#define DAEMON_VERSION_NUMBER 2014101200UL // yyyymmmddvv format: yyyy year, mm month, dd day, vv version change count for that day
29
30#endif // GLOBAL_H
diff --git a/other/bootstrap_daemon/src/logger.c b/other/bootstrap_daemon/src/logger.c
new file mode 100644
index 00000000..3cf18fd6
--- /dev/null
+++ b/other/bootstrap_daemon/src/logger.c
@@ -0,0 +1,112 @@
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 "logger.h"
25
26#include "global.h"
27
28#include <syslog.h>
29
30#include <stdarg.h>
31#include <stdio.h>
32
33typedef struct Logger {
34 LOGGER_BACKEND backend;
35} Logger;
36
37Logger* new_logger(LOGGER_BACKEND backend)
38{
39 if (backend == LOGGER_BACKEND_SYSLOG) {
40 openlog(DAEMON_NAME, LOG_NOWAIT | LOG_PID, LOG_DAEMON);
41 }
42
43 Logger* logger = malloc(sizeof(Logger));
44
45 if (logger == NULL) {
46 return NULL;
47 }
48
49 logger->backend = backend;
50
51 return logger;
52}
53
54void kill_logger(Logger* logger)
55{
56 if (backend == LOGGER_BACKEND_SYSLOG) {
57 closelog();
58 }
59
60 free(logger);
61}
62
63int level_syslog(LOG_LEVEL level)
64{
65 switch (level) {
66 case LOG_LEVEL_INFO:
67 return LOG_INFO;
68 case LOG_LEVEL_WARNING:
69 return LOG_WARNING;
70 case LOG_LEVEL_ERROR:
71 return LOG_ERR;
72 }
73}
74
75void log_syslog(LOG_LEVEL level, const char *format, va_list args)
76{
77 vsyslog(level_syslog(level), format, args);
78}
79
80FILE* level_stdout(LOG_LEVEL level)
81{
82 switch (level) {
83 case LOG_LEVEL_INFO:
84 return stdout;
85 case LOG_LEVEL_WARNING: // intentional fallthrough
86 case LOG_LEVEL_ERROR:
87 return stderr;
88
89 }
90}
91
92void log_stdout(LOG_LEVEL level, const char *format, va_list args)
93{
94 vfprintf(level_stdout(level), format, args);
95}
96
97void log(Logger* logger, LOG_LEVEL level, const char *format, ...)
98{
99 va_list args;
100 va_start(args, format);
101
102 switch (logger->backend) {
103 case LOGGER_BACKEND_SYSLOG:
104 log_syslog(level, format, args);
105 break;
106 case LOGGER_BACKEND_STDOUT:
107 log_stdout(level, format, args);
108 break;
109 }
110
111 va_end(args);
112}
diff --git a/other/bootstrap_daemon/src/logger.h b/other/bootstrap_daemon/src/logger.h
new file mode 100644
index 00000000..174d3445
--- /dev/null
+++ b/other/bootstrap_daemon/src/logger.h
@@ -0,0 +1,63 @@
1/* logger.h
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#ifndef LOGGER_H
25#define LOGGER_H
26
27typedef enum LOGGER_BACKEND {
28 LOGGER_BACKEND_SYSLOG,
29 LOGGER_BACKEND_STDOUT
30} LOGGER_BACKEND;
31
32typedef enum LOG_LEVEL {
33 LOG_LEVEL_INFO,
34 LOG_LEVEL_WARNING,
35 LOG_LEVEL_ERROR
36} LOG_LEVEL;
37
38typedef struct Logger Logger;
39
40/**
41 * Creates new logger.
42 * @param backend Specifies which backend the logger should use.
43 * @return Logger object on success, NULL on failure.
44 */
45Logger* new_logger(LOGGER_BACKEND backend);
46
47/**
48 * Destroys a logger object, releasing all used resources.
49 * @param logger Logger object to destroy.
50 */
51void kill_logger(Logger* logger);
52
53/**
54 * Logs a message.
55 * @param logger Logger object to use.
56 * @param level Log level to use.
57 * @param format printf-like format string.
58 * @param ... Zero or more arguments, similar to printf function.
59 */
60void log(Logger* logger, LOG_LEVEL level, const char *format, ...);
61
62
63#endif // LOGGER_H
diff --git a/other/bootstrap_daemon/tox-bootstrapd.c b/other/bootstrap_daemon/src/tox-bootstrapd.c
index 71b1386d..71b1386d 100644
--- a/other/bootstrap_daemon/tox-bootstrapd.c
+++ b/other/bootstrap_daemon/src/tox-bootstrapd.c