-
Notifications
You must be signed in to change notification settings - Fork 35
/
Log.h
133 lines (106 loc) · 6.15 KB
/
Log.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
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
/*
*
Copyright (C) 2019-Present SKALE Labs
This file is part of sgxwallet.
skale-consensus is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
skale-consensus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with skale-consensus. If not, see <https://www.gnu.org/licenses/>.
@file Log.h
@author Stan Kladko
@date 2019
*/
#ifndef _LOG_H
#define _LOG_H
#include <iostream>
#include <map>
#include <memory>
#include <stdlib.h>
#include "third_party/spdlog/spdlog.h"
#include "json/json.h"
#include "InvalidArgumentException.h"
#include "InvalidStateException.h"
#include "SGXException.h"
#include "common.h"
#include <boost/core/ignore_unused.hpp>
#include <shared_mutex>
using namespace std;
class Exception;
#define __CLASS_NAME__ className(__PRETTY_FUNCTION__)
#define LOG(__SEVERITY__, __MESSAGE__) \
cerr << to_string(__SEVERITY__) << " " << __MESSAGE__ << " " \
<< className(__PRETTY_FUNCTION__) << endl;
enum level_enum { trace, debug, info, warn, err };
class Log {
public:
level_enum globalLogLevel;
void setGlobalLogLevel(string &_s);
static level_enum logLevelFromString(string &_s);
static void handleSGXException(Json::Value &_result, SGXException &_e);
};
#define COUNT_STATISTICS \
static uint64_t __COUNT__ = 0; \
__COUNT__++; \
if (__COUNT__ % 1000 == 0) { \
spdlog::info(string(__FUNCTION__) + " processed " + to_string(__COUNT__) + \
" requests"); \
struct sysinfo memInfo; \
sysinfo(&memInfo); \
long long totalPhysMem = memInfo.totalram; \
/*Multiply in next statement to avoid int overflow on right hand side...*/ \
totalPhysMem *= memInfo.mem_unit; \
int usedByCurrentProcess = getValue(); \
if (0.5 * totalPhysMem < usedByCurrentProcess) { \
exit(-103); \
} \
}
// if uknown error, the error is 10000 + line number
#define INIT_RESULT(__RESULT__) \
Json::Value __RESULT__; \
int errStatus = -1 * (10000 + __LINE__); \
boost::ignore_unused(errStatus); \
string errMsg(BUF_LEN, '\0'); \
__RESULT__["status"] = -1 * (10000 + __LINE__); \
__RESULT__["errorMessage"] = string(__FUNCTION__); \
string(__FUNCTION__) + ": server error. Please see server log.";
#define HANDLE_SGX_EXCEPTION(__RESULT__) \
catch (const SGXException &_e) { \
if (_e.getStatus() != 0) { \
__RESULT__["status"] = _e.getStatus(); \
} else { \
__RESULT__["status"] = -1 * (10000 + __LINE__); \
}; \
auto errStr = __FUNCTION__ + string(" failed:") + _e.getErrString(); \
__RESULT__["errorMessage"] = errStr; \
spdlog::error(errStr); \
return __RESULT__; \
} \
catch (const exception &_e) { \
__RESULT__["status"] = -1 * (10000 + __LINE__); \
exception_ptr p = current_exception(); \
auto errStr = __FUNCTION__ + string(" failed:") + \
p.__cxa_exception_type()->name() + ":" + _e.what(); \
__RESULT__["errorMessage"] = errStr; \
spdlog::error(errStr); \
return __RESULT__; \
} \
catch (...) { \
exception_ptr p = current_exception(); \
auto errStr = \
__FUNCTION__ + string(" failed:") + p.__cxa_exception_type()->name(); \
spdlog::error(errStr); \
__RESULT__["errorMessage"] = errStr; \
spdlog::error(errStr); \
return __RESULT__; \
}
#define RETURN_SUCCESS(__RESULT__) \
__RESULT__["status"] = 0; \
__RESULT__["errorMessage"] = ""; \
return __RESULT__;
#endif