From 398019afe87590842f85d011f10833a50bdb99d5 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Sun, 26 Jul 2020 21:00:05 +0200 Subject: [PATCH] fix: better logging behaviour --- CMakeLists.txt | 2 +- core.ini | 1 + include/config.h | 13 +--- include/logger.h | 16 +++-- src/command.c | 18 ++--- src/config.c | 63 ++++++++++++---- src/database.c | 6 +- src/endpoint.c | 4 +- src/endpoints/api_v1_controllers_STR.c | 16 ++--- src/endpoints/api_v1_controllers_STR_relays.c | 4 +- .../api_v1_controllers_STR_relays_INT.c | 20 +++--- .../api_v1_controllers_STR_relays_INT_pulse.c | 6 +- src/endpoints/api_v1_controllers_discover.c | 26 +++---- src/endpoints/api_v1_relays_tag_STR.c | 2 +- src/endpoints/api_v1_schedules.c | 14 ++-- src/endpoints/api_v1_schedules_STR.c | 26 +++---- src/endpoints/api_v1_schedules_list.c | 14 ++-- src/endpoints/api_v1_schedules_tag_STR.c | 2 +- src/endpoints/api_v1_tags.c | 2 +- src/endpoints/api_v1_tags_STR.c | 6 +- src/handlers/http.c | 6 +- src/handlers/mqtt.c | 2 +- src/helpers/connect_server.c | 4 +- src/helpers/drop_privileges.c | 10 +-- src/helpers/parse_cli.c | 4 +- src/logger.c | 71 +++++++++++++------ src/main.c | 28 ++++++-- src/models/controller.c | 6 +- src/models/junction_relay_schedule.c | 2 +- src/models/junction_tag.c | 2 +- src/models/relay.c | 18 ++--- src/models/schedule.c | 12 ++-- src/models/tag.c | 8 +-- src/router.c | 2 +- tests/controller.testing.ini | 4 ++ tests/core.testing.ini | 4 +- 36 files changed, 256 insertions(+), 188 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 94f6b16..b342e1d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project(core - VERSION 0.2.3 + VERSION 0.2.4 LANGUAGES C) add_executable(core src/main.c) diff --git a/core.ini b/core.ini index 7c9f16a..cd40564 100644 --- a/core.ini +++ b/core.ini @@ -13,3 +13,4 @@ discovery-port = 4421 mqtt-port = 1885 log-level = debug +log-file = stdout diff --git a/include/config.h b/include/config.h index 6dee2f2..49c04af 100644 --- a/include/config.h +++ b/include/config.h @@ -12,23 +12,14 @@ typedef enum RUN_TYPE_INVALID, } run_type_t; -typedef enum -{ - LOG_LEVEL_TRACE = 5, - LOG_LEVEL_DEBUG = 4, - LOG_LEVEL_INFO = 3, - LOG_LEVEL_WARN = 2, - LOG_LEVEL_ERROR = 1, - LOG_LEVEL_FATAL = 0, -} log_level_t; - typedef struct { char *file; char database[256]; char user[256]; char group[256]; - log_level_t log_level; + int log_level; + FILE *log_file; run_type_t run_type; uint16_t server_port; uint16_t discovery_port; diff --git a/include/logger.h b/include/logger.h index 37efc33..a84a511 100644 --- a/include/logger.h +++ b/include/logger.h @@ -3,18 +3,20 @@ #include #include +#include #include #include void -logger_log(FILE *stream, log_level_t level, const char *filename, int line, const char *func, const char *msg, ...); +logger_log(int level, const char *filename, int line, const char *func, const char *msg, ...); -#define LOG_TRACE(...) logger_log(stdout, LOG_LEVEL_TRACE, __FILE__, __LINE__, __func__, ##__VA_ARGS__) -#define LOG_DEBUG(...) logger_log(stdout, LOG_LEVEL_DEBUG, __FILE__, __LINE__, __func__, ##__VA_ARGS__) -#define LOG_INFO(...) logger_log(stdout, LOG_LEVEL_INFO , __FILE__, __LINE__, __func__, ##__VA_ARGS__) -#define LOG_WARN(...) logger_log(stdout, LOG_LEVEL_WARN , __FILE__, __LINE__, __func__, ##__VA_ARGS__) -#define LOG_ERROR(...) logger_log(stderr, LOG_LEVEL_ERROR, __FILE__, __LINE__, __func__, ##__VA_ARGS__) -#define LOG_FATAL(...) logger_log(stderr, LOG_LEVEL_FATAL, __FILE__, __LINE__, __func__, ##__VA_ARGS__) +#define LOGGER_DEBUG(...) logger_log(LOG_DEBUG , __FILE__, __LINE__, __func__, ##__VA_ARGS__) +#define LOGGER_INFO(...) logger_log(LOG_INFO , __FILE__, __LINE__, __func__, ##__VA_ARGS__) +#define LOGGER_NOTICE(...) logger_log(LOG_NOTICE , __FILE__, __LINE__, __func__, ##__VA_ARGS__) +#define LOGGER_WARNING(...) logger_log(LOG_WARNING, __FILE__, __LINE__, __func__, ##__VA_ARGS__) +#define LOGGER_ERR(...) logger_log(LOG_ERR , __FILE__, __LINE__, __func__, ##__VA_ARGS__) +#define LOGGER_CRIT(...) logger_log(LOG_CRIT , __FILE__, __LINE__, __func__, ##__VA_ARGS__) +#define LOGGER_EMERG(...) logger_log(LOG_EMERG , __FILE__, __LINE__, __func__, ##__VA_ARGS__) #endif //CORE_LOGGER_H diff --git a/src/command.c b/src/command.c index 860f9b1..bb9b3a5 100644 --- a/src/command.c +++ b/src/command.c @@ -15,7 +15,7 @@ command_set_relay_schedule(relay_t *relay) controller_t *controller = controller_get_by_id(relay->controller_id); if(!controller) { - LOG_ERROR("couldn't find controller\n"); + LOGGER_ERR("couldn't find controller\n"); return 1; } @@ -66,7 +66,7 @@ command_set_relay_schedule(relay_t *relay) // finish writing if (mpack_writer_destroy(&writer) != mpack_ok) { - LOG_ERROR("an error occurred encoding the data"); + LOGGER_ERR("an error occurred encoding the data"); controller_free(controller); return 1; } @@ -100,7 +100,7 @@ command_set_controller_name(controller_t *controller) // finish writing if (mpack_writer_destroy(&writer) != mpack_ok) { - LOG_ERROR("an error occurred encoding the data"); + LOGGER_ERR("an error occurred encoding the data"); return 1; } @@ -113,7 +113,7 @@ command_set_controller_name(controller_t *controller) int command_send(controller_t *controller, int command_code, char *payload, uint32_t payload_size) { - LOG_DEBUG("commanding %d\n", command_code); + LOGGER_DEBUG("commanding %d\n", command_code); int bytes_transferred; @@ -121,18 +121,18 @@ command_send(controller_t *controller, int command_code, char *payload, uint32_t if(fd_controller == -1) { - LOG_ERROR("can't open command socket %s:%d\n", controller->ip, controller->port); + LOGGER_ERR("can't open command socket %s:%d\n", controller->ip, controller->port); return 1; } if((bytes_transferred = send(fd_controller, &payload_size, sizeof(payload_size), 0)) <= 0) { - LOG_ERROR("error during sending size\n"); + LOGGER_ERR("error during sending size\n"); return 1; } if((bytes_transferred = send(fd_controller, payload, payload_size, 0)) <= 0) { - LOG_ERROR("error during sending\n"); + LOGGER_ERR("error during sending\n"); return 1; } @@ -146,7 +146,7 @@ command_pulse(relay_t *relay, uint8_t duration) controller_t *controller = controller_get_by_id(relay->controller_id); if(!controller) { - LOG_ERROR("couldn't find controller\n"); + LOGGER_ERR("couldn't find controller\n"); return 1; } @@ -172,7 +172,7 @@ command_pulse(relay_t *relay, uint8_t duration) // finish writing if (mpack_writer_destroy(&writer) != mpack_ok) { - LOG_ERROR("an error occurred encoding the data"); + LOGGER_ERR("an error occurred encoding the data"); controller_free(controller); return 1; } diff --git a/src/config.c b/src/config.c index bc9a87e..a674c86 100644 --- a/src/config.c +++ b/src/config.c @@ -10,40 +10,69 @@ config_t global_config; (ini_array_match(SECTION, disp->append_to, '.', disp->format) && \ ini_string_match_ii(KEY, disp->data, disp->format)) -int +static int config_load_log_level(IniDispatch *disp, config_t *config) { - if(strcasecmp(disp->value, "trace") == 0) - { - config->log_level = LOG_LEVEL_TRACE; - return 0; - } if(strcasecmp(disp->value, "debug") == 0) { - config->log_level = LOG_LEVEL_DEBUG; + setlogmask(LOG_UPTO(LOG_DEBUG)); + config->log_level = LOG_DEBUG; return 0; } if(strcasecmp(disp->value, "info") == 0) { - config->log_level = LOG_LEVEL_INFO; + setlogmask(LOG_UPTO(LOG_INFO)); + config->log_level = LOG_INFO; return 0; } - if(strcasecmp(disp->value, "warn") == 0) + if(strcasecmp(disp->value, "notice") == 0) { - config->log_level = LOG_LEVEL_WARN; + setlogmask(LOG_UPTO(LOG_NOTICE)); + config->log_level = LOG_NOTICE; return 0; } - if(strcasecmp(disp->value, "error") == 0) + if(strcasecmp(disp->value, "warning") == 0) { - config->log_level = LOG_LEVEL_ERROR; + setlogmask(LOG_UPTO(LOG_WARNING)); + config->log_level = LOG_WARNING; return 0; } - if(strcasecmp(disp->value, "fatal") == 0) + if(strcasecmp(disp->value, "err") == 0) { - config->log_level = LOG_LEVEL_FATAL; + setlogmask(LOG_UPTO(LOG_ERR)); + config->log_level = LOG_ERR; return 0; } - LOG_WARN("invalid log-level '%s'\n", disp->value); + if(strcasecmp(disp->value, "crit") == 0) + { + setlogmask(LOG_UPTO(LOG_CRIT)); + config->log_level = LOG_CRIT; + return 0; + } + if(strcasecmp(disp->value, "emerg") == 0) + { + setlogmask(LOG_UPTO(LOG_EMERG)); + config->log_level = LOG_EMERG; + return 0; + } + LOGGER_WARNING("invalid log-level '%s'\n", disp->value); + return 0; +} + +static int +config_load_log_file(IniDispatch *disp, config_t *config) +{ + if(strcasecmp(disp->value, "stdout") == 0) + { + config->log_file = stdout; + return 0; + } + if(strcasecmp(disp->value, "stderr") == 0) + { + config->log_file = stderr; + return 0; + } + config->log_file = fopen(disp->value, "a+"); return 0; } @@ -98,6 +127,10 @@ config_load(IniDispatch *disp, void *config_void) { return config_load_log_level(disp, config); } + if(CONFINI_IS_KEY("core", "log-file")) + { + return config_load_log_file(disp, config); + } if(CONFINI_IS_KEY("core", "server-port")) { config->server_port = atoi(disp->value); diff --git a/src/database.c b/src/database.c index ba80724..2429786 100644 --- a/src/database.c +++ b/src/database.c @@ -33,11 +33,11 @@ database_migrate() switch(version_num) { case 0: - LOG_INFO("migrating LEVEL 0\n"); + LOGGER_INFO("migrating LEVEL 0\n"); rc = sqlite3_exec(global_database, (const char *)sql_migration_0_sql, NULL, NULL, &err_msg); if(rc != 0) { - LOG_FATAL("couldn't migrate LEVEL 0 (%s)\n", err_msg); + LOGGER_CRIT("couldn't migrate LEVEL 0 (%s)\n", err_msg); break; } new_version_num = 1; @@ -58,7 +58,7 @@ database_migrate() rc = sqlite3_step(stmt); if (rc != SQLITE_DONE) { - LOG_FATAL("couldn't write new schema version\n"); + LOGGER_CRIT("couldn't write new schema version\n"); } sqlite3_finalize(stmt); diff --git a/src/endpoint.c b/src/endpoint.c index b068ed4..a56f277 100644 --- a/src/endpoint.c +++ b/src/endpoint.c @@ -32,7 +32,7 @@ endpoint_func_not_found(struct mg_connection *nc, struct http_message *hm, endpo } else { - LOG_DEBUG("404 file not found\n"); + LOGGER_DEBUG("404 file not found\n"); response->status_code = 404; response->content_type = global_config.not_found_content_type; response->content_length = strlen(global_config.not_found_content); @@ -84,7 +84,7 @@ endpoint_response_json(endpoint_response_t *response, int status_code, const cJS } } - LOG_ERROR("failed to print schedule json\n"); + LOGGER_ERR("failed to print schedule json\n"); static const char content[] = "failed to print json"; endpoint_response_text(response, status_code, content, STRLEN(content)); diff --git a/src/endpoints/api_v1_controllers_STR.c b/src/endpoints/api_v1_controllers_STR.c index e66d0fc..293506d 100644 --- a/src/endpoints/api_v1_controllers_STR.c +++ b/src/endpoints/api_v1_controllers_STR.c @@ -18,7 +18,7 @@ api_v1_controllers_STR_GET(struct mg_connection *nc, struct http_message *hm, en uuid_t target_uid; if(uuid_parse(args[0].value.v_str, target_uid)) { - LOG_DEBUG("failed to unparse uid\n"); + LOGGER_DEBUG("failed to unparse uid\n"); static const char content[] = "given id was invalid"; endpoint_response_text(response, 400, content, STRLEN(content)); @@ -29,7 +29,7 @@ api_v1_controllers_STR_GET(struct mg_connection *nc, struct http_message *hm, en if(!controller) { - LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str); + LOGGER_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str); static const char content[] = "no controller for id found"; endpoint_response_text(response, 404, content, STRLEN(content)); @@ -52,7 +52,7 @@ api_v1_controllers_STR_PUT(struct mg_connection *nc, struct http_message *hm, en uuid_t target_uid; if(uuid_parse(args[0].value.v_str, target_uid)) { - LOG_DEBUG("failed to unparse uid\n"); + LOGGER_DEBUG("failed to unparse uid\n"); static const char content[] = "given id was invalid"; endpoint_response_text(response, 400, content, STRLEN(content)); @@ -63,7 +63,7 @@ api_v1_controllers_STR_PUT(struct mg_connection *nc, struct http_message *hm, en if(!controller) { - LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str); + LOGGER_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str); static const char content[] = "no controller for id found"; endpoint_response_text(response, 404, content, STRLEN(content)); @@ -130,7 +130,7 @@ api_v1_controllers_STR_PUT(struct mg_connection *nc, struct http_message *hm, en if(controller_save(controller)) { - LOG_ERROR("failed to save controller\n"); + LOGGER_ERR("failed to save controller\n"); controller_free(controller); cJSON_Delete(json); @@ -160,7 +160,7 @@ api_v1_controllers_STR_DELETE(struct mg_connection *nc, struct http_message *hm, uuid_t target_uid; if(uuid_parse(target_uid_str, target_uid)) { - LOG_DEBUG("failed to unparse uid\n"); + LOGGER_DEBUG("failed to unparse uid\n"); static const char content[] = "given id was invalid"; endpoint_response_text(response, 400, content, STRLEN(content)); @@ -171,7 +171,7 @@ api_v1_controllers_STR_DELETE(struct mg_connection *nc, struct http_message *hm, if(!controller) { - LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str); + LOGGER_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str); static const char content[] = "no controller for id found"; endpoint_response_text(response, 404, content, STRLEN(content)); @@ -180,7 +180,7 @@ api_v1_controllers_STR_DELETE(struct mg_connection *nc, struct http_message *hm, if(controller_remove(controller)) { - LOG_ERROR("failed to remove controller from database\n"); + LOGGER_ERR("failed to remove controller from database\n"); static const char content[] = "failed to remove controller from database"; endpoint_response_text(response, 500, content, STRLEN(content)); diff --git a/src/endpoints/api_v1_controllers_STR_relays.c b/src/endpoints/api_v1_controllers_STR_relays.c index d00c61f..87c866f 100644 --- a/src/endpoints/api_v1_controllers_STR_relays.c +++ b/src/endpoints/api_v1_controllers_STR_relays.c @@ -15,7 +15,7 @@ api_v1_controllers_STR_relays_GET(struct mg_connection *nc, struct http_message uuid_t target_uid; if(uuid_parse(args[0].value.v_str, target_uid)) { - LOG_DEBUG("failed to unparse uid\n"); + LOGGER_DEBUG("failed to unparse uid\n"); static const char content[] = "given id was invalid"; endpoint_response_text(response, 400, content, STRLEN(content)); @@ -26,7 +26,7 @@ api_v1_controllers_STR_relays_GET(struct mg_connection *nc, struct http_message if(!controller) { - LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str); + LOGGER_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str); static const char content[] = "no controller for id found"; endpoint_response_text(response, 404, content, STRLEN(content)); diff --git a/src/endpoints/api_v1_controllers_STR_relays_INT.c b/src/endpoints/api_v1_controllers_STR_relays_INT.c index 956d71f..e6fa078 100644 --- a/src/endpoints/api_v1_controllers_STR_relays_INT.c +++ b/src/endpoints/api_v1_controllers_STR_relays_INT.c @@ -18,7 +18,7 @@ api_v1_controllers_STR_relays_INT_GET(struct mg_connection *nc, struct http_mess uuid_t target_uid; if(uuid_parse(args[0].value.v_str, target_uid)) { - LOG_DEBUG("failed to unparse uid\n"); + LOGGER_DEBUG("failed to unparse uid\n"); static const char content[] = "given id was invalid"; endpoint_response_text(response, 400, content, STRLEN(content)); @@ -29,7 +29,7 @@ api_v1_controllers_STR_relays_INT_GET(struct mg_connection *nc, struct http_mess if(!controller) { - LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str); + LOGGER_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str); static const char content[] = "no controller for id found"; endpoint_response_text(response, 404, content, STRLEN(content)); @@ -40,7 +40,7 @@ api_v1_controllers_STR_relays_INT_GET(struct mg_connection *nc, struct http_mess if(!relay) { - LOG_DEBUG("could not find a relay with num %d for controller '%s'\n", args[1].value.v_int, args[0].value.v_str); + LOGGER_DEBUG("could not find a relay with num %d for controller '%s'\n", args[1].value.v_int, args[0].value.v_str); static const char content[] = "no relay for this controller found"; endpoint_response_text(response, 404, content, STRLEN(content)); @@ -64,7 +64,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess uuid_t target_uid; if(uuid_parse(args[0].value.v_str, target_uid)) { - LOG_DEBUG("failed to unparse uid\n"); + LOGGER_DEBUG("failed to unparse uid\n"); static const char content[] = "given id was invalid"; endpoint_response_text(response, 400, content, STRLEN(content)); @@ -75,7 +75,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess if(!controller) { - LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str); + LOGGER_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str); static const char content[] = "no controller for id found"; endpoint_response_text(response, 404, content, STRLEN(content)); @@ -134,7 +134,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess cJSON *json_schedule_uid = cJSON_GetObjectItemCaseSensitive(json_schedule, "id"); if(!cJSON_IsString(json_schedule_uid) || (json_schedule_uid->valuestring == NULL)) { - LOG_DEBUG("schedules[%d] is missing uid\n", schedule_position); + LOGGER_DEBUG("schedules[%d] is missing uid\n", schedule_position); cJSON_Delete(json); static const char content[] = "at least one schedule is missing an id"; @@ -144,7 +144,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess uuid_t target_uid; if(schedule_uid_parse(json_schedule_uid->valuestring, target_uid)) { - LOG_DEBUG("schedules[%d] has bad uid\n", schedule_position); + LOGGER_DEBUG("schedules[%d] has bad uid\n", schedule_position); cJSON_Delete(json); static const char content[] = "at least one schedule has a bad id"; @@ -174,7 +174,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess uuid_t target_uid; if(schedule_uid_parse(json_active_schedule_uid->valuestring, target_uid)) { - LOG_DEBUG("active_schedule has bad uid\n"); + LOGGER_DEBUG("active_schedule has bad uid\n"); cJSON_Delete(json); static const char content[] = "active_schedule has a bad id"; @@ -187,7 +187,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess if(relay_save(relay)) { - LOG_ERROR("failed to save relay\n"); + LOGGER_ERR("failed to save relay\n"); free(controller); cJSON_Delete(json); @@ -206,7 +206,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess { if(!cJSON_IsString(json_tag) || (json_tag->valuestring == NULL)) { - LOG_DEBUG("invalid tag in tags\n"); + LOGGER_DEBUG("invalid tag in tags\n"); continue; } const char *tag = json_tag->valuestring; diff --git a/src/endpoints/api_v1_controllers_STR_relays_INT_pulse.c b/src/endpoints/api_v1_controllers_STR_relays_INT_pulse.c index 05efe7a..147b086 100644 --- a/src/endpoints/api_v1_controllers_STR_relays_INT_pulse.c +++ b/src/endpoints/api_v1_controllers_STR_relays_INT_pulse.c @@ -18,7 +18,7 @@ api_v1_controllers_STR_relays_INT_pulse_POST(struct mg_connection *nc, struct ht uuid_t target_uid; if(uuid_parse(args[0].value.v_str, target_uid)) { - LOG_DEBUG("failed to unparse uid\n"); + LOGGER_DEBUG("failed to unparse uid\n"); static const char content[] = "given id was invalid"; endpoint_response_text(response, 400, content, STRLEN(content)); @@ -29,7 +29,7 @@ api_v1_controllers_STR_relays_INT_pulse_POST(struct mg_connection *nc, struct ht if(!controller) { - LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str); + LOGGER_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str); static const char content[] = "no controller for id found"; endpoint_response_text(response, 404, content, STRLEN(content)); @@ -40,7 +40,7 @@ api_v1_controllers_STR_relays_INT_pulse_POST(struct mg_connection *nc, struct ht if(!relay) { - LOG_DEBUG("could not find a relay with num %d for controller '%s'\n", args[1].value.v_int, args[0].value.v_str); + LOGGER_DEBUG("could not find a relay with num %d for controller '%s'\n", args[1].value.v_int, args[0].value.v_str); static const char content[] = "no relay for this controller found"; endpoint_response_text(response, 404, content, STRLEN(content)); diff --git a/src/endpoints/api_v1_controllers_discover.c b/src/endpoints/api_v1_controllers_discover.c index 589c1e8..088696a 100644 --- a/src/endpoints/api_v1_controllers_discover.c +++ b/src/endpoints/api_v1_controllers_discover.c @@ -30,21 +30,21 @@ bind_tcp_server(const char *addr, const char *port, int max_client_backlog) if ((status = getaddrinfo(addr, port, &hints, &res)) != 0) { - LOG_ERROR("error getting address info: %s\n", gai_strerror(status)); + LOGGER_ERR("error getting address info: %s\n", gai_strerror(status)); } fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if ((status = bind(fd, res->ai_addr, res->ai_addrlen)) == -1) { - LOG_ERROR("error binding socket: %s\n", status); + LOGGER_ERR("error binding socket: %s\n", status); freeaddrinfo(res); return -1; } if ((status = listen(fd, max_client_backlog)) == -1) { - LOG_ERROR("error setting up listener: %s\n", status); + LOGGER_ERR("error setting up listener: %s\n", status); freeaddrinfo(res); return -1; } @@ -78,14 +78,14 @@ send_udp_broadcast(const char *addr, uint16_t port, void *message, size_t length if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { - LOG_ERROR("error creating socket\n"); + LOGGER_ERR("error creating socket\n"); return -1; } int broadcast = 1; if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof broadcast) < 0) { - LOG_ERROR("error setting broadcast\n"); + LOGGER_ERR("error setting broadcast\n"); return -1; } @@ -96,7 +96,7 @@ send_udp_broadcast(const char *addr, uint16_t port, void *message, size_t length if(sendto(fd, message, length, 0, (struct sockaddr *)&their_addr, sizeof(their_addr)) < 0) { - LOG_ERROR("error sending broadcast (%d): '%s'\n", errno, strerror(errno)); + LOGGER_ERR("error sending broadcast (%d): '%s'\n", errno, strerror(errno)); return -1; } close(fd); @@ -114,7 +114,7 @@ api_v1_controllers_discover_POST(struct mg_connection *nc, struct http_message * if(discover_server_port == -1) { - LOG_ERROR("failed to get server port for discovery\n"); + LOGGER_ERR("failed to get server port for discovery\n"); static const char content[] = ""; response->status_code = 500; response->content_type = "text/plain"; @@ -129,7 +129,7 @@ api_v1_controllers_discover_POST(struct mg_connection *nc, struct http_message * if(send_udp_broadcast("255.255.255.255", global_config.discovery_port, payload, sizeof(payload)) < 0) { - LOG_ERROR("failed to send UDP broadcast\n"); + LOGGER_ERR("failed to send UDP broadcast\n"); static const char content[] = ""; response->status_code = 500; response->content_type = "text/plain"; @@ -168,7 +168,7 @@ api_v1_controllers_discover_POST(struct mg_connection *nc, struct http_message * { if((client_fd = accept(discover_server_socket, (struct sockaddr *) &their_addr, &addr_size)) < 0) { - LOG_ERROR("error accepting client %s\n", strerror(errno)); + LOGGER_ERR("error accepting client %s\n", strerror(errno)); continue; } @@ -176,7 +176,7 @@ api_v1_controllers_discover_POST(struct mg_connection *nc, struct http_message * if(recv(client_fd, &payload_length, sizeof(payload_length), 0) <= 0) { - LOG_ERROR("error receiving header from client\n"); + LOGGER_ERR("error receiving header from client\n"); continue; } @@ -185,7 +185,7 @@ api_v1_controllers_discover_POST(struct mg_connection *nc, struct http_message * if((bytes_transferred = recv(client_fd, answer_payload, payload_length, 0)) <= 0) { - LOG_ERROR("error receiving payload from client\n"); + LOGGER_ERR("error receiving payload from client\n"); continue; } @@ -194,7 +194,7 @@ api_v1_controllers_discover_POST(struct mg_connection *nc, struct http_message * if(getpeername(client_fd, (struct sockaddr *)&addr, &client_addr_size) != 0) { - LOG_ERROR("error receiving payload from client\n"); + LOGGER_ERR("error receiving payload from client\n"); continue; } @@ -301,7 +301,7 @@ api_v1_controllers_discover_POST(struct mg_connection *nc, struct http_message * { known_controllers[i]->active = false; controller_save(known_controllers[i]); - LOG_DEBUG("lost: %s\n", known_controllers[i]->name); + LOGGER_DEBUG("lost: %s\n", known_controllers[i]->name); } controller_free_list(known_controllers); diff --git a/src/endpoints/api_v1_relays_tag_STR.c b/src/endpoints/api_v1_relays_tag_STR.c index cbe554e..4690a55 100644 --- a/src/endpoints/api_v1_relays_tag_STR.c +++ b/src/endpoints/api_v1_relays_tag_STR.c @@ -23,7 +23,7 @@ api_v1_relays_tag_STR_GET(struct mg_connection *nc, struct http_message *hm, end int *relays_ids = junction_tag_get_relays_for_tag_id(tag_id); if(relays_ids == NULL) { - LOG_ERROR("failed to load relays for tag from database\n"); + LOGGER_ERR("failed to load relays for tag from database\n"); static const char content[] = "failed to load relays for tag from database"; endpoint_response_text(response, 500, content, STRLEN(content)); diff --git a/src/endpoints/api_v1_schedules.c b/src/endpoints/api_v1_schedules.c index d5ee2ba..2dad24d 100644 --- a/src/endpoints/api_v1_schedules.c +++ b/src/endpoints/api_v1_schedules.c @@ -24,7 +24,7 @@ api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoin cJSON *json_name = cJSON_GetObjectItemCaseSensitive(json, "name"); if(!cJSON_IsString(json_name) || (json_name->valuestring == NULL)) { - LOG_DEBUG("no name for schedule provided\n"); + LOGGER_DEBUG("no name for schedule provided\n"); cJSON_Delete(json); static const char content[] = "no name for schedule provided"; @@ -34,7 +34,7 @@ api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoin cJSON *json_periods = cJSON_GetObjectItemCaseSensitive(json, "periods"); if(!cJSON_IsArray(json_periods)) { - LOG_DEBUG("no periods for schedule provided\n"); + LOGGER_DEBUG("no periods for schedule provided\n"); cJSON_Delete(json); static const char content[] = "no periods for schedule provided"; @@ -48,7 +48,7 @@ api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoin { if(!cJSON_IsString(json_tag) || (json_tag->valuestring == NULL)) { - LOG_DEBUG("invalid tag in tags\n"); + LOGGER_DEBUG("invalid tag in tags\n"); cJSON_Delete(json); static const char content[] = "invalid tag in tags"; @@ -78,7 +78,7 @@ api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoin if(!cJSON_IsString(json_period_start) || (json_period_start->valuestring == NULL)) { - LOG_DEBUG("period is missing start\n"); + LOGGER_DEBUG("period is missing start\n"); cJSON_Delete(json); schedule_free(new_schedule); @@ -88,7 +88,7 @@ api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoin } if(!cJSON_IsString(json_period_end) || (json_period_end->valuestring == NULL)) { - LOG_DEBUG("period is missing end\n"); + LOGGER_DEBUG("period is missing end\n"); cJSON_Delete(json); schedule_free(new_schedule); @@ -101,7 +101,7 @@ api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoin uint16_t end; if(period_helper_parse_hhmm(json_period_start->valuestring, &start)) { - LOG_DEBUG("couldn't parse start '%s'\n", json_period_start->valuestring); + LOGGER_DEBUG("couldn't parse start '%s'\n", json_period_start->valuestring); cJSON_Delete(json); schedule_free(new_schedule); @@ -111,7 +111,7 @@ api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoin } if(period_helper_parse_hhmm(json_period_end->valuestring, &end)) { - LOG_DEBUG("couldn't parse end '%s'\n", json_period_end->valuestring); + LOGGER_DEBUG("couldn't parse end '%s'\n", json_period_end->valuestring); cJSON_Delete(json); schedule_free(new_schedule); diff --git a/src/endpoints/api_v1_schedules_STR.c b/src/endpoints/api_v1_schedules_STR.c index 0c5c369..ca315b4 100644 --- a/src/endpoints/api_v1_schedules_STR.c +++ b/src/endpoints/api_v1_schedules_STR.c @@ -19,7 +19,7 @@ api_v1_schedules_STR_GET(struct mg_connection *nc, struct http_message *hm, endp uuid_t target_uid; if(schedule_uid_parse(args[0].value.v_str, target_uid)) { - LOG_DEBUG("failed to unparse uid\n"); + LOGGER_DEBUG("failed to unparse uid\n"); static const char content[] = "given id was invalid"; endpoint_response_text(response, 400, content, STRLEN(content)); @@ -30,7 +30,7 @@ api_v1_schedules_STR_GET(struct mg_connection *nc, struct http_message *hm, endp if(!schedule) { - LOG_DEBUG("could not find a schedule for uid '%s'\n", args[0].value.v_str); + LOGGER_DEBUG("could not find a schedule for uid '%s'\n", args[0].value.v_str); static const char content[] = "no schedule for id found"; endpoint_response_text(response, 404, content, STRLEN(content)); @@ -53,7 +53,7 @@ api_v1_schedules_STR_PUT(struct mg_connection *nc, struct http_message *hm, endp uuid_t target_uid; if(schedule_uid_parse(args[0].value.v_str, target_uid)) { - LOG_DEBUG("failed to unparse uid\n"); + LOGGER_DEBUG("failed to unparse uid\n"); static const char content[] = "given id was invalid"; endpoint_response_text(response, 400, content, STRLEN(content)); @@ -64,7 +64,7 @@ api_v1_schedules_STR_PUT(struct mg_connection *nc, struct http_message *hm, endp if(!schedule) { - LOG_DEBUG("could not find a schedule for uid '%s'\n", args[0].value.v_str); + LOGGER_DEBUG("could not find a schedule for uid '%s'\n", args[0].value.v_str); static const char content[] = "no schedule for id found"; endpoint_response_text(response, 404, content, STRLEN(content)); @@ -105,12 +105,12 @@ api_v1_schedules_STR_PUT(struct mg_connection *nc, struct http_message *hm, endp if(!cJSON_IsString(json_period_start) || (json_period_start->valuestring == NULL)) { - LOG_DEBUG("period is missing start\n"); + LOGGER_DEBUG("period is missing start\n"); continue; } if(!cJSON_IsString(json_period_end) || (json_period_end->valuestring == NULL)) { - LOG_DEBUG("period is missing end\n"); + LOGGER_DEBUG("period is missing end\n"); continue; } @@ -118,12 +118,12 @@ api_v1_schedules_STR_PUT(struct mg_connection *nc, struct http_message *hm, endp uint16_t end; if(period_helper_parse_hhmm(json_period_start->valuestring, &start)) { - LOG_DEBUG("couldn't parse start '%s'\n", json_period_start->valuestring); + LOGGER_DEBUG("couldn't parse start '%s'\n", json_period_start->valuestring); continue; } if(period_helper_parse_hhmm(json_period_end->valuestring, &end)) { - LOG_DEBUG("couldn't parse end '%s'\n", json_period_end->valuestring); + LOGGER_DEBUG("couldn't parse end '%s'\n", json_period_end->valuestring); continue; } @@ -137,7 +137,7 @@ api_v1_schedules_STR_PUT(struct mg_connection *nc, struct http_message *hm, endp if(schedule_save(schedule)) { - LOG_ERROR("failed to save schedule\n"); + LOGGER_ERR("failed to save schedule\n"); free(schedule); cJSON_Delete(json); @@ -162,7 +162,7 @@ api_v1_schedules_STR_PUT(struct mg_connection *nc, struct http_message *hm, endp { if(!cJSON_IsString(json_tag) || (json_tag->valuestring == NULL)) { - LOG_DEBUG("invalid tag in tags\n"); + LOGGER_DEBUG("invalid tag in tags\n"); continue; } const char *tag = json_tag->valuestring; @@ -195,7 +195,7 @@ api_v1_schedules_STR_DELETE(struct mg_connection *nc, struct http_message *hm, e uuid_t target_uid; if(schedule_uid_parse(target_uid_str, target_uid)) { - LOG_DEBUG("failed to unparse uid\n"); + LOGGER_DEBUG("failed to unparse uid\n"); static const char content[] = "given id was invalid"; endpoint_response_text(response, 400, content, STRLEN(content)); @@ -206,7 +206,7 @@ api_v1_schedules_STR_DELETE(struct mg_connection *nc, struct http_message *hm, e if(!schedule) { - LOG_DEBUG("could not find a schedule for uid '%s'\n", args[0].value.v_str); + LOGGER_DEBUG("could not find a schedule for uid '%s'\n", args[0].value.v_str); static const char content[] = "no schedule for id found"; endpoint_response_text(response, 404, content, STRLEN(content)); @@ -224,7 +224,7 @@ api_v1_schedules_STR_DELETE(struct mg_connection *nc, struct http_message *hm, e if(schedule_remove(schedule)) { - LOG_ERROR("failed to remove schedule from database\n"); + LOGGER_ERR("failed to remove schedule from database\n"); static const char content[] = "failed to remove schedule from database"; endpoint_response_text(response, 500, content, STRLEN(content)); diff --git a/src/endpoints/api_v1_schedules_list.c b/src/endpoints/api_v1_schedules_list.c index 1cd9430..ede980b 100644 --- a/src/endpoints/api_v1_schedules_list.c +++ b/src/endpoints/api_v1_schedules_list.c @@ -28,7 +28,7 @@ api_v1_schedules_list_POST(struct mg_connection *nc, struct http_message *hm, en cJSON *json_name = cJSON_GetObjectItemCaseSensitive(json, "name"); if(!cJSON_IsString(json_name) || (json_name->valuestring == NULL)) { - LOG_DEBUG("no name for schedule provided\n"); + LOGGER_DEBUG("no name for schedule provided\n"); cJSON_Delete(json_list); static const char content[] = "no name for schedule provided"; @@ -38,7 +38,7 @@ api_v1_schedules_list_POST(struct mg_connection *nc, struct http_message *hm, en cJSON *json_periods = cJSON_GetObjectItemCaseSensitive(json, "periods"); if(!cJSON_IsArray(json_periods)) { - LOG_DEBUG("no periods for schedule provided\n"); + LOGGER_DEBUG("no periods for schedule provided\n"); cJSON_Delete(json_list); static const char content[] = "no periods for schedule provided"; @@ -52,7 +52,7 @@ api_v1_schedules_list_POST(struct mg_connection *nc, struct http_message *hm, en { if(!cJSON_IsString(json_tag) || (json_tag->valuestring == NULL)) { - LOG_DEBUG("invalid tag in tags\n"); + LOGGER_DEBUG("invalid tag in tags\n"); cJSON_Delete(json_list); static const char content[] = "invalid tag in tags"; @@ -82,7 +82,7 @@ api_v1_schedules_list_POST(struct mg_connection *nc, struct http_message *hm, en if(!cJSON_IsString(json_period_start) || (json_period_start->valuestring == NULL)) { - LOG_DEBUG("period is missing start\n"); + LOGGER_DEBUG("period is missing start\n"); cJSON_Delete(json_list); schedule_free(new_schedule); @@ -92,7 +92,7 @@ api_v1_schedules_list_POST(struct mg_connection *nc, struct http_message *hm, en } if(!cJSON_IsString(json_period_end) || (json_period_end->valuestring == NULL)) { - LOG_DEBUG("period is missing end\n"); + LOGGER_DEBUG("period is missing end\n"); cJSON_Delete(json_list); schedule_free(new_schedule); @@ -105,7 +105,7 @@ api_v1_schedules_list_POST(struct mg_connection *nc, struct http_message *hm, en uint16_t end; if(period_helper_parse_hhmm(json_period_start->valuestring, &start)) { - LOG_DEBUG("couldn't parse start '%s'\n", json_period_start->valuestring); + LOGGER_DEBUG("couldn't parse start '%s'\n", json_period_start->valuestring); cJSON_Delete(json_list); schedule_free(new_schedule); @@ -115,7 +115,7 @@ api_v1_schedules_list_POST(struct mg_connection *nc, struct http_message *hm, en } if(period_helper_parse_hhmm(json_period_end->valuestring, &end)) { - LOG_DEBUG("couldn't parse end '%s'\n", json_period_end->valuestring); + LOGGER_DEBUG("couldn't parse end '%s'\n", json_period_end->valuestring); cJSON_Delete(json_list); schedule_free(new_schedule); diff --git a/src/endpoints/api_v1_schedules_tag_STR.c b/src/endpoints/api_v1_schedules_tag_STR.c index 3cbf403..b0d22ec 100644 --- a/src/endpoints/api_v1_schedules_tag_STR.c +++ b/src/endpoints/api_v1_schedules_tag_STR.c @@ -23,7 +23,7 @@ api_v1_schedules_tag_STR_GET(struct mg_connection *nc, struct http_message *hm, int *schedules_ids = junction_tag_get_schedules_for_tag_id(tag_id); if(schedules_ids == NULL) { - LOG_ERROR("failed to load schedules for tag from database\n"); + LOGGER_ERR("failed to load schedules for tag from database\n"); static const char content[] = "failed to load schedules for tag from database"; endpoint_response_text(response, 500, content, STRLEN(content)); diff --git a/src/endpoints/api_v1_tags.c b/src/endpoints/api_v1_tags.c index 6ee6226..66fc6e8 100644 --- a/src/endpoints/api_v1_tags.c +++ b/src/endpoints/api_v1_tags.c @@ -21,7 +21,7 @@ api_v1_tags_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args cJSON *json_tag = cJSON_CreateString(all_tags[i]); if (json_tag == NULL) { - LOG_DEBUG("failed to add tag from string '%s'\n", all_tags[i]); + LOGGER_DEBUG("failed to add tag from string '%s'\n", all_tags[i]); free(all_tags[i]); continue; } diff --git a/src/endpoints/api_v1_tags_STR.c b/src/endpoints/api_v1_tags_STR.c index ddac57f..6cff7df 100644 --- a/src/endpoints/api_v1_tags_STR.c +++ b/src/endpoints/api_v1_tags_STR.c @@ -23,7 +23,7 @@ api_v1_tags_STR_GET(struct mg_connection *nc, struct http_message *hm, endpoint_ int *relays_ids = junction_tag_get_relays_for_tag_id(tag_id); if(relays_ids == NULL) { - LOG_ERROR("failed to load relays for tag from database\n"); + LOGGER_ERR("failed to load relays for tag from database\n"); static const char content[] = "failed to load relays for tag from database"; endpoint_response_text(response, 500, content, STRLEN(content)); @@ -32,7 +32,7 @@ api_v1_tags_STR_GET(struct mg_connection *nc, struct http_message *hm, endpoint_ int *schedules_ids = junction_tag_get_schedules_for_tag_id(tag_id); if(schedules_ids == NULL) { - LOG_ERROR("failed to load schedules for tag from database\n"); + LOGGER_ERR("failed to load schedules for tag from database\n"); static const char content[] = "failed to load schedules for tag from database"; endpoint_response_text(response, 500, content, STRLEN(content)); @@ -98,7 +98,7 @@ api_v1_tags_STR_DELETE(struct mg_connection *nc, struct http_message *hm, endpoi if(tag_remove(tag_id)) { - LOG_ERROR("failed to remove tag from database\n"); + LOGGER_ERR("failed to remove tag from database\n"); static const char content[] = "failed to remove tag from database"; endpoint_response_text(response, 500, content, STRLEN(content)); diff --git a/src/handlers/http.c b/src/handlers/http.c index 3383521..5a3fbf3 100644 --- a/src/handlers/http.c +++ b/src/handlers/http.c @@ -55,8 +55,6 @@ send_response(struct mg_connection *nc, endpoint_response_t *response) static void handle_websocket_request(struct mg_connection *nc, struct http_message *hm) { - LOG_TRACE("new websocket %.*s request for %.*s\n", hm->method.len, hm->method.p, hm->uri.len, hm->uri.p); - struct mg_str method_websocket_str = mg_mk_str("WEBSOCKET"); endpoint_t *endpoint = router_find_endpoint(hm->uri.p, hm->uri.len, &method_websocket_str); @@ -81,8 +79,6 @@ handle_websocket_request(struct mg_connection *nc, struct http_message *hm) static void handle_http_request(struct mg_connection *nc, struct http_message *hm) { - LOG_TRACE("new http %.*s request for %.*s\n", hm->method.len, hm->method.p, hm->uri.len, hm->uri.p); - endpoint_t *endpoint = router_find_endpoint(hm->uri.p, hm->uri.len, &hm->method); endpoint_response_t response; @@ -106,7 +102,7 @@ handle_http_request(struct mg_connection *nc, struct http_message *hm) ++request_file; } - LOG_DEBUG("%s\n", request_file); + LOGGER_DEBUG("%s\n", request_file); char *request_file_path = malloc(sizeof(char) * (strlen(request_file) + strlen(global_config.content_dir) + 2)); sprintf(request_file_path, "%s/%s", global_config.content_dir, request_file); int access_result = access(request_file_path, R_OK); diff --git a/src/handlers/mqtt.c b/src/handlers/mqtt.c index c15091e..d7200fe 100644 --- a/src/handlers/mqtt.c +++ b/src/handlers/mqtt.c @@ -47,7 +47,7 @@ handle_mqtt_publish(struct mg_mqtt_message *msg) char *topic = malloc(sizeof(char) * (msg->topic.len + 1)); strncpy(topic, msg->topic.p, msg->topic.len); topic[msg->topic.len] = '\0'; - LOG_DEBUG("received mqtt publish for topic %s\n", topic); + LOGGER_DEBUG("received mqtt publish for topic %s\n", topic); char *payload = malloc(sizeof(char) * (msg->payload.len + 1)); strncpy(payload, msg->payload.p, msg->payload.len); diff --git a/src/helpers/connect_server.c b/src/helpers/connect_server.c index 1f81afb..dbfdc80 100644 --- a/src/helpers/connect_server.c +++ b/src/helpers/connect_server.c @@ -20,7 +20,7 @@ helper_connect_tcp_server(char* host, uint16_t port) hints.ai_socktype = SOCK_STREAM; //set socket flag if ((status = getaddrinfo(host, port_str, &hints, &res)) != 0) { //getaddrinfo() will evaluate the given address, using the hints-flags and port, and return an IP address and other server infos - LOG_ERROR("getaddrinfo: %s\n", gai_strerror(status)); + LOGGER_ERR("getaddrinfo: %s\n", gai_strerror(status)); return -1; } @@ -28,7 +28,7 @@ helper_connect_tcp_server(char* host, uint16_t port) s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); //creating Socket if ((status = connect(s, res->ai_addr, res->ai_addrlen)) != 0) { - LOG_ERROR("connect() failed\n"); + LOGGER_ERR("connect() failed\n"); freeaddrinfo(res); return -1; } diff --git a/src/helpers/drop_privileges.c b/src/helpers/drop_privileges.c index b19eaf5..e2dde8a 100644 --- a/src/helpers/drop_privileges.c +++ b/src/helpers/drop_privileges.c @@ -22,7 +22,7 @@ get_uid_for_user(char *user) if(pwd == NULL) { - LOG_FATAL("couldn't find user to drop privileges\n"); + LOGGER_CRIT("couldn't find user to drop privileges\n"); exit(1); } @@ -46,7 +46,7 @@ get_gid_for_group(char *group) if(grp == NULL) { - LOG_FATAL("couldn't find group to drop privileges\n"); + LOGGER_CRIT("couldn't find group to drop privileges\n"); exit(1); } @@ -62,16 +62,16 @@ helper_drop_privileges() uid_t uid = get_uid_for_user(global_config.user); gid_t gid = get_gid_for_group(global_config.group); - LOG_DEBUG("drop privileges to %lu:%lu\n", uid, gid); + LOGGER_DEBUG("drop privileges to %lu:%lu\n", uid, gid); if (setgid(gid) == -1) { - LOG_FATAL("failed to drop group privileges\n"); + LOGGER_CRIT("failed to drop group privileges\n"); exit(1); } if (setuid(uid) == -1) { - LOG_FATAL("failed to drop user privileges\n"); + LOGGER_CRIT("failed to drop user privileges\n"); exit(1); } diff --git a/src/helpers/parse_cli.c b/src/helpers/parse_cli.c index f708ed9..a65caed 100644 --- a/src/helpers/parse_cli.c +++ b/src/helpers/parse_cli.c @@ -50,12 +50,12 @@ helper_parse_cli(int argc, const char **argv, config_t *config) config->run_type = RUN_TYPE_START; return; } - LOG_FATAL("bad action '%s' given ('start')\n", argv[0]); + LOGGER_CRIT("bad action '%s' given ('start')\n", argv[0]); exit(1); } else { - LOG_FATAL("no action given ('start')\n"); + LOGGER_CRIT("no action given ('start')\n"); exit(1); } return; diff --git a/src/logger.c b/src/logger.c index fb26d56..7e7d2d7 100644 --- a/src/logger.c +++ b/src/logger.c @@ -5,44 +5,56 @@ #include #include -#define COLOR_TRACE COLOR_GREEN -#define COLOR_DEBUG COLOR_BLUE -#define COLOR_INFO COLOR_CYAN -#define COLOR_WARN COLOR_YELLOW -#define COLOR_ERROR COLOR_RED -#define COLOR_FATAL COLOR_MAGENTA +const char *COLOR_DEBUG = COLOR_GREEN; +const char *COLOR_INFO = COLOR_CYAN; +const char *COLOR_NOTICE = COLOR_CYAN; +const char *COLOR_WARNING = COLOR_YELLOW; +const char *COLOR_ERR = COLOR_RED; +const char *COLOR_CRIT = COLOR_MAGENTA; +const char *COLOR_EMERG = COLOR_MAGENTA; void -logger_log(FILE *stream, log_level_t level, const char *filename, int line, const char *func, const char *msg, ...) +logger_log(int level, const char *filename, int line, const char *func, const char *msg, ...) { if(global_config.log_level < level) { return; } + const char *level_str; + const char *color; switch(level) { - case LOG_LEVEL_TRACE: - fprintf(stream, COLOR_TRACE "[TRACE] "); + case LOG_DEBUG: + color = COLOR_DEBUG; + level_str = "DEBUG"; break; - case LOG_LEVEL_DEBUG: - fprintf(stream, COLOR_DEBUG "[DEBUG] "); + case LOG_INFO: + color = COLOR_INFO; + level_str = "INFO"; break; - case LOG_LEVEL_INFO: - fprintf(stream, COLOR_INFO "[INFO ] "); + case LOG_NOTICE: + color = COLOR_NOTICE; + level_str = "NOTE"; break; - case LOG_LEVEL_WARN: - fprintf(stream, COLOR_WARN "[WARN ] "); + case LOG_WARNING: + color = COLOR_WARNING; + level_str = "WARN"; break; - case LOG_LEVEL_ERROR: - fprintf(stream, COLOR_ERROR "[ERROR] "); + case LOG_ERR: + color = COLOR_ERR; + level_str = "ERROR"; break; - case LOG_LEVEL_FATAL: - fprintf(stream, COLOR_FATAL "[FATAL] "); + case LOG_CRIT: + color = COLOR_CRIT; + level_str = "CRIT"; + break; + case LOG_EMERG: + color = COLOR_EMERG; + level_str = "EMERG"; break; default: - fprintf(stream, COLOR_NONE "[LOG ] "); - break; + return; } char timestamp_str[32]; @@ -50,10 +62,23 @@ logger_log(FILE *stream, log_level_t level, const char *filename, int line, cons time(&rawtime); strftime(timestamp_str, 32, "%Y-%m-%d %H:%M:%S", localtime(&rawtime)); - fprintf(stream, "%s %s:%d:%s " COLOR_NONE, timestamp_str, filename, line, func); + char *buffer = malloc(sizeof(char) * (128 + strlen(msg))); + sprintf(buffer, "%s[%5s] %s:%d:%s " COLOR_NONE "%s", color, level_str, filename, line, func, msg); + + //fprintf(stream, "%s %s:%d:%s " COLOR_NONE, timestamp_str, filename, line, func); va_list args; va_start(args, msg); - vfprintf(stream, msg, args); + vsyslog(level, buffer, args); va_end(args); + + char *buffer_timed = malloc(sizeof(char) * (strlen(timestamp_str) + strlen(buffer) + 2)); + sprintf(buffer_timed, "%s %s", timestamp_str, buffer); + va_start(args, msg); + vfprintf(global_config.log_file, buffer_timed, args); + fflush(global_config.log_file); + va_end(args); + + free(buffer); + free(buffer_timed); } diff --git a/src/main.c b/src/main.c index 493df97..3ee3b8f 100644 --- a/src/main.c +++ b/src/main.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -20,7 +21,7 @@ static struct mg_mgr mgr; static void terminate(int signum) { - LOG_INFO("terminating controller (%d)\n", signum); + LOGGER_INFO("terminating controller (%d)\n", signum); mg_mgr_free(&mgr); @@ -29,6 +30,8 @@ terminate(int signum) router_free(); status_free(); + closelog(); + exit(signum); } @@ -47,15 +50,20 @@ main(int argc, const char** argv) signal(SIGABRT, terminate); signal(SIGTERM, terminate); + setlogmask(LOG_UPTO(LOG_INFO)); + + /******************** LOAD CONFIG ********************/ global_config.file = "core.ini"; - global_config.log_level = LOG_LEVEL_INFO; global_config.discovery_port = 4421; global_config.mqtt_port = 1885; global_config.server_port = 5000; + global_config.log_level = LOG_INFO; + global_config.log_file = stdout; + strcpy(global_config.user, ""); strcpy(global_config.group, ""); @@ -70,12 +78,12 @@ main(int argc, const char** argv) FILE * const ini_file = fopen(global_config.file, "rb"); if(ini_file == NULL) { - LOG_FATAL("config file '%s' was not found\n", global_config.file); + LOGGER_CRIT("config file '%s' was not found\n", global_config.file); exit(1); } if(load_ini_file( ini_file, INI_DEFAULT_FORMAT, NULL, config_load, &global_config)) { - LOG_FATAL("unable to parse ini file\n"); + LOGGER_CRIT("unable to parse ini file\n"); exit(1); } @@ -86,6 +94,12 @@ main(int argc, const char** argv) global_config.http_server_opts.enable_directory_listing = "no"; global_config.http_server_opts.extra_headers = "Access-Control-Allow-Origin: *\r\nAccess-Control-Allow-Headers: *\r\nAccess-Control-Allow-Methods: *"; + if(global_config.log_file == NULL) + { + global_config.log_file = stdout; + } + openlog("emgauwa-core", 0, LOG_USER); + /******************** SETUP CONNECTION ********************/ @@ -98,7 +112,7 @@ main(int argc, const char** argv) struct mg_connection *c_http = mg_bind(&mgr, address, handler_http); if(c_http == NULL) { - LOG_FATAL("failed to bind http server to port %u\n", global_config.server_port); + LOGGER_CRIT("failed to bind http server to port %u\n", global_config.server_port); exit(1); } mg_set_protocol_http_websocket(c_http); @@ -107,7 +121,7 @@ main(int argc, const char** argv) struct mg_connection *c_mqtt = mg_bind(&mgr, address, handler_mqtt); if(c_mqtt == NULL) { - LOG_FATAL("failed to bind mqtt server to port %u\n", global_config.mqtt_port); + LOGGER_CRIT("failed to bind mqtt server to port %u\n", global_config.mqtt_port); exit(1); } mg_mqtt_broker_init(&brk, NULL); @@ -123,7 +137,7 @@ main(int argc, const char** argv) if(rc) { - LOG_FATAL("can't open database: %s\n", sqlite3_errmsg(global_database)); + LOGGER_CRIT("can't open database: %s\n", sqlite3_errmsg(global_database)); return 1; } diff --git a/src/models/controller.c b/src/models/controller.c index 26b8188..d6be48e 100644 --- a/src/models/controller.c +++ b/src/models/controller.c @@ -101,7 +101,7 @@ controller_db_select(sqlite3_stmt *stmt) } else { - LOG_ERROR("error selecting controllers from database: %s\n", sqlite3_errstr(s)); + LOGGER_ERR("error selecting controllers from database: %s\n", sqlite3_errstr(s)); break; } } @@ -130,11 +130,11 @@ controller_save(controller_t *controller) { if(controller->id) { - LOG_ERROR("error inserting data: %s\n", sqlite3_errmsg(global_database)); + LOGGER_ERR("error inserting data: %s\n", sqlite3_errmsg(global_database)); } else { - LOG_ERROR("error updating data: %s\n", sqlite3_errmsg(global_database)); + LOGGER_ERR("error updating data: %s\n", sqlite3_errmsg(global_database)); } } else diff --git a/src/models/junction_relay_schedule.c b/src/models/junction_relay_schedule.c index c3d5250..594965f 100644 --- a/src/models/junction_relay_schedule.c +++ b/src/models/junction_relay_schedule.c @@ -21,7 +21,7 @@ junction_relay_schedule_insert(uint8_t weekday, int relay_id, int schedule_id) rc = sqlite3_step(stmt); if (rc != SQLITE_DONE) { - LOG_ERROR("error inserting data: %s", sqlite3_errmsg(global_database)); + LOGGER_ERR("error inserting data: %s", sqlite3_errmsg(global_database)); return false; } diff --git a/src/models/junction_tag.c b/src/models/junction_tag.c index c4c6a33..1526c02 100644 --- a/src/models/junction_tag.c +++ b/src/models/junction_tag.c @@ -78,7 +78,7 @@ get_ids(sqlite3_stmt *stmt) } else { - LOG_ERROR("error selecting relays from database: %s\n", sqlite3_errstr(s)); + LOGGER_ERR("error selecting relays from database: %s\n", sqlite3_errstr(s)); sqlite3_finalize(stmt); return NULL; } diff --git a/src/models/relay.c b/src/models/relay.c index 3463901..8bc238a 100644 --- a/src/models/relay.c +++ b/src/models/relay.c @@ -69,7 +69,7 @@ relay_db_select_mapper(sqlite3_stmt *stmt) { if(schedules[i] == NULL) { - LOG_ERROR("got only %d/7 schedules for relay_id %d\n", i, new_relay->id); + LOGGER_ERR("got only %d/7 schedules for relay_id %d\n", i, new_relay->id); relay_free(new_relay); free(schedules); return NULL; @@ -111,7 +111,7 @@ relay_db_select(sqlite3_stmt *stmt) } else { - LOG_ERROR("error selecting relays from database: %s\n", sqlite3_errstr(s)); + LOGGER_ERR("error selecting relays from database: %s\n", sqlite3_errstr(s)); break; } } @@ -140,11 +140,11 @@ relay_save(relay_t *relay) { if(relay->id) { - LOG_ERROR("error inserting data: %s\n", sqlite3_errmsg(global_database)); + LOGGER_ERR("error inserting data: %s\n", sqlite3_errmsg(global_database)); } else { - LOG_ERROR("error updating data: %s\n", sqlite3_errmsg(global_database)); + LOGGER_ERR("error updating data: %s\n", sqlite3_errmsg(global_database)); } } else @@ -203,7 +203,7 @@ relay_to_json(relay_t *relay) cJSON *json_number = cJSON_CreateNumber(relay->number); if(json_number == NULL) { - LOG_DEBUG("failed to make number\n"); + LOGGER_DEBUG("failed to make number\n"); cJSON_Delete(json); return NULL; } @@ -212,7 +212,7 @@ relay_to_json(relay_t *relay) cJSON *json_name = cJSON_CreateString(relay->name); if(json_name == NULL) { - LOG_DEBUG("failed to make name\n"); + LOGGER_DEBUG("failed to make name\n"); cJSON_Delete(json); return NULL; } @@ -221,7 +221,7 @@ relay_to_json(relay_t *relay) controller_t *controller = controller_get_by_id(relay->controller_id); if(!controller) { - LOG_WARN("failed to get controller\n"); + LOGGER_WARNING("failed to get controller\n"); cJSON_Delete(json); return NULL; } @@ -231,7 +231,7 @@ relay_to_json(relay_t *relay) cJSON *json_controller_id = cJSON_CreateString(uuid_str); if(json_controller_id == NULL) { - LOG_DEBUG("failed to make controller id\n"); + LOGGER_DEBUG("failed to make controller id\n"); cJSON_Delete(json); return NULL; } @@ -278,7 +278,7 @@ relay_to_json(relay_t *relay) cJSON *json_tag = cJSON_CreateString(tag); if (json_tag == NULL) { - LOG_DEBUG("failed to add tag from string '%s'\n", tag); + LOGGER_DEBUG("failed to add tag from string '%s'\n", tag); free(tag); continue; } diff --git a/src/models/schedule.c b/src/models/schedule.c index bd27200..bd9839d 100644 --- a/src/models/schedule.c +++ b/src/models/schedule.c @@ -96,7 +96,7 @@ schedule_db_select(sqlite3_stmt *stmt) } else { - LOG_ERROR("error selecting schedules from database: %s\n", sqlite3_errstr(s)); + LOGGER_ERR("error selecting schedules from database: %s\n", sqlite3_errstr(s)); break; } } @@ -125,11 +125,11 @@ schedule_save(schedule_t *schedule) { if(schedule->id) { - LOG_ERROR("error inserting data: %s\n", sqlite3_errmsg(global_database)); + LOGGER_ERR("error inserting data: %s\n", sqlite3_errmsg(global_database)); } else { - LOG_ERROR("error updating data: %s\n", sqlite3_errmsg(global_database)); + LOGGER_ERR("error updating data: %s\n", sqlite3_errmsg(global_database)); } } else @@ -262,7 +262,7 @@ schedule_to_json(schedule_t *schedule) cJSON *json_period_start = cJSON_CreateString(start_str); if (json_period_start == NULL) { - LOG_DEBUG("failed to add start period from string '%s'\n", start_str); + LOGGER_DEBUG("failed to add start period from string '%s'\n", start_str); cJSON_Delete(json_period); continue; } @@ -271,7 +271,7 @@ schedule_to_json(schedule_t *schedule) cJSON *json_period_end = cJSON_CreateString(end_str); if (json_period_end == NULL) { - LOG_DEBUG("failed to add end period from string '%s'\n", end_str); + LOGGER_DEBUG("failed to add end period from string '%s'\n", end_str); cJSON_Delete(json_period); continue; } @@ -295,7 +295,7 @@ schedule_to_json(schedule_t *schedule) cJSON *json_tag = cJSON_CreateString(tag); if (json_tag == NULL) { - LOG_DEBUG("failed to add tag from string '%s'\n", tag); + LOGGER_DEBUG("failed to add tag from string '%s'\n", tag); free(tag); continue; } diff --git a/src/models/tag.c b/src/models/tag.c index 1e91e16..66f840a 100644 --- a/src/models/tag.c +++ b/src/models/tag.c @@ -27,7 +27,7 @@ tag_save(int id, const char *tag) rc = sqlite3_step(stmt); if (rc != SQLITE_DONE) { - LOG_ERROR("error saving tag: %s\n", sqlite3_errmsg(global_database)); + LOGGER_ERR("error saving tag: %s\n", sqlite3_errmsg(global_database)); return false; } @@ -65,7 +65,7 @@ tag_get_tag(int id) } else { - LOG_ERROR("error selecting tags from database: %s\n", sqlite3_errstr(s)); + LOGGER_ERR("error selecting tags from database: %s\n", sqlite3_errstr(s)); sqlite3_finalize(stmt); return NULL; } @@ -111,7 +111,7 @@ tag_get_all() } else { - LOG_ERROR("error selecting tags from database: %s\n", sqlite3_errstr(s)); + LOGGER_ERR("error selecting tags from database: %s\n", sqlite3_errstr(s)); break; } } @@ -148,7 +148,7 @@ tag_get_id(const char *tag) } else { - LOG_ERROR("error selecting tags from database: %s\n", sqlite3_errstr(s)); + LOGGER_ERR("error selecting tags from database: %s\n", sqlite3_errstr(s)); sqlite3_finalize(stmt); return 0; } diff --git a/src/router.c b/src/router.c index 10fa4f4..14e53de 100644 --- a/src/router.c +++ b/src/router.c @@ -118,7 +118,7 @@ router_register_endpoint(const char *route, int method, endpoint_func_f func) if(endpoints_registered >= ROUTER_ENDPOINTS_MAX_COUNT) { - LOG_ERROR("can't register more than %d endpoints\n", ROUTER_ENDPOINTS_MAX_COUNT); + LOGGER_ERR("can't register more than %d endpoints\n", ROUTER_ENDPOINTS_MAX_COUNT); return NULL; } diff --git a/tests/controller.testing.ini b/tests/controller.testing.ini index b58e30e..b21277d 100644 --- a/tests/controller.testing.ini +++ b/tests/controller.testing.ini @@ -5,10 +5,12 @@ name = new emgauwa device discovery-port = 4422 : 1886 for testing; 1885 for dev-env; 1884 for testing-env; 1883 for prod-env mqtt-port = 1886 +mqtt-host = localhost relay-count = 10 database = controller_db.lmdb log-level = debug +log-file = stdout [relay-0] driver = piface @@ -44,11 +46,13 @@ inverted = 1 driver = gpio pin = 1 inverted = 1 +pulse-duration = 3 [relay-7] driver = gpio pin = 0 inverted = 1 +pulse-duration = 3 [relay-8] driver = gpio diff --git a/tests/core.testing.ini b/tests/core.testing.ini index 177fc17..c3710a1 100644 --- a/tests/core.testing.ini +++ b/tests/core.testing.ini @@ -1,14 +1,16 @@ [core] server-port = 5000 database = core.sqlite +content-dir = /usr/share/webapps/emgauwa not-found-file = 404.html not-found-file-mime = text/html not-found-content = 404 - NOT FOUND not-found-content-type = text/plain -: 4421 for dev-env; 4420 for testing-env; 4419 for prod-env; 4422 for testing +: 4422 for testing; 4421 for dev-env; 4420 for testing-env; 4419 for prod-env discovery-port = 4422 : 1886 for testing; 1885 for dev-env; 1884 for testing-env; 1883 for prod-env mqtt-port = 1886 log-level = debug +log-file = stdout