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
244
245
246
247
248
249
250
251
252
|
#pragma once
/**
@file
@brief definition of abstruct exception class
@author MITSUNARI Shigeo(@herumi)
*/
#ifdef CYBOZU_MINIMUM_EXCEPTION
#include <cybozu/inttype.hpp>
namespace cybozu {
namespace exception {
inline const char *makeString(const char *, size_t)
{
return "";
}
} // cybozu::exception
class Exception {
public:
explicit Exception(const char* = 0, bool = true)
{
}
~Exception() CYBOZU_NOEXCEPT {}
const char *what() const CYBOZU_NOEXCEPT { return "cybozu:Exception"; }
template<class T>
Exception& operator<<(const T&)
{
return *this;
}
};
} // cybozu
#else
#include <string>
#include <algorithm>
#include <sstream>
#include <errno.h>
#include <stdio.h>
#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
#else
#include <string.h> // for strerror_r
#endif
#include <cybozu/inttype.hpp>
#ifdef CYBOZU_EXCEPTION_WITH_STACKTRACE
#include <cybozu/stacktrace.hpp>
#endif
namespace cybozu {
const bool DontThrow = true;
namespace exception {
/* get max 16 characters to avoid buffer overrun */
inline std::string makeString(const char *str, size_t size)
{
return std::string(str, std::min<size_t>(size, 16));
}
#ifdef _WIN32
inline std::string wstr2str(const std::wstring& wstr)
{
std::string str;
for (size_t i = 0; i < wstr.size(); i++) {
uint16_t c = wstr[i];
if (c < 0x80) {
str += char(c);
} else {
char buf[16];
CYBOZU_SNPRINTF(buf, sizeof(buf), "\\u%04x", c);
str += buf;
}
}
return str;
}
#endif
} // cybozu::exception
/**
convert errno to string
@param err [in] errno
@note for both windows and linux
*/
inline std::string ConvertErrorNoToString(int err)
{
char errBuf[256];
std::string ret;
#ifdef _WIN32
if (strerror_s(errBuf, sizeof(errBuf), err) == 0) {
ret = errBuf;
} else {
ret = "err";
}
#elif defined(_GNU_SOURCE)
ret = ::strerror_r(err, errBuf, sizeof(errBuf));
#else
if (strerror_r(err, errBuf, sizeof(errBuf)) == 0) {
ret = errBuf;
} else {
ret = "err";
}
#endif
char buf2[64];
CYBOZU_SNPRINTF(buf2, sizeof(buf2), "(%d)", err);
ret += buf2;
return ret;
}
class Exception : public std::exception {
mutable std::string str_;
#ifdef CYBOZU_EXCEPTION_WITH_STACKTRACE
mutable std::string stackTrace_;
#endif
public:
explicit Exception(const std::string& name = "", bool enableStackTrace = true)
: str_(name)
{
#ifdef CYBOZU_EXCEPTION_WITH_STACKTRACE
if (enableStackTrace) stackTrace_ = cybozu::StackTrace().toString();
#else
cybozu::disable_warning_unused_variable(enableStackTrace);
#endif
}
~Exception() CYBOZU_NOEXCEPT {}
const char *what() const CYBOZU_NOEXCEPT { return toString().c_str(); }
const std::string& toString() const CYBOZU_NOEXCEPT
{
#ifdef CYBOZU_EXCEPTION_WITH_STACKTRACE
try {
if (!stackTrace_.empty()) {
#ifdef CYBOZU_STACKTRACE_ONELINE
str_ += "\n<<<STACKTRACE>>> ";
str_ += stackTrace_;
#else
str_ += "\n<<<STACKTRACE\n";
str_ += stackTrace_;
str_ += "\n>>>STACKTRACE";
#endif
}
} catch (...) {
}
stackTrace_.clear();
#endif
return str_;
}
Exception& operator<<(const char *s)
{
str_ += ':';
str_ += s;
return *this;
}
Exception& operator<<(const std::string& s)
{
return operator<<(s.c_str());
}
#ifdef _WIN32
Exception& operator<<(const std::wstring& s)
{
return operator<<(cybozu::exception::wstr2str(s));
}
#endif
template<class T>
Exception& operator<<(const T& x)
{
std::ostringstream os;
os << x;
return operator<<(os.str());
}
};
class ErrorNo {
public:
#ifdef _WIN32
typedef unsigned int NativeErrorNo;
#else
typedef int NativeErrorNo;
#endif
explicit ErrorNo(NativeErrorNo err)
: err_(err)
{
}
ErrorNo()
: err_(getLatestNativeErrorNo())
{
}
NativeErrorNo getLatestNativeErrorNo() const
{
#ifdef _WIN32
return ::GetLastError();
#else
return errno;
#endif
}
/**
convert NativeErrNo to string(maybe UTF8)
@param err [in] errno
@note Linux : same as ConvertErrorNoToString
Windows : for Win32 API(use en-us)
*/
std::string toString() const
{
#ifdef _WIN32
const int msgSize = 256;
wchar_t msg[msgSize];
int size = FormatMessageW(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
0,
err_,
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
msg,
msgSize,
NULL
);
if (size <= 0) return "";
// remove last "\r\n"
if (size > 2 && msg[size - 2] == '\r') {
msg[size - 2] = 0;
size -= 2;
}
std::string ret;
ret.resize(size);
// assume ascii only
for (int i = 0; i < size; i++) {
ret[i] = (char)msg[i];
}
char buf2[64];
CYBOZU_SNPRINTF(buf2, sizeof(buf2), "(%u)", err_);
ret += buf2;
return ret;
#else
return ConvertErrorNoToString(err_);
#endif
}
private:
NativeErrorNo err_;
};
inline std::ostream& operator<<(std::ostream& os, const cybozu::ErrorNo& self)
{
return os << self.toString();
}
} // cybozu
#endif
|