libcsdbg  1.28
C++ exception (and generic) stack trace debug library
exception.cpp
Go to the documentation of this file.
1 #include "../include/exception.hpp"
2 #include "../include/util.hpp"
3 
10 namespace csdbg {
11 
23 std::ostream& operator<<(std::ostream &lval, const std::exception &rval)
24 {
25  util::lock();
26  util::header(lval, "x");
27  lval << rval.what() << "\r\n";
28  util::unlock();
29  return lval;
30 }
31 
32 
42 std::ostream& operator<<(std::ostream &lval, const exception &rval)
43 {
44  util::lock();
45  util::header(lval, "x");
46 
47  if ( likely(rval.m_msg != NULL) )
48  lval << rval.m_msg << "\r\n";
49  else
50  lval << "n/a\r\n";
51 
52  util::unlock();
53  return lval;
54 }
55 
56 
64 exception::exception(const i8 *fmt, ...):
65 m_msg(NULL)
66 {
67  __D_ASSERT(fmt != NULL);
68  if ( unlikely(fmt == NULL) )
69  return;
70 
71  try {
72  va_list args;
73  va_start(args, fmt);
74  m_msg = util::va_format(fmt, args);
75  }
76 
77  catch (...) {
78  __D_ASSERT(m_msg != NULL);
79  }
80 }
81 
82 
89 m_msg(NULL)
90 {
91  *this = src;
92 }
93 
94 
99 {
100  delete[] m_msg;
101  m_msg = NULL;
102 }
103 
104 
113 {
114  return new (std::nothrow) exception(*this);
115 }
116 
117 
123 inline const i8* exception::msg() const
124 {
125  return m_msg;
126 }
127 
128 
137 {
138  if ( unlikely(this == &rval) )
139  return *this;
140 
141  i8 *buf = rval.m_msg;
142  if ( unlikely(buf == NULL) ) {
143  delete[] m_msg;
144  m_msg = NULL;
145  return *this;
146  }
147 
148  u32 len = strlen(buf);
149  if ( likely(m_msg == NULL || len > strlen(m_msg)) ) {
150  delete[] m_msg;
151  m_msg = NULL;
152  m_msg = new (std::nothrow) i8[len + 1];
153  }
154 
155  __D_ASSERT(m_msg != NULL);
156  if ( likely(m_msg != NULL) )
157  strcpy(m_msg, buf);
158 
159  return *this;
160 }
161 
162 }
163 
exception(const i8 *,...)
Object constructor.
Definition: exception.cpp:64
char i8
8-bit signed integer
Definition: config.hpp:72
std::ostream & operator<<(std::ostream &, const std::exception &)
Stream insertion operator for std::exception objects.
Definition: exception.cpp:23
virtual exception * clone() const
Object virtual copy constructor.
Definition: exception.cpp:112
#define likely(expr)
Offer a hint (positive) to the pipeline branch predictor.
Definition: config.hpp:344
static void lock()
Lock the global access mutex.
Definition: util.cpp:397
virtual const i8 * msg() const
Get the exception message.
Definition: exception.cpp:123
static void unlock()
Unlock the global access mutex.
Definition: util.cpp:406
virtual ~exception()
Object destructor.
Definition: exception.cpp:98
static i8 * va_format(const i8 *, va_list)
Format a buffer with a printf-style string expanded with the values of a variable argument list...
Definition: util.cpp:519
i8 * m_msg
Error description message.
Definition: exception.hpp:32
unsigned int u32
32-bit unsigned integer
Definition: config.hpp:102
virtual exception & operator=(const exception &)
Assignment operator.
Definition: exception.cpp:136
static void header(std::ostream &, const i8 *)
Print a tagged message header on an output stream.
Definition: util.cpp:594
This class is a throwable with a textual description of an error.
Definition: exception.hpp:26
#define unlikely(expr)
Offer a hint (negative) to the pipeline branch predictor.
Definition: config.hpp:349
#define __D_ASSERT(x)
Assertion macro.
Definition: config.hpp:268