source: cheroxy/trunk/src/CRXException.cpp@ 27

Last change on this file since 27 was 27, checked in by cheese, 11 years ago

#1 fix code-style

File size: 3.9 KB
Line 
1/**
2 * CRXException.cpp
3 */
4
5#include "CRXException.h"
6
7#include <sstream>
8
9#ifdef _WIN32
10# include <Windows.h>
11#else
12# include <errno.h>
13#endif
14
15CRXException::CRXException (void)
16 : mCode (0)
17{
18 /*----------------------------------------------------------------*/
19 /*----------------------------------------------------------------*/
20}
21
22CRXException::~CRXException (void)
23{
24 /*----------------------------------------------------------------*/
25 /*----------------------------------------------------------------*/
26}
27
28int
29CRXException::GetErrorCode (void) const
30{
31 /*----------------------------------------------------------------*/
32 /*----------------------------------------------------------------*/
33 return mCode;
34}
35
36std::string
37CRXException::GetErrorMessage (void) const
38{
39 /*----------------------------------------------------------------*/
40 /*----------------------------------------------------------------*/
41 return mMessage;
42}
43
44int
45CRXException::GetSystemErrorCode (void) const
46{
47 /*----------------------------------------------------------------*/
48 /*----------------------------------------------------------------*/
49#ifdef _WIN32
50 return GetLastError ();
51#else
52 return errno;
53#endif
54}
55
56void
57CRXException::SetErrorCode (const int aCode)
58{
59 /*----------------------------------------------------------------*/
60 mCode = aCode;
61 /*----------------------------------------------------------------*/
62}
63
64void
65CRXException::SetErrorMessage (const char * aFile,
66 const char * aFunction,
67 const int aLine,
68 const char * aFormat, ...)
69{
70 va_list aVaList;
71
72 /*----------------------------------------------------------------*/
73 va_start (aVaList, aFormat);
74
75 mMessage += MakeErrorMessage (aFile, aFunction, aLine, aFormat, aVaList) + "\n";
76
77 va_end (aVaList);
78 /*----------------------------------------------------------------*/
79}
80
81std::string
82CRXException::MakeErrorMessage (const char * aFile,
83 const char * aFunction,
84 const int aLine,
85 const char * aFormat,
86 va_list aVaList)
87{
88 /*---------------------------------------------------------------
89 * local variables
90 *--------------------------------------------------------------*/
91 double aDoubleValue = .0;
92 int aIntValue = 0;
93 char *aStringValue = NULL;
94 char *aPtr = NULL;
95 void * aHexValue = NULL;
96
97 std::stringstream aErrorStream;
98
99 /*----------------------------------------------------------------*/
100#ifdef _DEBUG
101 /*----------------------------------------------------------------
102 * 1. set default error message
103 *----------------------------------------------------------------*/
104 aErrorStream << "[" << aFile << "]";
105 if (aLine != 0)
106 aErrorStream << "[" << aLine << "]";
107 else
108 aErrorStream << "[EOF]";
109 aErrorStream << "[" << aFunction << "] ";
110#endif
111
112 /*----------------------------------------------------------------
113 * 2. set error string from arguments
114 *----------------------------------------------------------------*/
115 for (aPtr = (char*)aFormat ; *aPtr ; ++aPtr)
116 {
117 if (*aPtr == '%')
118 {
119 switch (*++aPtr)
120 {
121 case 'f': /* double value */
122 aDoubleValue = va_arg(aVaList, double);
123 aErrorStream << aDoubleValue;
124 break;
125
126 case 'd':
127 aIntValue = va_arg(aVaList, int);
128 aErrorStream << aIntValue;
129 break;
130
131 case 's':
132 aStringValue = va_arg(aVaList, char *);
133 if (aStringValue != NULL)
134 aErrorStream << aStringValue;
135 break;
136
137 case 'p':
138 aHexValue = va_arg(aVaList, void *);
139 aErrorStream << std::hex << aHexValue;
140 break;
141
142 default:
143 aErrorStream << *aPtr;
144 break;
145 }
146 }
147 else
148 aErrorStream << *aPtr;
149 }
150 /*----------------------------------------------------------------*/
151
152 return aErrorStream.str();
153}
154
155void
156CRXException::AddMessage (const char * aMessage)
157{
158 mMessage += aMessage;
159}
Note: See TracBrowser for help on using the repository browser.