GCC Code Coverage Report


Directory: gridformat/
File: gridformat/common/logging.hpp
Date: 2024-11-10 16:24:00
Exec Total Coverage
Lines: 30 32 93.8%
Functions: 10 10 100.0%
Branches: 33 62 53.2%

Line Branch Exec Source
1 // SPDX-FileCopyrightText: 2022-2023 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de>
2 // SPDX-License-Identifier: MIT
3 /*!
4 * \file
5 * \ingroup Common
6 * \brief Functionality for styling and logging strings.
7 */
8 #ifndef GRIDFORMAT_COMMON_LOGGING_HPP_
9 #define GRIDFORMAT_COMMON_LOGGING_HPP_
10
11 #include <array>
12 #include <ranges>
13 #include <utility>
14 #include <concepts>
15 #include <iostream>
16 #include <string_view>
17 #include <algorithm>
18 #include <string>
19
20 namespace GridFormat {
21
22 //! \addtogroup Common
23 //! \{
24
25 #ifndef DOXYGEN
26 namespace Detail {
27
28 template<std::size_t size>
29 struct AnsiiCodes {
30 template<std::integral... Args> requires(sizeof...(Args) == size)
31 constexpr explicit AnsiiCodes(Args&&... args) {
32 if constexpr (size > 0)
33 _set_code<0>(std::forward<Args>(args)...);
34 }
35
36 12118 std::string format(std::string_view msg) const {
37 12118 std::string result;
38
2/2
✓ Branch 2 taken 8024 times.
✓ Branch 3 taken 6059 times.
28166 for (auto _code : _codes)
39
2/4
✓ Branch 1 taken 8024 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8024 times.
✗ Branch 5 not taken.
16048 result += _format_code(_code);
40
1/2
✓ Branch 1 taken 6059 times.
✗ Branch 2 not taken.
12118 result += msg;
41
2/4
✓ Branch 1 taken 6059 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6059 times.
✗ Branch 5 not taken.
12118 result += _format_code(0);
42 12118 return result;
43 }
44
45 private:
46 std::array<int, size> _codes;
47
48 template<int i, std::integral T, std::integral... Ts>
49 constexpr void _set_code(T&& code, Ts&&... codes) {
50 _codes[i] = code;
51 if constexpr (sizeof...(Ts) > 0)
52 _set_code<i+1>(std::forward<Ts>(codes)...);
53 }
54
55 28166 std::string _format_code(int code) const {
56
3/6
✓ Branch 2 taken 14083 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 14083 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 14083 times.
✗ Branch 9 not taken.
84498 return std::string{"\033["} + std::to_string(code) + "m";
57 }
58 };
59
60 template<std::integral... Args>
61 AnsiiCodes(Args&&... args) -> AnsiiCodes<sizeof...(Args)>;
62
63 } // namespace Detail
64 #endif // DOXYGEN
65
66 //! Style the given string as a warning
67 1962 std::string as_warning(std::string_view msg) {
68 static constexpr Detail::AnsiiCodes codes{1, 33};
69 1962 return codes.format(msg);
70 }
71
72 //! Style the given string as an error
73 3 std::string as_error(std::string_view msg) {
74 static constexpr Detail::AnsiiCodes codes{1, 31};
75 3 return codes.format(msg);
76 }
77
78 //! Style the given string as highlighted
79 4094 std::string as_highlight(std::string_view msg) {
80 static constexpr Detail::AnsiiCodes codes{1};
81 4094 return codes.format(msg);
82 }
83
84 //! Log a warning message.
85 1962 void log_warning(std::string_view msg, std::ostream& s = std::cout) {
86
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1962 times.
1962 if (msg.empty())
87 return;
88
89 1962 constexpr std::string_view context = "[GFMT]";
90 1962 constexpr std::string_view prefix = "Warning";
91 1962 constexpr std::size_t indentation = context.size() + 1 + prefix.size() + 2;
92
5/10
✓ Branch 1 taken 1962 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1962 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1962 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1962 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1962 times.
✗ Branch 14 not taken.
1962 s << context << " " << as_warning(prefix) << ": ";
93
1/2
✓ Branch 1 taken 1962 times.
✗ Branch 2 not taken.
1962 std::ranges::for_each(
94
2/4
✓ Branch 1 taken 1962 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1962 times.
✗ Branch 5 not taken.
3924 std::views::split(msg, '\n') | std::views::transform([] (const auto& line_chars) {
95 1966 std::string line;
96
2/4
✓ Branch 1 taken 1966 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1966 times.
✗ Branch 5 not taken.
1966 std::ranges::copy(line_chars, std::back_inserter(line));
97 1966 return line;
98
1/2
✓ Branch 1 taken 1962 times.
✗ Branch 2 not taken.
1962 }),
99 1966 [&, i=0] (const std::string& line) mutable {
100
11/20
✓ Branch 0 taken 1962 times.
✓ Branch 1 taken 4 times.
✓ Branch 3 taken 1962 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 1966 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 1966 times.
✗ Branch 13 not taken.
✓ Branch 15 taken 1966 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 4 times.
✓ Branch 19 taken 1962 times.
✓ Branch 20 taken 1962 times.
✓ Branch 21 taken 4 times.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
3936 s << (i++ == 0 ? "" : std::string(indentation, ' ')) << line << std::endl;
101 1966 }
102 );
103 }
104
105 //! \} group Common
106
107 } // namespace GridFormat
108
109 #endif // GRIDFORMAT_COMMON_LOGGING_HPP_
110