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