From 4ccc28e7a51081ccb6c6772264da5b302012d362 Mon Sep 17 00:00:00 2001 From: kladko <13399135+kladkogex@users.noreply.github.com> Date: Fri, 20 Mar 2020 17:41:01 +0200 Subject: [PATCH] SKALE-2345 added logs --- Exception.cpp | 49 +++++++++++++++++++++++++ Exception.h | 48 +++++++++++++++++++++++++ InvalidArgumentException.cpp | 30 ++++++++++++++++ InvalidArgumentException.h | 32 +++++++++++++++++ InvalidStateException.cpp | 32 +++++++++++++++++ InvalidStateException.h | 32 +++++++++++++++++ Log.cpp | 62 ++++++++++++++++++++++++++++++++ Log.h | 69 ++++++++++++++++++++++++++++++++++++ Makefile.am | 13 +++---- common.h | 26 ++++++++++---- sgxwallet_common.h | 3 +- 11 files changed, 383 insertions(+), 13 deletions(-) create mode 100644 Exception.cpp create mode 100644 Exception.h create mode 100644 InvalidArgumentException.cpp create mode 100644 InvalidArgumentException.h create mode 100644 InvalidStateException.cpp create mode 100644 InvalidStateException.h create mode 100644 Log.cpp create mode 100644 Log.h diff --git a/Exception.cpp b/Exception.cpp new file mode 100644 index 0000000..83de22c --- /dev/null +++ b/Exception.cpp @@ -0,0 +1,49 @@ +/* + Copyright (C) 2018-2019 SKALE Labs + + This file is part of skale-consensus. + + 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 Exception.cpp + @author Stan Kladko + @date 2018 +*/ + + +#include "Log.h" +#include "Exception.h" + +void Exception::logNested(const std::exception &e, int level) +{ + string prefix; + + + if (level == 0) { + prefix = "!Exception:"; + } else { + prefix = "!Caused by:"; + } + if (dynamic_cast<const std::nested_exception*>(&e) == nullptr) { + LOG(err, string(level, ' ') + prefix + e.what()); + return; + } else { + LOG(err, string(level, ' ') + prefix + e.what()); + } + try { + std::rethrow_if_nested(e); + } catch(const std::exception& e) { + logNested(e, level + 1); + } catch(...) {} +}; \ No newline at end of file diff --git a/Exception.h b/Exception.h new file mode 100644 index 0000000..fb9b06b --- /dev/null +++ b/Exception.h @@ -0,0 +1,48 @@ +/* + Copyright (C) 2018-2019 SKALE Labs + + This file is part of skale-consensus. + + 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 Exception.h + @author Stan Kladko + @date 2018 +*/ + +#pragma once + + +class Exception : public std::exception { +public: + Exception( const std::string& _message, const std::string& _className ) { + message = _className + ":" + _message; + } + const char* what() const noexcept override { + return message.empty() ? std::exception::what() : message.c_str(); + } + + const std::string& getMessage() const { return message; } + + bool isFatal() const { return fatal; } + +private: + std::string message; + +protected: + bool fatal = false; + +public: + static void logNested( const std::exception& e, int level = 0 ); +}; diff --git a/InvalidArgumentException.cpp b/InvalidArgumentException.cpp new file mode 100644 index 0000000..492b2f9 --- /dev/null +++ b/InvalidArgumentException.cpp @@ -0,0 +1,30 @@ +/* + Copyright (C) 2018-2019 SKALE Labs + + This file is part of skale-consensus. + + 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 InvalidArgumentException.cpp + @author Stan Kladko + @date 2018 +*/ + +#include "Log.h" +#include "InvalidArgumentException.h" + +InvalidArgumentException::InvalidArgumentException(const std::string &_message, const string& _className) : + Exception(_message, _className) { + fatal = false; +} diff --git a/InvalidArgumentException.h b/InvalidArgumentException.h new file mode 100644 index 0000000..13d4bcc --- /dev/null +++ b/InvalidArgumentException.h @@ -0,0 +1,32 @@ +/* + Copyright (C) 2018-2019 SKALE Labs + + This file is part of skale-consensus. + + 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 InvalidArgumentException.h + @author Stan Kladko + @date 2018 +*/ + +#pragma once +#include "Exception.h" + +#include <string> + +class InvalidArgumentException : public Exception { +public: + InvalidArgumentException( const std::string& _message, const std::string& _className ); +}; diff --git a/InvalidStateException.cpp b/InvalidStateException.cpp new file mode 100644 index 0000000..4b50581 --- /dev/null +++ b/InvalidStateException.cpp @@ -0,0 +1,32 @@ +/* + Copyright (C) 2018-2019 SKALE Labs + + This file is part of skale-consensus. + + 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 InvalidStateException.cpp + @author Stan Kladko + @date 2018 +*/ + +#include "common.h" +#include "Log.h" +#include "InvalidStateException.h" + + +InvalidStateException::InvalidStateException(const std::string &_message, const string& _className) : + Exception(_message, _className) { + fatal = false; +} diff --git a/InvalidStateException.h b/InvalidStateException.h new file mode 100644 index 0000000..4809f68 --- /dev/null +++ b/InvalidStateException.h @@ -0,0 +1,32 @@ +/* + Copyright (C) 2018-2019 SKALE Labs + + This file is part of skale-consensus. + + 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 InvalidStateException.h + @author Stan Kladko + @date 2018 +*/ + +#pragma once + +#include "Exception.h" + + +class InvalidStateException : public Exception { +public: + InvalidStateException( const std::string& _message, const std::string& _className ); +}; diff --git a/Log.cpp b/Log.cpp new file mode 100644 index 0000000..782acdb --- /dev/null +++ b/Log.cpp @@ -0,0 +1,62 @@ +/* + Copyright (C) 2018-2019 SKALE Labs + + This file is part of skale-consensus. + + 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.cpp + @author Stan Kladko + @date 2018 +*/ + +#include "common.h" + +#include "Log.h" + +using namespace std; + + + + + +void Log::setGlobalLogLevel(string &_s) { + globalLogLevel = logLevelFromString(_s); +} + + + +level_enum Log::logLevelFromString(string &_s) { + + level_enum result = trace; + + if (_s == "trace") + result = trace; + else if (_s == "debug") + result = debug; + else if (_s == "info") + result = info; + else if (_s == "warn") + result = warn; + else if (_s == "err") + result = err; + else + throw InvalidArgumentException("Unknown level name " + _s, __CLASS_NAME__); + return result; +} + + + + + diff --git a/Log.h b/Log.h new file mode 100644 index 0000000..70be336 --- /dev/null +++ b/Log.h @@ -0,0 +1,69 @@ +/* + Copyright (C) 2018-2019 SKALE Labs + + This file is part of skale-consensus. + + 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 2018 +*/ + +#ifndef _LOG_H +#define _LOG_H + + +#include <stdlib.h> +#include <iostream> +#include <map> +#include <memory> + + +#include "InvalidArgumentException.h" +#include "InvalidStateException.h" + +#include "common.h" + +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); +}; +#endif diff --git a/Makefile.am b/Makefile.am index 9f74185..4c17ec9 100644 --- a/Makefile.am +++ b/Makefile.am @@ -66,11 +66,13 @@ bin_PROGRAMS = sgxwallet testw cert_util ## You can't use $(wildcard ...) with automake so all source files ## have to be explicitly listed. -COMMON_SRC = sgx_stub.c sgx_detect_linux.c create_enclave.c oc_alloc.c +COMMON_SRC = InvalidStateException.cpp Exception.cpp InvalidArgumentException.cpp Log.cpp \ + SGXWalletServer.cpp SGXRegistrationServer.cpp CSRManagerServer.cpp RPCException.cpp BLSCrypto.cpp ECDSACrypto.cpp \ + DKGCrypto.cpp ServerInit.cpp BLSPrivateKeyShareSGX.cpp LevelDB.cpp ServerDataChecker.cpp SEKManager.cpp \ + sgx_stub.c sgx_detect_linux.c create_enclave.c oc_alloc.c COMMON_ENCLAVE_SRC = secure_enclave_u.c secure_enclave_u.h -sgxwallet_SOURCES = sgxwallet.c SGXWalletServer.cpp SGXRegistrationServer.cpp CSRManagerServer.cpp RPCException.cpp BLSCrypto.cpp ECDSACrypto.cpp \ -DKGCrypto.cpp ServerInit.cpp BLSPrivateKeyShareSGX.cpp LevelDB.cpp ServerDataChecker.cpp SEKManager.cpp $(COMMON_SRC) +sgxwallet_SOURCES = sgxwallet.c $(COMMON_SRC) nodist_sgxwallet_SOURCES = $(COMMON_ENCLAVE_SRC) @@ -104,13 +106,12 @@ sgxwallet_LDADD=-l$(SGX_URTS_LIB) -l$(SGX_UAE_SERVICE_LIB) -LlibBLS/deps/deps_in -lgnutls -lgcrypt -lcurl -lssl -lcrypto -lz -lpthread -testw_SOURCES=testw.cpp stubclient.cpp SGXWalletServer.cpp RPCException.cpp BLSCrypto.cpp ServerInit.cpp LevelDB.cpp \ - DKGCrypto.cpp BLSPrivateKeyShareSGX.cpp ECDSACrypto.cpp ServerDataChecker.cpp SEKManager.cpp SGXRegistrationServer.cpp CSRManagerServer.cpp $(COMMON_SRC) +testw_SOURCES=testw.cpp $(COMMON_SRC) nodist_testw_SOURCES=${nodist_sgxwallet_SOURCES} EXTRA_testw_DEPENDENCIES=${EXTRA_sgxwallet_DEPENDENCIES} testw_LDADD= ${sgxwallet_LDADD} -cert_util_SOURCES=cert_util.cpp stubclient.cpp RPCException.cpp LevelDB.cpp SGXRegistrationServer.cpp CSRManagerServer.cpp +cert_util_SOURCES= InvalidStateException.cpp Exception.cpp InvalidArgumentException.cpp Log.cpp cert_util.cpp stubclient.cpp RPCException.cpp LevelDB.cpp SGXRegistrationServer.cpp CSRManagerServer.cpp cert_util_LDADD=-LlibBLS/deps/deps_inst/x86_or_x64/lib -Lleveldb/build -LlibBLS/build \ -LlibBLS/build/libff/libff \ -l:libbls.a -l:libleveldb.a \ diff --git a/common.h b/common.h index e8a3781..fa43e91 100644 --- a/common.h +++ b/common.h @@ -32,15 +32,29 @@ using namespace std; #include <map> #include <memory> -#define CHECK_ARGUMENT(_EXPRESSION_) \ - if (!(_EXPRESSION_)) { \ - auto __msg__ = string("Argument Check failed:") + #_EXPRESSION_ + "\n" + __CLASS_NAME__ + ":" + __FUNCTION__ + \ - + " " + string(__FILE__) + ":" + to_string(__LINE__); \ - throw runtime_error(__msg__);} + + + +#include "InvalidStateException.h" + + +inline std::string className(const std::string &prettyFunction) { + size_t colons = prettyFunction.find("::"); + if (colons == std::string::npos) + return "::"; + size_t begin = prettyFunction.substr(0, colons).rfind(" ") + 1; + size_t end = colons - begin; + + return prettyFunction.substr(begin, end); +} + + +#define __CLASS_NAME__ className( __PRETTY_FUNCTION__ ) #define CHECK_STATE(_EXPRESSION_) \ if (!(_EXPRESSION_)) { \ auto __msg__ = string("State check failed::") + #_EXPRESSION_ + " " + string(__FILE__) + ":" + to_string(__LINE__); \ - throw runtime_error(__msg__);} + throw InvalidStateException(__msg__, __CLASS_NAME__);} + #endif //SGXWALLET_COMMON_H diff --git a/sgxwallet_common.h b/sgxwallet_common.h index d0ef0df..3f086b9 100644 --- a/sgxwallet_common.h +++ b/sgxwallet_common.h @@ -30,11 +30,12 @@ #include <stdint.h> #include <string.h> - #include <stdbool.h> + + extern int printDebugInfo; extern int useHTTPS; extern int encryptKeys; -- 2.18.1