Documentation fixes.
[virt-hostinfo.git] / hostinfod / error.c
1 /* virt-hostinfo
2  * Copyright (C) 2009 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <unistd.h>
27 #include <syslog.h>
28
29 #include <apr_general.h>
30 #include <apr_errno.h>
31
32 #include "hostinfod.h"
33
34 void
35 init_syslog (void)
36 {
37   openlog ("hostinfo", 0, LOG_DAEMON);
38 }
39
40 static void
41 print (int level, const char *msg)
42 {
43   const char *errwarn;
44   switch (level) {
45   case LOG_WARNING: errwarn = "warning"; break;
46   case LOG_ERR:     errwarn = "error"; break;
47   default:          errwarn = "info";
48   }
49
50   if (messages_to_stderr)
51     fprintf (stderr, "hostinfo: %s: %s\n", errwarn, msg);
52   syslog (LOG_DAEMON|level, "%s: %s", errwarn, msg);
53 }
54
55 void
56 debug (const char *fs, ...)
57 {
58   va_list args;
59   char *msg;
60
61   if (verbose) {
62     va_start (args, fs);
63     int err = vasprintf (&msg, fs, args);
64     va_end (args);
65
66     if (err < 0) return;
67
68     print (LOG_INFO, msg);
69     free (msg);
70   }
71 }
72
73 void
74 message (const char *fs, ...)
75 {
76   va_list args;
77   char *msg;
78
79   va_start (args, fs);
80   int err = vasprintf (&msg, fs, args);
81   va_end (args);
82
83   if (err < 0) return;
84
85   print (LOG_INFO, msg);
86   free (msg);
87 }
88
89 void
90 error (const char *fs, ...)
91 {
92   va_list args;
93   char *msg;
94
95   va_start (args, fs);
96   int err = vasprintf (&msg, fs, args);
97   va_end (args);
98
99   if (err < 0) return;
100
101   print (LOG_ERR, msg);
102   free (msg);
103 }
104
105 void
106 warning (const char *fs, ...)
107 {
108   va_list args;
109   char *msg;
110
111   va_start (args, fs);
112   int err = vasprintf (&msg, fs, args);
113   va_end (args);
114
115   if (err < 0) return;
116
117   print (LOG_WARNING, msg);
118   free (msg);
119 }
120
121 void
122 perrorf (const char *fs, ...)
123 {
124   va_list args;
125   char *msg;
126   int errnum = errno;
127
128   va_start (args, fs);
129   int err = vasprintf (&msg, fs, args);
130   va_end (args);
131
132   if (err < 0) return;
133
134 #ifndef _GNU_SOURCE
135   char buf[256];
136   strerror_r (errnum, buf, sizeof buf);
137 #else
138   char _buf[256];
139   char *buf;
140   buf = strerror_r (errnum, _buf, sizeof _buf);
141 #endif
142
143   msg = realloc (msg, strlen (msg) + 2 + strlen (buf) + 1);
144   strcat (msg, ": ");
145   strcat (msg, buf);
146
147   print (LOG_ERR, msg);
148   free (msg);
149 }
150
151 void
152 pwarningf (const char *fs, ...)
153 {
154   va_list args;
155   char *msg;
156   int errnum = errno;
157
158   va_start (args, fs);
159   int err = vasprintf (&msg, fs, args);
160   va_end (args);
161
162   if (err < 0) return;
163
164 #ifndef _GNU_SOURCE
165   char buf[256];
166   strerror_r (errnum, buf, sizeof buf);
167 #else
168   char _buf[256];
169   char *buf;
170   buf = strerror_r (errnum, _buf, sizeof _buf);
171 #endif
172
173   msg = realloc (msg, strlen (msg) + 2 + strlen (buf) + 1);
174   strcat (msg, ": ");
175   strcat (msg, buf);
176
177   print (LOG_WARNING, msg);
178   free (msg);
179 }
180
181 void
182 paprerror (apr_status_t r, const char *fs, ...)
183 {
184   va_list args;
185   char *msg;
186   char buf[256];
187
188   va_start (args, fs);
189   int err = vasprintf (&msg, fs, args);
190   va_end (args);
191
192   if (err < 0) return;
193
194   apr_strerror (r, buf, sizeof buf);
195
196   msg = realloc (msg, strlen (msg) + 2 + strlen (buf) + 1);
197   strcat (msg, ": ");
198   strcat (msg, buf);
199
200   print (LOG_ERR, msg);
201   free (msg);
202 }