libcsdbg  1.28
C++ exception (and generic) stack trace debug library
streambuf.cpp
Go to the documentation of this file.
1 #include "../include/streambuf.hpp"
2 #include "../include/util.hpp"
3 #if !defined CSDBG_WITH_PLUGIN && !defined CSDBG_WITH_HIGHLIGHT
4 #include "../include/exception.hpp"
5 #endif
6 
13 namespace csdbg {
14 
21 try:
22 string(),
23 m_handle(-1)
24 {
25 }
26 
27 catch (...) {
28  m_handle = -1;
29 }
30 
31 
41 try:
42 string(),
43 m_handle(-1)
44 {
45  *this = src;
46 }
47 
48 catch (...) {
49  delete[] m_data;
50  m_data = NULL;
51  m_handle = -1;
52 }
53 
54 
59 {
60  close();
61 }
62 
63 
69 inline i32 streambuf::handle() const
70 {
71  return m_handle;
72 }
73 
74 
80 inline bool streambuf::is_opened() const
81 {
82  return m_handle >= 0;
83 }
84 
85 
97 {
98  if ( unlikely(this == &rval) )
99  return *this;
100 
101  /* Close the current stream (to sync current data) */
102  close();
103 
104  /* Copy the buffer */
105  string::operator=(rval);
106 
107  i32 fd = rval.m_handle;
108  if ( unlikely(fd < 0) )
109  return *this;
110 
111  /* Duplicate the handle (descriptor) */
112  m_handle = dup(fd);
113  if ( unlikely(m_handle < 0) )
114  throw exception(
115  "failed to duplicate descriptor %d (errno %d - %s)",
116  fd,
117  errno,
118  strerror(errno)
119  );
120 
121  return *this;
122 }
123 
124 
131 {
132  if ( likely(m_handle >= 0) ) {
133  i32 retval;
134  do {
135  retval = ::close(m_handle);
136  }
137  while ( unlikely(retval < 0 && errno == EINTR) );
138 
139  m_handle = -1;
140  }
141 
142  return *this;
143 }
144 
145 
157 {
158  i32 offset = 0, sz = m_length;
159  while ( likely(sz > 0) ) {
160  i32 written = write(m_handle, m_data + offset, sz);
161  if ( unlikely(written < 0) )
162  switch (errno) {
163  case EINTR:
164  case EAGAIN:
165  continue;
166 
167  default:
168  throw errno;
169  }
170 
171  sz -= written;
172  offset += written;
173  }
174 
175  /* Clear the buffer */
176  clear();
177  return *this;
178 }
179 
180 
189 {
190  i32 retval;
191  do {
192  retval = flock(m_handle, LOCK_EX);
193  }
194  while ( unlikely(retval < 0 && errno == EINTR) );
195 
196  if ( unlikely(retval < 0) )
197  throw errno;
198 
199  return const_cast<streambuf&> (*this);
200 }
201 
202 
211 {
212  i32 retval;
213  do {
214  retval = flock(m_handle, LOCK_UN);
215  }
216  while ( unlikely(retval < 0 && errno == EINTR) );
217 
218  if ( unlikely(retval < 0) )
219  throw errno;
220 
221  return const_cast<streambuf&> (*this);
222 }
223 
224 
249 {
250  const i8 *path = util::exec_path();
251 
252  struct timeval now;
253  gettimeofday(&now, NULL);
254  u64 tstamp = static_cast<u64> (now.tv_sec) * 10e+5 + now.tv_usec;
255 
256  try {
257  append("path: %s\r\n", path);
258  append("pid: %x\r\n", getpid());
259  append("tid: %lx\r\n", pthread_self());
260  append("tstamp: %lx\r\n", tstamp);
261 
262  delete[] path;
263  return *this;
264  }
265 
266  catch (...) {
267  delete[] path;
268  throw;
269  }
270 }
271 
272 }
273 
virtual streambuf & header()
Append LDP headers to the buffer.
Definition: streambuf.cpp:248
virtual string & append(const string &)
Append a string.
Definition: string.cpp:404
virtual streambuf & flush()=0
To be implemented.
Definition: streambuf.cpp:156
static const i8 * exec_path()
Get the absolute path of the executable.
Definition: util.cpp:87
This abstract class is the base for all buffered output stream types (for files, sockets, serial interfaces e.t.c)
Definition: streambuf.hpp:37
char i8
8-bit signed integer
Definition: config.hpp:72
unsigned long long u64
64-bit unsigned integer
Definition: config.hpp:107
i32 m_handle
Stream handle (descriptor)
Definition: streambuf.hpp:43
virtual string & operator=(const string &)
Assignment operator.
Definition: string.cpp:317
streambuf()
Object default constructor.
Definition: streambuf.cpp:20
#define likely(expr)
Offer a hint (positive) to the pipeline branch predictor.
Definition: config.hpp:344
virtual string & clear()
Clear contents.
Definition: string.cpp:387
virtual streambuf & operator=(const streambuf &)
Assignment operator.
Definition: streambuf.cpp:96
virtual streambuf & lock() const
Lock the stream (exclusively)
Definition: streambuf.cpp:188
Lightweight string buffer class (for ISO-8859-1 text)
Definition: string.hpp:36
i8 * m_data
String data.
Definition: string.hpp:42
virtual ~streambuf()=0
To be implemented.
Definition: streambuf.cpp:58
virtual i32 handle() const
Get the handle.
Definition: streambuf.cpp:69
int i32
32-bit signed integer
Definition: config.hpp:82
virtual streambuf & unlock() const
Unlock the stream.
Definition: streambuf.cpp:210
virtual bool is_opened() const
Check if the stream is opened for output.
Definition: streambuf.cpp:80
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
virtual streambuf & close()
Close the stream.
Definition: streambuf.cpp:130
u32 m_length
Character count.
Definition: string.hpp:44