ILLIXR: Illinois Extended Reality Testbed
plugin.hpp
1 #pragma once
2 #include "phonebook.hpp"
3 #include "record_logger.hpp"
4 #include "spdlog/common.h"
5 #include "spdlog/sinks/basic_file_sink.h"
6 #include "spdlog/sinks/stdout_color_sinks.h"
7 
8 #include <memory>
9 #include <spdlog/spdlog.h>
10 #include <string>
11 #include <typeinfo>
12 #include <utility>
13 
14 namespace ILLIXR {
15 
16 using plugin_id_t = std::size_t;
17 
18 /*
19  * This gets included, but it is functionally 'private'. Hence the double-underscores.
20  */
21 const record_header __plugin_start_header{
22  "plugin_name",
23  {
24  {"plugin_id", typeid(plugin_id_t)},
25  {"plugin_name", typeid(std::string)},
26  },
27 };
28 
32 class plugin {
33 public:
41  virtual void start() {
42  record_logger_->log(record{__plugin_start_header,
43  {
44  {id},
45  {name},
46  }});
47  }
48 
61  virtual void stop() { }
62 
63  plugin(std::string name_, phonebook* pb_)
64  : name{std::move(name_)}
65  , pb{pb_}
66  , record_logger_{pb->lookup_impl<record_logger>()}
67  , gen_guid_{pb->lookup_impl<gen_guid>()}
68  , id{gen_guid_->get()} { }
69 
70  virtual ~plugin() = default;
71 
72  [[nodiscard]] std::string get_name() const noexcept {
73  return name;
74  }
75 
76  void spdlogger(const char* log_level) {
77  if (!log_level) {
78 #ifdef NDEBUG
79  log_level = "warn";
80 #else
81  log_level = "debug";
82 #endif
83  }
84  std::vector<spdlog::sink_ptr> sinks;
85  auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs/" + name + ".log");
86  auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
87  sinks.push_back(file_sink);
88  sinks.push_back(console_sink);
89  auto plugin_logger = std::make_shared<spdlog::logger>(name, begin(sinks), end(sinks));
90  plugin_logger->set_level(spdlog::level::from_str(log_level));
91  spdlog::register_logger(plugin_logger);
92  }
93 
94 protected:
95  std::string name;
96  const phonebook* pb;
97  const std::shared_ptr<record_logger> record_logger_;
98  const std::shared_ptr<gen_guid> gen_guid_;
99  const std::size_t id;
100 };
101 
102 #define PLUGIN_MAIN(plugin_class) \
103  extern "C" plugin* this_plugin_factory(phonebook* pb) { \
104  auto* obj = new plugin_class{#plugin_class, pb}; \
105  return obj; \
106  }
107 } // namespace ILLIXR
This class generates unique IDs.
Definition: record_logger.hpp:262
A service locator for ILLIXR.
Definition: phonebook.hpp:68
A dynamically-loadable plugin for Spindle.
Definition: plugin.hpp:32
virtual void stop()
A method which Spindle calls when it stops the component.
Definition: plugin.hpp:61
virtual void start()
A method which Spindle calls when it starts the component.
Definition: plugin.hpp:41
The ILLIXR logging service for structured records.
Definition: record_logger.hpp:226
This class represents a tuple of fields which get logged by record_logger.
Definition: record_logger.hpp:144
RAC_ERRNO_MSG.
Definition: data_format.hpp:15