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
15
19#include "sham/term/tty.hpp"
20#include "shamcmdopt/env.hpp"
21#include "shamcomm/logs.hpp"
23
27namespace logformatter {
28
35
37 return "[" + (args.color) + args.module_name + shambase::term_colors::reset() + "] "
38 + (args.color) + (args.level_name) + shambase::term_colors::reset() + ": "
39 + args.content;
40 }
41
49 return "[" + (args.color) + args.module_name + shambase::term_colors::reset() + "] "
50 + (args.color) + (args.level_name) + shambase::term_colors::reset() + ": "
51 + args.content;
52 }
53
61 return sham::format(
62 "{5:}rank={6:<4}{2:} {5:}({3:^20}){2:} {0:}{1:}{2:}: {4:}",
63 args.color,
64 args.level_name,
66 args.module_name,
67 args.content,
70 }
71
79 return sham::format(
80 "{5:}({3:}){2:} : {4:}",
81 args.color,
82 args.level_name,
84 args.module_name,
85 args.content,
87 }
88
94
96
97 return sham::format(
98 "{0:}{1:}{2:}: {4:}{5:} | ({3:}) rank={6:<4}{2:}",
99 args.color,
100 args.level_name,
102 args.module_name,
103 args.content,
106 }
107
115 return sham::format(
116 "{5:}({3:}){2:} : {4:}",
117 args.color,
118 args.level_name,
120 args.module_name,
121 args.content,
123 }
124
132
133 u32 tty_width = sham::term::get_tty_columns();
134
135 std::string ansi_reset = shambase::term_colors::reset();
136 std::string ansi_faint = shambase::term_colors::faint();
137
138 std::string lineend = sham::format(
139 "{5:} [{3:}][rank={6:}]{2:}",
140 args.color,
141 args.level_name,
142 ansi_reset,
143 args.module_name,
144 args.content,
145 ansi_faint,
147
148 std::string log = sham::format(
149 "{0:}{1:}{2:}: {4:}",
150 args.color,
151 args.level_name,
152 ansi_reset,
153 args.module_name,
154 args.content,
155 ansi_faint,
157
158 std::string log_line1, log_line2;
159 size_t first_nl = log.find_first_of('\n');
160 if (first_nl != std::string::npos) {
161 log_line1 = log.substr(0, first_nl);
162 log_line2 = log.substr(first_nl);
163 } else {
164 log_line1 = log;
165 log_line2 = "";
166 }
167
168 u32 ansi_count = ansi_reset.size() * 2 + ansi_faint.size() + args.color.size();
169
170 return sham::format("{:<{}}", log_line1, tty_width - lineend.size() + ansi_count - 1)
171 + lineend + log_line2;
172 }
173
181 return sham::format(
182 "{5:}{3:}{2:}: {4:}",
183 args.color,
184 args.level_name,
186 args.module_name,
187 args.content,
189 }
190
198
199 u32 tty_width = sham::term::get_tty_columns();
200
201 std::string ansi_reset = shambase::term_colors::reset();
202 std::string ansi_faint = shambase::term_colors::faint();
203
204 std::string lineend = sham::format(
205 "{5:} [{3:}][rank={6:}][{7:.2f}s]{2:}",
206 args.color,
207 args.level_name,
208 ansi_reset,
209 args.module_name,
210 args.content,
211 ansi_faint,
214
215 std::string log = sham::format(
216 "{0:}{1:}{2:}: {4:}",
217 args.color,
218 args.level_name,
219 ansi_reset,
220 args.module_name,
221 args.content,
222 ansi_faint,
224
225 std::string log_line1, log_line2;
226 size_t first_nl = log.find_first_of('\n');
227 if (first_nl != std::string::npos) {
228 log_line1 = log.substr(0, first_nl);
229 log_line2 = log.substr(first_nl);
230 } else {
231 log_line1 = log;
232 log_line2 = "";
233 }
234
235 u32 ansi_count = ansi_reset.size() * 2 + ansi_faint.size() + args.color.size();
236
237 return sham::format("{:<{}}", log_line1, tty_width - lineend.size() + ansi_count - 1)
238 + lineend + log_line2;
239 }
240
248 return sham::format(
249 "{5:}{3:}{2:}: {4:}",
250 args.color,
251 args.level_name,
253 args.module_name,
254 args.content,
256 }
257
266 void exception_gen_callback(std::string msg) {
267 shamcomm::logs::err_ln("Exception", "Exception created :\n" + msg);
268 }
269
270} // namespace logformatter
271
272std::string SHAMLOGFORMATTER = shamcmdopt::getenv_str_default_register(
273 "SHAMLOGFORMATTER", "3", "Change the log formatter (values :0-4) [default: 3]");
274
275std::string SHAMLOG_ERR_ON_EXCEPT = shamcmdopt::getenv_str_default_register(
276 "SHAMLOG_ERR_ON_EXCEPT", "1", "Enable logging of exceptions (default to 1)");
277
278namespace shamsys {
279
281
282 shamlog_debug_ln("Sys", "changing formatter to MPI form");
283
284 if (SHAMLOGFORMATTER == "0") {
287 } else if (SHAMLOGFORMATTER == "1") {
290 } else if (SHAMLOGFORMATTER == "2") {
293 } else if (SHAMLOGFORMATTER == "3") {
296 } else if (SHAMLOGFORMATTER == "4") {
299 } else {
300 logger::err_ln("Log", "Unknown formatter");
301 shambase::throw_unimplemented("Unknown formatter");
302 }
303
304 if (SHAMLOG_ERR_ON_EXCEPT == "1") {
305 shamlog_debug_ln("Log", "Enabling exception handler callback");
307 }
308 }
309} // namespace shamsys
std::uint32_t u32
32 bit unsigned integer
This header file contains utility functions related to exception handling in the code.
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:39
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:97
i32 world_rank()
Gives the rank of the current process in the MPI communicator.
Definition worldInfo.cpp:41
namespace for the system handling
void change_log_format()
Change the log formatter according to the SHAMLOGFORMATTER and SHAMLOG_ERR_ON_EXCEPT environment vari...
void err_ln(std::string module_name, Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:132
This file contains the definition for the stacktrace related functionality.
f64 get_wtime()
Returns the current wall clock time in seconds.
A structure containing the arguments to a log formatter.
const std::string reset()
Get the reset terminal escape char.
const std::string faint()
Get the faint terminal escape char.
const std::string bold()
Get the bold terminal escape char.
This file contains tty info getters.
int get_tty_columns()
Get the number of columns of the current terminal.
Definition tty.cpp:92
Functions related to the MPI communicator.