/[path-sandbox]/trunk/libsbutil/src/debug.c
Gentoo

Contents of /trunk/libsbutil/src/debug.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 370 - (show annotations) (download) (as text)
Sun Nov 9 10:29:58 2008 UTC (5 years, 8 months ago) by vapier
File MIME type: text/x-csrc
File size: 5905 byte(s)
consolidate all random system includes into headers.h
1 /*
2 * debug.c
3 *
4 * Simle debugging/logging macro's and functions.
5 *
6 * Copyright 1999-2008 Gentoo Foundation
7 * Copyright 2004-2007 Martin Schlemmer <azarah@nosferatu.za.org>
8 * Licensed under the GPL-2
9 */
10
11 #include "headers.h"
12 #include "rcscripts/rcutil.h"
13
14 volatile static bool debug_enabled = TRUE;
15 volatile static int debug_errno = 0;
16
17 static char log_domain_default[] = "rcscripts";
18 static char *log_domain = log_domain_default;
19
20 void
21 rc_log_domain (const char *new_domain)
22 {
23 if (check_str (new_domain))
24 log_domain = (char *)new_domain;
25 }
26
27 void
28 rc_debug_enabled (bool enabled)
29 {
30 debug_enabled = enabled;
31 }
32
33 void
34 rc_errno_set (int rc_errno)
35 {
36 if (rc_errno >= 0)
37 debug_errno = rc_errno;
38 }
39
40 void
41 rc_errno_clear (void)
42 {
43 debug_errno = 0;
44 }
45
46 int
47 rc_errno_get (void)
48 {
49 return debug_errno;
50 }
51
52 bool
53 rc_errno_is_set (void)
54 {
55 return (debug_errno > 0);
56 }
57
58 void
59 debug_message (const char *file, const char *func, int line,
60 const char *format, ...)
61 {
62 va_list arg;
63 char *format_str;
64 int length;
65
66 #if !defined(RC_DEBUG)
67 if (!debug_enabled)
68 return;
69 #endif
70
71 length = strlen (log_domain) + strlen ("(): ") + 1;
72 /* Do not use xmalloc() here, else we may have recursive issues */
73 format_str = malloc (length);
74 if (NULL == format_str)
75 {
76 fprintf (stderr, "(%s) error: in %s, function %s(), line %i:\n",
77 log_domain, __FILE__, __func__, __LINE__);
78 fprintf (stderr, "(%s) Failed to allocate buffer!\n",
79 log_domain);
80 abort ();
81 }
82
83 snprintf (format_str, length, "(%s) ", log_domain);
84
85 va_start (arg, format);
86
87 #if !defined(RC_DEBUG)
88 /* Bit of a hack, as how we do things tend to cause seek
89 * errors when reading the parent/child pipes */
90 /* if ((0 != errno) && (ESPIPE != errno)) { */
91 if (rc_errno_is_set ())
92 {
93 #endif
94 if (rc_errno_is_set ())
95 fprintf (stderr, "(%s) error: ", log_domain);
96 else
97 fprintf (stderr, "(%s) debug: ", log_domain);
98
99 fprintf (stderr, "in %s, function %s(), line %i:\n", file, func, line);
100
101 if (rc_errno_is_set ())
102 fprintf (stderr, "%s strerror() = '%s'\n", format_str, strerror (rc_errno_get ()));
103
104 fprintf (stderr, "%s ", format_str);
105 vfprintf (stderr, format, arg);
106 #if !defined(RC_DEBUG)
107 }
108 #endif
109
110 va_end (arg);
111
112 free (format_str);
113 }
114
115 inline bool
116 check_ptr (const void *ptr)
117 {
118 if (NULL == ptr)
119 return FALSE;
120
121 return TRUE;
122 }
123
124 inline bool
125 check_str (const char *str)
126 {
127 if ((NULL == str) || (0 == strlen (str)))
128 return FALSE;
129
130 return TRUE;
131 }
132
133 inline bool
134 check_strv (char **str)
135 {
136 if ((NULL == str) || (NULL == *str) || (0 == strlen (*str)))
137 return FALSE;
138
139 return TRUE;
140 }
141
142 inline bool
143 check_fd (int fd)
144 {
145 if ((0 >= fd) || (-1 == fcntl (fd, F_GETFL)))
146 return FALSE;
147
148 return TRUE;
149 }
150
151 inline bool
152 check_fp (FILE *fp)
153 {
154 if ((NULL == fp) || (-1 == fileno (fp)))
155 return FALSE;
156
157 return TRUE;
158 }
159
160 inline bool
161 __check_arg_ptr (const void *ptr, const char *file, const char *func, size_t line)
162 {
163 if (!check_ptr (ptr))
164 {
165 rc_errno_set (EINVAL);
166
167 debug_message (file, func, line, "Invalid pointer passed!\n");
168
169 return FALSE;
170 }
171
172 return TRUE;
173 }
174
175 inline bool
176 __check_arg_str (const char *str, const char *file, const char *func, size_t line)
177 {
178 if (!check_str (str))
179 {
180 rc_errno_set (EINVAL);
181
182 debug_message (file, func, line, "Invalid string passed!\n");
183
184 return FALSE;
185 }
186
187 return TRUE;
188 }
189
190 inline bool
191 __check_arg_strv (char **str, const char *file, const char *func, size_t line)
192 {
193 if (!check_strv (str))
194 {
195 rc_errno_set (EINVAL);
196
197 debug_message (file, func, line, "Invalid string array passed!\n");
198
199 return FALSE;
200 }
201
202 return TRUE;
203 }
204
205 inline bool
206 __check_arg_fd (int fd, const char *file, const char *func, size_t line)
207 {
208 if (!check_fd (fd))
209 {
210 rc_errno_set (EBADF);
211
212 debug_message (file, func, line, "Invalid file descriptor passed!\n");
213
214 return FALSE;
215 }
216
217 return TRUE;
218 }
219
220 inline bool
221 __check_arg_fp (FILE *fp, const char *file, const char *func, size_t line)
222 {
223 if (!check_fp (fp))
224 {
225 rc_errno_set (EBADF);
226
227 debug_message (file, func, line, "Invalid file descriptor passed!\n");
228
229 return FALSE;
230 }
231
232 return TRUE;
233 }
234
235 inline void *
236 __xcalloc(size_t nmemb, size_t size, const char *file,
237 const char *func, size_t line)
238 {
239 void *new_ptr;
240
241 new_ptr = calloc (nmemb, size);
242 if (NULL == new_ptr)
243 {
244 /* Set errno in case specific malloc() implementation does not */
245 rc_errno_set (ENOMEM);
246
247 debug_message (file, func, line, "Failed to allocate buffer!\n");
248
249 return NULL;
250 }
251
252 return new_ptr;
253 }
254
255 inline void *
256 __xmalloc (size_t size, const char *file, const char *func, size_t line)
257 {
258 void *new_ptr;
259
260 new_ptr = malloc (size);
261 if (NULL == new_ptr)
262 {
263 /* Set errno in case specific malloc() implementation does not */
264 rc_errno_set (ENOMEM);
265
266 debug_message (file, func, line, "Failed to allocate buffer!\n");
267
268 return NULL;
269 }
270
271 return new_ptr;
272 }
273
274 inline void *
275 __xrealloc (void *ptr, size_t size, const char *file,
276 const char *func, size_t line)
277 {
278 void *new_ptr;
279
280 new_ptr = realloc (ptr, size);
281 if (NULL == new_ptr)
282 {
283 /* Set errno in case specific realloc() implementation does not */
284 rc_errno_set (ENOMEM);
285
286 debug_message (file, func, line, "Failed to reallocate buffer!\n");
287
288 return NULL;
289 }
290
291 return new_ptr;
292 }
293
294 inline char *
295 __xstrndup (const char *str, size_t size, const char *file,
296 const char *func, size_t line)
297 {
298 char *new_ptr;
299
300 new_ptr = rc_strndup (str, size);
301 if (NULL == new_ptr)
302 {
303 /* Set errno in case specific realloc() implementation does not */
304 rc_errno_set (ENOMEM);
305
306 debug_message (file, func, line,
307 "Failed to duplicate string via rc_strndup() !\n");
308
309 return NULL;
310 }
311
312 return new_ptr;
313 }

  ViewVC Help
Powered by ViewVC 1.1.20