FIX: context initialization, __func__ retrieval
authorChristian Thaeter <ct@pipapo.org>
Sat, 29 May 2010 14:08:20 +0000 (16:08 +0200)
committerChristian Thaeter <ct@pipapo.org>
Sun, 30 May 2010 21:27:28 +0000 (23:27 +0200)
 * define NOBUG_FUNC to a language and compiler defined macro
 * add detection heursistics therefor
 * add C++ ctors to construct contexts, C uses struct casts

Different standards (C/C++) and implementations provide different ways to
access the function name of the current called function. We abstract that
here and provide a fallback in case no suitable way is known.

One can override it with a CFLAG -DNOBUG_FUNC="something"

src/nobug.h

index fd98aa8..4e98787 100644 (file)
 #include <pthread.h>
 #endif
 
+
+/*
+  figure out how to find out the current function name, this is compiler and language dependent
+*/
+#if !defined(NOBUG_FUNC) && defined(__STDC_VERSION__)
+/* C99 defines __func__ */
+# if __STDC_VERSION__ >= 199901L
+#  define NOBUG_FUNC __func__
+# endif
+#endif
+
+#if !defined(NOBUG_FUNC) && defined(__GNUC__)
+/* the gnu compiler (since 2.x) defines __FUNCTION__ */
+# if __GNUC__ >= 2
+#  define NOBUG_FUNC __FUNCTION__
+# endif
+#endif
+
+#if !defined(NOBUG_FUNC) && defined(__SUNPRO_C)
+/* the sun C compiler defines __FUNCTION__, the version is determined by a sochastic test, please report! */
+# if __SUNPRO_C >= 0x5100
+#  define NOBUG_FUNC __FUNCTION__
+# endif
+#endif
+
+/* add more heuristics here... */
+
+/* finally a fallback when nothing found */
+#ifndef NOBUG_FUNC
+# define NOBUG_FUNC "-"
+#endif
+
+
 /*
 //assertions HEAD- Assertions;;
 //assertions
   NOBUG_IF_RELEASE(NOBUG_LOG_BASELIMIT_RELEASE)
 #endif
 
+
 /*
 //srccontext HEAD~ Source Contexts; NOBUG_CONTEXT; pass information about the source location
 //srccontext  NOBUG_CONTEXT
 //srccontext
 */
 
-#define NOBUG_CONTEXT ((const struct nobug_context){__FILE__, __LINE__, __func__})
+#ifndef __cplusplus
+#define NOBUG_CONTEXT ((const struct nobug_context){__FILE__, __LINE__, NOBUG_FUNC})
 
 #define NOBUG_CONTEXT_NOFUNC ((const struct nobug_context){__FILE__, __LINE__, "-"})
 
 #define NOBUG_CONTEXT_NIL ((const struct nobug_context){"-", 0, "-"})
+#else
+
+#define NOBUG_CONTEXT (nobug_context(__FILE__, __LINE__, NOBUG_FUNC))
+
+#define NOBUG_CONTEXT_NOFUNC (nobug_context(__FILE__, __LINE__, "-"))
 
+#define NOBUG_CONTEXT_NIL (nobug_context("-", 0, "-"))
+#endif
 
 /*
 //annotations HEAD- Source Annotations;;
@@ -1442,12 +1484,10 @@ NOTREACHED      abort           abort           nothing
       NOBUG_LOG_( &nobug_flag_NOBUG_ON, LOG_EMERG,                      \
                   what, context,                                        \
                   ""__VA_ARGS__);                                       \
-      nobug_resource_dump_all ((struct nobug_resource_dump_context)     \
-                               {                                        \
+      nobug_resource_dump_all (NOBUG_RESOURCE_DUMP_CONTEXT(             \
                                  &nobug_flag_NOBUG_ON,                  \
                                  LOG_EMERG,                             \
-                                 context                                \
-                               });                                      \
+                                 context));                             \
       NOBUG_BACKTRACE_CTX(context);                                     \
       NOBUG_ABORT))
 
@@ -1470,19 +1510,19 @@ NOTREACHED      abort           abort           nothing
 #define NOBUG_RESOURCE_DUMP(flag, handle)                                       \
   NOBUG_IF_ALPHA(                                                               \
     do {                                                                        \
-      nobug_resource_dump (handle, (struct nobug_resource_dump_context)         \
-                             {&NOBUG_FLAG(flag),                                \
-                              NOBUG_RESOURCE_LOG_LEVEL,                         \
-                              NOBUG_CONTEXT});                                  \
+      nobug_resource_dump (handle, NOBUG_RESOURCE_DUMP_CONTEXT(                 \
+                                     &NOBUG_FLAG(flag),                         \
+                                     NOBUG_RESOURCE_LOG_LEVEL,                  \
+                                     NOBUG_CONTEXT));                           \
     } while (0))
 
 #define NOBUG_RESOURCE_DUMP_IF(when, flag, handle)                              \
   NOBUG_IF_ALPHA(                                                               \
     NOBUG_WHEN(when,                                                            \
-      nobug_resource_dump (handle, (struct nobug_resource_dump_context)         \
-                             {&NOBUG_FLAG(flag),                                \
-                              NOBUG_RESOURCE_LOG_LEVEL,                         \
-                              NOBUG_CONTEXT});                                  \
+      nobug_resource_dump (handle, NOBUG_RESOURCE_DUMP_CONTEXT(                 \
+                                     &NOBUG_FLAG(flag),                         \
+                                     NOBUG_RESOURCE_LOG_LEVEL,                  \
+                                     NOBUG_CONTEXT));                           \
     ))
 
 
@@ -1502,20 +1542,20 @@ NOTREACHED      abort           abort           nothing
 #define NOBUG_RESOURCE_DUMPALL(flag)                                    \
   NOBUG_IF_ALPHA(                                                       \
     do {                                                                \
-      nobug_resource_dump_all ((struct nobug_resource_dump_context)     \
-                               {&NOBUG_FLAG(flag),                      \
-                                   NOBUG_RESOURCE_LOG_LEVEL,            \
-                                   NOBUG_CONTEXT});                     \
+      nobug_resource_dump_all (NOBUG_RESOURCE_DUMP_CONTEXT(             \
+                                 &NOBUG_FLAG(flag),                     \
+                                 NOBUG_RESOURCE_LOG_LEVEL,              \
+                                 NOBUG_CONTEXT));                       \
     } while (0))
 
 
 #define NOBUG_RESOURCE_DUMPALL_IF(when, flag)                           \
   NOBUG_IF_ALPHA(                                                       \
     NOBUG_WHEN(when,                                                    \
-      nobug_resource_dump_all ((struct nobug_resource_dump_context)     \
-                               {&NOBUG_FLAG(flag),                      \
-                                   NOBUG_RESOURCE_LOG_LEVEL,            \
-                                   NOBUG_CONTEXT});                     \
+      nobug_resource_dump_all (NOBUG_RESOURCE_DUMP_CONTEXT(             \
+                                 &NOBUG_FLAG(flag),                     \
+                                 NOBUG_RESOURCE_LOG_LEVEL,              \
+                                 NOBUG_CONTEXT));                       \
       ))
 
 /*
@@ -1534,23 +1574,37 @@ NOTREACHED      abort           abort           nothing
 #define NOBUG_RESOURCE_LIST(flag)                                       \
   NOBUG_IF_ALPHA(                                                       \
     do {                                                                \
-      nobug_resource_list ((struct nobug_resource_dump_context)         \
-                           {&NOBUG_FLAG(flag),                          \
-                               NOBUG_RESOURCE_LOG_LEVEL,                \
-                               NOBUG_CONTEXT});                         \
+      nobug_resource_list (NOBUG_RESOURCE_DUMP_CONTEXT(                 \
+                                 &NOBUG_FLAG(flag),                     \
+                                 NOBUG_RESOURCE_LOG_LEVEL,              \
+                                 NOBUG_CONTEXT));                       \
     } while (0))
 
 
 #define NOBUG_RESOURCE_LIST_IF(when, flag)                              \
   NOBUG_IF_ALPHA(                                                       \
     NOBUG_WHEN(when,                                                    \
-      nobug_resource_list ((struct nobug_resource_dump_context)         \
-                               {&NOBUG_FLAG(flag),                      \
-                                NOBUG_RESOURCE_LOG_LEVEL,               \
-                                NOBUG_CONTEXT});                        \
+      nobug_resource_list (NOBUG_RESOURCE_DUMP_CONTEXT(                 \
+                                 &NOBUG_FLAG(flag),                     \
+                                 NOBUG_RESOURCE_LOG_LEVEL,              \
+                                 NOBUG_CONTEXT));                       \
     ))
 
 
+/* constructing a resource-dump context */
+#ifndef __cplusplus
+#define NOBUG_RESOURCE_DUMP_CONTEXT(flag, level, context)               \
+  ((struct nobug_resource_dump_context){                                \
+    flag,                                                               \
+    level,                                                              \
+    context})
+#else
+#define NOBUG_CONTEXT (nobug_context(__FILE__, __LINE__, NOBUG_FUNC))
+#define NOBUG_RESOURCE_DUMP_CONTEXT(flag, level, context)               \
+  (nobug_resource_dump_context(flag, level, context))
+#endif
+
+
 /*
  threading support
 */
@@ -2022,6 +2076,11 @@ struct nobug_context
   const char* file;
   int line;
   const char* func;
+
+#ifdef __cplusplus
+  nobug_context (const char* file_, int line_, const char* func_)
+  : file(file_), line(line_), func(func_) {}
+#endif
 };
 
 const char*
@@ -2287,6 +2346,13 @@ struct nobug_resource_dump_context
   struct nobug_flag* flag;
   int level;
   struct nobug_context ctx;
+
+#ifdef __cplusplus
+  nobug_resource_dump_context (struct nobug_flag* flag_,
+                               int level_,
+                               struct nobug_context ctx_)
+  : flag(flag_), level(level_), ctx(ctx_) {}
+#endif
 };
 
 enum nobug_resource_state