LogIt++
Header-only, macro-first C++ logging library
Loading...
Searching...
No Matches
ScopeTimer.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_DETAIL_SCOPETIMER_HPP_INCLUDED
3#define LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_DETAIL_SCOPETIMER_HPP_INCLUDED
4
7
8#include <chrono>
9#include <string>
10
11namespace logit { namespace detail {
12
13 class ScopeTimer {
14 public:
16 std::string phase,
17 const char* file,
18 int line,
19 const char* function,
20 int logger_index,
21 int64_t threshold_ms = 0)
22 : level_(level)
23 , phase_(std::move(phase))
24 , file_(file)
25 , line_(line)
26 , function_(function)
27 , logger_index_(logger_index)
28 , threshold_ms_(threshold_ms)
29 , t0_(std::chrono::steady_clock::now()) {}
30
32 const auto t1 = std::chrono::steady_clock::now();
33 const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0_).count();
34 if (ms < threshold_ms_) return;
35
36 std::string msg = phase_;
37 msg += " | duration_ms=";
38 msg += std::to_string(ms);
39
41 level_,
44 line_,
46 msg, std::string(),
48 false
49 });
50 }
51
52 private:
54 std::string phase_;
55 const char* file_;
56 int line_;
57 const char* function_;
60 std::chrono::steady_clock::time_point t0_;
61 };
62
63}} // namespace logit::detail
64
65#endif // LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_DETAIL_SCOPETIMER_HPP_INCLUDED
auto log_and_return(const LogRecord &record, Ts &&... args) -> decltype(std::forward_as_tuple(std::forward< Ts >(args)...))
Logs message and returns tuple of arguments.
Definition Logger.hpp:386
static Logger & get_instance()
Retrieves singleton instance of Logger.
Definition Logger.hpp:53
ScopeTimer(LogLevel level, std::string phase, const char *file, int line, const char *function, int logger_index, int64_t threshold_ms=0)
std::chrono::steady_clock::time_point t0_
#define LOGIT_WALLCLOCK_MS()
Definition config.hpp:68
#define LOGIT_BASE_PATH
Defines the base path used for log file paths.
Definition config.hpp:14
The primary namespace for the LogIt++ library.
LogLevel
Logging levels.
Definition enums.hpp:15
std::string make_relative(const std::string &file_path, const std::string &base_path)
Computes the relative path from base_path to file_path.
Stores log metadata and content.
Definition LogRecord.hpp:17