Second pass on update of yatl.
[awesomized/libmemcached] / libtest / fatal.cc
index 3327e3819a1bfda7901e406b16055377c166a9f8..99b15535e0ea45b4641c2b082d8f6a85ff152269 100644 (file)
 namespace libtest {
 
 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
-fatal::fatal(const char *file_arg, int line_arg, const char *func_arg, const char *format, ...) :
-  std::runtime_error(func_arg),
-  _line(line_arg),
-  _file(file_arg),
-  _func(func_arg)
+
+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)
   {
-    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);
+    _error_message_size++;
+  }
+  va_end(args);
+}
 
-    strncpy(_mesg, last_error, sizeof(_mesg));
+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)
+  {
+    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;
@@ -90,8 +115,7 @@ void fatal::increment_disabled_counter()
 
 #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,
-                           const char *format, ...) :
+                           const std::string& instance, const in_port_t port, ...) :
   std::runtime_error(func_arg),
   _port(port),
   _line(line_arg),
@@ -99,7 +123,8 @@ disconnected::disconnected(const char *file_arg, int line_arg, const char *func_
   _func(func_arg)
 {
   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);
@@ -108,4 +133,3 @@ disconnected::disconnected(const char *file_arg, int line_arg, const char *func_
 }
 
 } // namespace libtest
-