]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
tools: share function for logging
authorLucas De Marchi <lucas.demarchi@profusion.mobi>
Tue, 6 Nov 2012 21:01:59 +0000 (19:01 -0200)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Tue, 6 Nov 2012 21:02:23 +0000 (19:02 -0200)
tools/depmod.c
tools/insmod.c
tools/log.c
tools/log.h
tools/modinfo.c
tools/modprobe.c
tools/rmmod.c

index 8741d0607b16f9236da7d459472d4bddaec04932..7d36a50945dc48d93c13ec6b0a843f71c00dbde0 100644 (file)
@@ -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__)
 
 
index c05a6fc0d9fae94111eae54dc9cc0facc49c6f03..f358b93774bd47b219ffa240ab3195cc08dc5e74 100644 (file)
@@ -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'},
index 229f41cc441736d50c06c8a2783345ae3de7d085..c6887bd75b1b31063ae5d98570019be7b3eb9133 100644 (file)
@@ -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;
index d4fbcdf5e854663fbe2e4a5c0ba2c61772c9ec27..90817a38d854946edb36a413f6756f19e1511170 100644 (file)
@@ -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);
index f770ec9b93b2a084fa5c5d6e7849c8f6efd68a80..8cfe381ff1f5ee043c404d2a1b8683a6229f711f 100644 (file)
@@ -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;
 
index 92fd9da237dd0c2cc8a2bf0028f3dfabe57aa23f..4b30759ef0ef9e632bb971757d263052e691651d 100644 (file)
@@ -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)
index e39a1f035413fa463401260ac3a3caeb564156c7..7be460417e60da8ef514d56effa20414f9b083bc 100644 (file)
@@ -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;