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
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 {
22 return fmt::format("{:>7.03f}us", static_cast<float>(us));
23 }
24
25 const auto ms = us / 1000.0f;
26 if (ms < 1000)
27 {
28 return fmt::format("{:>7.03f}ms", ms);
29 }
30
31 const auto s = ms / 1000.0f;
32 return fmt::format("{:>7.03f}s", s);
33 }
34
35 static std::chrono::microseconds default_max_time;
36
37 std::string message;
38 TClock::duration max_time;
39 TClock::time_point start_time;
40
42 {}
43
44 template <typename Rep, typename Period>
46 std::string m, const std::chrono::duration<Rep, Period>& mt) :
47 message(std::move(m)),
48 max_time(std::chrono::duration_cast<TClock::duration>(mt)),
49 start_time(TClock::now())
50 {}
51
53 {
54 const auto end_time = TClock::now();
55 const auto elapsed = end_time - start_time;
56 constexpr auto out_of_distribution_multiplier = 100;
57 if (elapsed > max_time * out_of_distribution_multiplier)
58 {
60 "Operation took too long ({}): {}", human_time(elapsed), message);
61 }
62 else if (elapsed > max_time)
63 {
65 "Operation took too long ({}): {}", human_time(elapsed), message);
66 }
67 }
68 };
69}
#define LOG_INFO_FMT
Definition internal_logger.h:15
#define LOG_FAIL_FMT
Definition internal_logger.h:16
Definition after_io.h:8
STL namespace.
Definition time_bound_logger.h:14
TClock::duration max_time
Definition time_bound_logger.h:38
std::chrono::steady_clock TClock
Definition time_bound_logger.h:15
~TimeBoundLogger()
Definition time_bound_logger.h:52
TimeBoundLogger(std::string m, const std::chrono::duration< Rep, Period > &mt)
Definition time_bound_logger.h:45
std::string message
Definition time_bound_logger.h:37
static std::chrono::microseconds default_max_time
Definition time_bound_logger.h:35
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:39
TimeBoundLogger(const std::string &m)
Definition time_bound_logger.h:41