add a PLANNED_TEST() macro to test.h
[nobug.git] / src / nobug.c
blobf24f7f0eeb0bbcc60b355110b6f5549d76a627a5
1 /*
2 This file is part of the NoBug debugging library.
4 Copyright (C)
5 2007, 2008, 2009, 2010, Christian Thaeter <ct@pipapo.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, contact Christian Thaeter <ct@pipapo.org>.
21 #if NOBUG_USE_PTHREAD
22 #include <pthread.h>
23 #endif
25 #define NOBUG_LIBNOBUG_C
26 #include "nobug.h"
28 struct nobug_ringbuffer nobug_default_ringbuffer;
29 FILE* nobug_default_file = NULL;
30 unsigned long long nobug_counter = 0;
31 nobug_logging_cb nobug_logging_callback = NULL;
32 nobug_logging_cb nobug_postlogging_callback = NULL;
33 nobug_abort_cb nobug_abort_callback = NULL;
34 void* nobug_callback_data = NULL;
38 //predefflags HEAD~ Predefined Flags;;
39 //predefflags
40 //predefflags There are some debugging flags which are predefined by NoBug.
41 //predefflags
42 //predefflags PARA NOBUG_ON; NOBUG_ON; log flag which is always enabled
43 //predefflags
44 //predefflags The flag `NOBUG_ON` is always enabled at LOG_DEBUG level. This is
45 //predefflags static and can not be changed.
46 //predefflags
48 struct nobug_flag nobug_flag_NOBUG_ON =
49 {NULL, NULL, 0, {LOG_DEBUG, LOG_DEBUG, LOG_DEBUG, LOG_DEBUG, LOG_DEBUG}, NULL, NULL, NULL};
52 //predefflags PARA NOBUG_ANN; NOBUG_ANN; log flag for annotations
53 //predefflags
54 //predefflags The flag `NOBUG_ANN` is used for the source annotations. This is
55 //predefflags static and can not be changed. It differs from `NOBUG_ON` as in
56 //predefflags never logging to syslog and only define a LOG_WARNING limit for the
57 //predefflags application callback.
58 //predefflags
60 struct nobug_flag nobug_flag_NOBUG_ANN =
61 {NULL, NULL, 0, {LOG_DEBUG, LOG_DEBUG, LOG_DEBUG, -1, LOG_WARNING}, NULL, NULL, NULL};
64 //predefflags PARA nobug (flag); nobug_flag; log flag used to show nobug actions
65 //predefflags
66 //predefflags Actions on NoBug itself will be logged under the `nobug` flag itself.
67 //predefflags When you want to see whats going on (useful to check if you call
68 //predefflags `NOBUG_INIT_FLAG()` on all flags) you can enable it with `NOBUG_LOG=nobug:TRACE`.
69 //predefflags
71 struct nobug_flag nobug_flag_nobug =
72 {"nobug", NULL, 0, {LOG_DEBUG, LOG_WARNING, LOG_ERR, LOG_ERR, -1}, NULL, NULL, NULL};
75 #if NOBUG_USE_PTHREAD
76 static void
77 nobug_tls_free (void * tls)
79 free((void*)((struct nobug_tls_data*)tls)->thread_id);
80 free(tls);
82 #endif
85 default initialization
87 static volatile int nobug_initialized = 0;
89 void
90 nobug_init (const struct nobug_context context)
92 if (nobug_initialized)
93 return;
94 nobug_initialized = 1;
96 #if NOBUG_USE_PTHREAD
97 pthread_key_create (&nobug_tls_key, nobug_tls_free);
98 #endif
100 /* we initialize a minimal ringbuffer, if not already done */
101 if (!nobug_default_ringbuffer.start)
102 nobug_ringbuffer_init (&nobug_default_ringbuffer, 4096, 4096, NULL, NOBUG_RINGBUFFER_DEFAULT);
104 /* initialize the always-on flag*/
105 nobug_flag_NOBUG_ON.ringbuffer_target = &nobug_default_ringbuffer;
106 nobug_flag_NOBUG_ON.console_target = stderr;
107 nobug_flag_NOBUG_ON.file_target = nobug_default_file;
108 nobug_flag_NOBUG_ON.initialized = 1;
110 nobug_flag_NOBUG_ANN.ringbuffer_target = &nobug_default_ringbuffer;
111 nobug_flag_NOBUG_ANN.console_target = stderr;
112 nobug_flag_NOBUG_ANN.file_target = nobug_default_file;
113 nobug_flag_NOBUG_ANN.initialized = 1;
115 nobug_resource_init ();
117 nobug_env_init_flag (&nobug_flag_nobug, NOBUG_TARGET_CONSOLE, LOG_WARNING, context);
119 nobug_log (&nobug_flag_nobug, LOG_NOTICE, "NOBUG", context, " INITIALIZED");
123 void
124 nobug_destroy (const struct nobug_context context)
126 if (nobug_initialized)
128 nobug_log (&nobug_flag_nobug, LOG_NOTICE, "NOBUG" , context, " DESTROYING");
130 nobug_resource_destroy ();
132 if (nobug_default_ringbuffer.start)
133 nobug_ringbuffer_destroy (&nobug_default_ringbuffer);
135 #if NOBUG_USE_PTHREAD
136 nobug_tls_free (nobug_thread_get ());
137 pthread_key_delete (nobug_tls_key);
138 #endif
140 nobug_initialized = 0;
144 const char*
145 nobug_basename (const char* const file)
147 return strrchr(file, '/') ? strrchr(file, '/') + 1 : file;
151 void
152 nobug_log_targets (const char* start, struct nobug_flag* flag, int lvl)
154 if (NOBUG_EXPECT_FALSE(lvl <= flag->limits[NOBUG_TARGET_CONSOLE]))
156 #if HAVE_VALGRIND
157 if (NOBUG_EXPECT_FALSE (NOBUG_ACTIVE_DBG == NOBUG_DBG_VALGRIND))
159 VALGRIND_PRINTF ("%s"VALGRIND_NEWLINE, start);
161 else
162 #endif
163 fprintf (flag->console_target?flag->console_target:stderr, "%s\n", start);
166 if (NOBUG_EXPECT_FALSE(lvl <= flag->limits[NOBUG_TARGET_FILE] && flag->file_target))
168 fprintf (flag->file_target, "%s\n", start);
171 if (NOBUG_EXPECT_FALSE(lvl <= flag->limits[NOBUG_TARGET_SYSLOG]))
173 syslog (lvl, "%s\n", start);
176 if (nobug_logging_callback)
177 nobug_logging_callback (flag, lvl, start, nobug_callback_data);
181 void
182 nobug_log_va_ (char* start, char* header, struct nobug_flag* flag, int lvl, const char* fmt, va_list ap)
184 nobug_ringbuffer_printf (flag->ringbuffer_target, "%s", header);
185 nobug_ringbuffer_append (flag->ringbuffer_target);
187 nobug_ringbuffer_vprintf (flag->ringbuffer_target, fmt, ap);
189 char* lines_in[NOBUG_MAX_LOG_LINES];
190 char* lines_out[NOBUG_MAX_LOG_LINES];
191 lines_in[0] = lines_out[0] = start-1;
192 int n=1;
194 while (n<NOBUG_MAX_LOG_LINES-1 &&(lines_in[n] = strchr(lines_in[n-1]+1, '\n')))
195 ++n;
197 if (n>1)
199 lines_in[n] = lines_in[n-1] + strlen(lines_in[n-1]+1) + 1;
201 size_t newlen = lines_in[n]-lines_in[0] - 1 + (strlen (header)+1) * (n-1);
203 if (nobug_ringbuffer_extend (flag->ringbuffer_target, newlen, 0))
205 header[10] = '!';
207 size_t headerlen = strlen(header);
209 for (int i = n-1; i>0; --i)
211 size_t len = lines_in[i+1] - lines_in[i] - 1;
212 char* dst = start+newlen-len;
214 memmove (dst, lines_in[i]+1, len);
215 dst[-1] = ' ';
217 dst -= (headerlen+1);
219 memmove (dst, header, headerlen);
220 dst[-1] ='\0';
222 lines_out[i] = dst-1;
223 newlen -= (len+headerlen+2);
228 for (int i = 0; i < n; ++i)
229 nobug_log_targets (lines_out[i]+1, flag, lvl);
233 char*
234 nobug_log_begin (char* header, struct nobug_flag* flag, const char* what, const struct nobug_context ctx)
236 #if NOBUG_USE_PTHREAD
237 pthread_mutex_lock (&nobug_logging_mutex);
238 #endif
240 snprintf (header, NOBUG_MAX_LOG_HEADER_SIZE, "%.10llu: %s: %s:%d: "
241 #if NOBUG_USE_PTHREAD
242 "%s: "
243 #else
244 "-: " /*no thread id*/
245 #endif
246 "%s:",
247 ++nobug_counter,
248 what?what:"NOBUG",
249 nobug_basename(ctx.file),
250 ctx.line,
251 #if NOBUG_USE_PTHREAD
252 nobug_thread_id_get(),
253 #endif
254 ctx.func);
256 return nobug_ringbuffer_pos (flag->ringbuffer_target);
260 void
261 nobug_log_end (struct nobug_flag* flag, int lvl)
263 #if NOBUG_USE_PTHREAD
264 /* the resource tracker bails out with the resource lock held for logging it, we can release it here now */
265 if (nobug_resource_error)
267 nobug_resource_error = NULL;
268 pthread_mutex_unlock (&nobug_resource_mutex);
271 pthread_mutex_unlock (&nobug_logging_mutex);
272 #endif
274 if (nobug_postlogging_callback)
275 nobug_postlogging_callback (flag, lvl, NULL, nobug_callback_data);
278 /* must be called inbetween log_begin and log_end */
279 void
280 nobug_log_line (char** start, char* header, struct nobug_flag* flag, int lvl, const char* fmt, ...)
282 va_list ap;
283 va_start (ap, fmt);
284 nobug_log_va_ (*start, header, flag, lvl, fmt, ap);
285 va_end (ap);
287 *start = nobug_ringbuffer_pos (flag->ringbuffer_target);
291 void
292 nobug_log (struct nobug_flag* flag, int lvl, const char* what, const struct nobug_context ctx, const char* fmt, ...)
294 char header[NOBUG_MAX_LOG_HEADER_SIZE];
296 char* start = nobug_log_begin (header, flag, what, ctx);
298 va_list ap;
299 va_start (ap, fmt);
300 nobug_log_va_ (start, header, flag, lvl, fmt, ap);
301 va_end (ap);
303 nobug_log_end (flag, lvl);
307 void
308 nobug_backtrace_glibc (const struct nobug_context context)
310 #ifdef HAVE_EXECINFO_H
311 void* nobug_backtrace_buffer[NOBUG_BACKTRACE_DEPTH];
313 int depth = backtrace (nobug_backtrace_buffer, NOBUG_BACKTRACE_DEPTH);
315 char** symbols = backtrace_symbols (nobug_backtrace_buffer, depth);
317 if (symbols)
319 /* start at [1] to suppress nobug_backtrace_glibc itself */
320 int i;
321 for (i = 1; i<depth; ++i)
322 nobug_log (&nobug_flag_NOBUG_ON, LOG_NOTICE, "BACKTRACE", context, " %s", symbols[i]);
324 free (symbols);
326 #endif
329 void
330 nobug_backtrace_valgrind (const struct nobug_context context)
332 #if NOBUG_USE_VALGRIND
333 #if NOBUG_USE_PTHREAD
334 pthread_mutex_lock (&nobug_logging_mutex);
335 #endif
336 VALGRIND_PRINTF_BACKTRACE("----------: BACKTRACE: %s@%d %s",
337 nobug_basename(context.file),
338 context.line,
339 context.func);
340 #if NOBUG_USE_PTHREAD
341 pthread_mutex_unlock (&nobug_logging_mutex);
342 #endif
343 #else
344 (void) context;
345 #endif
349 #if !NOBUG_USE_PTHREAD
350 static void* nobug_nothread_data = NULL;
352 void**
353 nobug_thread_data (void)
355 return &nobug_nothread_data;
358 #endif