@@ -5,18 +5,45 @@ PYBIND11_EMBEDDED_MODULE(example_interface, m) { }
5
5
6
6
namespace fs = std::filesystem;
7
7
8
+ struct SimpleExecutionContext : MoniLog::MoniLogExecutionContext
9
+ {
10
+ const pybind11::object get_foo () const { if (foo != nullptr ) return pybind11::cast (*foo); else return pybind11::cast<pybind11::none>(Py_None); }
11
+
12
+ double *foo = nullptr ;
13
+
14
+ using MoniLog::MoniLogExecutionContext::MoniLogExecutionContext;
15
+ };
16
+
8
17
int main ()
9
18
{
10
19
// Retrieve the path where the Python scripts to use are located (can provide multiple locations).
11
20
std::string path = fs::current_path ();
12
21
std::vector<std::string> python_path = { path + " /src/" };
22
+
13
23
// Provide the Python scripts to include.
14
24
std::vector<std::string> python_scripts = {" example_moniloggers" };
25
+
15
26
// Name of the interface module (here, an embedded module declared above) exposing the execution context of the application.
16
27
std::string interface_module = " example_interface" ;
28
+
17
29
// Initialization function for the interface module.
18
30
std::function<void (pybind11::module_, pybind11::object)> interface_module_initializer =
19
- [](pybind11::module_ iterativeheatequation_module, pybind11::object context_class) { };
31
+ [](pybind11::module_ interface_module, pybind11::object context_class) {
32
+ pybind11::class_<SimpleExecutionContext, std::shared_ptr<SimpleExecutionContext>>(interface_module, " SimpleExecutionContext" , context_class)
33
+ .def_property_readonly (" foo" , &SimpleExecutionContext::get_foo)
34
+ .def (" __str__" , [](SimpleExecutionContext &self)
35
+ {
36
+ std::ostringstream oss;
37
+ oss << self.name ;
38
+ return oss.str ();
39
+ })
40
+ .def (" __repr__" , [](SimpleExecutionContext &self)
41
+ {
42
+ std::ostringstream oss;
43
+ oss << self.name ;
44
+ return oss.str ();
45
+ });
46
+ };
20
47
21
48
// Define base execution events emitted by the application, to which moniloggers can register.
22
49
MoniLog::register_base_events ({
@@ -28,17 +55,27 @@ int main()
28
55
MoniLog::register_composite_event (" SomeCompositeEvent" , {" SomeEvent" , " SomeOtherEvent" });
29
56
30
57
// Instantiating the execution context accessible from Python.
31
- std::shared_ptr<MoniLog::MoniLogExecutionContext > ctx (new MoniLog::MoniLogExecutionContext ( ));
58
+ std::shared_ptr<SimpleExecutionContext > ctx (new SimpleExecutionContext ( " SimpleExecutionContext " ));
32
59
33
60
// Bootstrapping monilog, consisting mainly of starting the Python interpreter, initializing
34
61
// the monilog module, and evaluating the provided scripts.
35
62
MoniLog::bootstrap_monilog (python_path, python_scripts, interface_module, interface_module_initializer);
36
63
64
+ double foo (0.0 );
65
+ ctx->foo = &foo;
66
+
37
67
// Emitting some base events, triggering the registered moniloggers.
38
68
MoniLog::trigger (0 , ctx);
69
+
70
+ foo = 17.0 ;
71
+
39
72
MoniLog::trigger (1 , ctx);
40
73
74
+ foo = 42.0 ;
75
+
41
76
// Emitting a composite event (can also emit base event by name).
42
77
MoniLog::trigger (" SomeCompositeEvent" , ctx);
43
78
79
+ foo = 17.0 ;
80
+
44
81
}
0 commit comments