diff options
-rw-r--r-- | src/pacman/conf.h | 3 | ||||
-rw-r--r-- | src/pacman/util.c | 87 | ||||
-rw-r--r-- | src/pacman/util.h | 3 |
3 files changed, 86 insertions, 7 deletions
diff --git a/src/pacman/conf.h b/src/pacman/conf.h index 8c55a2fc..49758418 100644 --- a/src/pacman/conf.h +++ b/src/pacman/conf.h @@ -33,6 +33,9 @@ typedef struct __config_t { unsigned short upgrade; unsigned short noconfirm; unsigned short noprogressbar; + unsigned short logmask; + + /* command line options */ unsigned short op_d_resolve; unsigned short op_q_isfile; unsigned short op_q_info; diff --git a/src/pacman/util.c b/src/pacman/util.c index eda32524..a0829e64 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -42,8 +42,6 @@ #include "util.h" #include "conf.h" -#define LOG_STR_LEN 256 - extern config_t *config; /* gets the current screen column width */ @@ -408,7 +406,6 @@ void display_targets(const alpm_list_t *syncpkgs) /* TODO there must be a better way */ int yesno(char *fmt, ...) { - char str[LOG_STR_LEN]; char response[32]; va_list args; @@ -417,11 +414,9 @@ int yesno(char *fmt, ...) } va_start(args, fmt); - vsnprintf(str, LOG_STR_LEN, fmt, args); - va_end(args); - /* Use stderr so questions are always displayed when redirecting output */ - fprintf(stderr, str); + vfprintf(stderr, fmt, args); + va_end(args); if(fgets(response, 32, stdin)) { if(strlen(response) != 0) { @@ -435,4 +430,82 @@ int yesno(char *fmt, ...) return(0); } +int pm_printf(pmloglevel_t level, const char *format, ...) +{ + int ret; + va_list args; + + /* print the message using va_arg list */ + va_start(args, format); + ret = pm_vfprintf(stdout, level, format, args); + va_end(args); + + return(ret); +} + +int pm_fprintf(FILE *stream, pmloglevel_t level, const char *format, ...) +{ + int ret; + va_list args; + + /* print the message using va_arg list */ + va_start(args, format); + ret = pm_vfprintf(stream, level, format, args); + va_end(args); + + return(ret); +} + +int pm_vfprintf(FILE *stream, pmloglevel_t level, const char *format, va_list args) +{ + int ret = 0; + + /* if current logmask does not overlap with level, do not print msg */ + if(!(config->logmask & level)) { + return ret; + } + +#if defined(PACMAN_DEBUG) + /* If debug is on, we'll timestamp the output */ + if(config->logmask & PM_LOG_DEBUG) { + time_t t; + struct tm *tmp; + char timestr[10] = {0}; + + t = time(NULL); + tmp = localtime(&t); + strftime(timestr, 9, "%H:%M:%S", tmp); + timestr[8] = '\0'; + + printf("[%s] ", timestr); + } +#endif + + /* print a prefix to the message */ + switch(level) { + case PM_LOG_DEBUG: + fprintf(stream, _("debug: ")); + break; + case PM_LOG_ERROR: + fprintf(stream, _("error: ")); + break; + case PM_LOG_WARNING: + fprintf(stream, _("warning: ")); + break; + case PM_LOG_FUNCTION: + /* TODO we should increase the indent level when this occurs so we can see + * program flow easier. It'll be fun */ + fprintf(stream, _("function: ")); + break; + default: + break; + } + + /* print the message using va_arg list */ + ret = vfprintf(stream, format, args); + /* TEMP HACK because libalpm strings don't have \n */ + fprintf(stream, "\n"); + return(ret); +} + /* vim: set ts=2 sw=2 noet: */ diff --git a/src/pacman/util.h b/src/pacman/util.h index 065f9531..d41b2ca0 100644 --- a/src/pacman/util.h +++ b/src/pacman/util.h @@ -47,6 +47,9 @@ char *strreplace(const char *str, const char *needle, const char *replace); void list_display(const char *title, const alpm_list_t *list); void display_targets(const alpm_list_t *syncpkgs); int yesno(char *fmt, ...); +int pm_printf(pmloglevel_t level, const char *format, ...); +int pm_fprintf(FILE *stream, pmloglevel_t level, const char *format, ...); +int pm_vfprintf(FILE *stream, pmloglevel_t level, const char *format, va_list args); #endif /* _PM_UTIL_H */ |