Add a 'guard' size parameter to ringbuffer construction, remove hardcoded
[nobug.git] / src / nobug.c
blob0284468a03f0d922f52fdc259060fcf66d16834f
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 NOBUG_LOGGING_LOCK;
238 snprintf (header, NOBUG_MAX_LOG_HEADER_SIZE, "%.10llu: %s: %s:%d: "
239 #if NOBUG_USE_PTHREAD
240 "%s: "
241 #endif
242 "%s:",
243 ++nobug_counter,
244 what?what:"NOBUG",
245 nobug_basename(ctx.file),
246 ctx.line,
247 #if NOBUG_USE_PTHREAD
248 nobug_thread_id_get(),
249 #endif
250 ctx.func);
252 return nobug_ringbuffer_pos (flag->ringbuffer_target);
256 void
257 nobug_log_end (struct nobug_flag* flag, int lvl)
259 #if NOBUG_USE_PTHREAD
260 /* the resource tracker bails out with the resource lock held for logging it, we can release it here now */
261 if (nobug_resource_error)
263 nobug_resource_error = NULL;
264 NOBUG_RESOURCE_UNLOCK;
266 #endif
267 NOBUG_LOGGING_UNLOCK;
269 if (nobug_postlogging_callback)
270 nobug_postlogging_callback (flag, lvl, NULL, nobug_callback_data);
273 /* must be called inbetween log_begin and log_end */
274 void
275 nobug_log_line (char** start, char* header, struct nobug_flag* flag, int lvl, const char* fmt, ...)
277 va_list ap;
278 va_start (ap, fmt);
279 nobug_log_va_ (*start, header, flag, lvl, fmt, ap);
280 va_end (ap);
282 *start = nobug_ringbuffer_pos (flag->ringbuffer_target);
286 void
287 nobug_log (struct nobug_flag* flag, int lvl, const char* what, const struct nobug_context ctx, const char* fmt, ...)
289 char header[NOBUG_MAX_LOG_HEADER_SIZE];
291 char* start = nobug_log_begin (header, flag, what, ctx);
293 va_list ap;
294 va_start (ap, fmt);
295 nobug_log_va_ (start, header, flag, lvl, fmt, ap);
296 va_end (ap);
298 nobug_log_end (flag, lvl);
302 void
303 nobug_backtrace_glibc (const struct nobug_context context)
305 #ifdef HAVE_EXECINFO_H
306 void* nobug_backtrace_buffer[NOBUG_BACKTRACE_DEPTH];
308 int depth = backtrace (nobug_backtrace_buffer, NOBUG_BACKTRACE_DEPTH);
310 char** symbols = backtrace_symbols (nobug_backtrace_buffer, depth);
312 if (symbols)
314 /* start at [1] to suppress nobug_backtrace_glibc itself */
315 int i;
316 for (i = 1; i<depth; ++i)
317 nobug_log (&nobug_flag_NOBUG_ON, LOG_NOTICE, "BACKTRACE", context, " %s", symbols[i]);
319 free (symbols);
321 #endif
325 #if !NOBUG_USE_PTHREAD
326 static void* nobug_nothread_data = NULL;
328 void**
329 nobug_thread_data (void)
331 return &nobug_nothread_data;
334 #endif