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