diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2021-05-07 02:18:07 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2021-05-07 02:18:07 +0300 |
commit | 7e743b73433475df086fcec81be7b10c1d695a42 (patch) | |
tree | 1737c5f9bdad2a40f740e9a655e510641331b9e2 /src/logging.h |
initial
Diffstat (limited to 'src/logging.h')
-rw-r--r-- | src/logging.h | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/logging.h b/src/logging.h new file mode 100644 index 0000000..2e84198 --- /dev/null +++ b/src/logging.h @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: BSD-3-Clause + +#ifndef INVERTER_TOOLS_LOGGING_H +#define INVERTER_TOOLS_LOGGING_H + +#include <iostream> +#include <string> +#include <string_view> + +class custom_log +{ +private: + std::ostream& os_; + +public: + custom_log(std::ostream& os, const std::string& func) : os_(os) { + os_ << func << ": "; + } + + template <class T> + custom_log &operator<<(const T &v) { + os_ << v; + return *this; + } + + ~custom_log() { + os_ << std::endl; + } +}; + +inline std::string method_name(const std::string& function, const std::string& pretty) { + size_t locFunName = pretty.find(function); + size_t begin = pretty.rfind(" ", locFunName) + 1; + size_t end = pretty.find("(", locFunName + function.length()); + return pretty.substr(begin, end - begin) + "()"; + } + +#define __METHOD_NAME__ method_name(__FUNCTION__, __PRETTY_FUNCTION__) + +#define mylog custom_log(std::cout, __METHOD_NAME__) +#define myerr custom_log(std::cerr, __METHOD_NAME__) + +#endif //INVERTER_TOOLS_LOGGING_H |