From eabd52462bfdb409121b351f198d535649d2b3b9 Mon Sep 17 00:00:00 2001 From: gramanas Date: Thu, 6 Dec 2018 09:23:52 +0200 Subject: Add log to file, fix list ckconf bug --- src/ck.c | 2 +- src/ckerrlog.c | 21 +++++++++++++++++++-- src/ckerrlog.h | 1 + src/cklist.c | 23 +++++++++++++++++++++++ src/cklist.h | 2 ++ src/confparser.c | 11 ++++++++--- src/confparser.h | 7 ++++--- src/init.c | 8 ++++++++ src/list.c | 12 +++++++----- 9 files changed, 73 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/ck.c b/src/ck.c index 361d9f0..b9e6f53 100644 --- a/src/ck.c +++ b/src/ck.c @@ -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, diff --git a/src/init.c b/src/init.c index 10f38cb..8123bae 100644 --- a/src/init.c +++ b/src/init.c @@ -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; } diff --git a/src/list.c b/src/list.c index 0976ac1..144dd41 100644 --- a/src/list.c +++ b/src/list.c @@ -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); -- cgit v1.2.3