diff options
author | Lucas De Marchi <lucas.demarchi@profusion.mobi> | 2012-11-06 19:01:59 -0200 |
---|---|---|
committer | Lucas De Marchi <lucas.demarchi@profusion.mobi> | 2012-11-06 19:02:23 -0200 |
commit | fcb0ce9475509f22031e77456c1075c891cbcd2e (patch) | |
tree | 8f50d9793cc6f172dd634a09e7be44404f2b817c | |
parent | 52a50fe2e548d3a3c112074e9c4e89237c257161 (diff) | |
download | kmod-fcb0ce9475509f22031e77456c1075c891cbcd2e.tar.gz |
tools: share function for logging
-rw-r--r-- | tools/depmod.c | 52 | ||||
-rw-r--r-- | tools/insmod.c | 3 | ||||
-rw-r--r-- | tools/log.c | 28 | ||||
-rw-r--r-- | tools/log.h | 7 | ||||
-rw-r--r-- | tools/modinfo.c | 3 | ||||
-rw-r--r-- | tools/modprobe.c | 35 | ||||
-rw-r--r-- | tools/rmmod.c | 30 |
7 files changed, 36 insertions, 122 deletions
diff --git a/tools/depmod.c b/tools/depmod.c index 8741d06..7d36a50 100644 --- a/tools/depmod.c +++ b/tools/depmod.c @@ -129,58 +129,6 @@ static inline void _show(const char *fmt, ...) fflush(stdout); va_end(args); } - -static void _log(int prio, const char *fmt, ...) -{ - const char *prioname; - char buf[32], *msg; - va_list args; - - if (prio > verbose) - return; - - va_start(args, fmt); - if (vasprintf(&msg, fmt, args) < 0) - msg = NULL; - va_end(args); - if (msg == NULL) - return; - - switch (prio) { - case LOG_CRIT: - prioname = "FATAL"; - break; - case LOG_ERR: - prioname = "ERROR"; - break; - case LOG_WARNING: - prioname = "WARNING"; - break; - case LOG_NOTICE: - prioname = "NOTICE"; - break; - case LOG_INFO: - prioname = "INFO"; - break; - case LOG_DEBUG: - prioname = "DEBUG"; - break; - default: - snprintf(buf, sizeof(buf), "LOG-%03d", prio); - prioname = buf; - } - - fprintf(stderr, "depmod: %s: %s", prioname, msg); - free(msg); - - if (prio <= LOG_CRIT) - exit(EXIT_FAILURE); -} -#define CRIT(...) _log(LOG_CRIT, __VA_ARGS__) -#define ERR(...) _log(LOG_ERR, __VA_ARGS__) -#define WRN(...) _log(LOG_WARNING, __VA_ARGS__) -#define INF(...) _log(LOG_INFO, __VA_ARGS__) -#define DBG(...) _log(LOG_DEBUG, __VA_ARGS__) #define SHOW(...) _show(__VA_ARGS__) diff --git a/tools/insmod.c b/tools/insmod.c index c05a6fc..f358b93 100644 --- a/tools/insmod.c +++ b/tools/insmod.c @@ -26,9 +26,6 @@ #include "kmod.h" -#define LOGPREFIX "insmod: " -#define ERR(...) fprintf(stderr, LOGPREFIX "ERROR: " __VA_ARGS__) - static const char cmdopts_s[] = "psfVh"; static const struct option cmdopts[] = { {"version", no_argument, 0, 'V'}, diff --git a/tools/log.c b/tools/log.c index 229f41c..c6887bd 100644 --- a/tools/log.c +++ b/tools/log.c @@ -70,6 +70,34 @@ void log_kmod(void *data, int priority, const char *file, int line, (void)data; } +void log_printf(int prio, const char *fmt, ...) +{ + const char *prioname; + char *msg; + va_list args; + + if (prio > log_priority) + return; + + va_start(args, fmt); + if (vasprintf(&msg, fmt, args) < 0) + msg = NULL; + va_end(args); + if (msg == NULL) + return; + + prioname = prio_to_str(prio); + + if (log_use_syslog) + syslog(prio, "%s: %s", prioname, msg); + else + fprintf(stderr, "%s: %s: %s", binname, prioname, msg); + free(msg); + + if (prio <= LOG_CRIT) + exit(EXIT_FAILURE); +} + void log_setup_kmod_log(struct kmod_ctx *ctx, int priority) { log_priority = priority; diff --git a/tools/log.h b/tools/log.h index d4fbcdf..90817a3 100644 --- a/tools/log.h +++ b/tools/log.h @@ -17,6 +17,7 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include <stdarg.h> #include <stdbool.h> #include <stdio.h> #include <syslog.h> @@ -27,6 +28,12 @@ void log_open(bool use_syslog); void log_close(void); void log_kmod(void *data, int priority, const char *file, int line, const char *fn, const char *format, va_list args); +void log_printf(int prio, const char *fmt, ...); +#define CRIT(...) log_printf(LOG_CRIT, __VA_ARGS__) +#define ERR(...) log_printf(LOG_ERR, __VA_ARGS__) +#define WRN(...) log_printf(LOG_WARNING, __VA_ARGS__) +#define INF(...) log_printf(LOG_INFO, __VA_ARGS__) +#define DBG(...) log_printf(LOG_DEBUG, __VA_ARGS__) struct kmod_ctx; void log_setup_kmod_log(struct kmod_ctx *ctx, int priority); diff --git a/tools/modinfo.c b/tools/modinfo.c index f770ec9..8cfe381 100644 --- a/tools/modinfo.c +++ b/tools/modinfo.c @@ -30,9 +30,6 @@ #include "kmod.h" -#define LOGPREFIX "modinfo: " -#define ERR(...) fprintf(stderr, LOGPREFIX "ERROR: " __VA_ARGS__) - static char separator = '\n'; static const char *field = NULL; diff --git a/tools/modprobe.c b/tools/modprobe.c index 92fd9da..4b30759 100644 --- a/tools/modprobe.c +++ b/tools/modprobe.c @@ -29,7 +29,6 @@ #include <sys/utsname.h> #include <sys/wait.h> #include <unistd.h> -#include <syslog.h> #include <limits.h> #include "libkmod.h" @@ -40,6 +39,7 @@ static int log_priority = LOG_CRIT; static int use_syslog = 0; +#define LOG(...) log_printf(log_priority, __VA_ARGS__) #define DEFAULT_VERBOSE LOG_WARNING static int verbose = DEFAULT_VERBOSE; @@ -152,39 +152,6 @@ static inline void _show(const char *fmt, ...) fflush(stdout); va_end(args); } - -static void _log(int prio, const char *fmt, ...) -{ - const char *prioname; - char *msg; - va_list args; - - if (prio > verbose) - return; - - va_start(args, fmt); - if (vasprintf(&msg, fmt, args) < 0) - msg = NULL; - va_end(args); - if (msg == NULL) - return; - - prioname = prio_to_str(prio); - - if (use_syslog) - syslog(prio, "%s: %s", prioname, msg); - else - fprintf(stderr, "modprobe: %s: %s", prioname, msg); - free(msg); - - if (prio <= LOG_CRIT) - exit(EXIT_FAILURE); -} -#define ERR(...) _log(LOG_ERR, __VA_ARGS__) -#define WRN(...) _log(LOG_WARNING, __VA_ARGS__) -#define INF(...) _log(LOG_INFO, __VA_ARGS__) -#define DBG(...) _log(LOG_DEBUG, __VA_ARGS__) -#define LOG(...) _log(log_priority, __VA_ARGS__) #define SHOW(...) _show(__VA_ARGS__) static int show_config(struct kmod_ctx *ctx) diff --git a/tools/rmmod.c b/tools/rmmod.c index e39a1f0..7be4604 100644 --- a/tools/rmmod.c +++ b/tools/rmmod.c @@ -25,7 +25,6 @@ #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> -#include <syslog.h> #include "libkmod.h" #include "macro.h" @@ -61,35 +60,6 @@ static void help(void) binname); } -static void _log(int prio, const char *fmt, ...) -{ - const char *prioname; - char *msg; - va_list args; - - if (prio > verbose) - return; - - va_start(args, fmt); - if (vasprintf(&msg, fmt, args) < 0) - msg = NULL; - va_end(args); - if (msg == NULL) - return; - - prioname = prio_to_str(prio); - - if (use_syslog) - syslog(prio, "%s: %s", prioname, msg); - else - fprintf(stderr, "rmmod: %s: %s", prioname, msg); - free(msg); - - if (prio <= LOG_CRIT) - exit(EXIT_FAILURE); -} -#define ERR(...) _log(LOG_ERR, __VA_ARGS__) - static int check_module_inuse(struct kmod_module *mod) { struct kmod_list *holders; |