forked from joyieldInc/predixy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathException.h
63 lines (57 loc) · 1.46 KB
/
Exception.h
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
/*
* predixy - A high performance and full features proxy for redis.
* Copyright (C) 2017 Joyield, Inc. <[email protected]>
* All rights reserved.
*/
#ifndef _PREDIXY_EXCEPTION_H_
#define _PREDIXY_EXCEPTION_H_
#include <exception>
#include <stdarg.h>
#include <stdio.h>
class ExceptionBase : public std::exception
{
public:
static const int MaxMsgLen = 1024;
public:
ExceptionBase()
{
mMsg[0] = '\0';
}
ExceptionBase(const char* file, int line, const char* fmt, ...)
{
int n = snprintf(mMsg, sizeof(mMsg), "%s:%d ", file, line);
va_list ap;
va_start(ap, fmt);
vsnprintf(mMsg + n, sizeof(mMsg) - n, fmt, ap);
va_end(ap);
}
ExceptionBase(const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsnprintf(mMsg, sizeof(mMsg), fmt, ap);
va_end(ap);
}
~ExceptionBase()
{
}
const char* what() const noexcept
{
return mMsg;
}
protected:
void init(const char* fmt, va_list ap)
{
vsnprintf(mMsg, sizeof(mMsg), fmt, ap);
}
private:
char mMsg[MaxMsgLen];
};
#define DefException(T) class T : public ExceptionBase \
{ \
public: \
template<class... A> \
T(A&&... args):ExceptionBase(args...) {} \
}
#define Throw(T, ...) throw T(__FILE__, __LINE__, ##__VA_ARGS__)
#endif