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 |
|
---|
15 | CRXException::CRXException (void)
|
---|
16 | : mCode (0)
|
---|
17 | {
|
---|
18 | /*----------------------------------------------------------------*/
|
---|
19 | /*----------------------------------------------------------------*/
|
---|
20 | }
|
---|
21 |
|
---|
22 | CRXException::~CRXException (void)
|
---|
23 | {
|
---|
24 | /*----------------------------------------------------------------*/
|
---|
25 | /*----------------------------------------------------------------*/
|
---|
26 | }
|
---|
27 |
|
---|
28 | int
|
---|
29 | CRXException::GetErrorCode (void) const
|
---|
30 | {
|
---|
31 | /*----------------------------------------------------------------*/
|
---|
32 | /*----------------------------------------------------------------*/
|
---|
33 | return mCode;
|
---|
34 | }
|
---|
35 |
|
---|
36 | std::string
|
---|
37 | CRXException::GetErrorMessage (void) const
|
---|
38 | {
|
---|
39 | /*----------------------------------------------------------------*/
|
---|
40 | /*----------------------------------------------------------------*/
|
---|
41 | return mMessage;
|
---|
42 | }
|
---|
43 |
|
---|
44 | int
|
---|
45 | CRXException::GetSystemErrorCode (void)
|
---|
46 | {
|
---|
47 | /*----------------------------------------------------------------*/
|
---|
48 | /*----------------------------------------------------------------*/
|
---|
49 | #ifdef _WIN32
|
---|
50 | return GetLastError ();
|
---|
51 | #else
|
---|
52 | return errno;
|
---|
53 | #endif
|
---|
54 | }
|
---|
55 |
|
---|
56 | void
|
---|
57 | CRXException::SetErrorCode (const int aCode)
|
---|
58 | {
|
---|
59 | /*----------------------------------------------------------------*/
|
---|
60 | mCode = aCode;
|
---|
61 | /*----------------------------------------------------------------*/
|
---|
62 | }
|
---|
63 |
|
---|
64 | void
|
---|
65 | CRXException::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 |
|
---|
81 | std::string
|
---|
82 | CRXException::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 |
|
---|
155 | void
|
---|
156 | CRXException::AddMessage (const char * aMessage)
|
---|
157 | {
|
---|
158 | mMessage += aMessage;
|
---|
159 | }
|
---|