Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
exception_ctx.hpp
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
10#pragma once
11
21#include "shambase/string.hpp"
22#include <string>
23#include <vector>
24
25namespace shambase {
26
33 struct args_info {
34 std::string name;
35 std::string value;
36
37 args_info() = default;
38
39 template<class T>
40 args_info(std::string name, const T &value) : name(std::move(name)) {
41 try {
42 this->value = shambase::format("{}", value);
43 } catch (const std::exception &e) {
44 this->value = "format failed : " + std::string(e.what());
45 }
46 }
47 };
48
55 struct arg_group {
56 std::string section_name;
57 std::vector<args_info> args;
58
59 arg_group() = default;
60
61 template<typename... Args>
62 arg_group(std::string name, Args &&...args_list)
63 : section_name(std::move(name)), args{std::forward<Args>(args_list)...} {}
64 };
65
72 struct context {
73 std::vector<arg_group> groups;
74
75 context() = default;
76
77 template<typename... Args>
78 context(Args &&...args_list) : groups{std::forward<Args>(args_list)...} {}
79 };
80
107 template<class exception_type>
109 std::string message, context ctx, SourceLocation loc = SourceLocation{}) {
110 std::string msg;
111 auto out = std::back_inserter(msg);
112 fmt::format_to(out, "{}\nexception context :\n", message);
113
114 for (const auto &group : ctx.groups) {
115 fmt::format_to(out, " {}:\n", group.section_name);
116 for (const auto &arg : group.args) {
117 fmt::format_to(out, " {} = {}\n", arg.name, arg.value);
118 }
119 }
120
122 }
123
124} // namespace shambase
125
138#define ARG_INFO(var) shambase::args_info(#var, var)
Source location utility.
This header file contains utility functions related to exception handling in the code.
namespace for basic c++ utilities
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
exception_type make_except_with_loc_with_ctx(std::string message, context ctx, SourceLocation loc=SourceLocation{})
Make an exception with a message and variadic context groups.
provide information about the source location
A context group containing a section name and a list of arguments.
An argument containing a name and a value.
A context containing a list of argument groups.