diff options
author | gramanas <anastasis.gramm2@gmail.com> | 2018-12-06 09:23:52 +0200 |
---|---|---|
committer | gramanas <anastasis.gramm2@gmail.com> | 2018-12-06 09:23:52 +0200 |
commit | eabd52462bfdb409121b351f198d535649d2b3b9 (patch) | |
tree | 434ec71438df3c822d34fc0b8d83fa977264c546 | |
parent | bd85acf6a408674bcdcb5116963fe4fea2247280 (diff) | |
download | ck-eabd52462bfdb409121b351f198d535649d2b3b9.tar.gz ck-eabd52462bfdb409121b351f198d535649d2b3b9.tar.bz2 ck-eabd52462bfdb409121b351f198d535649d2b3b9.zip |
Add log to file, fix list ckconf bug
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/ck.c | 2 | ||||
-rw-r--r-- | src/ckerrlog.c | 21 | ||||
-rw-r--r-- | src/ckerrlog.h | 1 | ||||
-rw-r--r-- | src/cklist.c | 23 | ||||
-rw-r--r-- | src/cklist.h | 2 | ||||
-rw-r--r-- | src/confparser.c | 11 | ||||
-rw-r--r-- | src/confparser.h | 7 | ||||
-rw-r--r-- | src/init.c | 8 | ||||
-rw-r--r-- | src/list.c | 12 |
10 files changed, 74 insertions, 15 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 8dcf30a..b050f44 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ project(ck C) # version set(ck_MAJOR_VERSION 0) set(ck_MINOR_VERSION 9) -set(ck_PATCH_VERSION 5) +set(ck_PATCH_VERSION 6) # Feature test macros set(FEATURE_TEST_MACROS "-D_DEFAULT_SOURCE") @@ -62,7 +62,7 @@ int main(int argc, const char **argv) { /* Run action and print results */ switch(opt.action) { -#define X(ACTION, MIN, MAX) \ +#define X(ACTION, MIN, MAX) \ case CKA_##ACTION: \ rc = run_##ACTION(&opt, &conf); \ break; diff --git a/src/ckerrlog.c b/src/ckerrlog.c index cf5a6e8..5605fbd 100644 --- a/src/ckerrlog.c +++ b/src/ckerrlog.c @@ -16,13 +16,14 @@ ERRLOG(logger); static int loglvl; +static char logfile[STR_L] = ""; static char buf[STR_M] = ""; #define X(stream) static cklist *stream; CK_STREAMS #undef X -char *get_time() { +static char *get_time() { time_t rawtime; struct tm * timeinfo; time (&rawtime); @@ -94,6 +95,16 @@ CK_STREAMS #undef X void report_errlog() { + LOG("------- end -------\n"); + if (!str_is_empty(logfile)) { +#define X(stream) \ + if (stream) { \ + list_write_to_file(stream, logfile, 1); \ + } + CK_STREAMS +#undef X + } + if (!loglvl) { #define X(stream) \ if (stream) { \ @@ -119,11 +130,17 @@ void ckerr_add_component(char *txt, ...) { va_end(args); } +void errlog_make_logfile(char *logdir) { + if (!logdir) { + return; + } + str_join_dirname_with_basename(logfile, logdir, "ck.log"); +} + void errlog_set_verbose(int level) { loglvl = level; } - void initialize_errlog(int argc, const char** argv) { #define X(stream) stream = NULL; CK_STREAMS diff --git a/src/ckerrlog.h b/src/ckerrlog.h index a4cc9e1..c5555e4 100644 --- a/src/ckerrlog.h +++ b/src/ckerrlog.h @@ -51,6 +51,7 @@ void initialize_errlog(int argc, const char** argv); void report_errlog(); void errlog_set_verbose(int level); +void errlog_make_logfile(char *logdir); #define X(stream) \ void ck## stream(const char *log, ...); \ diff --git a/src/cklist.c b/src/cklist.c index f255556..4ba51bc 100644 --- a/src/cklist.c +++ b/src/cklist.c @@ -191,6 +191,29 @@ int list_exists(cklist *ckl, const char *str) { return 0; } +int list_write_to_file(cklist *ckl, char *path, int append) { + list_rewind(ckl); + if (!list_size(ckl)) { + return -1; + } + FILE *f; + if (append) { + f = fopen(path, "a"); + } + else { + f = fopen(path, "w"); + } + if (!f) { + return -1; + } + do { + fputs(list_get(ckl), f); + } while(list_next(ckl)); + fclose(f); + list_rewind(ckl); + return 0; +} + unsigned int list_size(cklist *ckl) { return ckl->size; } diff --git a/src/cklist.h b/src/cklist.h index 6ce8960..da41782 100644 --- a/src/cklist.h +++ b/src/cklist.h @@ -57,6 +57,8 @@ void list_print_python(cklist *ckl); void list_print(cklist *ckl); void list_print_concat(cklist *ckl); +int list_write_to_file(cklist *ckl, char *path, int append); + /* Deallocate resources */ void list_free(cklist *ckl); diff --git a/src/confparser.c b/src/confparser.c index f7b6268..fe1c19a 100644 --- a/src/confparser.c +++ b/src/confparser.c @@ -129,15 +129,20 @@ int config_file_parse(Conf *conf, UserOpt *opt) { return -1; } } - /* Could add an optional row that would make the config var - * optional. */ + /* Check if every non optional + * variable was read. */ #define X(var, str, name, optional) \ if (!optional && !conf->var) { \ ERR("Missing %s", name); \ return -1; \ } - CONFIG_VARIABLES_TABLE + CONFIG_VARIABLES_TABLE; #undef X + + /* Update ckerrlog logfile */ + if (conf->log_dir) { + errlog_make_logfile(conf->log_dir); + } return 0; } diff --git a/src/confparser.h b/src/confparser.h index 10e7e18..61f03f4 100644 --- a/src/confparser.h +++ b/src/confparser.h @@ -19,10 +19,11 @@ #include "clparser.h" /* name | match str | desc | optional */ -#define CONFIG_VARIABLES_TABLE \ +#define CONFIG_VARIABLES_TABLE \ X(vc_dir, " version_control_dir = %s ", "Version Control directory", 0) \ - X(scrt_dir, " secret_dir = %s " , "Secret directory", 1) \ - X(home_dir, " home_dir = %s " , "Home directory", 0) + X(scrt_dir, " secret_dir = %s " , "Secret directory", 1) \ + X(home_dir, " home_dir = %s " , "Home directory", 0) \ + X(log_dir, " log_dir = %s " , "Log directory", 1) enum ConfingVariables { CV_NO_VAL_OR_COMMENT, @@ -59,6 +59,14 @@ static int init_create_config_file(UserOpt *opt) { strcat(tmp, "\n"); fputs(tmp, f); + strcpy(tmp, "# Uncomment this line to enable logging in file\n"); + fputs(tmp, f); + + strcpy(tmp, "# log_dir = "); + strcat(tmp, opt->confDir); + strcat(tmp, "\n"); + fputs(tmp, f); + fclose(f); return 0; } @@ -245,11 +245,13 @@ int run_LIST(UserOpt *opt, Conf *conf) { strcat(tmp, opt->confDir); list_add(the_list, tmp); #define X(var, str, name, optional) \ - strcpy(tmp, ""); \ - strcat(tmp, name); \ - strcat(tmp, ": "); \ - strcat(tmp, conf->var); \ - list_add(the_list, tmp); + if (conf->var) { \ + strcpy(tmp, ""); \ + strcat(tmp, name); \ + strcat(tmp, ": "); \ + strcat(tmp, conf->var); \ + list_add(the_list, tmp); \ + } CONFIG_VARIABLES_TABLE; #undef X list_print(the_list); |