Ver código fonte

Merge branch 'maint'

* maint:
  Add ASSERT and WARN macros.
Bryan Drewery 10 anos atrás
pai
commit
a2504131d2
3 arquivos alterados com 41 adições e 0 exclusões
  1. 21 0
      src/debug.cc
  2. 18 0
      src/debug.h
  3. 2 0
      src/main.cc

+ 21 - 0
src/debug.cc

@@ -108,6 +108,27 @@ void sdprintf (const char *format, ...)
 #endif
 }
 
+void _assert(int recoverable, const char *file, int line, const char *function,
+    const char *exp, const char *format, ...)
+{
+    char msg[1024], buf[1024];
+    va_list va;
+
+    if (format != NULL) {
+      va_start(va, format);
+      egg_vsnprintf(msg, sizeof(msg), format, va);
+      va_end(va);
+    }
+
+    simple_snprintf(buf, sizeof(buf),
+        STR("Assertion failed%s: (%s:%d:%s) [%s]%s%s"),
+        recoverable == 0 ? "" : " (ignoring)",
+        file, line, function, exp,
+        format != NULL ? ": ": "",
+        format != NULL ? msg : "");
+    fatal(buf, recoverable);
+}
+
 static void write_debug(bool fatal = 1)
 {
   if (fatal) {

+ 18 - 0
src/debug.h

@@ -37,4 +37,22 @@ void setlimits();
 void sdprintf (const char *, ...) __attribute__((format(printf, 1, 2)));
 void init_signals();
 void init_debug();
+
+#ifdef DEBUG
+void _assert(int, const char *, int, const char *, const char *,
+    const char *, ...) __attribute__((format(printf, 6, 7)));
+#define _ASSERT(recoverable, cond, msg...) do { \
+  if (__predict_false(!(cond))) { \
+    _assert(recoverable, __FILE__, __LINE__, __func__, # cond, msg); \
+  } \
+  Context; \
+} while (0)
+#else
+#define _ASSERT(recoverable, cond, msg...) do { \
+} while (0)
+#endif
+#define ASSERT(cond, msg...) _ASSERT(0, cond, msg)
+#define ASSERT0(cond) ASSERT(cond, NULL)
+#define WARN(cond, msg...) _ASSERT(1, cond, msg)
+
 #endif /* !_DEBUG_H */

+ 2 - 0
src/main.cc

@@ -205,6 +205,8 @@ void fatal(const char *s, int recoverable)
 //    if (conf.bot && conf.bot->pid_file)
 //      unlink(conf.bot->pid_file);
     exit(1);
+  } else {
+    ContextNote(__func__, s);
   }
 }