blob: c6887bd75b1b31063ae5d98570019be7b3eb9133 [file] [log] [blame]
Lucas De Marchi92aad742012-11-06 18:04:09 -02001/*
2 * kmod - log infrastructure
3 *
4 * Copyright (C) 2012 ProFUSION embedded systems
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <syslog.h>
23
Lucas De Marchi52a50fe2012-11-06 18:26:34 -020024#include "libkmod.h"
Lucas De Marchi92aad742012-11-06 18:04:09 -020025#include "kmod.h"
26
27static bool log_use_syslog;
Lucas De Marchi52a50fe2012-11-06 18:26:34 -020028static int log_priority = LOG_ERR;
Lucas De Marchi92aad742012-11-06 18:04:09 -020029
30void log_open(bool use_syslog)
31{
32 log_use_syslog = use_syslog;
33
34 if (log_use_syslog)
35 openlog(binname, LOG_CONS, LOG_DAEMON);
36}
37
38void log_close(void)
39{
40 if (log_use_syslog)
41 closelog();
42}
43
44void log_kmod(void *data, int priority, const char *file, int line,
45 const char *fn, const char *format, va_list args)
46{
47 const char *prioname = prio_to_str(priority);
48 char *str;
49
50 if (vasprintf(&str, format, args) < 0)
51 return;
52
53 if (log_use_syslog) {
54#ifdef ENABLE_DEBUG
55 syslog(priority, "%s: %s:%d %s() %s", prioname, file, line,
56 fn, str);
57#else
58 syslog(priority, "%s: %s", prioname, str);
59#endif
60 } else {
61#ifdef ENABLE_DEBUG
62 fprintf(stderr, "%s: %s: %s:%d %s() %s", binname, prioname,
63 file, line, fn, str);
64#else
65 fprintf(stderr, "%s: %s: %s", binname, prioname, str);
66#endif
67 }
68
69 free(str);
70 (void)data;
71}
Lucas De Marchi52a50fe2012-11-06 18:26:34 -020072
Lucas De Marchifcb0ce92012-11-06 19:01:59 -020073void log_printf(int prio, const char *fmt, ...)
74{
75 const char *prioname;
76 char *msg;
77 va_list args;
78
79 if (prio > log_priority)
80 return;
81
82 va_start(args, fmt);
83 if (vasprintf(&msg, fmt, args) < 0)
84 msg = NULL;
85 va_end(args);
86 if (msg == NULL)
87 return;
88
89 prioname = prio_to_str(prio);
90
91 if (log_use_syslog)
92 syslog(prio, "%s: %s", prioname, msg);
93 else
94 fprintf(stderr, "%s: %s: %s", binname, prioname, msg);
95 free(msg);
96
97 if (prio <= LOG_CRIT)
98 exit(EXIT_FAILURE);
99}
100
Lucas De Marchi52a50fe2012-11-06 18:26:34 -0200101void log_setup_kmod_log(struct kmod_ctx *ctx, int priority)
102{
103 log_priority = priority;
104
105 kmod_set_log_priority(ctx, log_priority);
106 kmod_set_log_fn(ctx, log_kmod, NULL);
107}