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