• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #882

31 Aug 2023 07:44PM UTC coverage: 41.798% (-44.7%) from 86.546%
#882

push

19442 of 46514 relevant lines covered (41.8%)

126375.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.88
/libs/core/logging/src/format/formatter/high_precision_time.cpp
1
// high_precision_time.hpp
2

3
// Boost Logging library
4
//
5
// Author: John Torjo, www.torjo.com
6
//
7
// Copyright (C) 2007 John Torjo (see www.torjo.com for email)
8
//
9
//  SPDX-License-Identifier: BSL-1.0
10
// Distributed under the Boost Software License, Version 1.0.
11
//    (See accompanying file LICENSE_1_0.txt or copy at
12
//          http://www.boost.org/LICENSE_1_0.txt)
13
//
14
// See http://www.boost.org for updates, documentation, and revision history.
15
// See http://www.torjo.com/log2/ for more details
16

17
#include <hpx/config.hpp>
18
#include <hpx/assert.hpp>
19
#include <hpx/logging/format/formatters.hpp>
20
#include <hpx/modules/format.hpp>
21

22
#include <chrono>
23
#include <cstdint>
24
#include <cstring>
25
#include <ctime>
26
#include <memory>
27
#include <ostream>
28
#include <string>
29

30
#if !(defined(__linux) || defined(linux) || defined(__linux__) ||              \
31
    defined(__FreeBSD__) || defined(__APPLE__) || defined(HPX_MSVC))
32
#include <hpx/modules/thread_support.hpp>
33

34
#include <mutex>
35
#endif
36

37
namespace hpx::util::logging::formatter {
38

39
    high_precision_time::~high_precision_time() = default;
40

832✔
41
    struct high_precision_time_impl final : high_precision_time
42
    {
43
        /**
44
            constructs a high_precision_time object
45
        */
46
        explicit high_precision_time_impl(std::string const& format)
47
          : high_precision_time(format)
48
        {
832✔
49
        }
50

51
        void operator()(std::ostream& to) const override
52
        {
×
53
            auto const val = std::chrono::system_clock::now();
54
            std::time_t const tt = std::chrono::system_clock::to_time_t(val);
×
55

×
56
#if defined(__linux) || defined(linux) || defined(__linux__) ||                \
57
    defined(__FreeBSD__) || defined(__APPLE__)
58
            std::tm local_tm;
59
            localtime_r(&tt, &local_tm);
60
#elif defined(HPX_MSVC)
×
61
            std::tm local_tm{};
62
            [[maybe_unused]] auto const err = localtime_s(&local_tm, &tt);
63
            HPX_ASSERT(err == 0);
64
#else
65
            // fall back to non-thread-safe version on other platforms
66
            std::tm local_tm{};
67
            {
68
                static hpx::util::detail::spinlock mutex;
69
                std::lock_guard<hpx::util::detail::spinlock> ul(mutex);
70
                if (auto const* lt = std::localtime(&tt); lt != nullptr)
71
                {
72
                    local_tm = *lt;
73
                }
74
            }
75
#endif
76

77
            auto const nanosecs =
78
                std::chrono::duration_cast<std::chrono::nanoseconds>(
79
                    val.time_since_epoch());
80
            auto const microsecs =
81
                std::chrono::duration_cast<std::chrono::microseconds>(
82
                    val.time_since_epoch());
83
            auto const millisecs =
84
                std::chrono::duration_cast<std::chrono::milliseconds>(
85
                    val.time_since_epoch());
86

87
            util::format_to(to, m_format, local_tm.tm_mday, local_tm.tm_mon + 1,
88
                local_tm.tm_year + 1900, local_tm.tm_year % 100,
×
89
                local_tm.tm_hour, local_tm.tm_min, local_tm.tm_sec,
×
90
                millisecs.count() % 1000, microsecs.count() % 1000,
91
                nanosecs.count() % 1000);
×
92
        }
×
93

×
94
        /** @brief configure through script
95

96
            the string = the time format
97
        */
98
        void configure(std::string const& str) override
99
        {
128✔
100
            m_format = str;
101
            replace_format("$dd", "{1:02d}");
128✔
102
            replace_format("$MM", "{2:02d}");
128✔
103
            replace_format("$yyyy", "{3:04d}");
128✔
104
            replace_format("$yy", "{4:02d}");
128✔
105
            replace_format("$hh", "{5:02d}");
128✔
106
            replace_format("$mm", "{6:02d}");
128✔
107
            replace_format("$ss", "{7:02d}");
128✔
108
            replace_format("$mili", "{8:03d}");
128✔
109
            replace_format("$micro", "{9:06d}");
128✔
110
            replace_format("$nano", "{10:09d}");
128✔
111
        }
128✔
112

128✔
113
    private:
114
        bool replace_format(char const* from, char const* to)
115
        {
1,280✔
116
            size_t const start_pos = m_format.find(from);
117
            if (start_pos == std::string::npos)
1,280✔
118
                return false;
1,280✔
119

120
            // MSVC: using std::string::replace triggers ASAN reports
121
            // m_format.replace(start_pos, std::strlen(from), to);
122
            std::string fmt(m_format.substr(0, start_pos));
123
            fmt += to;
512✔
124
            fmt += m_format.substr(start_pos + std::strlen(from));
125
            m_format = HPX_MOVE(fmt);
512✔
126
            return true;
512✔
127
        }
128

129
        std::string m_format;
130
    };
131

132
    std::unique_ptr<high_precision_time> high_precision_time::make(
133
        std::string const& format)
832✔
134
    {
135
        return std::make_unique<high_precision_time_impl>(format);
136
    }
832✔
137
}    // namespace hpx::util::logging::formatter
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc