]> git.neil.brown.name Git - metad.git/blob - error.c
Assorted reformating
[metad.git] / error.c
1
2 #define IN_ERROR
3 #include        "metad.h"
4 #include        <stdio.h>
5 #include        <stdarg.h>
6 #define STDARGS
7 /* following for irix6 !! */
8 #define _VA_FP_SAVE_AREA 0
9 static int error_dest = ERROR_STDERR;
10 static char **error_str = NULL;
11 int err_str_len;
12
13 int errors_to(int where, char **place)
14 {
15         int rv = error_dest;
16         error_dest = where;
17         if (where == ERROR_STRING)
18                 error_str = place;
19         if (rv == ERROR_SYSLOG && where != ERROR_SYSLOG)
20                 closelog();
21         if (where == ERROR_SYSLOG && rv != ERROR_SYSLOG)
22         {
23 #ifdef LOG_DAEMON
24                 openlog("metad", LOG_PID, LOG_DAEMON);
25 #else
26                 openlog("metad", 0);
27 #endif
28         }
29         return rv;
30 }
31
32 void error(char *mesg, char *a, char *b, char *c)
33 {
34         char buf[1024];
35
36         sprintf(buf, mesg, a, b, c);
37
38         switch(error_dest)
39         {
40         case ERROR_STDERR:
41                 fprintf(stderr, "metad: %s\n", buf);
42                 break;
43         case ERROR_STRING:
44                 if (*error_str == NULL)
45                 {
46                         *error_str = (char*)malloc(err_str_len=(strlen(buf)+100));
47                         strcpy(*error_str, buf);
48                 }
49                 else if (strlen(*error_str)+strlen(buf)+1 > err_str_len)
50                 {
51                         *error_str = (char*)realloc(*error_str, err_str_len += strlen(buf)+100);
52                         strcat(*error_str, buf);
53                 }
54                 else
55                         strcat(*error_str, buf);
56                 break;
57         case ERROR_SYSLOG:
58                 syslog(LOG_ERR, "%s", buf);
59                 break;
60         }
61 }
62
63 #ifdef STDARGS
64 void logmsg(int level,...)
65 #else
66 void logmsg(va_alist)
67 va_dcl
68 #endif
69 {
70         va_list pvar;
71         char buf[1024];
72         char *format;
73
74 #ifdef STDARGS
75         va_start(pvar, level);
76 #else
77         int level;
78         va_start(pvar);
79         level = va_arg(pvar, int);
80 #endif
81         format = va_arg(pvar, char *);
82         vsprintf(buf, format, pvar);
83         switch(error_dest)
84         {
85         case ERROR_STDERR:
86                 fprintf(stderr, "metad: %s\n", buf);
87                 break;
88         case ERROR_STRING:
89                 if (*error_str == NULL)
90                 {
91                         *error_str = (char*)malloc(err_str_len=(strlen(buf)+100));
92                         strcpy(*error_str, buf);
93                 }
94                 else if (strlen(*error_str)+strlen(buf)+1 > err_str_len)
95                 {
96                         *error_str = (char*)realloc(*error_str, err_str_len += strlen(buf)+100);
97                         strcat(*error_str, buf);
98                 }
99                 else
100                         strcat(*error_str, buf);
101                 break;
102         case ERROR_SYSLOG:
103                 syslog(level, "%s", buf);
104                 break;
105         }
106 }
107
108 void dolog(service_t sv, proc_t p, char *buf)
109 {
110         logmsg(LOG_INFO, "%s: %d: %s\n", sv->service, p->pid, buf);
111 }
112