libadc-cxx 1.0.0
Structured logging for scientific computing
Loading...
Searching...
No Matches
builder.hpp
Go to the documentation of this file.
1/* Copyright 2025 NTESS. See the top-level LICENSE.txt file for details.
2 *
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5#ifndef adc_builder_impl_hpp
6#define adc_builder_impl_hpp
7#include <string>
8#include <iostream>
9#include <sstream>
10#include <map>
11#include <memory>
12#include <boost/dll/import.hpp>
13#include <boost/algorithm/string.hpp>
14#include "boost/json.hpp"
15#include "adc/types.hpp"
16#include <sys/time.h>
17
18namespace adc {
19
20std::string format_timespec_8601(struct timespec& ts);
21std::string format_timespec_utc_ns(struct timespec& ts);
22
23/// \return true if uuid is in json array.
24bool array_contains_string( boost::json::array& av, string_view uuid);
25
26/// \return values in s when split at delimiter.
27std::vector<std::string> split_string(const std::string& s, char delimiter);
28
29
30inline version builder_version("1.0.0", {"none"});
31
32/*! \brief Implementation of builder_api with optional (compile-time)
33 support of MPI. If compiled without MPI, the mpi-related calls devolve
34 to serial behavior.
35 */
36class BOOST_SYMBOL_VISIBLE builder : public builder_api, public std::enable_shared_from_this< builder > {
37public:
38 builder(void *mpi_communicator_p=NULL);
39
40 // copy populated generic section into the builder under specified name.
41 void add_section(std::string_view name, std::shared_ptr< builder_api > section);
42
43 // auto-populate the header section with application name
44 void add_header_section(std::string_view application_name);
45
46 // auto-populate the host section
47 void add_host_section(int32_t subsections);
48
49 // populate application run-time data to app_data section.
50 // any relationship to previous jobs/higher level workflows goes in app_data
51 // somehow.
52 void add_app_data_section(std::shared_ptr< builder_api > app_data);
53
54 // populate application run-time physics (re)configuration/result to model_data section.
55 // e.g. changes in mesh/particle decomp go here.
56 void add_model_data_section(std::shared_ptr< builder_api > model_data);
57
58 // auto-populate code section with os-derived info at time of call,
59 // tag, version, and code_details blob.
60 void add_code_section(std::string tag, std::shared_ptr< builder_api > version, std::shared_ptr< builder_api > code_details);
61
62 // populate build/install configuration information like options enabled
63 void add_code_configuration_section(std::shared_ptr< builder_api > build_details);
64
65 // populate exit_data section
66 void add_exit_data_section(int return_code, std::string status, std::shared_ptr< builder_api > status_details);
67
68 void add_memory_usage_section();
69
70 /// \brief add data about a named mpi communicator.
71 /// In most applications, "mpi_comm_world" is the recommended name.
72 /// Applications with multiple communicators for data separation can make
73 /// multiple calls to add_mpi_section with distinct names, such as "comm_ocean" or "comm_atmosphere".
74 void add_mpi_section(std::string_view name, void *mpi_comm_p, adc_mpi_field_flags bitflags);
75
76 void add_workflow_section();
77 void add_workflow_children(std::vector< std::string >& child_uuids);
78
79 void add_slurm_section();
80 void add_slurm_section(const std::vector< std::string >& slurmvars);
81
82 void add_gitlab_ci_section();
83
84 // return the section, or an empty pointer if it doesn't exist.
85 std::shared_ptr< builder_api > get_section(std::string_view name);
86 std::vector< std::string > get_section_names();
87 std::vector< std::string > get_field_names();
88 const field get_value(std::string_view path);
89 const char *get_value_string(std::string_view path);
90 int64_t get_value_int64(std::string_view path);
91 uint64_t get_value_uint64(std::string_view path);
92
93
94 void add(std::string_view name, bool value);
95
96 void add(std::string_view name, char value);
97
98 void add(std::string_view name, char16_t value);
99
100 void add(std::string_view name, char32_t value);
101
102 // add null-terminated string
103 void add(std::string_view name, char* value);
104 void add(std::string_view name, const char* value);
105 void add(std::string_view name, std::string& value);
106 void add(std::string_view name, std::string_view value);
107
108 // add null-terminated string filepath
109 void add_path(std::string_view name, char* value);
110 void add_path(std::string_view name, const char* value);
111 void add_path(std::string_view name, std::string& value);
112 void add_path(std::string_view name, std::string_view value);
113
114 // add string which is serialized json.
115 void add_json_string(std::string_view name, std::string_view value);
116 // add string which is yaml
117 void add_yaml_string(std::string_view name, std::string_view value);
118 // add string which is xml
119 void add_xml_string(std::string_view name, std::string_view value);
120 // add string which is an arbitrary precision number
121 void add_number_string(std::string_view name, std::string_view value);
122#if ADC_BOOST_JSON_PUBLIC
123 // add a named raw json value (array, obj, or value)
124 void add(std::string_view name, boost::json::value value);
125#endif
126
127 void add(std::string_view name, uint8_t value);
128 void add(std::string_view name, uint16_t value);
129 void add(std::string_view name, uint32_t value);
130 void add(std::string_view name, uint64_t value);
131
132 void add(std::string_view name, int8_t value);
133 void add(std::string_view name, int16_t value);
134 void add(std::string_view name, int32_t value);
135 void add(std::string_view name, int64_t value);
136
137 void add(std::string_view name, float value);
138 void add(std::string_view name, const std::complex<float>& value);
139 void add(std::string_view name, double value);
140 void add(std::string_view name, const std::complex<double>& value);
141
142 /// add timeval
143 void add(std::string_view name, const struct timeval& tv);
144 /// add timespec
145 void add(std::string_view name, const struct timespec& ts);
146 /// add unix epoch seconds (gettimeofday)
147 void add_epoch(std::string_view name, int64_t epoch);
148
149 /// add data from a c pointer
150 void add_from_pointer_type(std::string_view name, void*, enum scalar_type t);
151
152 /// Fixed length arrays of scalar members.
153 void add_array(std::string_view name, bool value[], size_t len, std::string_view c);
154
155 // this is how a character buffer containing nuls is stored.
156 void add_array(std::string_view name, const char *value, size_t len, std::string_view c);
157 void add_array(std::string_view name, char16_t value[], size_t len, std::string_view c);
158 void add_array(std::string_view name, char32_t value[], size_t len, std::string_view c);
159
160 void add_array(std::string_view name, uint8_t value[], size_t len, std::string_view c);
161 void add_array(std::string_view name, uint16_t value[], size_t len, std::string_view c);
162 void add_array(std::string_view name, uint32_t value[], size_t len, std::string_view c);
163 void add_array(std::string_view name, uint64_t value[], size_t len, std::string_view c);
164
165 void add_array(std::string_view name, int8_t value[], size_t len, std::string_view c);
166 void add_array(std::string_view name, int16_t value[], size_t len, std::string_view c);
167 void add_array(std::string_view name, int32_t value[], size_t len, std::string_view c);
168 void add_array(std::string_view name, int64_t value[], size_t len, std::string_view c);
169
170 void add_array(std::string_view name, float value[], size_t len, std::string_view c);
171#if 0
172 void add_array(std::string_view name, const std::complex<float> value[], size_t len);
173#endif
174 void add_array(std::string_view name, double value[], size_t len, std::string_view c);
175#if 0
176 void add_array(std::string_view name, const std::complex<double> value[], size_t len);
177#endif
178 /// Irregular element size array members.
179 // Arrays of null-terminated strings
180 void add_array(std::string_view name, char* value[], size_t len, std::string_view c);
181 void add_array(std::string_view name, const char* value[], size_t len, std::string_view c);
182 void add_array(std::string_view name, std::string value[], size_t len, std::string_view c);
183 void add_array(std::string_view name, const std::string value[], size_t len, std::string_view c);
184 void add_array(std::string_view name, const std::vector<std::string> sv, std::string_view c);
185 void add_array(std::string_view name, const std::set<std::string> sv, std::string_view container);
186 void add_array(std::string_view name, const std::list<std::string> sv, std::string_view container);
187
188 // Array of strings which are serialized json.
189 void add_array_json_string(std::string_view name, const std::string value[], size_t len, std::string_view c);
190
191 std::string serialize();
192
193private:
194 // this is private because it must be of a specific structure, not arbitrary json
195 boost::json::object d;
196 bool debug; ///< true if ADC_BUILDER_DEBUG exists in the environment
197 key_type kind(std::string_view name);
198
199 std::map< std::string, std::shared_ptr< builder > > sections;
200 // merge sections recursively into a pure json object
201 boost::json::object flatten();
202 void *mpi_comm_p;
203
204 /// \return names of env vars listed in colon-separated env(ADC_HOST_SECTION_ENV)
205 std::vector<std::string> get_host_env_vars();
206
207}; // class builder
208
209
210
211} // namespace adc
212#endif // adc_builder_impl_hpp
The builder api is used to construct structured log (json) messages that follow naming conventions.
Definition builder.hpp:140
Implementation of builder_api with optional (compile-time) support of MPI. If compiled without MPI,...
Definition builder.hpp:36
void add_array(std::string_view name, const std::string value[], size_t len, std::string_view c)
std::string format_timespec_utc_ns(struct timespec &ts)
Definition builder.ipp:96
key_type
when expanding scalar_type, always update enum.ipp to match.
Definition types.hpp:112
std::string format_timespec_8601(struct timespec &ts)
Definition builder.ipp:81
scalar_type
field types for scientific data encode/decode with json.
Definition types.hpp:62
int32_t adc_mpi_field_flags
Definition builder.hpp:87
Definition adc.hpp:82
std::string_view string_view
Definition curl.ipp:18
std::vector< std::string > split_string(const std::string &s, char delimiter)
Definition builder.ipp:473
bool array_contains_string(boost::json::array &av, string_view uuid)
Definition builder.ipp:1587
version builder_version("1.0.0", {"none"})
A version with tags list.
Definition types.hpp:28