From be8e5fa7e3d1adbdd99a64c2c7bf37e04f443a24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ilja=20Karta=C5=A1ov?= Date: Fri, 5 Apr 2019 16:20:30 +0200 Subject: [PATCH] Initialize cStuff --- cStuff/log.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++ cStuff/log.h | 62 +++++++++++++++++++++++++++++ cStuff/retcode.h | 28 +++++++++++++ 3 files changed, 191 insertions(+) create mode 100644 cStuff/log.c create mode 100644 cStuff/log.h create mode 100644 cStuff/retcode.h diff --git a/cStuff/log.c b/cStuff/log.c new file mode 100644 index 0000000..af6c3e6 --- /dev/null +++ b/cStuff/log.c @@ -0,0 +1,101 @@ +/****************************************************************************** + * + * Copyright (c) 2017-2019 by Löwenware Ltd + * Please, refer LICENSE file for legal information + * + ******************************************************************************/ + +/** + * @file log.c + * @author Ilja Kartašov + * @brief cStuff log module source file + * + * @see https://lowenware.com/ + */ +#include +#include "log.h" + + +void +cstuff_log_init(cstuff_log_t self, uint8_t level) +{ + self->target = stderr; + self->level = level; +} + + +void +cstuff_log_release(cstuff_log_t self) +{ + if (self->target != stderr) + fclose(self->target); +} + + +cstuff_retcode_t +cstuff_log_set_file(cstuff_log_t self, const char * filename) +{ + FILE * fp; + + + if ( !(fp = fopen(filename, "a")) ) + return CSTUFF_SYSCALL_ERROR; + + self->target = fp; + return CSTUFF_SUCCESS; +} + + +int +cstuff_log_printf(cstuff_log_t self, uint8_t level, const char * format, ...) +{ + int result; + va_list vl; + + va_start(vl, format); + result = cstuff_log_vprintf(self, level, format, vl); + va_end(vl); + + return result; +} + + +int +cstuff_log_vprintf(cstuff_log_t self, + uint8_t level, + const char * format, + va_list vl) +{ + int result; + + if (self->level & level) + { + FILE * f = self->target; + char * label; + time_t rawtime; + char strtime[32+1]; + struct tm timeinfo; + + time (&rawtime); + localtime_r (&rawtime, &timeinfo); + strftime (strtime, 32, "%F %T", &timeinfo); + + switch (level) + { + case CSTUFF_LOG_ERROR : label = "!!"; break; + case CSTUFF_LOG_ALERT : label = "~!"; break; + case CSTUFF_LOG_DEBUG : label = "**"; break; + default : label = "--"; + } + + result = fprintf(f, "%s %s ", strtime, label); + result += vfprintf(f, format, vl); + fputc('\n', f); + result++; + fflush(f); + } + else + result = 0; + + return result; +} diff --git a/cStuff/log.h b/cStuff/log.h new file mode 100644 index 0000000..e2bcea6 --- /dev/null +++ b/cStuff/log.h @@ -0,0 +1,62 @@ +/****************************************************************************** + * + * Copyright (c) 2017-2019 by Löwenware Ltd + * Please, refer LICENSE file for legal information + * + ******************************************************************************/ + +/** + * @file log.h + * @author Ilja Kartašov + * @brief cStuff log module header file + * + * @see https://lowenware.com/cStuff/ + */ + +#ifndef CSTUFF_LOG_H_557ADEFE_2BE8_4B9B_A894_F08FF64008EF +#define CSTUFF_LOG_H_557ADEFE_2BE8_4B9B_A894_F08FF64008EF + +#include +#include +#include +#include "retcode.h" + +#define CSTUFF_LOG_OFF (0) +#define CSTUFF_LOG_ERROR (1<<0) +#define CSTUFF_LOG_ALERT (1<<1) +#define CSTUFF_LOG_STATE (1<<2) +#define CSTUFF_LOG_DEBUG (1<<3) +#define CSTUFF_LOG_ALL (0xFF) + +struct cstuff_log +{ + FILE * target; + uint8_t level; +}; + +typedef struct cstuff_log * cstuff_log_t; + + +void +cstuff_log_init(cstuff_log_t self, uint8_t level); + + +void +cstuff_log_release(cstuff_log_t self); + + +cstuff_retcode_t +cstuff_log_set_file(cstuff_log_t self, const char * filename); + + +int +cstuff_log_printf(cstuff_log_t self, uint8_t level, const char * format, ...); + + +int +cstuff_log_vprintf(cstuff_log_t self, + uint8_t level, + const char * format, + va_list vl); + +#endif /* !CSTUFF_LOG_H */ diff --git a/cStuff/retcode.h b/cStuff/retcode.h new file mode 100644 index 0000000..82c425b --- /dev/null +++ b/cStuff/retcode.h @@ -0,0 +1,28 @@ +/****************************************************************************** + * + * Copyright (c) 2017-2019 by Löwenware Ltd + * Please, refer LICENSE file for legal information + * + ******************************************************************************/ + +/** + * @file retcode.h + * @author Ilja Kartašov + * @brief cStuff return codes + * + * @see https://lowenware.com/ + */ + +#ifndef CSTUFF_RETCODE_H_CBA3C16B_88D6_49F4_966D_DBDA727583FE +#define CSTUFF_RETCODE_H_CBA3C16B_88D6_49F4_966D_DBDA727583FE + +typedef enum +{ + CSTUFF_MALLOC_ERROR = -2 + , CSTUFF_SYSCALL_ERROR = -1 + , CSTUFF_SUCCESS = 0 + , CSTUFF_IDLE = 1 + +} cstuff_retcode_t; + +#endif /* !CSTUFF_RETCODE_H */