Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
change_log_format.cpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
18#include "shamcmdopt/env.hpp"
19#include "shamcmdopt/tty.hpp"
20#include "shamcomm/logs.hpp"
22
26namespace logformatter {
27
36 return "[" + (args.color) + args.module_name + shambase::term_colors::reset() + "] "
37 + (args.color) + (args.level_name) + shambase::term_colors::reset() + ": "
38 + args.content;
39 }
40
48 return "[" + (args.color) + args.module_name + shambase::term_colors::reset() + "] "
49 + (args.color) + (args.level_name) + shambase::term_colors::reset() + ": "
50 + args.content;
51 }
52
60 return shambase::format(
61 "{5:}rank={6:<4}{2:} {5:}({3:^20}){2:} {0:}{1:}{2:}: {4:}",
62 args.color,
63 args.level_name,
64 shambase::term_colors::reset(),
65 args.module_name,
66 args.content,
67 shambase::term_colors::faint(),
69 }
70
78 return shambase::format(
79 "{5:}({3:}){2:} : {4:}",
80 args.color,
81 args.level_name,
82 shambase::term_colors::reset(),
83 args.module_name,
84 args.content,
85 shambase::term_colors::faint());
86 }
95
96 return shambase::format(
97 "{0:}{1:}{2:}: {4:}{5:} | ({3:}) rank={6:<4}{2:}",
98 args.color,
99 args.level_name,
100 shambase::term_colors::reset(),
101 args.module_name,
102 args.content,
103 shambase::term_colors::faint(),
105 }
106
114 return shambase::format(
115 "{5:}({3:}){2:} : {4:}",
116 args.color,
117 args.level_name,
118 shambase::term_colors::reset(),
119 args.module_name,
120 args.content,
121 shambase::term_colors::faint());
122 }
123
131
132 u32 tty_width = shamcmdopt::get_tty_columns();
133
134 std::string ansi_reset = shambase::term_colors::reset();
135 std::string ansi_faint = shambase::term_colors::faint();
136
137 std::string lineend = shambase::format(
138 "{5:} [{3:}][rank={6:}]{2:}",
139 args.color,
140 args.level_name,
141 ansi_reset,
142 args.module_name,
143 args.content,
144 ansi_faint,
146
147 std::string log = shambase::format(
148 "{0:}{1:}{2:}: {4:}",
149 args.color,
150 args.level_name,
151 ansi_reset,
152 args.module_name,
153 args.content,
154 ansi_faint,
156
157 std::string log_line1, log_line2;
158 size_t first_nl = log.find_first_of('\n');
159 if (first_nl != std::string::npos) {
160 log_line1 = log.substr(0, first_nl);
161 log_line2 = log.substr(first_nl);
162 } else {
163 log_line1 = log;
164 log_line2 = "";
165 }
166
167 u32 ansi_count = ansi_reset.size() * 2 + ansi_faint.size() + args.color.size();
168
169 return shambase::format("{:<{}}", log_line1, tty_width - lineend.size() + ansi_count - 1)
170 + lineend + log_line2;
171 }
172
180 return shambase::format(
181 "{5:}{3:}{2:}: {4:}",
182 args.color,
183 args.level_name,
184 shambase::term_colors::reset(),
185 args.module_name,
186 args.content,
187 shambase::term_colors::bold());
188 }
189
197
198 u32 tty_width = shamcmdopt::get_tty_columns();
199
200 std::string ansi_reset = shambase::term_colors::reset();
201 std::string ansi_faint = shambase::term_colors::faint();
202
203 std::string lineend = shambase::format(
204 "{5:} [{3:}][rank={6:}][{7:.2f}s]{2:}",
205 args.color,
206 args.level_name,
207 ansi_reset,
208 args.module_name,
209 args.content,
210 ansi_faint,
212 shambase::details::get_wtime());
213
214 std::string log = shambase::format(
215 "{0:}{1:}{2:}: {4:}",
216 args.color,
217 args.level_name,
218 ansi_reset,
219 args.module_name,
220 args.content,
221 ansi_faint,
223
224 std::string log_line1, log_line2;
225 size_t first_nl = log.find_first_of('\n');
226 if (first_nl != std::string::npos) {
227 log_line1 = log.substr(0, first_nl);
228 log_line2 = log.substr(first_nl);
229 } else {
230 log_line1 = log;
231 log_line2 = "";
232 }
233
234 u32 ansi_count = ansi_reset.size() * 2 + ansi_faint.size() + args.color.size();
235
236 return shambase::format("{:<{}}", log_line1, tty_width - lineend.size() + ansi_count - 1)
237 + lineend + log_line2;
238 }
239
247 return shambase::format(
248 "{5:}{3:}{2:}: {4:}",
249 args.color,
250 args.level_name,
251 shambase::term_colors::reset(),
252 args.module_name,
253 args.content,
254 shambase::term_colors::bold());
255 }
256
265 void exception_gen_callback(std::string msg) {
266 shamcomm::logs::err_ln("Exception", "Exception created :\n" + msg);
267 }
268
269} // namespace logformatter
270
271std::string SHAMLOGFORMATTER = shamcmdopt::getenv_str_default_register(
272 "SHAMLOGFORMATTER", "3", "Change the log formatter (values :0-4) [default: 3]");
273
274std::string SHAMLOG_ERR_ON_EXCEPT = shamcmdopt::getenv_str_default_register(
275 "SHAMLOG_ERR_ON_EXCEPT", "1", "Enable logging of exceptions (default to 1)");
276
277namespace shamsys {
278
280
281 shamlog_debug_ln("Sys", "changing formatter to MPI form");
282
283 if (SHAMLOGFORMATTER == "0") {
286 } else if (SHAMLOGFORMATTER == "1") {
289 } else if (SHAMLOGFORMATTER == "2") {
292 } else if (SHAMLOGFORMATTER == "3") {
295 } else if (SHAMLOGFORMATTER == "4") {
298 } else {
299 logger::err_ln("Log", "Unknown formatter");
300 shambase::throw_unimplemented("Unknown formatter");
301 }
302
303 if (SHAMLOG_ERR_ON_EXCEPT == "1") {
304 shamlog_debug_ln("Log", "Enabling exception handler callback");
306 }
307 }
308} // namespace shamsys
std::uint32_t u32
32 bit unsigned integer
Namespace for log formatters.
std::string style3_formatter_simple(const logger::ReformatArgs &args)
Log formatter for style 3, simple details.
std::string style0_formatter_full(const logger::ReformatArgs &args)
Log formatter for style 0, full details.
void exception_gen_callback(std::string msg)
The callback called when an exception is thrown.
std::string style3_formatter_full(const logger::ReformatArgs &args)
Log formatter for style 3, full details.
std::string style1_formatter_full(const logger::ReformatArgs &args)
Log formatter for style 1, full details.
std::string style4_formatter_full(const logger::ReformatArgs &args)
Log formatter for style 3, full details + time.
std::string style2_formatter_simple(const logger::ReformatArgs &args)
Log formatter for style 2, simple details.
std::string style1_formatter_simple(const logger::ReformatArgs &args)
Log formatter for style 1, simple details.
std::string style2_formatter_full(const logger::ReformatArgs &args)
Log formatter for style 2, full details.
std::string style0_formatter_simple(const logger::ReformatArgs &args)
Log formatter for style 0, simple details.
std::string style4_formatter_simple(const logger::ReformatArgs &args)
Log formatter for style 3, simple details + time.
void change_formaters(reformat_func_ptr full, reformat_func_ptr simple)
Changes the log formatter functions.
void set_exception_gen_callback(exception_gen_callback_t callback)
Set the exception generator callback.
Definition exception.cpp:37
void throw_unimplemented(SourceLocation loc=SourceLocation{})
Throw a std::runtime_error saying that the function is unimplemented.
std::string getenv_str_default_register(const char *env_var, std::string default_val, std::string desc)
Get the content of the environment variable if it exist and register it documentation,...
Definition env.hpp:88
u32 get_tty_columns()
Get the number of columns of the current terminal.
Definition tty.cpp:81
i32 world_rank()
Gives the rank of the current process in the MPI communicator.
Definition worldInfo.cpp:40
namespace for the system handling
void change_log_format()
Change the log formatter according to the SHAMLOGFORMATTER and SHAMLOG_ERR_ON_EXCEPT environment vari...
This file contains the definition for the stacktrace related functionality.
A structure containing the arguments to a log formatter.
This file contains tty info getters.
Functions related to the MPI communicator.