Second pass on update of yatl.
[awesomized/libmemcached] / libtest / fatal.cc
index 9517f778d23ab7587876eaf897b2d4183ccd599e..99b15535e0ea45b4641c2b082d8f6a85ff152269 100644 (file)
  *
  */
 
-#include <config.h>
+#include "libtest/yatlcon.h"
 #include <libtest/common.h>
 #include <cstdarg>
 
 namespace libtest {
 
-exception::exception(const char *file_, int line_, const char *func_) :
-  std::runtime_error(func_),
-  _file(file_),
-  _line(line_),
-  _func(func_)
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
+
+fatal::fatal(const char *file_arg, int line_arg, const char *func_arg, ...) :
+  __test_result(file_arg, line_arg, func_arg),
+  _error_message(NULL),
+  _error_message_size(0)
+{
+  va_list args;
+  va_start(args, func_arg);
+  const char *format= va_arg(args, const char *);
+  _error_message_size= vasprintf(&_error_message, format, args);
+  assert(_error_message_size != -1);
+  if (_error_message_size > 0)
   {
+    _error_message_size++;
   }
+  va_end(args);
+}
 
-fatal::fatal(const char *file_, int line_, const char *func_, const char *format, ...) :
-  exception(file_, line_, func_)
+fatal::fatal( const fatal& other ) :
+  __test_result(other),
+  _error_message_size(other._error_message_size)
+{
+  _error_message= (char*) malloc(_error_message_size);
+  if (_error_message)
   {
-    va_list args;
-    va_start(args, format);
-    char last_error[BUFSIZ];
-    int last_error_length= vsnprintf(last_error, sizeof(last_error), format, args);
-    va_end(args);
+    memcpy(_error_message, other._error_message, _error_message_size);
+  }
+  else
+  {
+    _error_message_size= -1;
+  }
+}
 
-    snprintf(_error_message, sizeof(_error_message), "%.*s", last_error_length, last_error);
+fatal::~fatal() throw()
+{
+  if ((_error_message_size > 0) and _error_message)
+  {
+    free(_error_message);
+    _error_message= NULL;
   }
+}
 
 static bool _disabled= false;
 static uint32_t _counter= 0;
@@ -70,11 +93,13 @@ bool fatal::is_disabled()
 
 void fatal::disable()
 {
+  _counter= 0;
   _disabled= true;
 }
 
 void fatal::enable()
 {
+  _counter= 0;
   _disabled= false;
 }
 
@@ -88,29 +113,18 @@ void fatal::increment_disabled_counter()
   _counter++;
 }
 
-disconnected::disconnected(const char *file_, int line_, const char *func_,
-                           const std::string& instance, const in_port_t port,
-                           const char *format, ...) :
-  exception(file_, line_, func_),
-  _port(port)
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
+disconnected::disconnected(const char *file_arg, int line_arg, const char *func_arg,
+                           const std::string& instance, const in_port_t port, ...) :
+  std::runtime_error(func_arg),
+  _port(port),
+  _line(line_arg),
+  _file(file_arg),
+  _func(func_arg)
 {
   va_list args;
-  va_start(args, format);
-  char last_error[BUFSIZ];
-  (void)vsnprintf(last_error, sizeof(last_error), format, args);
-  va_end(args);
-
-  snprintf(_error_message, sizeof(_error_message), "%s:%u %s", instance.c_str(), uint32_t(port), last_error);
-}
-
-start::start(const char *file_, int line_, const char *func_,
-             const std::string& instance, const in_port_t port,
-             const char *format, ...) :
-  exception(file_, line_, func_),
-  _port(port)
-{
-  va_list args;
-  va_start(args, format);
+  va_start(args, port);
+  const char *format= va_arg(args, const char *);
   char last_error[BUFSIZ];
   (void)vsnprintf(last_error, sizeof(last_error), format, args);
   va_end(args);
@@ -119,4 +133,3 @@ start::start(const char *file_, int line_, const char *func_,
 }
 
 } // namespace libtest
-