fix: better logging behaviour
This commit is contained in:
parent
011c5a6102
commit
0530a350df
30 changed files with 343 additions and 176 deletions
|
@ -1,6 +1,6 @@
|
|||
cmake_minimum_required (VERSION 3.7)
|
||||
project(controller
|
||||
VERSION 0.2.6
|
||||
VERSION 0.2.7
|
||||
LANGUAGES C)
|
||||
|
||||
add_executable(controller src/main.c)
|
||||
|
|
|
@ -10,6 +10,7 @@ mqtt-host = localhost
|
|||
relay-count = 10
|
||||
database = controller_db.lmdb
|
||||
log-level = debug
|
||||
log-file = stdout
|
||||
|
||||
[relay-0]
|
||||
driver = piface
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#define CONTROLLER_CONFIG_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <confini.h>
|
||||
|
||||
#include <constants.h>
|
||||
#include <enums.h>
|
||||
|
@ -14,11 +15,17 @@ typedef struct
|
|||
uint8_t pulse_duration;
|
||||
} config_relay_t;
|
||||
|
||||
int
|
||||
config_load(IniDispatch *disp, void *config_void);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char *file;
|
||||
char database[256];
|
||||
log_level_t log_level;
|
||||
char user[256];
|
||||
char group[256];
|
||||
int log_level;
|
||||
FILE *log_file;
|
||||
run_type_t run_type;
|
||||
char name[MAX_NAME_LENGTH + 1];
|
||||
uint16_t discovery_port;
|
||||
|
|
|
@ -53,14 +53,4 @@ typedef enum
|
|||
RUN_TYPE_TEST,
|
||||
} 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;
|
||||
|
||||
#endif /* CONTROLLER_ENUMS_H */
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
#define CONTROLLER_HELPERS_H
|
||||
|
||||
#include <time.h>
|
||||
#include <confini.h>
|
||||
#include <config.h>
|
||||
|
||||
int
|
||||
|
@ -26,13 +25,13 @@ helper_get_port(int sock);
|
|||
int
|
||||
helper_open_discovery_socket(uint16_t discovery_port);
|
||||
|
||||
int
|
||||
helper_load_config(IniDispatch *disp, void *config_void);
|
||||
|
||||
void
|
||||
helper_parse_cli(int argc, const char **argv, config_t *config);
|
||||
|
||||
int
|
||||
helper_get_weekday(const struct tm *time_struct);
|
||||
|
||||
int
|
||||
helper_drop_privileges();
|
||||
|
||||
#endif /* CONTROLLER_HELPERS_H */
|
||||
|
|
|
@ -1,20 +1,22 @@
|
|||
#ifndef CONTROLLER_LOGGER_H
|
||||
#define CONTROLLER_LOGGER_H
|
||||
#ifndef CORE_LOGGER_H
|
||||
#define CORE_LOGGER_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <syslog.h>
|
||||
|
||||
#include <colors.h>
|
||||
#include <config.h>
|
||||
|
||||
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 //CONTROLLER_LOGGER_H
|
||||
#endif //CORE_LOGGER_H
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#include <logger.h>
|
||||
|
||||
#ifdef WIRING_PI_DEBUG
|
||||
#define LOG_WIRING_PI LOG_TRACE
|
||||
#define LOG_WIRING_PI LOGGER_DEBUG
|
||||
#define wiringPiSetup() LOG_WIRING_PI("wiringPi wiringPiSetup()\n")
|
||||
#define wiringPiSetupSys() LOG_WIRING_PI("wiringPi wiringPiSetupSys()\n")
|
||||
#define pinMode(x,y) LOG_WIRING_PI("wiringPi pinMode(%d, %d)\n", x, y)
|
||||
|
|
|
@ -10,8 +10,74 @@
|
|||
(ini_array_match(SECTION, disp->append_to, '.', disp->format) && \
|
||||
ini_string_match_ii(KEY, disp->data, disp->format))
|
||||
|
||||
static int
|
||||
config_load_log_level(IniDispatch *disp, config_t *config)
|
||||
{
|
||||
if(strcasecmp(disp->value, "debug") == 0)
|
||||
{
|
||||
setlogmask(LOG_UPTO(LOG_DEBUG));
|
||||
config->log_level = LOG_DEBUG;
|
||||
return 0;
|
||||
}
|
||||
if(strcasecmp(disp->value, "info") == 0)
|
||||
{
|
||||
setlogmask(LOG_UPTO(LOG_INFO));
|
||||
config->log_level = LOG_INFO;
|
||||
return 0;
|
||||
}
|
||||
if(strcasecmp(disp->value, "notice") == 0)
|
||||
{
|
||||
setlogmask(LOG_UPTO(LOG_NOTICE));
|
||||
config->log_level = LOG_NOTICE;
|
||||
return 0;
|
||||
}
|
||||
if(strcasecmp(disp->value, "warning") == 0)
|
||||
{
|
||||
setlogmask(LOG_UPTO(LOG_WARNING));
|
||||
config->log_level = LOG_WARNING;
|
||||
return 0;
|
||||
}
|
||||
if(strcasecmp(disp->value, "err") == 0)
|
||||
{
|
||||
setlogmask(LOG_UPTO(LOG_ERR));
|
||||
config->log_level = LOG_ERR;
|
||||
return 0;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
int
|
||||
helper_load_config(IniDispatch *disp, void *config_void)
|
||||
config_load(IniDispatch *disp, void *config_void)
|
||||
{
|
||||
config_t *config = (config_t*)config_void;
|
||||
char relay_section_name[10]; // "relay-255\0" is longest name
|
||||
|
@ -29,40 +95,23 @@ helper_load_config(IniDispatch *disp, void *config_void)
|
|||
strcpy(config->database, disp->value);
|
||||
return 0;
|
||||
}
|
||||
if(CONFINI_IS_KEY("controller", "user"))
|
||||
{
|
||||
strcpy(config->user, disp->value);
|
||||
return 0;
|
||||
}
|
||||
if(CONFINI_IS_KEY("controller", "group"))
|
||||
{
|
||||
strcpy(config->group, disp->value);
|
||||
return 0;
|
||||
}
|
||||
if(CONFINI_IS_KEY("controller", "log-level"))
|
||||
{
|
||||
if(strcasecmp(disp->value, "trace") == 0)
|
||||
{
|
||||
config->log_level = LOG_LEVEL_TRACE;
|
||||
return 0;
|
||||
return config_load_log_level(disp, config);
|
||||
}
|
||||
if(strcasecmp(disp->value, "debug") == 0)
|
||||
if(CONFINI_IS_KEY("controller", "log-file"))
|
||||
{
|
||||
config->log_level = LOG_LEVEL_DEBUG;
|
||||
return 0;
|
||||
}
|
||||
if(strcasecmp(disp->value, "info") == 0)
|
||||
{
|
||||
config->log_level = LOG_LEVEL_INFO;
|
||||
return 0;
|
||||
}
|
||||
if(strcasecmp(disp->value, "warn") == 0)
|
||||
{
|
||||
config->log_level = LOG_LEVEL_WARN;
|
||||
return 0;
|
||||
}
|
||||
if(strcasecmp(disp->value, "error") == 0)
|
||||
{
|
||||
config->log_level = LOG_LEVEL_ERROR;
|
||||
return 0;
|
||||
}
|
||||
if(strcasecmp(disp->value, "fatal") == 0)
|
||||
{
|
||||
config->log_level = LOG_LEVEL_FATAL;
|
||||
return 0;
|
||||
}
|
||||
LOG_WARN("invalid log-level '%s'\n", disp->value);
|
||||
return 0;
|
||||
return config_load_log_file(disp, config);
|
||||
}
|
||||
if(CONFINI_IS_KEY("controller", "discovery-port"))
|
||||
{
|
||||
|
@ -90,7 +139,6 @@ helper_load_config(IniDispatch *disp, void *config_void)
|
|||
config->relay_configs[i].pin = 0;
|
||||
config->relay_configs[i].pulse_duration = 0;
|
||||
}
|
||||
LOG_TRACE("config relay-count set to %u\n", config->relay_count);
|
||||
return 0;
|
||||
}
|
||||
for(uint8_t i = 0; i < config->relay_count; ++i)
|
||||
|
@ -123,7 +171,7 @@ helper_load_config(IniDispatch *disp, void *config_void)
|
|||
config->relay_configs[i].driver = RELAY_DRIVER_PIFACE;
|
||||
return 0;
|
||||
}
|
||||
LOG_WARN("invalid driver '%s' in section '%s'\n", disp->value, relay_section_name);
|
||||
LOGGER_WARNING("invalid driver '%s' in section '%s'\n", disp->value, relay_section_name);
|
||||
return 0;
|
||||
}
|
||||
}
|
|
@ -93,7 +93,7 @@ handler_command(struct mg_connection *c, int ev, void *ev_data)
|
|||
return;
|
||||
}
|
||||
uint32_t payload_length = *((uint32_t*)c->recv_mbuf.buf);
|
||||
LOG_DEBUG("payload_length %d\n", payload_length);
|
||||
LOGGER_DEBUG("payload_length %d\n", payload_length);
|
||||
|
||||
if(c->recv_mbuf.len < payload_length + sizeof(payload_length))
|
||||
{
|
||||
|
@ -109,7 +109,7 @@ handler_command(struct mg_connection *c, int ev, void *ev_data)
|
|||
|
||||
uint8_t command_code = mpack_node_u8(mpack_node_map_uint(root, COMMAND_MAPPING_CODE));
|
||||
|
||||
LOG_INFO("received command %d\n", command_code);
|
||||
LOGGER_INFO("received command %d\n", command_code);
|
||||
|
||||
switch(command_code)
|
||||
{
|
||||
|
@ -136,12 +136,12 @@ handler_command(struct mg_connection *c, int ev, void *ev_data)
|
|||
handler_command_pulse(root, global_controller);
|
||||
break;
|
||||
default:
|
||||
LOG_ERROR("received invalid command\n");
|
||||
LOGGER_ERR("received invalid command\n");
|
||||
}
|
||||
|
||||
if(mpack_tree_destroy(&tree) != mpack_ok)
|
||||
{
|
||||
LOG_WARN("error when destroying mpack tree\n");
|
||||
LOGGER_WARNING("error when destroying mpack tree\n");
|
||||
}
|
||||
controller_save(global_controller, global_mdb_env);
|
||||
}
|
||||
|
|
|
@ -29,16 +29,16 @@ handler_discovery(struct mg_connection *c, int ev, void *ev_data)
|
|||
|
||||
if(c->recv_mbuf.len != sizeof(discovery_answer_port))
|
||||
{
|
||||
LOG_ERROR("received invalid discovery from %s\n", ip_buf);
|
||||
LOGGER_ERR("received invalid discovery from %s\n", ip_buf);
|
||||
return;
|
||||
}
|
||||
discovery_answer_port = *((uint16_t*)c->recv_mbuf.buf);
|
||||
|
||||
LOG_INFO("received discovery from %s:%d\n", ip_buf, discovery_answer_port);
|
||||
LOGGER_INFO("received discovery from %s:%d\n", ip_buf, discovery_answer_port);
|
||||
|
||||
if(discovery_answer_port == 0)
|
||||
{
|
||||
LOG_ERROR("invalid port received\n");
|
||||
LOGGER_ERR("invalid port received\n");
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -61,7 +61,7 @@ handler_discovery(struct mg_connection *c, int ev, void *ev_data)
|
|||
// finish writing
|
||||
if(mpack_writer_destroy(&writer) != mpack_ok)
|
||||
{
|
||||
LOG_ERROR("error writing discovery answer payload\n");
|
||||
LOGGER_ERR("error writing discovery answer payload\n");
|
||||
return;
|
||||
|
||||
}
|
||||
|
@ -70,21 +70,21 @@ handler_discovery(struct mg_connection *c, int ev, void *ev_data)
|
|||
int fd_answer = helper_connect_tcp_server(ip_buf, discovery_answer_port);
|
||||
if(fd_answer == -1)
|
||||
{
|
||||
LOG_ERROR("error during connecting\n");
|
||||
LOGGER_ERR("error during connecting\n");
|
||||
free(payload);
|
||||
return;
|
||||
}
|
||||
|
||||
if((bytes_transferred = send(fd_answer, &payload_size, sizeof(payload_size), 0)) <= 0)
|
||||
{
|
||||
LOG_ERROR("error during sending\n");
|
||||
LOGGER_ERR("error during sending\n");
|
||||
free(payload);
|
||||
close(fd_answer);
|
||||
return;
|
||||
}
|
||||
if((bytes_transferred = send(fd_answer, payload, payload_size, 0)) <= 0)
|
||||
{
|
||||
LOG_ERROR("error during sending\n");
|
||||
LOGGER_ERR("error during sending\n");
|
||||
free(payload);
|
||||
close(fd_answer);
|
||||
return;
|
||||
|
|
|
@ -28,7 +28,6 @@ handler_loop(struct mg_connection *c_mqtt)
|
|||
localtime_r(×tamp, &time_last);
|
||||
timestamp = time(NULL);
|
||||
localtime_r(×tamp, &time_now);
|
||||
LOG_TRACE("===== IDLE LOOP START =====\n");
|
||||
for(uint_fast8_t i = 0; i < global_controller->relay_count; ++i)
|
||||
{
|
||||
relay_t *relay = global_controller->relays[i];
|
||||
|
@ -52,7 +51,7 @@ handler_loop(struct mg_connection *c_mqtt)
|
|||
|
||||
if(is_on)
|
||||
{
|
||||
LOG_DEBUG("relay %d is active\n", i);
|
||||
LOGGER_DEBUG("relay %d is active\n", i);
|
||||
}
|
||||
|
||||
if(relay->is_on != is_on)
|
||||
|
@ -82,7 +81,7 @@ handler_loop(struct mg_connection *c_mqtt)
|
|||
driver_piface_set(global_config.relay_configs[i].pin, is_on);
|
||||
break;
|
||||
default:
|
||||
LOG_WARN("relay %d is not using a driver\n", i);
|
||||
LOGGER_WARNING("relay %d is not using a driver\n", i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,19 +23,19 @@ handler_mqtt(struct mg_connection *nc, int ev, void *p) {
|
|||
case MG_EV_MQTT_CONNACK:
|
||||
if(msg->connack_ret_code != MG_EV_MQTT_CONNACK_ACCEPTED)
|
||||
{
|
||||
LOG_INFO("Got MQTT connection error: %d\n", msg->connack_ret_code);
|
||||
LOGGER_INFO("Got MQTT connection error: %d\n", msg->connack_ret_code);
|
||||
break;
|
||||
}
|
||||
if(!global_connection_mqtt)
|
||||
{
|
||||
LOG_DEBUG("connected to MQTT server\n");
|
||||
LOGGER_DEBUG("connected to MQTT server\n");
|
||||
global_connection_mqtt = nc;
|
||||
}
|
||||
break;
|
||||
case MG_EV_CLOSE:
|
||||
if(global_connection_mqtt)
|
||||
{
|
||||
LOG_DEBUG("disconnected from MQTT server\n");
|
||||
LOGGER_DEBUG("disconnected from MQTT server\n");
|
||||
}
|
||||
global_connection_mqtt = NULL;
|
||||
break;
|
||||
|
|
|
@ -26,7 +26,7 @@ helper_bind_tcp_server(char* addr, uint16_t port, int max_client_backlog)
|
|||
|
||||
if ((status = getaddrinfo(addr, port_str, &hints, &res)) != 0)
|
||||
{
|
||||
LOG_ERROR("getaddrinfo: %s\n", gai_strerror(status));
|
||||
LOGGER_ERR("getaddrinfo: %s\n", gai_strerror(status));
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -34,14 +34,14 @@ helper_bind_tcp_server(char* addr, uint16_t port, int max_client_backlog)
|
|||
|
||||
if ((status = bind(fd, res->ai_addr, res->ai_addrlen)) == -1)
|
||||
{
|
||||
LOG_ERROR("error binding socket: %s\n", strerror(errno));
|
||||
LOGGER_ERR("error binding socket: %s\n", strerror(errno));
|
||||
freeaddrinfo(res);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((status = listen(fd, max_client_backlog)) == -1)
|
||||
{
|
||||
LOG_ERROR("error setting up listener: %s\n", strerror(errno));
|
||||
LOGGER_ERR("error setting up listener: %s\n", strerror(errno));
|
||||
freeaddrinfo(res);
|
||||
return -1;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
81
src/helpers/drop_privileges.c
Normal file
81
src/helpers/drop_privileges.c
Normal file
|
@ -0,0 +1,81 @@
|
|||
#include <sys/types.h>
|
||||
#include <pwd.h>
|
||||
#include <grp.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <config.h>
|
||||
#include <logger.h>
|
||||
|
||||
static uid_t
|
||||
get_uid_for_user(char *user)
|
||||
{
|
||||
if(user == NULL || user[0] == '\0')
|
||||
{
|
||||
return getuid();
|
||||
}
|
||||
struct passwd *pwd = calloc(1, sizeof(struct passwd));
|
||||
size_t buffer_len = sysconf(_SC_GETPW_R_SIZE_MAX) * sizeof(char);
|
||||
char *buffer = malloc(buffer_len);
|
||||
getpwnam_r(user, pwd, buffer, buffer_len, &pwd);
|
||||
|
||||
if(pwd == NULL)
|
||||
{
|
||||
LOGGER_CRIT("couldn't find user to drop privileges\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
uid_t result = pwd->pw_uid;
|
||||
free(buffer);
|
||||
free(pwd);
|
||||
return result;
|
||||
}
|
||||
|
||||
static gid_t
|
||||
get_gid_for_group(char *group)
|
||||
{
|
||||
if(group == NULL || group[0] == '\0')
|
||||
{
|
||||
return getgid();
|
||||
}
|
||||
struct group *grp = calloc(1, sizeof(struct group));
|
||||
size_t buffer_len = sysconf(_SC_GETPW_R_SIZE_MAX) * sizeof(char);
|
||||
char *buffer = malloc(buffer_len);
|
||||
getgrnam_r(group, grp, buffer, buffer_len, &grp);
|
||||
|
||||
if(grp == NULL)
|
||||
{
|
||||
LOGGER_CRIT("couldn't find group to drop privileges\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
gid_t result = grp->gr_gid;
|
||||
free(buffer);
|
||||
free(grp);
|
||||
return result;
|
||||
}
|
||||
|
||||
int
|
||||
helper_drop_privileges()
|
||||
{
|
||||
uid_t uid = get_uid_for_user(global_config.user);
|
||||
gid_t gid = get_gid_for_group(global_config.group);
|
||||
|
||||
LOGGER_DEBUG("drop privileges to %lu:%lu\n", uid, gid);
|
||||
|
||||
if (setgid(gid) == -1)
|
||||
{
|
||||
LOGGER_CRIT("failed to drop group privileges\n");
|
||||
exit(1);
|
||||
}
|
||||
if (setuid(uid) == -1)
|
||||
{
|
||||
LOGGER_CRIT("failed to drop user privileges\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -14,7 +14,7 @@ helper_get_port(int sock)
|
|||
socklen_t len = sizeof(sin);
|
||||
if (getsockname(sock, (struct sockaddr *)&sin, &len) == -1)
|
||||
{
|
||||
LOG_ERROR("could not get socket name for port: %s\n", strerror(errno));
|
||||
LOGGER_ERR("could not get socket name for port: %s\n", strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
|
|
|
@ -25,7 +25,7 @@ helper_open_discovery_socket(uint16_t discovery_port)
|
|||
//get connection info for our computer
|
||||
if ((status = getaddrinfo(NULL, discovery_port_str, &hints, &res)) != 0)
|
||||
{
|
||||
LOG_FATAL("getaddrinfo: %s\n", gai_strerror(status));
|
||||
LOGGER_CRIT("getaddrinfo: %s\n", gai_strerror(status));
|
||||
freeaddrinfo(res);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
@ -37,21 +37,21 @@ helper_open_discovery_socket(uint16_t discovery_port)
|
|||
// lose the pesky "Address already in use" error message
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1)
|
||||
{
|
||||
LOG_FATAL("setsockopt: %s\n", strerror(errno));
|
||||
LOGGER_CRIT("setsockopt: %s\n", strerror(errno));
|
||||
freeaddrinfo(res);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (bind(fd, res->ai_addr, res->ai_addrlen) == -1)
|
||||
{
|
||||
LOG_FATAL("bind: %s\n", strerror(errno));
|
||||
LOGGER_CRIT("bind: %s\n", strerror(errno));
|
||||
freeaddrinfo(res);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
freeaddrinfo(res);
|
||||
|
||||
LOG_INFO("opened discovery socket on port %u\n", discovery_port);
|
||||
LOGGER_INFO("opened discovery socket on port %u\n", discovery_port);
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
|
|
@ -58,12 +58,12 @@ helper_parse_cli(int argc, const char **argv, config_t *config)
|
|||
config->run_type = RUN_TYPE_TEST;
|
||||
return;
|
||||
}
|
||||
LOG_FATAL("bad action '%s' given ('start', 'test')\n", argv[0]);
|
||||
LOGGER_CRIT("bad action '%s' given ('start', 'test')\n", argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_FATAL("no action given ('start', 'test')\n");
|
||||
LOGGER_CRIT("no action given ('start', 'test')\n");
|
||||
exit(1);
|
||||
}
|
||||
return;
|
||||
|
|
75
src/logger.c
75
src/logger.c
|
@ -1,58 +1,85 @@
|
|||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <config.h>
|
||||
#include <logger.h>
|
||||
|
||||
#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];
|
||||
time_t rawtime = time(NULL);
|
||||
time_t rawtime;
|
||||
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);
|
||||
}
|
||||
|
|
37
src/main.c
37
src/main.c
|
@ -4,6 +4,7 @@
|
|||
#include <time.h>
|
||||
#include <lmdb.h>
|
||||
#include <signal.h>
|
||||
#include <syslog.h>
|
||||
|
||||
#include <logger.h>
|
||||
#include <mongoose.h>
|
||||
|
@ -31,19 +32,19 @@ static struct mg_mgr mgr;
|
|||
static void
|
||||
terminate(int signum)
|
||||
{
|
||||
LOG_INFO("terminating controller (%d)\n", signum);
|
||||
LOGGER_INFO("terminating controller (%d)\n", signum);
|
||||
|
||||
// TODO fix mg_mgr_free() causing loop (can't terminate)
|
||||
//LOG_DEBUG("freeing mongoose manager\n");
|
||||
//LOGGER_DEBUG("freeing mongoose manager\n");
|
||||
//mg_mgr_free(&mgr);
|
||||
|
||||
LOG_DEBUG("closing database\n");
|
||||
LOGGER_DEBUG("closing database\n");
|
||||
mdb_env_close(global_mdb_env);
|
||||
|
||||
LOG_DEBUG("freeing global controller\n");
|
||||
LOGGER_DEBUG("freeing global controller\n");
|
||||
controller_free(global_controller);
|
||||
|
||||
LOG_DEBUG("freeing relay configs config\n");
|
||||
LOGGER_DEBUG("freeing relay configs config\n");
|
||||
free(global_config.relay_configs);
|
||||
|
||||
exit(signum);
|
||||
|
@ -67,32 +68,46 @@ main(int argc, const char** argv)
|
|||
signal(SIGABRT, terminate);
|
||||
signal(SIGTERM, terminate);
|
||||
|
||||
setlogmask(LOG_UPTO(LOG_INFO));
|
||||
|
||||
|
||||
/******************** LOAD CONFIG ********************/
|
||||
|
||||
global_config.file = "controller.ini";
|
||||
global_config.log_level = LOG_LEVEL_INFO;
|
||||
global_config.discovery_port = 4421;
|
||||
global_config.mqtt_port = 1885;
|
||||
|
||||
global_config.log_level = LOG_INFO;
|
||||
global_config.log_file = stdout;
|
||||
|
||||
strcpy(global_config.user, "");
|
||||
strcpy(global_config.group, "");
|
||||
|
||||
helper_parse_cli(argc, argv, &global_config);
|
||||
|
||||
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, helper_load_config, &global_config))
|
||||
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);
|
||||
}
|
||||
|
||||
fclose(ini_file);
|
||||
|
||||
if(global_config.log_file == NULL)
|
||||
{
|
||||
global_config.log_file = stdout;
|
||||
}
|
||||
openlog("emgauwa-controller", 0, LOG_USER);
|
||||
|
||||
if(sizeof(time_t) < 8)
|
||||
{
|
||||
LOG_WARN("this system is not using 8-bit time\n");
|
||||
LOGGER_WARNING("this system is not using 8-bit time\n");
|
||||
}
|
||||
|
||||
|
||||
|
@ -112,6 +127,8 @@ main(int argc, const char** argv)
|
|||
|
||||
controller_save(global_controller, global_mdb_env);
|
||||
|
||||
helper_drop_privileges();
|
||||
|
||||
|
||||
/******************** SETUP WIRINGPI ********************/
|
||||
|
||||
|
|
|
@ -55,16 +55,16 @@ controller_debug(controller_t *controller)
|
|||
{
|
||||
if(controller == NULL)
|
||||
{
|
||||
LOG_DEBUG("controller is NULL\n");
|
||||
LOGGER_DEBUG("controller is NULL\n");
|
||||
return;
|
||||
}
|
||||
char uuid_str[37];
|
||||
uuid_unparse(controller->id, uuid_str);
|
||||
LOG_DEBUG("(1/5) %s @ %p\n", uuid_str, (void*)controller);
|
||||
LOG_DEBUG("(2/5) name: %s\n", controller->name);
|
||||
LOG_DEBUG("(3/5) command_port: %5d discovery_port: %5d\n", controller->command_port, controller->discovery_port);
|
||||
LOG_DEBUG("(4/5) relay count: %3d\n", controller->relay_count);
|
||||
LOG_DEBUG("(5/5) relays @ %p:\n", (void*)controller->relays);
|
||||
LOGGER_DEBUG("(1/5) %s @ %p\n", uuid_str, (void*)controller);
|
||||
LOGGER_DEBUG("(2/5) name: %s\n", controller->name);
|
||||
LOGGER_DEBUG("(3/5) command_port: %5d discovery_port: %5d\n", controller->command_port, controller->discovery_port);
|
||||
LOGGER_DEBUG("(4/5) relay count: %3d\n", controller->relay_count);
|
||||
LOGGER_DEBUG("(5/5) relays @ %p:\n", (void*)controller->relays);
|
||||
for(int i = 0; i < controller->relay_count; ++i)
|
||||
{
|
||||
relay_debug(controller->relays[i]);
|
||||
|
|
|
@ -18,7 +18,7 @@ controller_load_single(MDB_txn *mdb_txn, MDB_dbi mdb_dbi, db_key_controller_e ke
|
|||
|
||||
if((err = mdb_get(mdb_txn, mdb_dbi, &key, value)) != 0)
|
||||
{
|
||||
LOG_ERROR("mdb_get error %s\n", mdb_strerror(err));
|
||||
LOGGER_ERR("mdb_get error %s\n", mdb_strerror(err));
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ controller_load(MDB_env *mdb_env)
|
|||
|
||||
if((err = mdb_txn_begin(mdb_env, NULL, MDB_RDONLY, &mdb_txn)) != 0)
|
||||
{
|
||||
LOG_ERROR("mdb_txn_begin error %s\n", mdb_strerror(err));
|
||||
LOGGER_ERR("mdb_txn_begin error %s\n", mdb_strerror(err));
|
||||
return NULL;
|
||||
}
|
||||
if((err = mdb_dbi_open(mdb_txn, "controller", 0, &mdb_dbi)) != 0)
|
||||
|
@ -42,13 +42,13 @@ controller_load(MDB_env *mdb_env)
|
|||
switch(err)
|
||||
{
|
||||
case MDB_NOTFOUND:
|
||||
LOG_INFO("no controller found in db. creating new one\n");
|
||||
LOGGER_INFO("no controller found in db. creating new one\n");
|
||||
mdb_txn_abort(mdb_txn);
|
||||
new_controller = controller_create();
|
||||
controller_save(new_controller, mdb_env);
|
||||
return new_controller;
|
||||
default:
|
||||
LOG_ERROR("mdb_txn_begin error %s\n", mdb_strerror(err));
|
||||
LOGGER_ERR("mdb_txn_begin error %s\n", mdb_strerror(err));
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
@ -77,7 +77,6 @@ controller_load(MDB_env *mdb_env)
|
|||
new_controller->relays = malloc(sizeof(relay_t*) * new_controller->relay_count);
|
||||
for(uint8_t i = 0; i < new_controller->relay_count; i++)
|
||||
{
|
||||
LOG_TRACE("loading relay %d\n", i);
|
||||
new_controller->relays[i] = relay_load(mdb_env, i);
|
||||
}
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ controller_save_single(MDB_txn *mdb_txn, MDB_dbi mdb_dbi, db_key_controller_e ke
|
|||
|
||||
if((err = mdb_put(mdb_txn, mdb_dbi, &key, &value, 0)) != 0)
|
||||
{
|
||||
LOG_ERROR("mdb_put error %s\n", mdb_strerror(err));
|
||||
LOGGER_ERR("mdb_put error %s\n", mdb_strerror(err));
|
||||
mdb_txn_abort(mdb_txn);
|
||||
return 1;
|
||||
}
|
||||
|
@ -36,13 +36,13 @@ controller_save(controller_t *controller, MDB_env *mdb_env)
|
|||
|
||||
if((err = mdb_txn_begin(mdb_env, NULL, 0, &mdb_txn)) != 0)
|
||||
{
|
||||
LOG_ERROR("mdb_txn_begin error %s\n", mdb_strerror(err));
|
||||
LOGGER_ERR("mdb_txn_begin error %s\n", mdb_strerror(err));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if((err = mdb_dbi_open(mdb_txn, "controller", MDB_CREATE, &mdb_dbi)) != 0)
|
||||
{
|
||||
LOG_ERROR("mdb_dbi_open error %s\n", mdb_strerror(err));
|
||||
LOGGER_ERR("mdb_dbi_open error %s\n", mdb_strerror(err));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
@ -50,7 +50,7 @@ controller_save(controller_t *controller, MDB_env *mdb_env)
|
|||
value.mv_data = controller->id;
|
||||
if(controller_save_single(mdb_txn, mdb_dbi, DB_KEY_CONTROLLER_ID, value))
|
||||
{
|
||||
LOG_ERROR("failed to save ID\n");
|
||||
LOGGER_ERR("failed to save ID\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -58,7 +58,7 @@ controller_save(controller_t *controller, MDB_env *mdb_env)
|
|||
value.mv_data = controller->name;
|
||||
if(controller_save_single(mdb_txn, mdb_dbi, DB_KEY_CONTROLLER_NAME, value))
|
||||
{
|
||||
LOG_ERROR("failed to save name\n");
|
||||
LOGGER_ERR("failed to save name\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -66,7 +66,7 @@ controller_save(controller_t *controller, MDB_env *mdb_env)
|
|||
value.mv_data = &controller->command_port;
|
||||
if(controller_save_single(mdb_txn, mdb_dbi, DB_KEY_CONTROLLER_COMMAND_PORT, value))
|
||||
{
|
||||
LOG_ERROR("failed to save command port\n");
|
||||
LOGGER_ERR("failed to save command port\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -74,7 +74,7 @@ controller_save(controller_t *controller, MDB_env *mdb_env)
|
|||
value.mv_data = &controller->discovery_port;
|
||||
if(controller_save_single(mdb_txn, mdb_dbi, DB_KEY_CONTROLLER_DISCOVERY_PORT, value))
|
||||
{
|
||||
LOG_ERROR("failed to save discovery port\n");
|
||||
LOGGER_ERR("failed to save discovery port\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -82,7 +82,6 @@ controller_save(controller_t *controller, MDB_env *mdb_env)
|
|||
|
||||
for(uint8_t i = 0; i < controller->relay_count; ++i)
|
||||
{
|
||||
LOG_TRACE("saving relays[%d/%d]\n", i, controller->relay_count);
|
||||
relay_save(controller->relays[i], mdb_env);
|
||||
}
|
||||
|
||||
|
|
|
@ -62,12 +62,12 @@ relay_debug(relay_t *relay)
|
|||
{
|
||||
if(relay == NULL)
|
||||
{
|
||||
LOG_DEBUG("relay is NULL\n");
|
||||
LOGGER_DEBUG("relay is NULL\n");
|
||||
return;
|
||||
}
|
||||
LOG_DEBUG("(1/3) %d @ %p\n", relay->number, (void*)relay);
|
||||
LOG_DEBUG("(2/3) name: %s\n", relay->name);
|
||||
LOG_DEBUG("(3/3) schedules @ %p:\n", (void*)relay->schedules);
|
||||
LOGGER_DEBUG("(1/3) %d @ %p\n", relay->number, (void*)relay);
|
||||
LOGGER_DEBUG("(2/3) name: %s\n", relay->name);
|
||||
LOGGER_DEBUG("(3/3) schedules @ %p:\n", (void*)relay->schedules);
|
||||
for(int i = 0; i < 7; ++i)
|
||||
{
|
||||
schedule_debug(relay->schedules[i]);
|
||||
|
|
|
@ -25,7 +25,7 @@ relay_load_single(MDB_txn *mdb_txn, MDB_dbi mdb_dbi, db_key_relay_e key_relay, u
|
|||
|
||||
if((err = mdb_get(mdb_txn, mdb_dbi, &key, value)) != 0)
|
||||
{
|
||||
LOG_ERROR("mdb_get error %s\n", mdb_strerror(err));
|
||||
LOGGER_ERR("mdb_get error %s\n", mdb_strerror(err));
|
||||
mdb_txn_abort(mdb_txn);
|
||||
free(key_data);
|
||||
return 1;
|
||||
|
@ -45,7 +45,7 @@ relay_load(MDB_env *mdb_env, uint8_t num)
|
|||
|
||||
if((err = mdb_txn_begin(mdb_env, NULL, MDB_RDONLY, &mdb_txn)) != 0)
|
||||
{
|
||||
LOG_ERROR("mdb_txn_begin error %s\n", mdb_strerror(err));
|
||||
LOGGER_ERR("mdb_txn_begin error %s\n", mdb_strerror(err));
|
||||
return relay_create(num);
|
||||
}
|
||||
|
||||
|
@ -54,11 +54,11 @@ relay_load(MDB_env *mdb_env, uint8_t num)
|
|||
switch(err)
|
||||
{
|
||||
case MDB_NOTFOUND:
|
||||
LOG_INFO("no relay for num %d found in db. returning new one (no relays db)\n", num);
|
||||
LOGGER_INFO("no relay for num %d found in db. returning new one (no relays db)\n", num);
|
||||
mdb_txn_abort(mdb_txn);
|
||||
return relay_create(num);
|
||||
default:
|
||||
LOG_ERROR("mdb_txn_begin error %s\n", mdb_strerror(err));
|
||||
LOGGER_ERR("mdb_txn_begin error %s\n", mdb_strerror(err));
|
||||
return relay_create(num);
|
||||
}
|
||||
}
|
||||
|
@ -75,7 +75,7 @@ relay_load(MDB_env *mdb_env, uint8_t num)
|
|||
|
||||
if((err = relay_load_single(mdb_txn, mdb_dbi, DB_KEY_RELAY_NAME, num, &value)) != 0)
|
||||
{
|
||||
LOG_INFO("no relay for num %d found in db. returning new one\n", num);
|
||||
LOGGER_INFO("no relay for num %d found in db. returning new one\n", num);
|
||||
mdb_txn_abort(mdb_txn); // transaction is read only
|
||||
return relay_create(num);
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ relay_save_single(MDB_txn *mdb_txn, MDB_dbi mdb_dbi, db_key_relay_e key_relay, u
|
|||
|
||||
if((err = mdb_put(mdb_txn, mdb_dbi, &key, &value, 0)) != 0)
|
||||
{
|
||||
LOG_ERROR("mdb_put error %s\n", mdb_strerror(err));
|
||||
LOGGER_ERR("mdb_put error %s\n", mdb_strerror(err));
|
||||
mdb_txn_abort(mdb_txn);
|
||||
free(key_data);
|
||||
return 1;
|
||||
|
@ -37,7 +37,6 @@ relay_save_single(MDB_txn *mdb_txn, MDB_dbi mdb_dbi, db_key_relay_e key_relay, u
|
|||
int
|
||||
relay_save(relay_t *relay, MDB_env *mdb_env)
|
||||
{
|
||||
LOG_TRACE("saving relay %d @ %p\n", relay->number, relay);
|
||||
int err;
|
||||
|
||||
MDB_txn *mdb_txn;
|
||||
|
@ -46,13 +45,13 @@ relay_save(relay_t *relay, MDB_env *mdb_env)
|
|||
|
||||
if((err = mdb_txn_begin(mdb_env, NULL, 0, &mdb_txn)) != 0)
|
||||
{
|
||||
LOG_ERROR("mdb_txn_begin error %s\n", mdb_strerror(err));
|
||||
LOGGER_ERR("mdb_txn_begin error %s\n", mdb_strerror(err));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if((err = mdb_dbi_open(mdb_txn, "relays", MDB_CREATE, &mdb_dbi)) != 0)
|
||||
{
|
||||
LOG_ERROR("mdb_dbi_open error %s\n", mdb_strerror(err));
|
||||
LOGGER_ERR("mdb_dbi_open error %s\n", mdb_strerror(err));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
@ -60,7 +59,7 @@ relay_save(relay_t *relay, MDB_env *mdb_env)
|
|||
value.mv_data = relay->name;
|
||||
if(relay_save_single(mdb_txn, mdb_dbi, DB_KEY_RELAY_NAME, relay->number, value))
|
||||
{
|
||||
LOG_ERROR("failed to save name\n");
|
||||
LOGGER_ERR("failed to save name\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
|
@ -63,14 +63,14 @@ schedule_debug(schedule_t *schedule)
|
|||
{
|
||||
if(schedule == NULL)
|
||||
{
|
||||
LOG_DEBUG("schedule is NULL\n");
|
||||
LOGGER_DEBUG("schedule is NULL\n");
|
||||
return;
|
||||
}
|
||||
char uuid_str[UUID_STR_LEN];
|
||||
uuid_unparse(schedule->id, uuid_str);
|
||||
LOG_DEBUG("(1/3) %s @ %p\n", uuid_str, (void*)schedule);
|
||||
LOG_DEBUG("(2/4) period count: %3d\n", schedule->length);
|
||||
LOG_DEBUG("(3/4) weekday: %3d\n", schedule->weekday);
|
||||
LOGGER_DEBUG("(1/3) %s @ %p\n", uuid_str, (void*)schedule);
|
||||
LOGGER_DEBUG("(2/4) period count: %3d\n", schedule->length);
|
||||
LOGGER_DEBUG("(3/4) weekday: %3d\n", schedule->weekday);
|
||||
|
||||
// one block: "HH:MM-HH:MM, " --> size: 13 (14 with '\0')
|
||||
char *periods_debug_str = malloc(sizeof(char) * ((schedule->length * 13) + 1));
|
||||
|
@ -88,7 +88,7 @@ schedule_debug(schedule_t *schedule)
|
|||
);
|
||||
}
|
||||
|
||||
LOG_DEBUG("(4/4) periods: %s\n", periods_debug_str);
|
||||
LOGGER_DEBUG("(4/4) periods: %s\n", periods_debug_str);
|
||||
|
||||
free(periods_debug_str);
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ schedule_load_single(MDB_txn *mdb_txn, MDB_dbi mdb_dbi, db_key_schedule_e key_sc
|
|||
|
||||
if((err = mdb_get(mdb_txn, mdb_dbi, &key, value)) != 0)
|
||||
{
|
||||
LOG_ERROR("mdb_get error %s\n", mdb_strerror(err));
|
||||
LOGGER_ERR("mdb_get error %s\n", mdb_strerror(err));
|
||||
mdb_txn_abort(mdb_txn);
|
||||
free(key_data);
|
||||
return 1;
|
||||
|
@ -51,7 +51,7 @@ schedule_load(MDB_env *mdb_env, uint8_t relay_num, uint8_t weekday)
|
|||
|
||||
if((err = mdb_txn_begin(mdb_env, NULL, MDB_RDONLY, &mdb_txn)) != 0)
|
||||
{
|
||||
LOG_ERROR("mdb_txn_begin error %s\n", mdb_strerror(err));
|
||||
LOGGER_ERR("mdb_txn_begin error %s\n", mdb_strerror(err));
|
||||
return schedule_create(off_id, weekday, 0, NULL);
|
||||
}
|
||||
|
||||
|
@ -60,11 +60,11 @@ schedule_load(MDB_env *mdb_env, uint8_t relay_num, uint8_t weekday)
|
|||
switch(err)
|
||||
{
|
||||
case MDB_NOTFOUND:
|
||||
LOG_INFO("no schedule db found in db. returning new one (no schedules db)\n");
|
||||
LOGGER_INFO("no schedule db found in db. returning new one (no schedules db)\n");
|
||||
mdb_txn_abort(mdb_txn);
|
||||
break;
|
||||
default:
|
||||
LOG_ERROR("mdb_txn_begin error %s\n", mdb_strerror(err));
|
||||
LOGGER_ERR("mdb_txn_begin error %s\n", mdb_strerror(err));
|
||||
}
|
||||
return schedule_create(off_id, weekday, 0, NULL);
|
||||
}
|
||||
|
@ -73,7 +73,7 @@ schedule_load(MDB_env *mdb_env, uint8_t relay_num, uint8_t weekday)
|
|||
|
||||
if((err = schedule_load_single(mdb_txn, mdb_dbi, DB_KEY_SCHEDULE_ID, relay_num, weekday, &value)) != 0)
|
||||
{
|
||||
LOG_INFO("no schedule for relay %d and weekday %d found in db. returning new one\n", relay_num, weekday);
|
||||
LOGGER_INFO("no schedule for relay %d and weekday %d found in db. returning new one\n", relay_num, weekday);
|
||||
mdb_txn_abort(mdb_txn); // transaction is read only
|
||||
return schedule_create(off_id, weekday, 0, NULL);
|
||||
}
|
||||
|
@ -81,7 +81,7 @@ schedule_load(MDB_env *mdb_env, uint8_t relay_num, uint8_t weekday)
|
|||
|
||||
if((err = schedule_load_single(mdb_txn, mdb_dbi, DB_KEY_SCHEDULE_PERIODS, relay_num, weekday, &value)) != 0)
|
||||
{
|
||||
LOG_INFO("no schedule for relay %d and weekday %d found in db. returning new one\n", relay_num, weekday);
|
||||
LOGGER_INFO("no schedule for relay %d and weekday %d found in db. returning new one\n", relay_num, weekday);
|
||||
mdb_txn_abort(mdb_txn); // transaction is read only
|
||||
return schedule_create(off_id, weekday, 0, NULL);
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ schedule_save_single(MDB_txn *mdb_txn, MDB_dbi mdb_dbi, db_key_schedule_e key_sc
|
|||
|
||||
if((err = mdb_put(mdb_txn, mdb_dbi, &key, &value, 0)) != 0)
|
||||
{
|
||||
LOG_ERROR("mdb_put error %s\n", mdb_strerror(err));
|
||||
LOGGER_ERR("mdb_put error %s\n", mdb_strerror(err));
|
||||
mdb_txn_abort(mdb_txn);
|
||||
free(key_data);
|
||||
return 1;
|
||||
|
@ -42,7 +42,6 @@ schedule_save(schedule_t *schedule, uint8_t relay_num, MDB_env *mdb_env)
|
|||
{
|
||||
char uuid_str[37];
|
||||
uuid_unparse(schedule->id, uuid_str);
|
||||
LOG_TRACE("saving schedule %s @ %p\n", uuid_str, schedule);
|
||||
int err;
|
||||
|
||||
MDB_txn *mdb_txn;
|
||||
|
@ -51,13 +50,13 @@ schedule_save(schedule_t *schedule, uint8_t relay_num, MDB_env *mdb_env)
|
|||
|
||||
if((err = mdb_txn_begin(mdb_env, NULL, 0, &mdb_txn)) != 0)
|
||||
{
|
||||
LOG_ERROR("mdb_txn_begin error %s\n", mdb_strerror(err));
|
||||
LOGGER_ERR("mdb_txn_begin error %s\n", mdb_strerror(err));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if((err = mdb_dbi_open(mdb_txn, "schedules", MDB_CREATE, &mdb_dbi)) != 0)
|
||||
{
|
||||
LOG_ERROR("mdb_dbi_open error %s\n", mdb_strerror(err));
|
||||
LOGGER_ERR("mdb_dbi_open error %s\n", mdb_strerror(err));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
@ -65,7 +64,7 @@ schedule_save(schedule_t *schedule, uint8_t relay_num, MDB_env *mdb_env)
|
|||
value.mv_data = schedule->id;
|
||||
if(schedule_save_single(mdb_txn, mdb_dbi, DB_KEY_SCHEDULE_ID, relay_num, schedule->weekday, value))
|
||||
{
|
||||
LOG_ERROR("failed to save ID\n");
|
||||
LOGGER_ERR("failed to save ID\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -76,7 +75,7 @@ schedule_save(schedule_t *schedule, uint8_t relay_num, MDB_env *mdb_env)
|
|||
if(schedule_save_single(mdb_txn, mdb_dbi, DB_KEY_SCHEDULE_PERIODS, relay_num, schedule->weekday, value))
|
||||
{
|
||||
free(periods_blob);
|
||||
LOG_ERROR("failed to save periods\n");
|
||||
LOGGER_ERR("failed to save periods\n");
|
||||
return 1;
|
||||
}
|
||||
free(periods_blob);
|
||||
|
|
|
@ -27,7 +27,7 @@ runner_test(controller_t *controller)
|
|||
driver_piface_set(global_config.relay_configs[i].pin, is_active);
|
||||
break;
|
||||
default:
|
||||
LOG_WARN("relay %d is not using a driver\n", i);
|
||||
LOGGER_WARNING("relay %d is not using a driver\n", i);
|
||||
}
|
||||
sleep(1);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue