strusBase  0.17
localErrorBuffer.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Patrick P. Frey
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
10 #ifndef _STRUS_LOCAL_ERROR_BUFFER_HPP_INCLUDED
11 #define _STRUS_LOCAL_ERROR_BUFFER_HPP_INCLUDED
12 #include "strus/errorCodes.hpp"
14 #include <cstdio>
15 #include <cstdarg>
16 #include <cstring>
17 #include <stdexcept>
18 
20 namespace strus
21 {
22 
26 {
27 public:
29  :m_errorCode(-1)
30  {
31  m_buf[0] = 0;
32  }
33 
34  virtual ~LocalErrorBuffer(){}
35 
36  virtual void setLogFile( FILE* )
37  {
38  throw std::logic_error( "not implemented");
39  }
40 
41  virtual bool setMaxNofThreads( unsigned int)
42  {
43  throw std::logic_error( "not implemented");
44  }
45 
46  virtual void report( int errorcode, const char* format, ...)
47  {
48  va_list ap;
49  va_start( ap, format);
50  std::size_t hdrlen = 0;
51  if (errorcode)
52  {
53  hdrlen = std::snprintf( m_buf, sizeof(m_buf), "##%d ", errorcode);
54  }
55  std::vsnprintf( m_buf + hdrlen, sizeof(m_buf) - hdrlen, format, ap);
56  m_buf[ sizeof(m_buf)-1] = 0;
57  va_end( ap);
58  m_errorCode = errorcode;
59  }
60 
61  virtual void explain( const char* format)
62  {
63  char tmpbuf[ BufferSize];
64  std::size_t len = std::snprintf( tmpbuf, sizeof(tmpbuf), format, m_buf);
65  if (len >= sizeof(tmpbuf))
66  {
67  len = sizeof(tmpbuf)-1;
68  tmpbuf[ len] = 0;
69  }
70  std::memcpy( m_buf, tmpbuf, len);
71  m_buf[ len] = 0;
72  }
73 
74  virtual const char* fetchError()
75  {
76  if (m_errorCode >= 0)
77  {
78  m_errorCode = -1;
79  return m_buf;
80  }
81  return 0;
82  }
83 
84  virtual bool hasError() const
85  {
86  return m_errorCode >= 0;
87  }
88 
89  virtual void allocContext()
90  {
91  throw std::logic_error( "not implemented");
92  }
93 
94  virtual void releaseContext()
95  {
96  throw std::logic_error( "not implemented");
97  }
98 
100  {
101  return NULL;
102  }
103 
104  int errorCode() const
105  {
106  return m_errorCode;
107  }
108 
109 private:
110  enum {BufferSize=2048};
111  mutable char m_buf[ BufferSize];
112  int m_errorCode;
113 };
114 
115 }//namespace
116 #endif
117 
LocalErrorBuffer()
Definition: localErrorBuffer.hpp:28
virtual bool setMaxNofThreads(unsigned int)
Redefine the maximum number of threads using the error buffer.
Definition: localErrorBuffer.hpp:41
virtual void report(int errorcode, const char *format,...)
Report an error.
Definition: localErrorBuffer.hpp:46
virtual void explain(const char *format)
Report an error, overwriting the previous error.
Definition: localErrorBuffer.hpp:61
virtual DebugTraceInterface * debugTrace() const
Fetches the debug trace interface if defined.
Definition: localErrorBuffer.hpp:99
virtual void setLogFile(FILE *)
Define error log file.
Definition: localErrorBuffer.hpp:36
virtual void releaseContext()
Dellocate context for current thread.
Definition: localErrorBuffer.hpp:94
Managing interface for reporting debug trace messages in a uniform way.
Definition: debugTraceInterface.hpp:81
virtual void allocContext()
Allocate context for current thread.
Definition: localErrorBuffer.hpp:89
Interface for reporting and catching errors in modules.
Definition: errorBufferInterface.hpp:24
Error buffer implementation for a context transforming the error message into an exceptions at the en...
Definition: localErrorBuffer.hpp:25
virtual const char * fetchError()
Check, if an error has occurred and return it.
Definition: localErrorBuffer.hpp:74
int errorCode() const
Definition: localErrorBuffer.hpp:104
Structured error codes for strus components.
virtual bool hasError() const
Check, if an error has occurred.
Definition: localErrorBuffer.hpp:84
Interface for reporting and catching errors.
virtual ~LocalErrorBuffer()
Definition: localErrorBuffer.hpp:34