add: logger

This commit is contained in:
Tobias Reisinger 2020-02-08 13:50:54 +01:00
parent f03b3f7fb7
commit 18804da4d8
8 changed files with 159 additions and 39 deletions

View file

@ -7,6 +7,9 @@ option(WIRING_PI_DEBUG "Use WiringPi Debugging Tool" OFF)
SET(CMAKE_C_FLAGS "-Wall -Wextra -lwiringPi -luuid -llmdb -g") SET(CMAKE_C_FLAGS "-Wall -Wextra -lwiringPi -luuid -llmdb -g")
string(LENGTH "${CMAKE_SOURCE_DIR}/" SOURCE_PATH_SIZE)
add_definitions("-DSOURCE_PATH_SIZE=${SOURCE_PATH_SIZE}")
if(WIRING_PI_DEBUG) if(WIRING_PI_DEBUG)
add_compile_definitions("WIRING_PI_DEBUG") add_compile_definitions("WIRING_PI_DEBUG")
endif(WIRING_PI_DEBUG) endif(WIRING_PI_DEBUG)
@ -16,3 +19,9 @@ aux_source_directory(models MODEL_SRC)
target_sources(controller PRIVATE ${SRC_DIR} ${MODEL_SRC}) target_sources(controller PRIVATE ${SRC_DIR} ${MODEL_SRC})
target_include_directories(controller PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include) target_include_directories(controller PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
add_custom_target(run
COMMAND controller
DEPENDS controller
WORKING_DIRECTORY ${CMAKE_PROJECT_DIR}
)

18
include/colors.h Normal file
View file

@ -0,0 +1,18 @@
#ifndef CONTROLLER_COLORS_H
#define CONTROLLER_COLORS_H
#define COLOR_RED "\033[0;31m"
#define COLORB_RED "\033[1;31m"
#define COLOR_GREEN "\033[0;32m"
#define COLORB_GREEN "\033[1;32m"
#define COLOR_YELLOW "\033[0;33m"
#define COLORB_YELLOW "\033[1;33m"
#define COLOR_BLUE "\033[0;34m"
#define COLORB_BLUE "\033[1;34m"
#define COLOR_MAGENTA "\033[0;35m"
#define COLORB_MAGENTA "\033[1;35m"
#define COLOR_CYAN "\033[0;36m"
#define COLORB_CYAN "\033[1;36m"
#define COLOR_NONE "\033[0m"
#endif //CONTROLLER_COLORS_H

View file

@ -1,6 +1,8 @@
#ifndef CONTROLLER_CONFIG_H #ifndef CONTROLLER_CONFIG_H
#define CONTROLLER_CONFIG_H #define CONTROLLER_CONFIG_H
#include <log_levels.h>
#define CONTROLLER_FILE_NAME "emgauwa-controller.dat" #define CONTROLLER_FILE_NAME "emgauwa-controller.dat"
#define CONTROLLER_FILE_VERSION 1 #define CONTROLLER_FILE_VERSION 1
#define CONTROLLER_FILE_HEADER_SIZE 4 #define CONTROLLER_FILE_HEADER_SIZE 4
@ -12,4 +14,6 @@
#define TEST_KEY "blubb" #define TEST_KEY "blubb"
#define TEST_VALUE "bla bla bla" #define TEST_VALUE "bla bla bla"
#define LOG_LEVEL LOG_LEVEL_TRACE
#endif //CONTROLLER_CONFIG_H #endif //CONTROLLER_CONFIG_H

11
include/log_levels.h Normal file
View file

@ -0,0 +1,11 @@
#ifndef CONTROLLER_LOG_LEVEL_H
#define CONTROLLER_LOG_LEVEL_H
#define LOG_LEVEL_TRACE 5
#define LOG_LEVEL_DEBUG 4
#define LOG_LEVEL_INFO 3
#define LOG_LEVEL_WARN 2
#define LOG_LEVEL_ERROR 1
#define LOG_LEVEL_FATAL 0
#endif //CONTROLLER_LOG_LEVEL_H

46
include/logger.h Normal file
View file

@ -0,0 +1,46 @@
#ifndef CONTROLLER_LOGGER_H
#define CONTROLLER_LOGGER_H
#include <colors.h>
#include <config.h>
#include <macros.h>
#define _LOGGER_MESSAGE(msg) COLOR_NONE " %s:%s:%d: " msg "\n", __FILENAME__, __func__, __LINE__
#if LOG_LEVEL >= LOG_LEVEL_TRACE
#define LOG_TRACE(msg, ...) printf(COLOR_GREEN "[TRACE]" _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
#else
#define LOG_TRACE(msg, ...)
#endif
#if LOG_LEVEL >= LOG_LEVEL_DEBUG
#define LOG_DEBUG(msg, ...) printf(COLOR_BLUE "[DEBUG]" _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
#else
#define LOG_DEBUG(msg, ...)
#endif
#if LOG_LEVEL >= LOG_LEVEL_INFO
#define LOG_INFO(msg, ...) printf(COLOR_CYAN "[INFO] " _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
#else
#define LOG_INFO(msg, ...)
#endif
#if LOG_LEVEL >= LOG_LEVEL_WARN
#define LOG_WARN(msg, ...) printf(COLOR_YELLOW "[WARN] " _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
#else
#define LOG_WARN(msg, ...)
#endif
#if LOG_LEVEL >= LOG_LEVEL_ERROR
#define LOG_ERROR(msg, ...) printf(COLOR_RED "[ERROR]" _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
#else
#define LOG_ERROR(msg, ...)
#endif
#if LOG_LEVEL >= LOG_LEVEL_FATAL
#define LOG_FATAL(msg, ...) printf(COLOR_MAGENTA "[FATAL]" _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
#else
#define LOG_FATAL(msg, ...)
#endif
#endif //CONTROLLER_LOGGER_H

View file

@ -1,6 +1,13 @@
#ifndef CONTROLLER_MACROS_H #ifndef CONTROLLER_MACROS_H
#define CONTROLLER_MACROS_H #define CONTROLLER_MACROS_H
#define LOG_ERROR(msg) printf("ERROR: \"%s\" in function %s in file %s:%d\n", msg, __func__, __FILE__, __LINE__) #include <colors.h>
#include <logger.h>
#ifndef SOURCE_PATH_SIZE
#define SOURCE_PATH_SIZE 0
#endif
#define __FILENAME__ (__FILE__ + SOURCE_PATH_SIZE)
#endif //CONTROLLER_MACROS_H #endif //CONTROLLER_MACROS_H

6
main.c
View file

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <lmdb.h> #include <lmdb.h>
#include <logger.h>
#include <models/controller.h> #include <models/controller.h>
int int
@ -32,9 +33,8 @@ main(int argc, char** argv)
exit(1); exit(1);
} }
controller *this = controller_read(mdb_env); controller *this_cntrlr = controller_read(mdb_env);
(void)this; controller_save(this_cntrlr, mdb_env);
//controller_save(this);
mdb_env_close(mdb_env); mdb_env_close(mdb_env);

View file

@ -27,11 +27,26 @@ controller_create(void)
return result; return result;
} }
void
controller_read_single(MDB_txn *mdb_txn, MDB_dbi mdb_dbi, controller_db_key key_meta, MDB_val *value)
{
int err;
MDB_val key;
key.mv_size = sizeof(controller_db_key);
key.mv_data = &key_meta;
if((err = mdb_get(mdb_txn, mdb_dbi, &key, value)) != 0)
{
fprintf(stderr, "mdb_get error %s\n", mdb_strerror(err));
exit(1);
}
}
controller* controller*
controller_read(MDB_env *mdb_env) controller_read(MDB_env *mdb_env)
{ {
int err; int err;
int meta_key_data;
MDB_txn *mdb_txn; MDB_txn *mdb_txn;
MDB_dbi mdb_dbi; MDB_dbi mdb_dbi;
@ -59,44 +74,53 @@ controller_read(MDB_env *mdb_env)
new_controller = malloc(sizeof(*new_controller)); new_controller = malloc(sizeof(*new_controller));
MDB_val key;
MDB_val value; MDB_val value;
key.mv_size = sizeof(controller_db_key); controller_read_single(mdb_txn, mdb_dbi, KEY_META_UUID, &value);
meta_key_data = KEY_META_UUID;
key.mv_data = &meta_key_data;
if((err = mdb_get(mdb_txn, mdb_dbi, &key, &value)) != 0)
{
fprintf(stderr, "mdb_get error %s\n", mdb_strerror(err));
}
memmove(new_controller->uuid, (uuid_t*)value.mv_data, sizeof(uuid_t)); memmove(new_controller->uuid, (uuid_t*)value.mv_data, sizeof(uuid_t));
meta_key_data = KEY_META_NAME; controller_read_single(mdb_txn, mdb_dbi, KEY_META_NAME, &value);
key.mv_data = &meta_key_data;
if((err = mdb_get(mdb_txn, mdb_dbi, &key, &value)) != 0)
{
fprintf(stderr, "mdb_get error %s\n", mdb_strerror(err));
}
strncpy(new_controller->name, (char*)value.mv_data, CONTROLLER_NAME_LENGTH); strncpy(new_controller->name, (char*)value.mv_data, CONTROLLER_NAME_LENGTH);
new_controller->name[CONTROLLER_NAME_LENGTH] = '\0'; new_controller->name[CONTROLLER_NAME_LENGTH] = '\0';
controller_read_single(mdb_txn, mdb_dbi, KEY_META_PORT, &value);
new_controller->port = ((uint16_t*)value.mv_data)[0];
controller_read_single(mdb_txn, mdb_dbi, KEY_META_RELAY_COUNT, &value);
new_controller->relay_count = ((uint8_t*)value.mv_data)[0];
controller_debug(new_controller); controller_debug(new_controller);
mdb_txn_abort(mdb_txn); // transaction is read only mdb_txn_abort(mdb_txn); // transaction is read only
return NULL; return new_controller;
}
int
controller_save_single(MDB_txn *mdb_txn, MDB_dbi mdb_dbi, controller_db_key key_meta, MDB_val value)
{
int err;
MDB_val key;
key.mv_size = sizeof(controller_db_key);
key.mv_data = &key_meta;
if((err = mdb_put(mdb_txn, mdb_dbi, &key, &value, 0)) != 0)
{
fprintf(stderr, "mdb_put error %s\n", mdb_strerror(err));
mdb_txn_abort(mdb_txn);
return 1;
}
return 0;
} }
int int
controller_save(controller *cntrlr, MDB_env *mdb_env) controller_save(controller *cntrlr, MDB_env *mdb_env)
{ {
int err; int err;
int meta_key_data;
MDB_txn *mdb_txn; MDB_txn *mdb_txn;
MDB_dbi mdb_dbi; MDB_dbi mdb_dbi;
MDB_val key;
MDB_val value; MDB_val value;
controller_debug(cntrlr); controller_debug(cntrlr);
@ -113,31 +137,31 @@ controller_save(controller *cntrlr, MDB_env *mdb_env)
exit(1); exit(1);
} }
key.mv_size = sizeof(controller_db_key);
meta_key_data = KEY_META_UUID;
key.mv_data = &meta_key_data;
value.mv_size = sizeof(cntrlr->uuid); value.mv_size = sizeof(cntrlr->uuid);
value.mv_data = cntrlr->uuid; value.mv_data = cntrlr->uuid;
if(controller_save_single(mdb_txn, mdb_dbi, KEY_META_UUID, value))
if((err = mdb_put(mdb_txn, mdb_dbi, &key, &value, 0)) != 0)
{ {
fprintf(stderr, "mdb_put error %s\n", mdb_strerror(err));
mdb_txn_abort(mdb_txn);
return 1; return 1;
} }
meta_key_data = KEY_META_NAME;
key.mv_data = &meta_key_data;
value.mv_size = sizeof(char) * strlen(cntrlr->name); value.mv_size = sizeof(char) * strlen(cntrlr->name);
value.mv_data = cntrlr->name; value.mv_data = cntrlr->name;
if(controller_save_single(mdb_txn, mdb_dbi, KEY_META_NAME, value))
if((err = mdb_put(mdb_txn, mdb_dbi, &key, &value, 0)) != 0) {
return 1;
}
value.mv_size = sizeof(cntrlr->port);
value.mv_data = &cntrlr->port;
if(controller_save_single(mdb_txn, mdb_dbi, KEY_META_PORT, value))
{
return 1;
}
value.mv_size = sizeof(cntrlr->relay_count);
value.mv_data = &cntrlr->relay_count;
if(controller_save_single(mdb_txn, mdb_dbi, KEY_META_RELAY_COUNT, value))
{ {
fprintf(stderr, "mdb_put error %s\n", mdb_strerror(err));
mdb_txn_abort(mdb_txn);
return 1; return 1;
} }
@ -153,4 +177,5 @@ controller_debug(controller *cntrlr)
uuid_unparse(cntrlr->uuid, uuid_str); uuid_unparse(cntrlr->uuid, uuid_str);
printf("%s @ %p\n", uuid_str, cntrlr); printf("%s @ %p\n", uuid_str, cntrlr);
printf("name: %s\n", cntrlr->name); printf("name: %s\n", cntrlr->name);
printf("port: %5d relays: %3d\n", cntrlr->port, cntrlr->relay_count);
} }