logger: add class for logging a scope's lifetime

This commit is contained in:
Moritz Bunkus 2022-04-26 22:25:14 +02:00
parent 7639fac210
commit 9a1f5290ba
No known key found for this signature in database
GPG Key ID: 74AF00ADF2E32C85
2 changed files with 24 additions and 0 deletions

View File

@ -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();

View File

@ -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)