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