diff options
author | 2023-02-02 14:36:29 +0100 | |
---|---|---|
committer | 2023-02-02 14:36:29 +0100 | |
commit | 2c2d36f6fe220ca34dc4a55137c8382bb8e397e7 (patch) | |
tree | 1144efd6efb832acb977fbc3307be886120e730b | |
parent | 0051ede482acd2058fd6ddfecb5f5eafe47962b6 (diff) | |
download | log-2c2d36f6fe220ca34dc4a55137c8382bb8e397e7.tar.gz log-2c2d36f6fe220ca34dc4a55137c8382bb8e397e7.zip |
Add all stuff for getting work the log util
-rw-r--r-- | log.c | 73 |
1 files changed, 60 insertions, 13 deletions
@@ -13,7 +13,9 @@ #define ERR_LVL 3 #define BUFF_SIZE 256 +#define HALF_SIZE (BUFF_SIZE / 2 - 1) +/* Checks if the terminal supports colors */ static int check_term_colors() { @@ -42,18 +44,58 @@ check_term_colors() return -1; } +/* Converts the property values */ +static int +log_parse_level_property(char *str_level) +{ + if (strcmp( str_level, "DEBUG" ) == 0) return DBG_LVL; + if (strcmp( str_level, "INFO" ) == 0) return INF_LVL; + if (strcmp( str_level, "WARNING" ) == 0) return WRN_LVL; + if (strcmp( str_level, "ERROR" ) == 0) return ERR_LVL; + + return -1; +} + +/* Parses the configuration line to get log level and the context */ +static int +log_get_level(const char *filename, char *conf_key, char *conf_value) +{ + char *key = strndup(conf_key, strlen(conf_key)); + char *context = strndup(strtok(key, "."), strlen(key)); + int level_context = strcmp(strtok(NULL, "."), "level"); + char *file_context = strndup(filename, HALF_SIZE); + int root_level = 0; + int level = 0; + + file_context = strtok(file_context, "."); + + if (level_context == 0) { + if (strcmp(context, "root") == 0) { + root_level = log_parse_level_property(conf_value); + } else if (strcmp(context, file_context) == 0 + && (level = log_parse_level_property(conf_value)) >= 0) { + return level; + } + } + + return root_level; +} + +/* Initialize the log structure */ static void log_init(Log *log) { - char buff[BUFF_SIZE]; + char conf_key[HALF_SIZE], conf_value[HALF_SIZE], buff[BUFF_SIZE]; FILE *file = fopen("log.config", "r"); /* Use the coded configuration */ if (file == NULL) return; - while (fgets(buff, BUFF_SIZE, file) != NULL) { - /*printf("%s", strtok(buff, "="));*/ + while (fgets(buff, BUFF_SIZE, file) != NULL + && buff[0] != '#') { + sscanf(buff, "%[^=]=%s", conf_key, conf_value); + log->level = log_get_level(log->filename, conf_key, conf_value); } fclose(file); @@ -62,8 +104,8 @@ log_init(Log *log) Log * log_create(const char *filename) { Log *log = (Log *) malloc(sizeof(Log)); - /*log->filename = filename; - log->level = DBG_LVL;*/ + log->filename = filename; + log->level = DBG_LVL; log_init(log); @@ -74,14 +116,16 @@ static void print_log(Log *log, int level, char *msg) { time_t t; - t = time(NULL); - struct tm tm = *localtime(&t); + struct tm *date; const char *lvl; /* Don't display log if the choosen level is major. * For example: log->level is ERROR and level is INFO, only ERROR logs will be shown. */ if (log->level > level) return; + t = time(NULL); + date = localtime(&t); + switch (level) { case DBG_LVL: lvl = "[DEBUG]"; @@ -89,6 +133,9 @@ print_log(Log *log, int level, char *msg) case INF_LVL: lvl = "[INFO]"; break; + case WRN_LVL: + lvl = "[WARNING]"; + break; case ERR_LVL: lvl = "[ERROR]"; break; @@ -99,11 +146,11 @@ print_log(Log *log, int level, char *msg) check_term_colors(); printf("%d-%02d-%02d %02d:%02d %10s: <%s> %s\n", - tm.tm_year + 1900, - tm.tm_mon, - tm.tm_mday, - tm.tm_hour, - tm.tm_min, + date->tm_year + 1900, + date->tm_mon, + date->tm_mday, + date->tm_hour, + date->tm_min, lvl, log->filename, msg); @@ -128,7 +175,7 @@ log_info(Log *log, char *msg) } void -log_warning(Log *log, char *msg) +log_warn(Log *log, char *msg) { print_log(log, WRN_LVL, msg); } |