File: Error.cpp

package info (click to toggle)
rlog 1.4-4
  • links: PTS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 2,640 kB
  • ctags: 1,696
  • sloc: sh: 9,196; cpp: 1,601; makefile: 84
file content (243 lines) | stat: -rw-r--r-- 5,024 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*****************************************************************************
 * Author:   Valient Gough <vgough@pobox.com>
 *
 *****************************************************************************
 * Copyright (c) 2004, Valient Gough
 *
 * This library is free software; you can distribute it and/or modify it under
 * the terms of the GNU Lesser General Public License (LGPL), as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the LGPL in the file COPYING for more
 * details.
 *
 */
		                                                                                
                             
#include "Error.h"

#include <stdio.h>
#include <stdarg.h>

#ifdef HAVE_CONFIG_H
#include "config.h"

#  ifdef HAVE_SSTREAM
#    include <sstream>
#  else
// old STL -- use strstream instead of stringstream
#    include <strstream.h>
#    define USE_STRSTREAM
#  endif

#else
#  include <sstream>
#endif

using namespace rlog;
using namespace std;


namespace rlog
{
    /*! @struct ErrorData <rlog/Error.h>
      @brief Internal RLog structure - holds internal data for rlog::Error

      @internal
    */
    struct ErrorData
    {
	//! for reference counting
	int usageCount; // to make copy fast

	//! component where error occured
	string component;
	//! file where error occured
	string file;
	//! function name where error occured
	string function;
	//! line number where error occured
	int line;
	//! condition string (in case of rAssert) at error location
	string msg;
    };
}

static 
string errorMessage( const char *file, int line, 
	const char *msg)
{
#ifdef USE_STRSTREAM
    ostrstream ss;
#else
    ostringstream ss;
#endif

    ss << "Assert failure at " << file << ':' << line << " -- " << msg;

    return ss.str();
}



/*! @class rlog::Error <rlog/Error.h>
  @brief Error Used as exception thrown from rAssert() on failure.
  
  Error is derived from std::runtime_error exception and is thrown from
  rAssert() to indicate the reason and location of the failure.

  @code
  void testFunc()
  {
      bool testAssert = true;
      rAssert( testAssert == false ); // fails..
  }

  void A()
  {
      try
      {
	  testFunc();
      } catch( Error &err )
      {
	  rDebug("caught assert failure from %s line %i ( %s )",
	      err.file(), err.line(), err.function() );
      }
  }
  @endcode

  @author Valient Gough
  @see rAssert()
*/


Error::Error( const char *component, const char *file, const char *function,
	int line, const char *msg )
    : runtime_error( errorMessage( file, line, msg ) )
{
    data = new ErrorData;
    data->usageCount = 1;
    data->component = component;
    data->file = file;
    data->function = function;
    data->line = line;
    data->msg = msg;
}

Error::Error( const char *component, const char *file, const char *function,
	int line, const std::string &msg )
    : runtime_error( errorMessage( file, line, msg.c_str() ) )
{
    data = new ErrorData;
    data->usageCount = 1;
    data->component = component;
    data->file = file;
    data->function = function;
    data->line = line;
    data->msg = msg;
}

Error::Error( const Error &src )
    : runtime_error( src.what() )
{
    ++src.data->usageCount;
    data = src.data;
}

Error::~Error() throw()
{
    if(data)
    {
	if(--data->usageCount == 0)
	    delete data;
	data = 0;
    }
}

Error & Error::operator = (const Error & src)
{
    if(data != src.data)
    {
	++src.data->usageCount;
	if(--data->usageCount == 0)
	    delete data;
	data = src.data;
    }
	
    return *this;
}

/*! Log the error to the given channel
*/
void Error::log( RLogChannel * channel ) const
{
    (void) channel;
}

/*! return component string.  If this was due to an rAssert() then this will
  be the definition of RLOG_COMPONENT
  at the point of the rAssert.
*/
const char *Error::component() const
{
    return data->component.c_str();
}

const char *Error::file() const
{
    return data->file.c_str();
}

const char *Error::function() const
{
    return data->function.c_str();
}

int Error::line() const
{
    return data->line;
}

const char *Error::message() const
{
    return data->msg.c_str();
}


string rlog::_format_msg( const char *format, ... )
{
    char msgBuf[64];
    va_list args;
    va_start( args, format );

    int ncpy = vsnprintf( msgBuf, sizeof(msgBuf), format, args );

    va_end( args );

    std::string result;
    if(ncpy < (int)sizeof(msgBuf))
    {
	if(ncpy > 0)
	    result = msgBuf;
	else
	    result = "RLOG internal formatting error";
    } else
    {
	// buffer wasn't large ehough, allocate a temporary
	int len = ncpy + 1;
	char *buf = new char[len];
	
	va_start( args, format );
	vsnprintf( buf, len, format, args );
	va_end( args );

	result = buf;
	delete[] buf;
    }

    return result;
}