2 * kmod - log infrastructure
4 * Copyright (C) 2012-2013 ProFUSION embedded systems
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include <libkmod/libkmod.h>
29 #define PRIO_MAX_SIZE 32
31 static bool log_use_syslog;
32 static int log_priority = LOG_WARNING;
34 static const char *prio_to_str(char buf[static PRIO_MAX_SIZE], int prio)
58 snprintf(buf, PRIO_MAX_SIZE, "LOG-%03d", prio);
66 static void log_kmod(void *data, int priority, const char *file, int line,
67 const char *fn, const char *format, va_list args)
69 char buf[PRIO_MAX_SIZE];
73 prioname = prio_to_str(buf, priority);
75 if (vasprintf(&str, format, args) < 0)
80 syslog(priority, "%s: %s:%d %s() %s", prioname, file, line,
83 syslog(priority, "%s: %s", prioname, str);
87 fprintf(stderr, "%s: %s: %s:%d %s() %s",
88 program_invocation_short_name, prioname, file, line,
91 fprintf(stderr, "%s: %s: %s", program_invocation_short_name,
99 void log_open(bool use_syslog)
101 log_use_syslog = use_syslog;
104 openlog(program_invocation_short_name, LOG_CONS, LOG_DAEMON);
113 void log_printf(int prio, const char *fmt, ...)
115 char buf[PRIO_MAX_SIZE];
116 const char *prioname;
120 if (prio > log_priority)
124 if (vasprintf(&msg, fmt, args) < 0)
130 prioname = prio_to_str(buf, prio);
133 syslog(prio, "%s: %s", prioname, msg);
135 fprintf(stderr, "%s: %s: %s", program_invocation_short_name,
139 if (prio <= LOG_CRIT)
143 void log_setup_kmod_log(struct kmod_ctx *ctx, int priority)
145 log_priority = priority;
147 kmod_set_log_priority(ctx, log_priority);
148 kmod_set_log_fn(ctx, log_kmod, NULL);