libadc-cxx 1.0.0
Structured logging for scientific computing
Loading...
Searching...
No Matches
demoBuilder.cpp
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#if ADC_BOOST_JSON_PUBLIC
6#include "boost/json/src.hpp"
7#endif
8#include "adc/factory.hpp"
9#include <cstring>
10#include <cerrno>
11#include <limits>
12
13/*! \file demoBuilder.cpp
14 */
15
16/** \addtogroup examples
17 * @{
18 */
19
20namespace adc_examples {
21namespace demoBuilder {
22
23std::map<std::string, std::string> file_config =
24 {{ "DIRECTORY", "./test.outputs"},
25 { "FILE", "out.file.log" },
26 { "APPEND", "true" }
27 };
28
29std::map<std::string, std::string> curl_config =
30 {
31 {"URL", "https://data-submission-server.your.org/log"}
32 };
33
34std::map<std::string, std::string> ldmsd_stream_publish_config =
35 {
36 { "STREAM", "adc_publish_api" }
37 };
38
39std::map<std::string, std::string> script_config =
40 {
41 { "PROG", "./plugin_script.sh" }
42 };
43
44
45int test_publisher(std::shared_ptr<adc::publisher_api> pi, std::shared_ptr< adc::builder_api> b, std::map<std::string, std::string>& pconfig ) {
46 std::cout << "------------------- begin --------------------" << std::endl;
47 int err = 0;
48 int e = 0;
49 err = pi->config(pconfig);
50 if (err) {
51 std::cout << "config failed " <<
52 std::strerror(err) << std::endl;
53 e += 1;
54 err = 0;
55 }
56 err = pi->initialize();
57 if (err) {
58 std::cout << "initialize failed " <<
59 std::strerror(err) << std::endl;
60 e += 1;
61 err = 0;
62 }
63 err = pi->publish(b); // there should be 1 b in the output
64 if (err) {
65 std::cout << "publish 1 failed " <<
66 std::strerror(err) << std::endl;
67 e += 1;
68 err = 0;
69 }
70 pi->pause();
71 err = pi->publish(b); // this publication should be silent in output
72 if (err) {
73 std::cout << "publish 2 failed " <<
74 std::strerror(err) << std::endl;
75 e += 1;
76 err = 0;
77 }
78 pi->resume();
79 err = pi->publish(b); // there should be 2 b in the output
80 if (err) {
81 std::cout << "publish 3 failed " <<
82 std::strerror(err) << std::endl;
83 e += 1;
84 err = 0;
85 }
86 pi->finalize();
87 std::cout << "------------------- end --------------------" << std::endl;
88 return e;
89}
90
91void populate_builder(std::shared_ptr<adc::builder_api> b, adc::factory & f) {
92
93 std::shared_ptr< adc::builder_api > app_data = f.get_builder();
94#if ADC_BOOST_JSON_PUBLIC
95 boost::json::object jo = {{"a","b"},{"C","d"},{"n",1}};
96#endif
97 app_data->add("bool0", false);
98 app_data->add("bool1", true);
99 app_data->add("char1", 'A');
100 app_data->add("c16", u'¢');
101 app_data->add("c32", U'猫');
102 const std::string ccppstr("ccppstr");
103 app_data->add("ccppstr", ccppstr);
104 std::string cppstr("cppstr");
105 app_data->add("cppstr", cppstr);
106 // add null-terminated string
107 const char *cstr = "cstr_nul";
108 app_data->add("cstr1", cstr);
109 // add string which is serialized json.
110 app_data->add_json_string("jstr1", std::string ("{\"a\":\"b\", \"c\":[1,2, 3]}"));
111#if ADC_BOOST_JSON_PUBLIC
112 app_data->add("jsonobj", jo);
113#endif
114 uint8_t u8 = std::numeric_limits<uint8_t>::max();
115 uint16_t u16 = std::numeric_limits<uint16_t>::max();
116 uint32_t u32 = std::numeric_limits<uint32_t>::max();
117 uint64_t u64 = std::numeric_limits<uint64_t>::max();
118 app_data->add("u8", u8);
119 app_data->add("u16", u16);
120 app_data->add("u32", u32);
121 app_data->add("u64", u64);
122 int8_t i8 = std::numeric_limits<int8_t>::max();
123 int16_t i16 = std::numeric_limits<int16_t>::max();
124 int32_t i32 = std::numeric_limits<int32_t>::max();
125 int64_t i64 = std::numeric_limits<int64_t>::max();
126 app_data->add("i8", i8);
127 app_data->add("i16", i16);
128 app_data->add("i32", i32);
129 app_data->add("i64", i64);
130 app_data->add("flt", std::numeric_limits<float>::max());
131 app_data->add("dbl", std::numeric_limits<double>::max() );
132 app_data->add("fcplx", std::complex<float>(
133 std::numeric_limits<float>::max(),std::numeric_limits<float>::max()));
134 app_data->add("dcplx", std::complex<double>(
135 std::numeric_limits<double>::max(),std::numeric_limits<double>::max()));
136 const char *cstrings[] = {"a", "B", "c2"};
137 char *vcstrings[4];
138 int ia[4];
139 float fa[4];
140 double da[4];
141 unsigned ua[4];
142 uint64_t ua64[4];
143 for (int i = 0; i < 4; i++) {
144 vcstrings[i] = new char[2];
145 snprintf(vcstrings[i], 2, "%d", i);
146 ua[i] = i;
147 ua64[i] = std::numeric_limits<uint64_t>::max() - i;
148 ia[i] = -i;
149 da[i] = 3.14*i;
150 fa[i] = 3.14*i *2;
151 }
152 std::string cppstrings[] = {"ap", "Bp", "c2p"};
153 app_data->add_array("nulembed", "a\0b", 3);
154 app_data->add_array("cstrs", cstrings, 3);
155 app_data->add_array("cppstrs", cppstrings, 3);
156 app_data->add_array("vcstrs", vcstrings, 4);
157 for (int i = 0; i < 4; i++) {
158 delete [] vcstrings[i];
159 }
160 app_data->add_array("ia", ia, 4);
161 app_data->add_array("ua", ua, 4);
162 app_data->add_array("ua64", ua64, 4);
163 app_data->add_array("fa", fa, 4);
164 app_data->add_array("da", da, 4);
165
166 b->add_header_section("cxx_demo_1");
167
168 b->add_host_section(ADC_HS_ALL);
169
170 b->add_app_data_section(app_data);
171
172 b->add_memory_usage_section();
173
174 std::shared_ptr< adc::builder_api > code_details = f.get_builder();
175 // more here
176
177 std::shared_ptr< adc::builder_api > version = f.get_builder();
178 version->add("version", "1.1.2");
179 const char* tags[] = { "boca_raton", "saronida_2"};
180 version->add_array("tags", tags ,2);
181 version->add("mesh_version", "5.0.0");
182
183 b->add_code_section("repartitioner", version, code_details);
184
185 std::shared_ptr< adc::builder_api > build_details = f.get_builder();
186 b->add_code_configuration_section(build_details);
187
188 std::shared_ptr< adc::builder_api > model_data = f.get_builder();
189 model_data->add("nx", 3);
190 model_data->add("ny", 10);
191 model_data->add("step", 0);
192 b->add_model_data_section(model_data);
193
194 std::shared_ptr< adc::builder_api > status_details = f.get_builder();
195 status_details->add("tmax", 15000.25);
196 status_details->add("tmax_loc", 10325);
197 status_details->add("step", 234);
198 b->add_exit_data_section(1,"we didn't succeed due to high temperatures", status_details);
199
200#if 0
201 std::string ss = b->serialize();
202 std::cout << "-------------------------------" << std::endl;
203 std::cout << ss << std::endl;
204 std::cout << "-------------------------------" << std::endl;
205#endif
206}
207
208/**
209 * \brief Demo of all the data types and generally available publishers.
210 */
211int main(int /* argc */, char ** /* argv */) {
212 std::cout << "adc pub version: " << adc::publisher_api_version.name << std::endl;
213 std::cout << "adc builder version: " << adc::builder_api_version.name << std::endl;
214 std::cout << "adc enum version: " << adc::enum_version.name << std::endl;
215
216 adc::factory f;
217 std::shared_ptr< adc::builder_api > b = f.get_builder();
218
219 populate_builder(b, f);
220
221 // try individual publishers from the factory class.
222 std::shared_ptr< adc::publisher_api > p1 = f.get_publisher("file");
223 std::cout << test_publisher(p1, b, file_config) << std::endl;
224 std::shared_ptr< adc::publisher_api > p2 = f.get_publisher("stdout");
225 std::cout << test_publisher(p2, b, file_config) << std::endl;
226 std::shared_ptr< adc::publisher_api > p3 = f.get_publisher("curl");
227 std::cout << test_publisher(p3, b, curl_config) << std::endl;
228 std::shared_ptr< adc::publisher_api > p4 = f.get_publisher("ldmsd_stream_publish");
229 std::cout << test_publisher(p4, b, ldmsd_stream_publish_config) << std::endl;
230 std::shared_ptr< adc::publisher_api > p5 = f.get_publisher("script");
231 std::cout << test_publisher(p5, b, script_config) << std::endl;
232
233 return 0;
234}
235
236} // demoBuilder
237} // adc_examples
238
239/** @}*/
240/** @}*/
241
242int main(int argc, char **argv)
243{
244 return adc_examples::demoBuilder::main(argc, argv);
245}
provides publishers and builders of application metadata.
Definition factory.hpp:34
std::shared_ptr< publisher_api > get_publisher(const std::string &name)
Definition factory.ipp:178
std::shared_ptr< builder_api > get_builder()
Definition factory.ipp:300
int main(int argc, char **argv)
version enum_version("1.0.0", {"none"})
the version number of enum scalar_type and object_type
version publisher_api_version("1.0.0", {"none"})
version builder_api_version("1.0.0", {"none"})
#define ADC_HS_ALL
all ADC_HS_* optional data included
Definition builder.hpp:73
std::map< std::string, std::string > curl_config
void populate_builder(std::shared_ptr< adc::builder_api > b, adc::factory &f)
std::map< std::string, std::string > file_config
int main(int, char **)
Demo of all the data types and generally available publishers.
std::map< std::string, std::string > script_config
std::map< std::string, std::string > ldmsd_stream_publish_config
int test_publisher(std::shared_ptr< adc::publisher_api > pi, std::shared_ptr< adc::builder_api > b, std::map< std::string, std::string > &pconfig)
const std::string name
Definition types.hpp:29