diff options
author | 2008-11-16 04:11:23 +0000 | |
---|---|---|
committer | 2008-11-16 04:11:23 +0000 | |
commit | a7afc3f309c901c544c1be8ff72d61d2f45137f8 (patch) | |
tree | 4eab2d95921d9663c82af9ab4c5d3be6a4a0b114 /libsbutil | |
parent | libsbutil: simple custom printf() replacement (diff) | |
download | sandbox-a7afc3f309c901c544c1be8ff72d61d2f45137f8.tar.gz sandbox-a7afc3f309c901c544c1be8ff72d61d2f45137f8.tar.bz2 sandbox-a7afc3f309c901c544c1be8ff72d61d2f45137f8.zip |
unify SB_E{INFO,WARN,ERROR} functions and have them call the internal sb_printf function
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Diffstat (limited to 'libsbutil')
-rw-r--r-- | libsbutil/sb_printf.c | 23 | ||||
-rw-r--r-- | libsbutil/sbutil.h | 39 |
2 files changed, 30 insertions, 32 deletions
diff --git a/libsbutil/sb_printf.c b/libsbutil/sb_printf.c index 9d3f819..f17a018 100644 --- a/libsbutil/sb_printf.c +++ b/libsbutil/sb_printf.c @@ -148,3 +148,26 @@ void sb_printf(const char *format, ...) sb_vfdprintf(STDOUT_FILENO, format, args); va_end(args); } + +static bool nocolor, init_color = false; +void sb_efunc(int fd, const char *color, const char *hilight, const char *format, ...) +{ + save_errno(); + + if (!init_color) { + nocolor = is_env_on(ENV_NOCOLOR); + init_color = true; + } + + if (!nocolor) + sb_fdprintf(fd, "%s%s%s", color, hilight, COLOR_NORMAL); + else + sb_fdprintf(fd, "%s", hilight); + + va_list args; + va_start(args, format); + sb_vfdprintf(fd, format, args); + va_end(args); + + restore_errno(); +} diff --git a/libsbutil/sbutil.h b/libsbutil/sbutil.h index 332d536..46f851a 100644 --- a/libsbutil/sbutil.h +++ b/libsbutil/sbutil.h @@ -75,35 +75,9 @@ #define COLOR_RED "\033[31;01m" /* Gentoo style e* printing macro's */ -#define SB_EINFO(_color, _hilight, _args...) \ - do { \ - save_errno(); \ - if (_color) \ - fprintf(stderr, COLOR_GREEN _hilight COLOR_NORMAL _args); \ - else \ - fprintf(stderr, _hilight _args); \ - restore_errno(); \ - } while (0) - -#define SB_EWARN(_color, _hilight, _args...) \ - do { \ - save_errno(); \ - if (_color) \ - fprintf(stderr, COLOR_YELLOW _hilight COLOR_NORMAL _args); \ - else \ - fprintf(stderr, _hilight _args); \ - restore_errno(); \ - } while (0) - -#define SB_EERROR(_color, _hilight, _args...) \ - do { \ - save_errno(); \ - if (_color) \ - fprintf(stderr, COLOR_RED _hilight COLOR_NORMAL _args); \ - else \ - fprintf(stderr, _hilight _args); \ - restore_errno(); \ - } while (0) +#define SB_EINFO(_hilight, _args...) sb_efunc(STDOUT_FILENO, COLOR_GREEN, _hilight, _args) +#define SB_EWARN(_hilight, _args...) sb_efunc(STDERR_FILENO, COLOR_YELLOW, _hilight, _args) +#define SB_EERROR(_hilight, _args...) sb_efunc(STDERR_FILENO, COLOR_RED, _hilight, _args) void get_sandbox_lib(char *path); void get_sandbox_rc(char *path); @@ -122,9 +96,10 @@ size_t sb_write(int fd, const void *buf, size_t count); int sb_close(int fd); /* Reliable output */ -void sb_printf(const char *format, ...) __attribute__ ((__format__ (__printf__, 1, 2))); -void sb_fdprintf(int fd, const char *format, ...) __attribute__ ((__format__ (__printf__, 2, 3))); -void sb_vfdprintf(int fd, const char *format, va_list args) __attribute__ ((__format__ (__printf__, 2, 0))); +__attribute__((__format__(__printf__, 1, 2))) void sb_printf(const char *format, ...); +__attribute__((__format__(__printf__, 2, 3))) void sb_fdprintf(int fd, const char *format, ...); +__attribute__((__format__(__printf__, 2, 0))) void sb_vfdprintf(int fd, const char *format, va_list args); +__attribute__((__format__(__printf__, 4, 5))) void sb_efunc(int fd, const char *color, const char *hilight, const char *format, ...); /* Macro for sb_read() to goto an label on error */ #define SB_WRITE(_fd, _buf, _count, _error) \ |