From 764a205fe9676afcdb94998b5e553d650bda86ba Mon Sep 17 00:00:00 2001 From: Anastasis Grammenos Date: Thu, 4 Oct 2018 17:20:37 +0300 Subject: Errlog with cklist, verbose flag, bugfix in search --- src/actionparser.c | 34 ++++++++++++++-- src/actions.c | 2 +- src/ckerrlog.c | 116 ++++++++++++++++++++++++++--------------------------- src/ckerrlog.h | 8 +++- src/cklist.c | 11 +++++ src/cklist.h | 4 +- 6 files changed, 109 insertions(+), 66 deletions(-) diff --git a/src/actionparser.c b/src/actionparser.c index deae72a..d5ad071 100644 --- a/src/actionparser.c +++ b/src/actionparser.c @@ -18,7 +18,7 @@ #include "ckinfo.h" #include "ckerrlog.h" -#define COMPONENT "action parser" +ERRLOG(action parser); /* accepted commands */ /* [0] is the count */ @@ -30,7 +30,9 @@ const char* const strLIST[] = {"5", "list", "ls", "l", "-l", "-ls"}; const char* const strSEARCH[] = {"3", "search", "s", "-s"}; const char* const strHELP[] = {"5", "help", "h", "-?", "-h", "--help"}; const char* const strConfDir[] = {"4", "config", "conf", "c", "-c"}; -const char* const strVersion[] = {"4", "version", "--version", "-v", "-V"}; +const char* const strVersion[] = {"2", "version", "--version"}; +const char* const strVerbose1[] = {"2", "--verbose", "-v"}; +const char* const strVerbose2[] = {"2", "--Verbose", "-V"}; /* Number of opts */ static int optNum; @@ -140,7 +142,7 @@ int parse_LIST(UserOpt *opt) { int parse_SEARCH(UserOpt *opt) { /* Search expects a maximum of 1 argument */ - if (optNum > pos + 1) { + if (optNum <= pos || optNum > pos + 1) { opt->err = PERR_SEARCH_WRONG; return -1; } @@ -264,6 +266,29 @@ int version() { return 0; } +void verbose() { + /* get first token */ + next_token(); + + for (int i = 1; i < atoi(strVerbose1[0]) + 1; i++) { + if (strcmp(token, strVerbose1[i]) == 0) { + errlog_set_verbose(1);; + return; + } + } + + for (int i = 1; i < atoi(strVerbose2[0]) + 1; i++) { + if (strcmp(token, strVerbose2[i]) == 0) { + errlog_set_verbose(2);; + return; + } + } + + // rewind + pos = pos - 1; + token = opts[pos]; +} + void get_possible_action_strings(char *dest, CkAction ckAction) { char buf[STR_S]; switch (ckAction) { @@ -361,6 +386,9 @@ ActionParseResult parse_action(int argc, char* argv[], UserOpt *opt) { return APR_VERSION; } + /* set verbose level */ + verbose(); + /* figure what is the config file */ get_config(opt); cklog("Found ck configuration directory in %s", opt->confDir); diff --git a/src/actions.c b/src/actions.c index 2a94427..337911f 100644 --- a/src/actions.c +++ b/src/actions.c @@ -209,7 +209,7 @@ int run_SEARCH(UserOpt *opt, Conf *conf) { cklist *paths = list_make_new(); list_get_paths(&db, paths, 0); close_DB(&db); - if (list_size(paths)) { + if (list_size(paths) && list_size(opt->args)) { do { FILE *cmd; char result[STR_L] = ""; diff --git a/src/ckerrlog.c b/src/ckerrlog.c index 9e1fc51..9a63ffd 100644 --- a/src/ckerrlog.c +++ b/src/ckerrlog.c @@ -12,13 +12,14 @@ #include "ckerrlog.h" #include "ckutil.h" +#include "cklist.h" -#define COMPONENT "ckerrlog" +ERRLOG(ckerrlog); static int loglvl; static char buf[STR_M]; -#define X(stream) static char *stream; +#define X(stream) static cklist *stream; CK_STREAMS #undef X @@ -39,53 +40,65 @@ void initialize_errlog() { cklog("%s Log session started", get_time()); } -void report_errlog() { - if (err) - printf("Errors:\n%s", err); - free(err); - if (log) - printf("Logs:\n%s", log); - free(log); -} +#define X(stream) \ + void add_ ## stream ## _with_delim(char *delim, char *txt, \ + va_list args) { \ + char tmp[STR_L]; \ + vsprintf(tmp, txt, args); \ + if (stream) { \ + list_add(stream, tmp); \ + list_add(stream, delim); \ + return; \ + } \ + stream = list_make_and_add(tmp); \ + list_add(stream, delim); \ + } +CK_STREAMS +#undef X -#define X(stream) \ - void add_ ## stream ## _with_delim(char *delim, char *txt, \ - va_list args) { \ - char tmp[STR_L]; \ - vsprintf(tmp, txt, args); \ - if (stream) { \ - stream = (char *)realloc(stream, strlen(stream) + \ - strlen(tmp) + strlen(delim) + 1); \ - strcat(stream, tmp); \ - strcat(stream, delim); \ - return; \ - } \ - stream = (char *)malloc(strlen(tmp) + strlen(delim) + 1); \ - strcpy(stream, tmp); \ - strcat(stream, delim); \ +#define X(stream) \ + void ck## stream(char *txt, ...) { \ + va_list args; \ + va_start(args, txt); \ + add_## stream ##_with_delim("\n", txt, args); \ + va_end(args); \ } CK_STREAMS #undef X -void ckerr(char *txt, ...) { - va_list args; - va_start(args, txt); - add_err_with_delim("\n", txt, args); - va_end(args); -} +#define X(stream) \ + void reset_## stream() { \ + list_free(stream); \ + stream = NULL; \ + } +CK_STREAMS +#undef X -void cklog(char *txt, ...) { - va_list args; - va_start(args, txt); - add_log_with_delim("\n", txt, args); - va_end(args); -} +#define X(stream) \ + void report_## stream() { \ + list_print_concat(stream); \ + reset_## stream(); \ + } +CK_STREAMS +#undef X -void ckhelp(char *txt, ...) { - va_list args; - va_start(args, txt); - add_help_with_delim("\n", txt, args); - va_end(args); +void report_errlog() { + if (!loglvl) { +#define X(stream) \ + if (stream) { \ + reset_##stream(); \ + } + CK_STREAMS +#undef X + return; + } +#define X(stream) \ + if (stream) { \ + printf("%s:\n", #stream); \ + report_##stream(); \ + } + CK_STREAMS +#undef X } void ckerr_add_component(char *txt, ...) { @@ -95,21 +108,6 @@ void ckerr_add_component(char *txt, ...) { va_end(args); } -void ckhelp_add_component(char *txt, ...) { - va_list args; - va_start(args, txt); - add_help_with_delim(" ", txt, args); - va_end(args); -} - -void report_err() { - printf("%s", err); - free(err); - err = NULL; -} - -void report_help() { - printf("%s", help); - free(help); - help = NULL; +extern void errlog_set_verbose(int level) { + loglvl = level; } diff --git a/src/ckerrlog.h b/src/ckerrlog.h index be5257d..d3d7243 100644 --- a/src/ckerrlog.h +++ b/src/ckerrlog.h @@ -25,6 +25,8 @@ void ckerr_add_component(char *txt, ...); extern void initialize_errlog(); extern void report_errlog(); +extern void errlog_set_verbose(int level); + extern void ckerr(char *err, ...); extern void cklog(char *log, ...); extern void ckhelp(char *log, ...); @@ -35,8 +37,12 @@ extern void report_help(); /* Macros */ /**********/ +/* define the component's name */ +#define ERRLOG(_COMPONENT) \ + static const char COMPONENT[STR_S] = #_COMPONENT + #define ERR(...) \ - ckerr_add_component("Error in [%s]:", COMPONENT); \ + ckerr("Error in [%s]:", COMPONENT); \ ckerr(__VA_ARGS__); \ report_err(); diff --git a/src/cklist.c b/src/cklist.c index e1ca7d8..14d5d82 100644 --- a/src/cklist.c +++ b/src/cklist.c @@ -175,6 +175,17 @@ void list_print(cklist *ckl) { } } +void list_print_concat(cklist *ckl) { + if (ckl->size > 0) { + list_rewind(ckl); + printf("%s", list_get(ckl)); + while (list_next(ckl)) { + printf("%s", list_get(ckl)); + } + list_rewind(ckl); + } +} + int list_size(cklist *ckl) { return ckl->size; } diff --git a/src/cklist.h b/src/cklist.h index b5608e5..346e55f 100644 --- a/src/cklist.h +++ b/src/cklist.h @@ -37,7 +37,7 @@ extern int list_size(cklist *ckl); extern cklist* list_duplicate(cklist *ckl); /* rewinds */ extern cklist* list_move(cklist *ckl); -/* rewinds +/* rewinds * copy from index (>=) to the end */ extern cklist* list_copy_from(cklist *ckl, int index); /* rewinds @@ -51,8 +51,8 @@ extern cklist* list_copy_part(cklist *ckl, int from, int until); /* rewinds */ extern void list_print_lisp(cklist *ckl); extern void list_print_python(cklist *ckl); -/* rewinds */ extern void list_print(cklist *ckl); +extern void list_print_concat(cklist *ckl); /* Deallocate resources */ extern void list_free(cklist *ckl); -- cgit v1.2.3