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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20