From 9a1f5290ba141e721d95f3cbae8830344fb74bcc Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Tue, 26 Apr 2022 22:25:14 +0200 Subject: [PATCH] logger: add class for logging a scope's lifetime --- src/common/logger.cpp | 13 +++++++++++++ src/common/logger.h | 11 +++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/common/logger.cpp b/src/common/logger.cpp index 1fe67739b..6d9df5280 100644 --- a/src/common/logger.cpp +++ b/src/common/logger.cpp @@ -132,6 +132,19 @@ stderr_target_c::log_line(std::string const &message) { // ---------------------------------------------------------------------- +lifetime_logger_c::lifetime_logger_c(std::string const &comment) + : m_comment{comment} + , m_start{mtx::sys::get_current_time_millis()} +{ + target_c::get_default_logger() << fmt::format("lifetime log start for {0}\n", m_comment); +} + +lifetime_logger_c::~lifetime_logger_c() { + target_c::get_default_logger() << fmt::format("lifetime log runtime +{0}ms for {1}\n", mtx::sys::get_current_time_millis() - m_start, m_comment); +} + +// ---------------------------------------------------------------------- + void init() { s_program_start_time = QDateTime::currentDateTime(); diff --git a/src/common/logger.h b/src/common/logger.h index ee7238320..b3acb598d 100644 --- a/src/common/logger.h +++ b/src/common/logger.h @@ -43,6 +43,16 @@ public: static int64_t runtime(); }; +class lifetime_logger_c { +private: + std::string const m_comment; + int64_t m_start{}; + +public: + lifetime_logger_c(std::string const &comment); + ~lifetime_logger_c(); +}; + class file_target_c: public target_c { private: std::filesystem::path m_file_name; @@ -77,4 +87,5 @@ void init(); } #define log_current_location() mtx::log::target_c::get_default_logger() << fmt::format("Current file, line, function: {0}:{1} in {2}", __FILE__, __LINE__, __PRETTY_FUNCTION__) +#define log_scope_lifetime() mtx::log::lifetime_logger_c mtx_scope_lifetime_logger{fmt::format("{0}:{1} in {2}", __FILE__, __LINE__, __PRETTY_FUNCTION__)} #define log_it(arg) mtx::log::target_c::get_default_logger() << (arg)