Log.h 3.78 KB
Newer Older
kladko's avatar
kladko committed
1
/*
kladko's avatar
kladko committed
2
 *
3
    Copyright (C) 2019-Present SKALE Labs
kladko's avatar
kladko committed
4

5
    This file is part of sgxwallet.
kladko's avatar
kladko committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

    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
22
    @date 2019
kladko's avatar
kladko committed
23 24
*/

kladko's avatar
kladko committed
25

kladko's avatar
kladko committed
26 27 28 29 30 31 32 33 34
#ifndef _LOG_H
#define _LOG_H


#include <stdlib.h>
#include <iostream>
#include <map>
#include <memory>

kladko's avatar
kladko committed
35
#include "json/json.h"
36
#include "third_party/spdlog/spdlog.h"
kladko's avatar
kladko committed
37

kladko's avatar
kladko committed
38 39

#include "SGXException.h"
kladko's avatar
kladko committed
40 41 42
#include "InvalidArgumentException.h"
#include "InvalidStateException.h"

kladko's avatar
kladko committed
43
#include <boost/core/ignore_unused.hpp>
kladko's avatar
kladko committed
44 45
#include "common.h"

kladko's avatar
kladko committed
46 47
#include <shared_mutex>

kladko's avatar
kladko committed
48 49 50 51 52 53 54 55
using namespace std;


class Exception;


#define __CLASS_NAME__ className( __PRETTY_FUNCTION__ )

kladko's avatar
kladko committed
56
#define LOG(__SEVERITY__, __MESSAGE__) \
kladko's avatar
kladko committed
57 58 59
    cerr <<  to_string(__SEVERITY__) << " " <<  __MESSAGE__ << " " << className( __PRETTY_FUNCTION__ ) << endl;


kladko's avatar
kladko committed
60 61 62
enum level_enum {
    trace, debug, info, warn, err
};
kladko's avatar
kladko committed
63 64 65 66 67 68 69 70


class Log {

public:

    level_enum globalLogLevel;

kladko's avatar
kladko committed
71
    void setGlobalLogLevel(string &_s);
kladko's avatar
kladko committed
72 73

    static level_enum logLevelFromString(string &_s);
kladko's avatar
kladko committed
74 75

    static void handleSGXException(Json::Value &_result, SGXException &_e);
kladko's avatar
kladko committed
76
};
kladko's avatar
kladko committed
77

78 79 80 81 82 83

#define COUNT_STATISTICS \
static uint64_t __COUNT__ = 0; \
__COUNT__++; \
if (__COUNT__ % 1000 == 0) { \
spdlog::info(string(__FUNCTION__) +  " processed " + to_string(__COUNT__) + " requests"); \
84 85 86 87 88 89
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(); \
Oleh Nikolaiev's avatar
Oleh Nikolaiev committed
90
if ( 0.5 * totalPhysMem < usedByCurrentProcess ) { \
91 92
  exit(-103); \
} \
93 94 95
}


96

kladko's avatar
kladko committed
97 98
// if uknown error, the error is 10000 + line number

99

kladko's avatar
kladko committed
100
#define INIT_RESULT(__RESULT__)     Json::Value __RESULT__; \
kladko's avatar
kladko committed
101 102 103
              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.";
kladko's avatar
kladko committed
104

kladko's avatar
kladko committed
105
#define HANDLE_SGX_EXCEPTION(__RESULT__) \
kladko's avatar
kladko committed
106
    catch (const SGXException& _e) { \
kladko's avatar
kladko committed
107
      if (_e.getStatus() != 0) {__RESULT__["status"] = _e.getStatus();} else { __RESULT__["status"]  = -1 * (10000 + __LINE__);}; \
kladko's avatar
kladko committed
108 109 110
      auto errStr = __FUNCTION__ + string(" failed:") + _e.getErrString(); \
      __RESULT__["errorMessage"] = errStr; \
      spdlog::error(errStr); \
kladko's avatar
kladko committed
111
      return __RESULT__; \
kladko's avatar
kladko committed
112
      } catch (const exception& _e) { \
kladko's avatar
kladko committed
113
      __RESULT__["status"]  = -1 * (10000 + __LINE__); \
kladko's avatar
kladko committed
114
      exception_ptr p = current_exception(); \
kladko's avatar
kladko committed
115 116 117
      auto errStr = __FUNCTION__ + string(" failed:") + p.__cxa_exception_type()->name() + ":" + _e.what(); \
      __RESULT__["errorMessage"] = errStr; \
      spdlog::error(errStr); \
kladko's avatar
kladko committed
118
      return __RESULT__; \
kladko's avatar
kladko committed
119
      } \
kladko's avatar
kladko committed
120 121
      catch (...) { \
      exception_ptr p = current_exception(); \
kladko's avatar
kladko committed
122 123 124 125
      auto errStr = __FUNCTION__ + string(" failed:") + p.__cxa_exception_type()->name(); \
      spdlog::error(errStr); \
      __RESULT__["errorMessage"] = errStr ; \
      spdlog::error(errStr); \
kladko's avatar
kladko committed
126 127
      return __RESULT__; \
      }
kladko's avatar
kladko committed
128 129 130 131 132 133

#define RETURN_SUCCESS(__RESULT__) \
    __RESULT__["status"] = 0; \
    __RESULT__["errorMessage"] = ""; \
    return __RESULT__;

kladko's avatar
kladko committed
134
#endif
kladko's avatar
kladko committed
135