From: Lucas De Marchi Date: Tue, 6 Nov 2012 20:04:09 +0000 (-0200) Subject: tools: use a single function for logging libkmod output X-Git-Tag: v11~6 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=92aad74925f788097b54a3f45392e7d272c0201b;p=platform%2Fupstream%2Fkmod.git tools: use a single function for logging libkmod output --- diff --git a/Makefile.am b/Makefile.am index d49f360..6dc4d8c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -108,7 +108,7 @@ noinst_SCRIPTS = tools/insmod tools/rmmod tools/lsmod \ tools_kmod_SOURCES = tools/kmod.c tools/kmod.h tools/lsmod.c \ tools/rmmod.c tools/insmod.c \ tools/modinfo.c tools/modprobe.c \ - tools/depmod.c tools/log.h + tools/depmod.c tools/log.h tools/log.c tools_kmod_LDADD = libkmod/libkmod-util.la \ libkmod/libkmod.la diff --git a/tools/log.c b/tools/log.c new file mode 100644 index 0000000..4ca4687 --- /dev/null +++ b/tools/log.c @@ -0,0 +1,69 @@ +/* + * kmod - log infrastructure + * + * Copyright (C) 2012 ProFUSION embedded systems + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include + +#include "kmod.h" + +static bool log_use_syslog; + +void log_open(bool use_syslog) +{ + log_use_syslog = use_syslog; + + if (log_use_syslog) + openlog(binname, LOG_CONS, LOG_DAEMON); +} + +void log_close(void) +{ + if (log_use_syslog) + closelog(); +} + +void log_kmod(void *data, int priority, const char *file, int line, + const char *fn, const char *format, va_list args) +{ + const char *prioname = prio_to_str(priority); + char *str; + + if (vasprintf(&str, format, args) < 0) + return; + + if (log_use_syslog) { +#ifdef ENABLE_DEBUG + syslog(priority, "%s: %s:%d %s() %s", prioname, file, line, + fn, str); +#else + syslog(priority, "%s: %s", prioname, str); +#endif + } else { +#ifdef ENABLE_DEBUG + fprintf(stderr, "%s: %s: %s:%d %s() %s", binname, prioname, + file, line, fn, str); +#else + fprintf(stderr, "%s: %s: %s", binname, prioname, str); +#endif + } + + free(str); + (void)data; +} diff --git a/tools/log.h b/tools/log.h index 3527bd9..6005029 100644 --- a/tools/log.h +++ b/tools/log.h @@ -17,14 +17,22 @@ * along with this program. If not, see . */ +#include #include #include #include "kmod.h" +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); + _always_inline_ const char *prio_to_str(int prio); +/* inline functions */ + _always_inline_ const char *prio_to_str(int prio) { const char *prioname; diff --git a/tools/modprobe.c b/tools/modprobe.c index 0dea878..78bbd22 100644 --- a/tools/modprobe.c +++ b/tools/modprobe.c @@ -153,35 +153,6 @@ static inline void _show(const char *fmt, ...) va_end(args); } -static void log_modprobe(void *data, int priority, const char *file, int line, - const char *fn, const char *format, va_list args) -{ - const char *prioname = prio_to_str(priority); - char *str; - - if (vasprintf(&str, format, args) < 0) - return; - - if (use_syslog > 1) { -#ifdef ENABLE_DEBUG - syslog(priority, "%s: %s:%d %s() %s", prioname, file, line, - fn, str); -#else - syslog(priority, "%s: %s", prioname, str); -#endif - } else { -#ifdef ENABLE_DEBUG - fprintf(stderr, "modprobe: %s: %s:%d %s() %s", prioname, file, - line, fn, str); -#else - fprintf(stderr, "modprobe: %s: %s", prioname, str); -#endif - } - - free(str); - (void)data; -} - static void _log(int prio, const char *fmt, ...) { const char *prioname; @@ -200,7 +171,7 @@ static void _log(int prio, const char *fmt, ...) prioname = prio_to_str(prio); - if (use_syslog > 1) + if (use_syslog) syslog(prio, "%s: %s", prioname, msg); else fprintf(stderr, "modprobe: %s: %s", prioname, msg); @@ -923,10 +894,7 @@ static int do_modprobe(int argc, char **orig_argv) args = argv + optind; nargs = argc - optind; - if (use_syslog) { - openlog("modprobe", LOG_CONS, LOG_DAEMON); - use_syslog++; - } + log_open(use_syslog); if (!do_show_config) { if (nargs == 0) { @@ -962,7 +930,7 @@ static int do_modprobe(int argc, char **orig_argv) } kmod_set_log_priority(ctx, verbose); - kmod_set_log_fn(ctx, log_modprobe, NULL); + kmod_set_log_fn(ctx, log_kmod, NULL); kmod_load_resources(ctx); @@ -986,8 +954,7 @@ static int do_modprobe(int argc, char **orig_argv) kmod_unref(ctx); done: - if (use_syslog > 1) - closelog(); + log_close(); if (argv != orig_argv) free(argv); diff --git a/tools/rmmod.c b/tools/rmmod.c index 689675b..f7201ae 100644 --- a/tools/rmmod.c +++ b/tools/rmmod.c @@ -61,35 +61,6 @@ static void help(void) binname); } -static void log_rmmod(void *data, int priority, const char *file, int line, - const char *fn, const char *format, va_list args) -{ - const char *prioname = prio_to_str(priority); - char *str; - - if (vasprintf(&str, format, args) < 0) - return; - - if (use_syslog > 1) { -#ifdef ENABLE_DEBUG - syslog(priority, "%s: %s:%d %s() %s", prioname, file, line, - fn, str); -#else - syslog(priority, "%s: %s", prioname, str); -#endif - } else { -#ifdef ENABLE_DEBUG - fprintf(stderr, "rmmod: %s: %s:%d %s() %s", prioname, file, - line, fn, str); -#else - fprintf(stderr, "rmmod: %s: %s", prioname, str); -#endif - } - - free(str); - (void)data; -} - static void _log(int prio, const char *fmt, ...) { const char *prioname; @@ -108,7 +79,7 @@ static void _log(int prio, const char *fmt, ...) prioname = prio_to_str(prio); - if (use_syslog > 1) + if (use_syslog) syslog(prio, "%s: %s", prioname, msg); else fprintf(stderr, "rmmod: %s: %s", prioname, msg); @@ -194,10 +165,7 @@ static int do_rmmod(int argc, char *argv[]) } } - if (use_syslog) { - openlog("rmmod", LOG_CONS, LOG_DAEMON); - use_syslog++; - } + log_open(use_syslog); if (optind >= argc) { ERR("missing module name.\n"); @@ -213,7 +181,7 @@ static int do_rmmod(int argc, char *argv[]) } kmod_set_log_priority(ctx, kmod_get_log_priority(ctx)); - kmod_set_log_fn(ctx, log_rmmod, NULL); + kmod_set_log_fn(ctx, log_kmod, NULL); for (i = optind; i < argc; i++) { struct kmod_module *mod; @@ -249,8 +217,7 @@ next: kmod_unref(ctx); done: - if (use_syslog > 1) - closelog(); + log_close(); return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE; }