CCF
Loading...
Searching...
No Matches
time_bound_logger.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
5#include "ccf/ds/logger.h"
6
7#include <chrono>
8#include <fmt/format.h>
9#include <string>
10
11namespace asynchost
12{
14 {
15 using TClock = std::chrono::steady_clock;
16 static std::string human_time(const TClock::duration& d)
17 {
18 const auto us =
19 std::chrono::duration_cast<std::chrono::microseconds>(d).count();
20 if (us < 1000)
21 return fmt::format("{:>7.03f}us", static_cast<float>(us));
22
23 const auto ms = us / 1000.0f;
24 if (ms < 1000)
25 return fmt::format("{:>7.03f}ms", ms);
26
27 const auto s = ms / 1000.0f;
28 return fmt::format("{:>7.03f}s", s);
29 }
30
31 static std::chrono::microseconds default_max_time;
32
33 std::string message;
34 TClock::duration max_time;
35 TClock::time_point start_time;
36
38 {}
39
40 template <typename Rep, typename Period>
42 const std::string& m, const std::chrono::duration<Rep, Period>& mt) :
43 message(m),
44 max_time(std::chrono::duration_cast<TClock::duration>(mt)),
45 start_time(TClock::now())
46 {}
47
49 {
50 const auto end_time = TClock::now();
51 const auto elapsed = end_time - start_time;
52 if (elapsed > max_time)
53 {
55 "Operation took too long ({}): {}", human_time(elapsed), message);
56 }
57 }
58 };
59}
#define LOG_INFO_FMT
Definition logger.h:362
Definition after_io.h:8
STL namespace.
Definition time_bound_logger.h:14
TClock::duration max_time
Definition time_bound_logger.h:34
std::chrono::steady_clock TClock
Definition time_bound_logger.h:15
~TimeBoundLogger()
Definition time_bound_logger.h:48
std::string message
Definition time_bound_logger.h:33
static std::chrono::microseconds default_max_time
Definition time_bound_logger.h:31
static std::string human_time(const TClock::duration &d)
Definition time_bound_logger.h:16
TClock::time_point start_time
Definition time_bound_logger.h:35
TimeBoundLogger(const std::string &m)
Definition time_bound_logger.h:37
TimeBoundLogger(const std::string &m, const std::chrono::duration< Rep, Period > &mt)
Definition time_bound_logger.h:41