]> andersk Git - moira.git/blob - update/log.c
second code style cleanup: void/void * usage, proper #includes. try to
[moira.git] / update / log.c
1 /* $Id$
2  *
3  * handle logging for dcm and update server
4  *
5  * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology.
6  * For copying and distribution information, please see the file
7  * <mit-copyright.h>.
8  */
9
10 #include <mit-copyright.h>
11 #include <moira.h>
12 #include "update_server.h"
13
14 #include <stdio.h>
15 #include <stdarg.h>
16
17 #include "update.h"
18
19 #ifdef use_syslog
20 #include <syslog.h>
21 #else
22 #define use_tty
23 #endif
24
25 RCSID("$Header$");
26
27 void mr_update_com_err_hook(const char *whoami, long code,
28                             const char *fmt, va_list args);
29
30 #ifdef use_syslog
31 int syslog_prio[] = {
32   LOG_DEBUG,
33   LOG_INFO,
34   LOG_WARNING,
35   LOG_ERR
36 };
37 #endif
38 extern int log_priority;
39 extern char *whoami;
40
41 void mr_update_com_err_hook(const char *whoami, long code,
42                             const char *fmt, va_list args)
43 {
44   char buf[BUFSIZ], *cp;
45
46 #ifndef use_syslog
47   strcpy(buf, whoami);
48   for (cp = buf; *cp; cp++)
49     ;
50   *cp++ = ':';
51   *cp++ = ' ';
52 #else
53   cp = buf;
54   *cp = '\0';
55 #endif
56   if (code)
57     {
58       strcpy(cp, error_message(code));
59       while (*cp)
60         cp++;
61     }
62   vsprintf(cp, fmt, args);
63 #ifdef use_syslog
64   syslog(syslog_prio[log_priority], "%s", buf);
65 #endif
66 #ifdef use_tty
67   puts(buf);
68 #endif
69 }
70
71 void mr_update_initialize(void)
72 {
73   static int initialized = 0;
74   if (initialized)
75     return;
76 #ifdef use_syslog
77   openlog(whoami, LOG_PID, LOG_DAEMON);
78 #endif
79   set_com_err_hook(mr_update_com_err_hook);
80   log_priority = log_INFO;
81   initialized = 1;
82 }
83
84
85 static char fmt[] = "[%s] %s";
86
87 #define def(name, level, prio) \
88 void name(char *msg)\
89 {\
90    int old_prio; \
91    old_prio = log_priority; \
92    mr_update_initialize(); \
93    com_err(whoami, 0, fmt, level, msg); \
94    log_priority = old_prio; \
95 }
96
97 def(mr_log_error, "error", log_ERROR)
98 def(mr_log_warning, "warning", log_WARNING)
99 def(mr_log_info, "info", log_INFO)
100 def(mr_debug, "debug", log_DEBUG)
This page took 0.04274 seconds and 5 git commands to generate.