From c50781a2b7d0ab56acb31b8f79df2e69f9f74b8b Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Thu, 31 Dec 2015 01:24:31 -0500 Subject: 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) --- other/bootstrap_daemon/src/log.h | 61 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 other/bootstrap_daemon/src/log.h (limited to 'other/bootstrap_daemon/src/log.h') diff --git a/other/bootstrap_daemon/src/log.h b/other/bootstrap_daemon/src/log.h new file mode 100644 index 00000000..7e5dce99 --- /dev/null +++ b/other/bootstrap_daemon/src/log.h @@ -0,0 +1,61 @@ +/* log.h + * + * Tox DHT bootstrap daemon. + * + * Copyright (C) 2015 Tox project All Rights Reserved. + * + * This file is part of Tox. + * + * Tox is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tox is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tox. If not, see . + * + */ + +#ifndef LOG_H +#define LOG_H + +typedef enum LOGGER_BACKEND { + LOGGER_BACKEND_SYSLOG, + LOGGER_BACKEND_STDOUT +} LOGGER_BACKEND; + +typedef enum LOG_LEVEL { + LOG_LEVEL_INFO, + LOG_LEVEL_WARNING, + LOG_LEVEL_ERROR +} LOG_LEVEL; + +/** + * Initializes logger. + * @param backend Specifies which backend to use. + * @return true on success, flase if log is already opened. + */ +bool open_log(LOGGER_BACKEND backend); + +/** + * Releases all used resources by the logger. + * @return true on success, flase if log is already closed. + */ +bool close_log(); + +/** + * Logs a message. + * @param level Log level to use. + * @param format printf-like format string. + * @param ... Zero or more arguments, similar to printf function. + * @return true on success, flase if log is closed. + */ +bool log(LOG_LEVEL level, const char *format, ...); + + +#endif // LOG_H -- cgit v1.2.3 From 8c812f534175d43d93e022af49f0441cfca0ea0d Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Thu, 31 Dec 2015 02:44:44 -0500 Subject: Make daemon use the new log code "log" is a reserved name (log from math.h), so it got changed into write_log. --- other/bootstrap_daemon/Makefile.inc | 4 +- other/bootstrap_daemon/src/log.c | 2 +- other/bootstrap_daemon/src/log.h | 6 +- other/bootstrap_daemon/src/tox-bootstrapd.c | 164 ++++++++++++++-------------- 4 files changed, 90 insertions(+), 86 deletions(-) (limited to 'other/bootstrap_daemon/src/log.h') diff --git a/other/bootstrap_daemon/Makefile.inc b/other/bootstrap_daemon/Makefile.inc index f274aba0..eb94f0e7 100644 --- a/other/bootstrap_daemon/Makefile.inc +++ b/other/bootstrap_daemon/Makefile.inc @@ -3,7 +3,9 @@ if BUILD_DHT_BOOTSTRAP_DAEMON bin_PROGRAMS += tox-bootstrapd tox_bootstrapd_SOURCES = \ - ../other/bootstrap_daemon/tox-bootstrapd.c + ../other/bootstrap_daemon/src/tox-bootstrapd.c \ + ../other/bootstrap_daemon/src/log.c \ + ../other/bootstrap_daemon/src/log.h tox_bootstrapd_CFLAGS = \ -I$(top_srcdir)/other/bootstrap_daemon \ diff --git a/other/bootstrap_daemon/src/log.c b/other/bootstrap_daemon/src/log.c index dff2dbad..b5f838fb 100644 --- a/other/bootstrap_daemon/src/log.c +++ b/other/bootstrap_daemon/src/log.c @@ -95,7 +95,7 @@ void log_stdout(LOG_LEVEL level, const char *format, va_list args) vfprintf(level_stdout(level), format, args); } -bool log(LOG_LEVEL level, const char *format, ...) +bool write_log(LOG_LEVEL level, const char *format, ...) { va_list args; va_start(args, format); diff --git a/other/bootstrap_daemon/src/log.h b/other/bootstrap_daemon/src/log.h index 7e5dce99..b97d3733 100644 --- a/other/bootstrap_daemon/src/log.h +++ b/other/bootstrap_daemon/src/log.h @@ -24,6 +24,8 @@ #ifndef LOG_H #define LOG_H +#include + typedef enum LOGGER_BACKEND { LOGGER_BACKEND_SYSLOG, LOGGER_BACKEND_STDOUT @@ -49,13 +51,13 @@ bool open_log(LOGGER_BACKEND backend); bool close_log(); /** - * Logs a message. + * Writes a message to the log. * @param level Log level to use. * @param format printf-like format string. * @param ... Zero or more arguments, similar to printf function. * @return true on success, flase if log is closed. */ -bool log(LOG_LEVEL level, const char *format, ...); +bool write_log(LOG_LEVEL level, const char *format, ...); #endif // LOG_H diff --git a/other/bootstrap_daemon/src/tox-bootstrapd.c b/other/bootstrap_daemon/src/tox-bootstrapd.c index 71b1386d..9a4ccf9f 100644 --- a/other/bootstrap_daemon/src/tox-bootstrapd.c +++ b/other/bootstrap_daemon/src/tox-bootstrapd.c @@ -51,9 +51,9 @@ #include "../bootstrap_node_packets.c" #include "../../testing/misc_tools.c" +#include "global.h" +#include "log.h" -#define DAEMON_NAME "tox-bootstrapd" -#define DAEMON_VERSION_NUMBER 2014101200UL // yyyymmmddvv format: yyyy year, mm month, dd day, vv version change count for that day #define SLEEP_TIME_MILLISECONDS 30 #define sleep usleep(1000*SLEEP_TIME_MILLISECONDS) @@ -136,15 +136,15 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int config_setting_t *ports_array = config_lookup(cfg, NAME_TCP_RELAY_PORTS); if (ports_array == NULL) { - syslog(LOG_WARNING, "No '%s' setting in the configuration file.\n", NAME_TCP_RELAY_PORTS); - syslog(LOG_WARNING, "Using default '%s':\n", NAME_TCP_RELAY_PORTS); + write_log(LOG_LEVEL_WARNING, "No '%s' setting in the configuration file.\n", NAME_TCP_RELAY_PORTS); + write_log(LOG_LEVEL_WARNING, "Using default '%s':\n", NAME_TCP_RELAY_PORTS); uint16_t default_ports[DEFAULT_TCP_RELAY_PORTS_COUNT] = {DEFAULT_TCP_RELAY_PORTS}; int i; for (i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; i ++) { - syslog(LOG_INFO, "Port #%d: %u\n", i, default_ports[i]); + write_log(LOG_LEVEL_INFO, "Port #%d: %u\n", i, default_ports[i]); } // similar procedure to the one of reading config file below @@ -156,7 +156,7 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) { - syslog(LOG_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, + write_log(LOG_LEVEL_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); continue; } @@ -176,7 +176,7 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int } if (config_setting_is_array(ports_array) == CONFIG_FALSE) { - syslog(LOG_ERR, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n", + write_log(LOG_LEVEL_ERROR, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n", NAME_TCP_RELAY_PORTS); return; } @@ -184,7 +184,7 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int int config_port_count = config_setting_length(ports_array); if (config_port_count == 0) { - syslog(LOG_ERR, "'%s' is empty.\n", NAME_TCP_RELAY_PORTS); + write_log(LOG_LEVEL_ERROR, "'%s' is empty.\n", NAME_TCP_RELAY_PORTS); return; } @@ -197,12 +197,12 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int if (elem == NULL) { // it's NULL if `ports_array` is not an array (we have that check earlier) or if `i` is out of range, which should not be - syslog(LOG_WARNING, "Port #%d: Something went wrong while parsing the port. Stopping reading ports.\n", i); + write_log(LOG_LEVEL_WARNING, "Port #%d: Something went wrong while parsing the port. Stopping reading ports.\n", i); break; } if (config_setting_is_number(elem) == CONFIG_FALSE) { - syslog(LOG_WARNING, "Port #%d: Not a number. Skipping.\n", i); + write_log(LOG_LEVEL_WARNING, "Port #%d: Not a number. Skipping.\n", i); continue; } @@ -210,7 +210,7 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) { - syslog(LOG_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, + write_log(LOG_LEVEL_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); continue; } @@ -257,15 +257,15 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k // Read the file. If there is an error, report it and exit. if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) { - syslog(LOG_ERR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); + write_log(LOG_LEVEL_ERROR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); config_destroy(&cfg); return 0; } // Get port if (config_lookup_int(&cfg, NAME_PORT, port) == CONFIG_FALSE) { - syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_PORT); - syslog(LOG_WARNING, "Using default '%s': %d\n", NAME_PORT, DEFAULT_PORT); + write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_PORT); + write_log(LOG_LEVEL_WARNING, "Using default '%s': %d\n", NAME_PORT, DEFAULT_PORT); *port = DEFAULT_PORT; } @@ -273,8 +273,8 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k const char *tmp_pid_file; if (config_lookup_string(&cfg, NAME_PID_FILE_PATH, &tmp_pid_file) == CONFIG_FALSE) { - syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_PID_FILE_PATH); - syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_PID_FILE_PATH, DEFAULT_PID_FILE_PATH); + write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_PID_FILE_PATH); + write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_PID_FILE_PATH, DEFAULT_PID_FILE_PATH); tmp_pid_file = DEFAULT_PID_FILE_PATH; } @@ -285,8 +285,8 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k const char *tmp_keys_file; if (config_lookup_string(&cfg, NAME_KEYS_FILE_PATH, &tmp_keys_file) == CONFIG_FALSE) { - syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_KEYS_FILE_PATH); - syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_KEYS_FILE_PATH, DEFAULT_KEYS_FILE_PATH); + write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_KEYS_FILE_PATH); + write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_KEYS_FILE_PATH, DEFAULT_KEYS_FILE_PATH); tmp_keys_file = DEFAULT_KEYS_FILE_PATH; } @@ -295,31 +295,31 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k // Get IPv6 option if (config_lookup_bool(&cfg, NAME_ENABLE_IPV6, enable_ipv6) == CONFIG_FALSE) { - syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV6); - syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV6, DEFAULT_ENABLE_IPV6 ? "true" : "false"); + write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV6); + write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV6, DEFAULT_ENABLE_IPV6 ? "true" : "false"); *enable_ipv6 = DEFAULT_ENABLE_IPV6; } // Get IPv4 fallback option if (config_lookup_bool(&cfg, NAME_ENABLE_IPV4_FALLBACK, enable_ipv4_fallback) == CONFIG_FALSE) { - syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV4_FALLBACK); - syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, + write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV4_FALLBACK); + write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, DEFAULT_ENABLE_IPV4_FALLBACK ? "true" : "false"); *enable_ipv4_fallback = DEFAULT_ENABLE_IPV4_FALLBACK; } // Get LAN discovery option if (config_lookup_bool(&cfg, NAME_ENABLE_LAN_DISCOVERY, enable_lan_discovery) == CONFIG_FALSE) { - syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_LAN_DISCOVERY); - syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, + write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_LAN_DISCOVERY); + write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, DEFAULT_ENABLE_LAN_DISCOVERY ? "true" : "false"); *enable_lan_discovery = DEFAULT_ENABLE_LAN_DISCOVERY; } // Get TCP relay option if (config_lookup_bool(&cfg, NAME_ENABLE_TCP_RELAY, enable_tcp_relay) == CONFIG_FALSE) { - syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_TCP_RELAY); - syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_ENABLE_TCP_RELAY, + write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_TCP_RELAY); + write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_TCP_RELAY, DEFAULT_ENABLE_TCP_RELAY ? "true" : "false"); *enable_tcp_relay = DEFAULT_ENABLE_TCP_RELAY; } @@ -332,8 +332,8 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k // Get MOTD option if (config_lookup_bool(&cfg, NAME_ENABLE_MOTD, enable_motd) == CONFIG_FALSE) { - syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_MOTD); - syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_ENABLE_MOTD, + write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_MOTD); + write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_MOTD, DEFAULT_ENABLE_MOTD ? "true" : "false"); *enable_motd = DEFAULT_ENABLE_MOTD; } @@ -343,8 +343,8 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k const char *tmp_motd; if (config_lookup_string(&cfg, NAME_MOTD, &tmp_motd) == CONFIG_FALSE) { - syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_MOTD); - syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_MOTD, DEFAULT_MOTD); + write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_MOTD); + write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_MOTD, DEFAULT_MOTD); tmp_motd = DEFAULT_MOTD; } @@ -357,34 +357,34 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k config_destroy(&cfg); - syslog(LOG_INFO, "Successfully read:\n"); - syslog(LOG_INFO, "'%s': %s\n", NAME_PID_FILE_PATH, *pid_file_path); - syslog(LOG_INFO, "'%s': %s\n", NAME_KEYS_FILE_PATH, *keys_file_path); - syslog(LOG_INFO, "'%s': %d\n", NAME_PORT, *port); - syslog(LOG_INFO, "'%s': %s\n", NAME_ENABLE_IPV6, *enable_ipv6 ? "true" : "false"); - syslog(LOG_INFO, "'%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, *enable_ipv4_fallback ? "true" : "false"); - syslog(LOG_INFO, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false"); + write_log(LOG_LEVEL_INFO, "Successfully read:\n"); + write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_PID_FILE_PATH, *pid_file_path); + write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_KEYS_FILE_PATH, *keys_file_path); + write_log(LOG_LEVEL_INFO, "'%s': %d\n", NAME_PORT, *port); + write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_IPV6, *enable_ipv6 ? "true" : "false"); + write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, *enable_ipv4_fallback ? "true" : "false"); + write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false"); - syslog(LOG_INFO, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false"); + write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false"); // show info about tcp ports only if tcp relay is enabled if (*enable_tcp_relay) { if (*tcp_relay_port_count == 0) { - syslog(LOG_ERR, "No TCP ports could be read.\n"); + write_log(LOG_LEVEL_ERROR, "No TCP ports could be read.\n"); } else { - syslog(LOG_INFO, "Read %d TCP ports:\n", *tcp_relay_port_count); + write_log(LOG_LEVEL_INFO, "Read %d TCP ports:\n", *tcp_relay_port_count); int i; for (i = 0; i < *tcp_relay_port_count; i ++) { - syslog(LOG_INFO, "Port #%d: %u\n", i, (*tcp_relay_ports)[i]); + write_log(LOG_LEVEL_INFO, "Port #%d: %u\n", i, (*tcp_relay_ports)[i]); } } } - syslog(LOG_INFO, "'%s': %s\n", NAME_ENABLE_MOTD, *enable_motd ? "true" : "false"); + write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_MOTD, *enable_motd ? "true" : "false"); if (*enable_motd) { - syslog(LOG_INFO, "'%s': %s\n", NAME_MOTD, *motd); + write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_MOTD, *motd); } return 1; @@ -408,7 +408,7 @@ int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6) config_init(&cfg); if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) { - syslog(LOG_ERR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); + write_log(LOG_LEVEL_ERROR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); config_destroy(&cfg); return 0; } @@ -416,13 +416,13 @@ int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6) config_setting_t *node_list = config_lookup(&cfg, NAME_BOOTSTRAP_NODES); if (node_list == NULL) { - syslog(LOG_WARNING, "No '%s' setting in the configuration file. Skipping bootstrapping.\n", NAME_BOOTSTRAP_NODES); + write_log(LOG_LEVEL_WARNING, "No '%s' setting in the configuration file. Skipping bootstrapping.\n", NAME_BOOTSTRAP_NODES); config_destroy(&cfg); return 1; } if (config_setting_length(node_list) == 0) { - syslog(LOG_WARNING, "No bootstrap nodes found. Skipping bootstrapping.\n"); + write_log(LOG_LEVEL_WARNING, "No bootstrap nodes found. Skipping bootstrapping.\n"); config_destroy(&cfg); return 1; } @@ -446,29 +446,29 @@ int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6) // Check that all settings are present if (config_setting_lookup_string(node, NAME_PUBLIC_KEY, &bs_public_key) == CONFIG_FALSE) { - syslog(LOG_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_PUBLIC_KEY); + write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_PUBLIC_KEY); goto next; } if (config_setting_lookup_int(node, NAME_PORT, &bs_port) == CONFIG_FALSE) { - syslog(LOG_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_PORT); + write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_PORT); goto next; } if (config_setting_lookup_string(node, NAME_ADDRESS, &bs_address) == CONFIG_FALSE) { - syslog(LOG_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_ADDRESS); + write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_ADDRESS); goto next; } // Process settings if (strlen(bs_public_key) != crypto_box_PUBLICKEYBYTES * 2) { - syslog(LOG_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_PUBLIC_KEY, + write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_PUBLIC_KEY, bs_public_key); goto next; } if (bs_port < MIN_ALLOWED_PORT || bs_port > MAX_ALLOWED_PORT) { - syslog(LOG_WARNING, "Bootstrap node #%d: Invalid '%s': %d, should be in [%d, %d]. Skipping the node.\n", i, NAME_PORT, + write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %d, should be in [%d, %d]. Skipping the node.\n", i, NAME_PORT, bs_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); goto next; } @@ -479,11 +479,11 @@ int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6) free(bs_public_key_bin); if (!address_resolved) { - syslog(LOG_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_ADDRESS, bs_address); + write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_ADDRESS, bs_address); goto next; } - syslog(LOG_INFO, "Successfully added bootstrap node #%d: %s:%d %s\n", i, bs_address, bs_port, bs_public_key); + write_log(LOG_LEVEL_INFO, "Successfully added bootstrap node #%d: %s:%d %s\n", i, bs_address, bs_port, bs_public_key); next: // config_setting_lookup_string() allocates string inside and doesn't allow us to free it direcly @@ -511,19 +511,19 @@ void print_public_key(const uint8_t *public_key) index += sprintf(buffer + index, "%02hhX", public_key[i]); } - syslog(LOG_INFO, "Public Key: %s\n", buffer); + write_log(LOG_LEVEL_INFO, "Public Key: %s\n", buffer); return; } int main(int argc, char *argv[]) { - openlog(DAEMON_NAME, LOG_NOWAIT | LOG_PID, LOG_DAEMON); + open_log(LOGGER_BACKEND_SYSLOG); - syslog(LOG_INFO, "Running \"%s\" version %lu.\n", DAEMON_NAME, DAEMON_VERSION_NUMBER); + write_log(LOG_LEVEL_INFO, "Running \"%s\" version %lu.\n", DAEMON_NAME, DAEMON_VERSION_NUMBER); if (argc < 2) { - syslog(LOG_ERR, "Please specify a path to a configuration file as the first argument. Exiting.\n"); + write_log(LOG_LEVEL_ERROR, "Please specify a path to a configuration file as the first argument. Exiting.\n"); return 1; } @@ -541,14 +541,14 @@ int main(int argc, char *argv[]) if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &port, &enable_ipv6, &enable_ipv4_fallback, &enable_lan_discovery, &enable_tcp_relay, &tcp_relay_ports, &tcp_relay_port_count, &enable_motd, &motd)) { - syslog(LOG_INFO, "General config read successfully\n"); + write_log(LOG_LEVEL_INFO, "General config read successfully\n"); } else { - syslog(LOG_ERR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path); + write_log(LOG_LEVEL_ERROR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path); return 1; } if (port < MIN_ALLOWED_PORT || port > MAX_ALLOWED_PORT) { - syslog(LOG_ERR, "Invalid port: %d, should be in [%d, %d]. Exiting.\n", port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); + write_log(LOG_LEVEL_ERROR, "Invalid port: %d, should be in [%d, %d]. Exiting.\n", port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); return 1; } @@ -556,7 +556,7 @@ int main(int argc, char *argv[]) FILE *pid_file; if ((pid_file = fopen(pid_file_path, "r"))) { - syslog(LOG_WARNING, "Another instance of the daemon is already running, PID file %s exists.\n", pid_file_path); + write_log(LOG_LEVEL_WARNING, "Another instance of the daemon is already running, PID file %s exists.\n", pid_file_path); fclose(pid_file); } @@ -567,17 +567,17 @@ int main(int argc, char *argv[]) if (net == NULL) { if (enable_ipv6 && enable_ipv4_fallback) { - syslog(LOG_WARNING, "Couldn't initialize IPv6 networking. Falling back to using IPv4.\n"); + write_log(LOG_LEVEL_WARNING, "Couldn't initialize IPv6 networking. Falling back to using IPv4.\n"); enable_ipv6 = 0; ip_init(&ip, enable_ipv6); net = new_networking(ip, port); if (net == NULL) { - syslog(LOG_ERR, "Couldn't fallback to IPv4. Exiting.\n"); + write_log(LOG_LEVEL_ERROR, "Couldn't fallback to IPv4. Exiting.\n"); return 1; } } else { - syslog(LOG_ERR, "Couldn't initialize networking. Exiting.\n"); + write_log(LOG_LEVEL_ERROR, "Couldn't initialize networking. Exiting.\n"); return 1; } } @@ -586,7 +586,7 @@ int main(int argc, char *argv[]) DHT *dht = new_DHT(net); if (dht == NULL) { - syslog(LOG_ERR, "Couldn't initialize Tox DHT instance. Exiting.\n"); + write_log(LOG_LEVEL_ERROR, "Couldn't initialize Tox DHT instance. Exiting.\n"); return 1; } @@ -594,15 +594,15 @@ int main(int argc, char *argv[]) Onion_Announce *onion_a = new_onion_announce(dht); if (!(onion && onion_a)) { - syslog(LOG_ERR, "Couldn't initialize Tox Onion. Exiting.\n"); + write_log(LOG_LEVEL_ERROR, "Couldn't initialize Tox Onion. Exiting.\n"); return 1; } if (enable_motd) { if (bootstrap_set_callbacks(dht->net, DAEMON_VERSION_NUMBER, (uint8_t *)motd, strlen(motd) + 1) == 0) { - syslog(LOG_INFO, "Set MOTD successfully.\n"); + write_log(LOG_LEVEL_INFO, "Set MOTD successfully.\n"); } else { - syslog(LOG_ERR, "Couldn't set MOTD: %s. Exiting.\n", motd); + write_log(LOG_LEVEL_ERROR, "Couldn't set MOTD: %s. Exiting.\n", motd); return 1; } @@ -610,9 +610,9 @@ int main(int argc, char *argv[]) } if (manage_keys(dht, keys_file_path)) { - syslog(LOG_INFO, "Keys are managed successfully.\n"); + write_log(LOG_LEVEL_INFO, "Keys are managed successfully.\n"); } else { - syslog(LOG_ERR, "Couldn't read/write: %s. Exiting.\n", keys_file_path); + write_log(LOG_LEVEL_ERROR, "Couldn't read/write: %s. Exiting.\n", keys_file_path); return 1; } @@ -620,7 +620,7 @@ int main(int argc, char *argv[]) if (enable_tcp_relay) { if (tcp_relay_port_count == 0) { - syslog(LOG_ERR, "No TCP relay ports read. Exiting.\n"); + write_log(LOG_LEVEL_ERROR, "No TCP relay ports read. Exiting.\n"); return 1; } @@ -630,17 +630,17 @@ int main(int argc, char *argv[]) free(tcp_relay_ports); if (tcp_server != NULL) { - syslog(LOG_INFO, "Initialized Tox TCP server successfully.\n"); + write_log(LOG_LEVEL_INFO, "Initialized Tox TCP server successfully.\n"); } else { - syslog(LOG_ERR, "Couldn't initialize Tox TCP server. Exiting.\n"); + write_log(LOG_LEVEL_ERROR, "Couldn't initialize Tox TCP server. Exiting.\n"); return 1; } } if (bootstrap_from_config(cfg_file_path, dht, enable_ipv6)) { - syslog(LOG_INFO, "List of bootstrap nodes read successfully.\n"); + write_log(LOG_LEVEL_INFO, "List of bootstrap nodes read successfully.\n"); } else { - syslog(LOG_ERR, "Couldn't read list of bootstrap nodes in %s. Exiting.\n", cfg_file_path); + write_log(LOG_LEVEL_ERROR, "Couldn't read list of bootstrap nodes in %s. Exiting.\n", cfg_file_path); return 1; } @@ -650,7 +650,7 @@ int main(int argc, char *argv[]) FILE *pidf = fopen(pid_file_path, "a+"); if (pidf == NULL) { - syslog(LOG_ERR, "Couldn't open the PID file for writing: %s. Exiting.\n", pid_file_path); + write_log(LOG_LEVEL_ERROR, "Couldn't open the PID file for writing: %s. Exiting.\n", pid_file_path); return 1; } @@ -663,14 +663,14 @@ int main(int argc, char *argv[]) if (pid > 0) { fprintf(pidf, "%d", pid); fclose(pidf); - syslog(LOG_INFO, "Forked successfully: PID: %d.\n", pid); + write_log(LOG_LEVEL_INFO, "Forked successfully: PID: %d.\n", pid); return 0; } else { fclose(pidf); } if (pid < 0) { - syslog(LOG_ERR, "Forking failed. Exiting.\n"); + write_log(LOG_LEVEL_ERROR, "Forking failed. Exiting.\n"); return 1; } @@ -679,13 +679,13 @@ int main(int argc, char *argv[]) // Create a new SID for the child process if (setsid() < 0) { - syslog(LOG_ERR, "SID creation failure. Exiting.\n"); + write_log(LOG_LEVEL_ERROR, "SID creation failure. Exiting.\n"); return 1; } // Change the current working directory if ((chdir("/")) < 0) { - syslog(LOG_ERR, "Couldn't change working directory to '/'. Exiting.\n"); + write_log(LOG_LEVEL_ERROR, "Couldn't change working directory to '/'. Exiting.\n"); return 1; } @@ -701,7 +701,7 @@ int main(int argc, char *argv[]) if (enable_lan_discovery) { LANdiscovery_init(dht); - syslog(LOG_INFO, "Initialized LAN discovery.\n"); + write_log(LOG_LEVEL_INFO, "Initialized LAN discovery.\n"); } while (1) { @@ -719,7 +719,7 @@ int main(int argc, char *argv[]) networking_poll(dht->net); if (waiting_for_dht_connection && DHT_isconnected(dht)) { - syslog(LOG_INFO, "Connected to other bootstrap node successfully.\n"); + write_log(LOG_LEVEL_INFO, "Connected to other bootstrap node successfully.\n"); waiting_for_dht_connection = 0; } -- cgit v1.2.3 From ffa927fa361c366d8d1d2fefdeb58d0977ab91ae Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Thu, 31 Dec 2015 20:01:24 -0500 Subject: Rename LOGGER_BACKEND to LOG_BACKEND --- other/bootstrap_daemon/src/log.c | 12 ++++++------ other/bootstrap_daemon/src/log.h | 10 +++++----- other/bootstrap_daemon/src/tox-bootstrapd.c | 14 +++++++------- 3 files changed, 18 insertions(+), 18 deletions(-) (limited to 'other/bootstrap_daemon/src/log.h') diff --git a/other/bootstrap_daemon/src/log.c b/other/bootstrap_daemon/src/log.c index b5f838fb..f41b8420 100644 --- a/other/bootstrap_daemon/src/log.c +++ b/other/bootstrap_daemon/src/log.c @@ -30,15 +30,15 @@ #include #include -LOGGER_BACKEND current_backend = -1; +LOG_BACKEND current_backend = -1; -bool open_log(LOGGER_BACKEND backend) +bool open_log(LOG_BACKEND backend) { if (current_backend != -1) { return false; } - if (backend == LOGGER_BACKEND_SYSLOG) { + if (backend == LOG_BACKEND_SYSLOG) { openlog(DAEMON_NAME, LOG_NOWAIT | LOG_PID, LOG_DAEMON); } @@ -53,7 +53,7 @@ bool close_log() return false; } - if (current_backend == LOGGER_BACKEND_SYSLOG) { + if (current_backend == LOG_BACKEND_SYSLOG) { closelog(); } @@ -101,10 +101,10 @@ bool write_log(LOG_LEVEL level, const char *format, ...) va_start(args, format); switch (current_backend) { - case LOGGER_BACKEND_SYSLOG: + case LOG_BACKEND_SYSLOG: log_syslog(level, format, args); break; - case LOGGER_BACKEND_STDOUT: + case LOG_BACKEND_STDOUT: log_stdout(level, format, args); break; } diff --git a/other/bootstrap_daemon/src/log.h b/other/bootstrap_daemon/src/log.h index b97d3733..b7712368 100644 --- a/other/bootstrap_daemon/src/log.h +++ b/other/bootstrap_daemon/src/log.h @@ -26,10 +26,10 @@ #include -typedef enum LOGGER_BACKEND { - LOGGER_BACKEND_SYSLOG, - LOGGER_BACKEND_STDOUT -} LOGGER_BACKEND; +typedef enum LOG_BACKEND { + LOG_BACKEND_SYSLOG, + LOG_BACKEND_STDOUT +} LOG_BACKEND; typedef enum LOG_LEVEL { LOG_LEVEL_INFO, @@ -42,7 +42,7 @@ typedef enum LOG_LEVEL { * @param backend Specifies which backend to use. * @return true on success, flase if log is already opened. */ -bool open_log(LOGGER_BACKEND backend); +bool open_log(LOG_BACKEND backend); /** * Releases all used resources by the logger. diff --git a/other/bootstrap_daemon/src/tox-bootstrapd.c b/other/bootstrap_daemon/src/tox-bootstrapd.c index e2bf7ade..3bbddf98 100644 --- a/other/bootstrap_daemon/src/tox-bootstrapd.c +++ b/other/bootstrap_daemon/src/tox-bootstrapd.c @@ -544,7 +544,7 @@ bool print_help() // Handels command line arguments, setting cfg_file_path and log_backend. // Terminates the application if incorrect arguments are specified. -void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, LOGGER_BACKEND *log_backend) +void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, LOG_BACKEND *log_backend) { if (argc < 2) { write_log(LOG_LEVEL_ERROR, "Error: No arguments provided.\n\n"); @@ -581,10 +581,10 @@ void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, case 'l': if (strcmp(optarg, "syslog") == 0) { - *log_backend = LOGGER_BACKEND_SYSLOG; + *log_backend = LOG_BACKEND_SYSLOG; log_backend_set = true; } else if (strcmp(optarg, "stdout") == 0) { - *log_backend = LOGGER_BACKEND_STDOUT; + *log_backend = LOG_BACKEND_STDOUT; log_backend_set = true; } else { write_log(LOG_LEVEL_ERROR, "Error: Invalid BACKEND value for --log-backend option passed: %s\n\n", optarg); @@ -610,7 +610,7 @@ void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, } if (!log_backend_set) { - *log_backend = LOGGER_BACKEND_SYSLOG; + *log_backend = LOG_BACKEND_SYSLOG; } if (!cfg_file_path_set) { @@ -623,10 +623,10 @@ void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, int main(int argc, char *argv[]) { char *cfg_file_path; - LOGGER_BACKEND log_backend; + LOG_BACKEND log_backend; // choose backend for printing command line argument parsing output based on whether the daemon is being run from a terminal - log_backend = isatty(STDOUT_FILENO) ? LOGGER_BACKEND_STDOUT : LOGGER_BACKEND_SYSLOG; + log_backend = isatty(STDOUT_FILENO) ? LOG_BACKEND_STDOUT : LOG_BACKEND_SYSLOG; open_log(log_backend); handle_command_line_arguments(argc, argv, &cfg_file_path, &log_backend); @@ -798,7 +798,7 @@ int main(int argc, char *argv[]) } // Go quiet - if (log_backend != LOGGER_BACKEND_STDOUT) { + if (log_backend != LOG_BACKEND_STDOUT) { close(STDOUT_FILENO); close(STDIN_FILENO); close(STDERR_FILENO); -- cgit v1.2.3 From 232488816eb68d2306f95bf30f003d9a044a7863 Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Fri, 1 Jan 2016 01:26:00 -0500 Subject: Fix license headers and file descriptions --- other/bootstrap_daemon/src/command_line_arguments.c | 3 ++- other/bootstrap_daemon/src/command_line_arguments.h | 3 ++- other/bootstrap_daemon/src/config.c | 3 ++- other/bootstrap_daemon/src/config.h | 3 ++- other/bootstrap_daemon/src/config_defaults.h | 3 ++- other/bootstrap_daemon/src/global.h | 3 ++- other/bootstrap_daemon/src/log.c | 3 ++- other/bootstrap_daemon/src/log.h | 3 ++- other/bootstrap_daemon/src/tox-bootstrapd.c | 4 ++-- 9 files changed, 18 insertions(+), 10 deletions(-) (limited to 'other/bootstrap_daemon/src/log.h') diff --git a/other/bootstrap_daemon/src/command_line_arguments.c b/other/bootstrap_daemon/src/command_line_arguments.c index 9b3b2e8c..d364d329 100644 --- a/other/bootstrap_daemon/src/command_line_arguments.c +++ b/other/bootstrap_daemon/src/command_line_arguments.c @@ -1,8 +1,9 @@ /* command_line_arguments.c * * Tox DHT bootstrap daemon. + * Command line argument handling. * - * Copyright (C) 2015 Tox project All Rights Reserved. + * Copyright (C) 2015-2016 Tox project All Rights Reserved. * * This file is part of Tox. * diff --git a/other/bootstrap_daemon/src/command_line_arguments.h b/other/bootstrap_daemon/src/command_line_arguments.h index 1581278f..c96e8eaa 100644 --- a/other/bootstrap_daemon/src/command_line_arguments.h +++ b/other/bootstrap_daemon/src/command_line_arguments.h @@ -1,8 +1,9 @@ /* command_line_arguments.h * * Tox DHT bootstrap daemon. + * Command line argument handling. * - * Copyright (C) 2015 Tox project All Rights Reserved. + * Copyright (C) 2015-2016 Tox project All Rights Reserved. * * This file is part of Tox. * diff --git a/other/bootstrap_daemon/src/config.c b/other/bootstrap_daemon/src/config.c index ee9fbdd2..84e18a69 100644 --- a/other/bootstrap_daemon/src/config.c +++ b/other/bootstrap_daemon/src/config.c @@ -1,8 +1,9 @@ /* config.c * * Tox DHT bootstrap daemon. + * Functionality related to dealing with the config file. * - * Copyright (C) 2014-2015 Tox project All Rights Reserved. + * Copyright (C) 2014-2016 Tox project All Rights Reserved. * * This file is part of Tox. * diff --git a/other/bootstrap_daemon/src/config.h b/other/bootstrap_daemon/src/config.h index 73bc6e0b..91f9c43f 100644 --- a/other/bootstrap_daemon/src/config.h +++ b/other/bootstrap_daemon/src/config.h @@ -1,8 +1,9 @@ /* config.h * * Tox DHT bootstrap daemon. + * Functionality related to dealing with the config file. * - * Copyright (C) 2014-2015 Tox project All Rights Reserved. + * Copyright (C) 2014-2016 Tox project All Rights Reserved. * * This file is part of Tox. * diff --git a/other/bootstrap_daemon/src/config_defaults.h b/other/bootstrap_daemon/src/config_defaults.h index 34d715e7..17bffd3a 100644 --- a/other/bootstrap_daemon/src/config_defaults.h +++ b/other/bootstrap_daemon/src/config_defaults.h @@ -1,8 +1,9 @@ /* config_defaults.h * * Tox DHT bootstrap daemon. + * Default config options for when they are missing in the config file. * - * Copyright (C) 2014-2015 Tox project All Rights Reserved. + * Copyright (C) 2014-2016 Tox project All Rights Reserved. * * This file is part of Tox. * diff --git a/other/bootstrap_daemon/src/global.h b/other/bootstrap_daemon/src/global.h index 71f24635..a18afee3 100644 --- a/other/bootstrap_daemon/src/global.h +++ b/other/bootstrap_daemon/src/global.h @@ -1,8 +1,9 @@ /* global.h * * Tox DHT bootstrap daemon. + * Globally used defines. * - * Copyright (C) 2015 Tox project All Rights Reserved. + * Copyright (C) 2014-2016 Tox project All Rights Reserved. * * This file is part of Tox. * diff --git a/other/bootstrap_daemon/src/log.c b/other/bootstrap_daemon/src/log.c index ec68050e..b3aec180 100644 --- a/other/bootstrap_daemon/src/log.c +++ b/other/bootstrap_daemon/src/log.c @@ -1,8 +1,9 @@ /* log.c * * Tox DHT bootstrap daemon. + * Logging utility with support of multipel logging backends. * - * Copyright (C) 2015 Tox project All Rights Reserved. + * Copyright (C) 2015-2016 Tox project All Rights Reserved. * * This file is part of Tox. * diff --git a/other/bootstrap_daemon/src/log.h b/other/bootstrap_daemon/src/log.h index b7712368..61cb2ee3 100644 --- a/other/bootstrap_daemon/src/log.h +++ b/other/bootstrap_daemon/src/log.h @@ -1,8 +1,9 @@ /* log.h * * Tox DHT bootstrap daemon. + * Logging utility with support of multipel logging backends. * - * Copyright (C) 2015 Tox project All Rights Reserved. + * Copyright (C) 2015-2016 Tox project All Rights Reserved. * * This file is part of Tox. * diff --git a/other/bootstrap_daemon/src/tox-bootstrapd.c b/other/bootstrap_daemon/src/tox-bootstrapd.c index 4883f885..e252a37d 100644 --- a/other/bootstrap_daemon/src/tox-bootstrapd.c +++ b/other/bootstrap_daemon/src/tox-bootstrapd.c @@ -1,8 +1,9 @@ /* tox-bootstrapd.c * * Tox DHT bootstrap daemon. + * Main file. * - * Copyright (C) 2014 Tox project All Rights Reserved. + * Copyright (C) 2014-2016 Tox project All Rights Reserved. * * This file is part of Tox. * @@ -35,7 +36,6 @@ #include "../../../toxcore/TCP_server.h" #include "../../../toxcore/util.h" - // misc #include "../../bootstrap_node_packets.h" -- cgit v1.2.3