Re-import of fprobe-ulog
[fprobe-ulog.git] / trunk / src / my_log.c
1 /*
2         Copyright (C) Slava Astashonok <sla@0n.ru>
3
4         This program is free software; you can redistribute it and/or
5         modify it under the terms of the GNU General Public License.
6
7         $Id: my_log.c,v 1.3.2.2 2004/02/02 08:06:24 sla Exp $
8 */
9
10 #include <common.h>
11
12 #include <syslog.h>
13 #include <stdarg.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16
17 #include <my_log.h>
18
19 static char *my_log_indent;
20 static unsigned my_log_min_level;
21 static unsigned my_log_flags;
22 static char *my_log_names[] = {
23         "EMERG", "ALERT", "CRIT", "ERR", "WARNING", "NOTICE", "INFO", "DEBUG"
24 };
25
26 void my_log_open(char *indent, unsigned min_level, unsigned flags)
27 {
28         my_log_indent = indent;
29         my_log_min_level = min_level;
30         my_log_flags = flags;
31         //openlog(0, LOG_PID, MY_LOG_SYSLOG_FACILITY);
32         openlog(my_log_indent, 0, MY_LOG_SYSLOG_FACILITY);
33 }
34
35 void my_log_close(void)
36 {
37         closelog();
38 }
39
40 void my_log(unsigned level, const char *format, ...)
41 {
42         va_list args;
43         char msg[256];
44         char msg_prefix[64];
45
46         if (level <= my_log_min_level) {
47                 va_start(args, format);
48                 vsnprintf(msg, sizeof(msg), format, args);
49                 snprintf(msg_prefix, sizeof(msg_prefix), "[%s]: ", my_log_names[level]);
50
51                 if (my_log_flags & MY_LOG_SYSLOG)
52                         syslog(level, "%s%s", msg_prefix, msg);
53
54                 if (my_log_flags & MY_LOG_STDOUT)
55                         fprintf(stdout, "%s%s\n", msg_prefix, msg);
56         }
57 }