srs-control
0.2.2
Toggle main menu visibility
Loading...
Searching...
No Matches
srs_check.cpp
Go to the documentation of this file.
1
#include "
srs/utils/CommonDefinitions.hpp
"
2
#include "
srs/utils/CommonFunctions.hpp
"
3
#include <CLI/CLI.hpp>
4
#include <array>
5
#include <cassert>
6
#include <concepts>
7
#include <cstdio>
8
#include <cstdlib>
9
#include <ctre.hpp>
10
#include <ctre/wrapper.hpp>
11
#include <exception>
12
#include <fmt/base.h>
13
#include <fmt/compile.h>
14
#include <format>
15
#include <fstream>
16
#include <ios>
17
#include <iterator>
18
#include <print>
19
#include <ranges>
20
#include <stdexcept>
21
#include <string>
22
#include <string_view>
23
#include <unordered_map>
24
#include <vector>
25
26
namespace
27
{
28
template
<std::regular_invocable<> auto fmt, std::regular_invocable<> auto str>
29
consteval
auto
compile_time_format_size()
30
{
31
const
auto
output_str = fmt::format(FMT_COMPILE(
fmt
()), str());
32
return
output_str.size();
33
}
34
35
template
<std::regular_invocable<> auto fmt, std::regular_invocable<> auto str>
36
consteval
auto
compile_time_format()
37
{
38
constexpr
auto
size = compile_time_format_size<fmt, str>();
39
auto
static_str = std::array<char, size>{};
40
fmt::format_to(static_str.data(), FMT_COMPILE(
fmt
()), str());
41
return
static_str;
42
}
43
44
}
// namespace
45
46
namespace
srs
47
{
48
class
LogFile
49
{
50
public
:
51
LogFile
(
const
std::string& filename)
52
:
input_file_
{ filename, std::ios::in }
53
{
54
if
(not
input_file_
.is_open())
55
{
56
throw
std::runtime_error(std::format(
"failed to open the file {}"
, filename));
57
}
58
grow_to
(
default_init_n_lines
);
59
}
60
61
void
read
()
62
{
63
while
(
true
)
64
{
65
if
(
line_counter_
>=
content_
.size())
66
{
67
grow_to
(
content_
.size() * 2);
68
}
69
content_
[
line_counter_
].clear();
70
if
(not std::getline(
input_file_
,
content_
[
line_counter_
]))
71
{
72
break
;
73
}
74
if
(
content_
[
line_counter_
].ends_with(
common::file_logger_new_instance_str
))
75
{
76
line_counter_
= 0;
77
}
78
else
79
{
80
++
line_counter_
;
81
}
82
}
83
}
84
85
auto
summarize
() ->
bool
86
{
87
if
(
call_map_
.empty())
88
{
89
std::println(stderr,
"Error: No functions are recorded!"
);
90
return
true
;
91
}
92
93
auto
filter_view =
94
call_map_
| std::views::filter([](
const
auto
& pair) ->
bool
{
return
pair.second != 0; });
95
96
if
(std::ranges::distance(filter_view.begin(), filter_view.end()) == 0)
97
{
98
std::println(
"All recorded scopes have exited correctly!"
);
99
return
true
;
100
}
101
102
std::println(
"Following recorded scopes didn't exit:"
);
103
for
(
const
auto
& [key, value] : filter_view)
104
{
105
std::println(
"\t{}"
, key);
106
}
107
return
false
;
108
}
109
110
void
print_last_log
()
111
{
112
for
(
const
auto
& line_str :
content_
| std::views::take(
line_counter_
))
113
{
114
std::println(
"{}"
, line_str);
115
}
116
}
117
118
void
analysis
()
119
{
120
121
constexpr
auto
begin_str = compile_time_format<[] {
return
"\\[.*\\]\\[.+\\]\\[\\d+\\] {} (.*)"
; },
122
[] {
return
srs::common::exit_logger_begin
; }>();
123
constexpr
auto
end_str = compile_time_format<[] {
return
"\\[.*\\]\\[.+\\]\\[\\d+\\] {} (.*)"
; },
124
[] {
return
srs::common::exit_logger_end
; }>();
125
for
(
const
auto
& line_str :
content_
| std::views::take(
line_counter_
))
126
{
127
if
(
auto
is_match = ctre::match<begin_str>(line_str))
128
{
129
auto
entry =
call_map_
.try_emplace(std::string{ is_match.get<1>().to_view() }, 0).first;
130
++(entry->second);
131
}
132
else
if
(
auto
is_match = ctre::match<end_str>(line_str))
133
{
134
auto
entry =
call_map_
.try_emplace(std::string{ is_match.get<1>().to_view() }, 0).first;
135
--(entry->second);
136
}
137
}
138
}
139
140
private
:
141
static
const
auto
default_init_n_lines
= 1000;
142
static
const
auto
default_init_n_chars_per_line
= 300;
143
std::ifstream
input_file_
;
144
std::size_t
line_counter_
= 0;
145
std::vector<std::string>
content_
;
146
std::unordered_map<std::string, int>
call_map_
;
147
148
void
grow_to
(std::size_t n_lines)
149
{
150
if
(n_lines <=
content_
.size())
151
{
152
return
;
153
}
154
const
auto
old_size =
content_
.size();
155
content_
.resize(n_lines);
156
for
(
auto
& str :
content_
| std::views::drop(old_size))
157
{
158
str.reserve(
default_init_n_chars_per_line
);
159
}
160
}
161
};
162
}
// namespace srs
163
164
// NOLINTNEXTLINE (bugprone-exception-escape)
165
auto
main
(
int
argc,
char
** argv) ->
int
166
{
167
168
auto
cli_args = CLI::App{
"Logger checking of srs-control program."
};
169
170
auto
default_log_filename =
srs::common::get_default_log_path
().string();
171
172
auto
is_show_last_log =
false
;
173
174
try
175
{
176
cli_args.add_option(
"file"
, default_log_filename,
"Set the path to the log file"
)
177
->capture_default_str()
178
->expected(0, 1);
179
cli_args.add_flag(
"-l, --show-last-log"
, is_show_last_log,
"Show last log content"
);
180
181
cli_args.parse(argc, argv);
182
183
auto
log_file =
srs::LogFile
(default_log_filename);
184
185
log_file.read();
186
187
if
(is_show_last_log)
188
{
189
log_file.print_last_log();
190
return
EXIT_SUCCESS;
191
}
192
193
log_file.analysis();
194
if
(not log_file.summarize())
195
{
196
return
EXIT_FAILURE;
197
}
198
}
199
catch
(
const
CLI::ParseError& e)
200
{
201
cli_args.exit(e);
202
}
203
catch
(std::exception& ex)
204
{
205
std::println(stderr,
"Exception occurred: {}"
, ex.what());
206
}
207
catch
(...)
208
{
209
std::println(stderr,
"A unrecognized exception occurred!"
);
210
}
211
212
return
EXIT_SUCCESS;
213
}
CommonDefinitions.hpp
CommonFunctions.hpp
srs::LogFile
Definition
srs_check.cpp:49
srs::LogFile::analysis
void analysis()
Definition
srs_check.cpp:118
srs::LogFile::LogFile
LogFile(const std::string &filename)
Definition
srs_check.cpp:51
srs::LogFile::content_
std::vector< std::string > content_
Definition
srs_check.cpp:145
srs::LogFile::line_counter_
std::size_t line_counter_
Definition
srs_check.cpp:144
srs::LogFile::default_init_n_chars_per_line
static const auto default_init_n_chars_per_line
Definition
srs_check.cpp:142
srs::LogFile::call_map_
std::unordered_map< std::string, int > call_map_
Definition
srs_check.cpp:146
srs::LogFile::grow_to
void grow_to(std::size_t n_lines)
Definition
srs_check.cpp:148
srs::LogFile::input_file_
std::ifstream input_file_
Definition
srs_check.cpp:143
srs::LogFile::read
void read()
Definition
srs_check.cpp:61
srs::LogFile::default_init_n_lines
static const auto default_init_n_lines
Definition
srs_check.cpp:141
srs::LogFile::summarize
auto summarize() -> bool
Definition
srs_check.cpp:85
srs::LogFile::print_last_log
void print_last_log()
Definition
srs_check.cpp:110
fmt
fmt formatter related specialization.
Definition
DoxygenScopeDef.hpp:8
srs::common::file_logger_new_instance_str
constexpr auto file_logger_new_instance_str
Definition
CommonDefinitions.hpp:63
srs::common::get_default_log_path
auto get_default_log_path() -> std::filesystem::path
Definition
CommonFunctions.hpp:117
srs::common::exit_logger_begin
constexpr auto exit_logger_begin
Definition
CommonDefinitions.hpp:61
srs::common::exit_logger_end
constexpr auto exit_logger_end
Definition
CommonDefinitions.hpp:62
srs
Definition
Application.cpp:45
main
auto main(int argc, char **argv) -> int
Definition
srs_check.cpp:165
backend
srs_check.cpp
Impressum
Datenschutzerklärung
Generated by
1.17.0