bactria  0.0.1
The bactria library is a header-only C++14 library for profiling and tracing.
Plugin.hpp
Go to the documentation of this file.
1 /* Copyright 2021 Jan Stephan
2  *
3  * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by
4  * the European Commission - subsequent versions of the EUPL (the “Licence”).
5  * You may not use this work except in compliance with the Licence.
6  * You may obtain a copy of the Licence at:
7  *
8  * http://ec.europa.eu/idabc/eupl.html
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the Licence is distributed on an “AS IS” basis, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * Licence permissions and limitations under the Licence.
14  */
15 
23 #pragma once
24 
26 #include <bactria/core/Plugin.hpp>
27 
28 #include <cstdint>
29 #include <cstdlib>
30 #include <stdexcept>
31 #include <string>
32 #include <type_traits>
33 
34 namespace bactria
35 {
36  namespace reports
37  {
44  namespace plugin
45  {
60  inline auto activated() -> bool
61  {
62  static bool const activated = is_active() && (std::getenv("BACTRIA_REPORTS_PLUGIN") != nullptr);
63  return activated;
64  }
65 
66  /* Standard function pointer declaration style is forbidden because of noexcept */
72  using create_report_t = std::add_pointer_t<void*(char const*)>;
73 
80 
86  using destroy_report_t = std::add_pointer_t<void(void*) noexcept>;
87 
94 
100  using write_report_t = std::add_pointer_t<void(void*)>;
101 
108 
114  using record_bool_t = std::add_pointer_t<void(void*, char const*, bool)>;
115 
122 
128  using record_int8_t = std::add_pointer_t<void(void*, char const*, std::int8_t)>;
129 
136 
142  using record_uint8_t = std::add_pointer_t<void(void*, char const*, std::uint8_t)>;
143 
150 
156  using record_int16_t = std::add_pointer_t<void(void*, char const*, std::int16_t)>;
157 
164 
170  using record_uint16_t = std::add_pointer_t<void(void*, char const*, std::uint16_t)>;
171 
178 
184  using record_int32_t = std::add_pointer_t<void(void*, char const*, std::int32_t)>;
185 
192 
198  using record_uint32_t = std::add_pointer_t<void(void*, char const*, std::uint32_t)>;
199 
206 
212  using record_int64_t = std::add_pointer_t<void(void*, char const*, std::int64_t)>;
213 
220 
226  using record_uint64_t = std::add_pointer_t<void(void*, char const*, std::uint64_t)>;
227 
234 
240  using record_float_t = std::add_pointer_t<void(void*, char const*, float)>;
241 
248 
254  using record_double_t = std::add_pointer_t<void(void*, char const*, double)>;
255 
262 
268  using record_string_t = std::add_pointer_t<void(void*, char const*, char const*)>;
269 
276 
285  [[nodiscard]] inline auto load() -> plugin_handle_t
286  {
287  auto const path = std::getenv("BACTRIA_REPORTS_PLUGIN");
288  if(path != nullptr)
289  {
290  auto handle = system::open_plugin(path);
291 
292  system::load_func(handle, create_report_ptr, "bactria_reports_create_report");
293  system::load_func(handle, destroy_report_ptr, "bactria_reports_destroy_report");
294  system::load_func(handle, write_report_ptr, "bactria_reports_write_report");
295  system::load_func(handle, record_bool_ptr, "bactria_reports_record_bool");
296  system::load_func(handle, record_int8_ptr, "bactria_reports_record_int8");
297  system::load_func(handle, record_uint8_ptr, "bactria_reports_record_uint8");
298  system::load_func(handle, record_int16_ptr, "bactria_reports_record_int16");
299  system::load_func(handle, record_uint16_ptr, "bactria_reports_record_uint16");
300  system::load_func(handle, record_int32_ptr, "bactria_reports_record_int32");
301  system::load_func(handle, record_uint32_ptr, "bactria_reports_record_uint32");
302  system::load_func(handle, record_int64_ptr, "bactria_reports_record_int64");
303  system::load_func(handle, record_uint64_ptr, "bactria_reports_record_uint64");
304  system::load_func(handle, record_float_ptr, "bactria_reports_record_float");
305  system::load_func(handle, record_double_ptr, "bactria_reports_record_double");
306  system::load_func(handle, record_string_ptr, "bactria_reports_record_string");
307 
308  return handle;
309  }
310 
311  // getenv failed
312  throw std::runtime_error{std::string{"Failed to load bactria reports plugin "} + path};
313  }
314 
323  [[gnu::always_inline]] inline auto create_report(char const* name)
324  {
325  if(create_report_ptr != nullptr)
326  return (create_report_ptr) (name);
327 
328  return static_cast<void*>(nullptr);
329  }
330 
338  [[gnu::always_inline]] inline auto destroy_report(void* report_handle) noexcept
339  {
340  if(destroy_report_ptr != nullptr)
341  (destroy_report_ptr)(report_handle);
342  }
343 
351  [[gnu::always_inline]] inline auto write_report(void* report_handle)
352  {
353  if(write_report_ptr != nullptr)
354  (write_report_ptr)(report_handle);
355  }
356 
364  [[gnu::always_inline]] inline auto record_value(void* report_handle, char const* key, bool value) -> void
365  {
366  if(record_bool_ptr != nullptr)
367  (record_bool_ptr)(report_handle, key, value);
368  }
369 
377  [[gnu::always_inline]] inline auto record_value(void* report_handle, char const* key, std::int8_t value)
378  -> void
379  {
380  if(record_int8_ptr != nullptr)
381  (record_int8_ptr)(report_handle, key, value);
382  }
383 
391  [[gnu::always_inline]] inline auto record_value(void* report_handle, char const* key, std::uint8_t value)
392  -> void
393  {
394  if(record_uint8_ptr != nullptr)
395  (record_uint8_ptr)(report_handle, key, value);
396  }
397 
405  [[gnu::always_inline]] inline auto record_value(void* report_handle, char const* key, std::int16_t value)
406  -> void
407  {
408  if(record_int16_ptr != nullptr)
409  (record_int16_ptr)(report_handle, key, value);
410  }
411 
419  [[gnu::always_inline]] inline auto record_value(void* report_handle, char const* key, std::uint16_t value)
420  -> void
421  {
422  if(record_uint16_ptr != nullptr)
423  (record_uint16_ptr)(report_handle, key, value);
424  }
425 
433  [[gnu::always_inline]] inline auto record_value(void* report_handle, char const* key, std::int32_t value)
434  -> void
435  {
436  if(record_int32_ptr != nullptr)
437  (record_int32_ptr)(report_handle, key, value);
438  }
439 
447  [[gnu::always_inline]] inline auto record_value(void* report_handle, char const* key, std::uint32_t value)
448  -> void
449  {
450  if(record_uint32_ptr != nullptr)
451  (record_uint32_ptr)(report_handle, key, value);
452  }
453 
461  [[gnu::always_inline]] inline auto record_value(void* report_handle, char const* key, std::int64_t value)
462  -> void
463  {
464  if(record_int64_ptr != nullptr)
465  (record_int64_ptr)(report_handle, key, value);
466  }
467 
475  [[gnu::always_inline]] inline auto record_value(void* report_handle, char const* key, std::uint64_t value)
476  -> void
477  {
478  if(record_uint64_ptr != nullptr)
479  (record_uint64_ptr)(report_handle, key, value);
480  }
481 
489  [[gnu::always_inline]] inline auto record_value(void* report_handle, char const* key, float value) -> void
490  {
491  if(record_float_ptr != nullptr)
492  (record_float_ptr)(report_handle, key, value);
493  }
494 
502  [[gnu::always_inline]] inline auto record_value(void* report_handle, char const* key, double value) -> void
503  {
504  if(record_double_ptr != nullptr)
505  (record_double_ptr)(report_handle, key, value);
506  }
507 
515  [[gnu::always_inline]] inline auto record_value(void* report_handle, char const* key, char const* value)
516  -> void
517  {
518  if(record_string_ptr != nullptr)
519  (record_string_ptr)(report_handle, key, value);
520  }
522  } // namespace plugin
523  } // namespace reports
524 } // namespace bactria
bactria::reports::plugin::record_float_t
std::add_pointer_t< void(void *, char const *, float)> record_float_t
Signature for plugin function bactria_reports_record_float().
Definition: Plugin.hpp:240
bactria::reports::plugin::record_int32_ptr
auto record_int32_ptr
Pointer to plugin function bactria_reports_record_int32().
Definition: Plugin.hpp:191
Plugin.hpp
bactria-internal plugin handling.
bactria::reports::plugin::record_string_ptr
auto record_string_ptr
Pointer to plugin function bactria_reports_record_string().
Definition: Plugin.hpp:275
bactria::reports::plugin::destroy_report_ptr
auto destroy_report_ptr
Pointer to plugin function bactria_reports_destroy_report().
Definition: Plugin.hpp:93
bactria::is_active
auto is_active() -> bool
Checks whether the bactria library has been deactivated by the user.
Definition: Activation.hpp:52
bactria::reports::plugin::record_uint64_t
std::add_pointer_t< void(void *, char const *, std::uint64_t)> record_uint64_t
Signature for plugin function bactria_reports_record_uint64().
Definition: Plugin.hpp:226
bactria::reports::plugin::record_uint32_ptr
auto record_uint32_ptr
Pointer to plugin function bactria_reports_record_uint32().
Definition: Plugin.hpp:205
bactria::reports::plugin::create_report_ptr
auto create_report_ptr
Pointer to plugin function bactria_reports_create_report().
Definition: Plugin.hpp:79
bactria::reports::plugin::create_report
auto create_report(char const *name)
Creates a plugin-specific report handle.
Definition: Plugin.hpp:323
bactria::reports::plugin::record_float_ptr
auto record_float_ptr
Pointer to plugin function bactria_reports_record_float().
Definition: Plugin.hpp:247
bactria::reports::plugin::record_int16_ptr
auto record_int16_ptr
Pointer to plugin function bactria_reports_record_int16().
Definition: Plugin.hpp:163
bactria::reports::plugin::record_double_ptr
auto record_double_ptr
Pointer to plugin function bactria_reports_record_double().
Definition: Plugin.hpp:261
bactria::reports::plugin::record_uint8_ptr
auto record_uint8_ptr
Pointer to plugin function bactria_reports_record_uint8().
Definition: Plugin.hpp:149
bactria::system::load_func
auto load_func(plugin_handle_t handle, Sig &ptr, const char *name)
The POSIX-specific function loader.
Definition: POSIX.hpp:80
bactria::reports::plugin::record_uint32_t
std::add_pointer_t< void(void *, char const *, std::uint32_t)> record_uint32_t
Signature for plugin function bactria_reports_record_uint32().
Definition: Plugin.hpp:198
bactria::reports::plugin::record_int32_t
std::add_pointer_t< void(void *, char const *, std::int32_t)> record_int32_t
Signature for plugin function bactria_reports_record_int32().
Definition: Plugin.hpp:184
bactria::reports::plugin::activated
auto activated() -> bool
Checks for an active reports plugin.
Definition: Plugin.hpp:60
bactria::reports::plugin::destroy_report
auto destroy_report(void *report_handle) noexcept
Destroys a plugin-specific report handle.
Definition: Plugin.hpp:338
bactria::reports::plugin::record_int8_ptr
auto record_int8_ptr
Pointer to plugin function bactria_reports_record_int8().
Definition: Plugin.hpp:135
bactria::system::open_plugin
auto open_plugin(const char *path)
The POSIX-specific plugin loader.
Definition: POSIX.hpp:57
bactria::reports::plugin::write_report_t
std::add_pointer_t< void(void *)> write_report_t
Signature for plugin function bactria_reports_write_report().
Definition: Plugin.hpp:100
bactria::reports::plugin::record_uint64_ptr
auto record_uint64_ptr
Pointer to plugin function bactria_reports_record_uint64().
Definition: Plugin.hpp:233
bactria::reports::plugin::write_report_ptr
auto write_report_ptr
Pointer to plugin function bactria_reports_write_report().
Definition: Plugin.hpp:107
bactria::reports::plugin::record_uint16_ptr
auto record_uint16_ptr
Pointer to plugin function bactria_reports_record_uint16().
Definition: Plugin.hpp:177
bactria::plugin_handle_t
system::plugin_handle_t plugin_handle_t
The platform-specific plugin handle type.
Definition: Plugin.hpp:41
bactria::reports::plugin::record_int64_ptr
auto record_int64_ptr
Pointer to plugin function bactria_reports_record_int64().
Definition: Plugin.hpp:219
bactria::reports::plugin::record_double_t
std::add_pointer_t< void(void *, char const *, double)> record_double_t
Signature for plugin function bactria_reports_record_double().
Definition: Plugin.hpp:254
bactria::reports::plugin::record_int64_t
std::add_pointer_t< void(void *, char const *, std::int64_t)> record_int64_t
Signature for plugin function bactria_reports_record_int64().
Definition: Plugin.hpp:212
bactria::reports::plugin::record_int16_t
std::add_pointer_t< void(void *, char const *, std::int16_t)> record_int16_t
Signature for plugin function bactria_reports_record_int16().
Definition: Plugin.hpp:156
bactria::reports::plugin::record_bool_ptr
auto record_bool_ptr
Pointer to plugin function bactria_reports_record_bool().
Definition: Plugin.hpp:121
bactria::reports::plugin::record_value
auto record_value(void *report_handle, char const *key, char const *value) -> void
Plugin-specific string recording.
Definition: Plugin.hpp:515
bactria::reports::plugin::destroy_report_t
std::add_pointer_t< void(void *) noexcept > destroy_report_t
Signature for plugin function bactria_reports_destroy_report().
Definition: Plugin.hpp:86
bactria::reports::plugin::write_report
auto write_report(void *report_handle)
Plugin-specific report writing.
Definition: Plugin.hpp:351
bactria::reports::plugin::record_uint8_t
std::add_pointer_t< void(void *, char const *, std::uint8_t)> record_uint8_t
Signature for plugin function bactria_reports_record_uint8().
Definition: Plugin.hpp:142
bactria::reports::plugin::load
auto load() -> plugin_handle_t
Initializes the reports plugin.
Definition: Plugin.hpp:285
Activation.hpp
Plugin activation file.
bactria::reports::plugin::create_report_t
std::add_pointer_t< void *(char const *)> create_report_t
Signature for plugin function bactria_reports_create_report().
Definition: Plugin.hpp:72
bactria::reports::plugin::record_string_t
std::add_pointer_t< void(void *, char const *, char const *)> record_string_t
Signature for plugin function bactria_reports_record_string().
Definition: Plugin.hpp:268
bactria::reports::plugin::record_bool_t
std::add_pointer_t< void(void *, char const *, bool)> record_bool_t
Signature for plugin function bactria_reports_record_bool().
Definition: Plugin.hpp:114
bactria::reports::plugin::record_int8_t
std::add_pointer_t< void(void *, char const *, std::int8_t)> record_int8_t
Signature for plugin function bactria_reports_record_int8().
Definition: Plugin.hpp:128
bactria::reports::plugin::record_uint16_t
std::add_pointer_t< void(void *, char const *, std::uint16_t)> record_uint16_t
Signature for plugin function bactria_reports_record_uint16().
Definition: Plugin.hpp:170