Skip to content

Commit 4db9661

Browse files
committed
[doc] improve documentation for C++ binding of monilogger
1 parent cc3da4d commit 4db9661

File tree

2 files changed

+117
-1
lines changed

2 files changed

+117
-1
lines changed

src/bindings/cpp/README.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# **Using monilogger in your C++ app**
2+
3+
## **Defining the execution events**
4+
5+
```cpp
6+
MoniLogger::register_base_events({
7+
{"SomeEvent", 0},
8+
{"SomeOtherEvent", 1}
9+
});
10+
```
11+
12+
```cpp
13+
MoniLogger::register_composite_event("SomeCompositeEvent", {"SomeEvent", "SomeOtherEvent"});
14+
```
15+
16+
## **Defining the exposed execution context**
17+
18+
```cpp
19+
struct MyExecutionContext : MoniLogger::MoniLoggerExecutionContext
20+
{
21+
/*
22+
Declare the exposed variables here.
23+
*/
24+
25+
// Default constructor for execution context structs.
26+
using MoniLogger::MoniLoggerExecutionContext::MoniLoggerExecutionContext;
27+
};
28+
```
29+
30+
### **Exposing local variables**
31+
32+
```cpp
33+
struct MyExecutionContext : MoniLogger::MoniLoggerExecutionContext
34+
{
35+
// Declaring a getter for the 'foo' variable.
36+
const pybind11::object get_foo() const { if (foo != nullptr) return pybind11::cast(*foo); else return pybind11::cast<pybind11::none>(Py_None); }
37+
// Struct member storing the pointer to the local variable.
38+
double *foo = nullptr;
39+
40+
using MoniLogger::MoniLoggerExecutionContext::MoniLoggerExecutionContext;
41+
};
42+
```
43+
44+
### **Exposing class members**
45+
46+
```cpp
47+
struct MyExecutionContext : MoniLogger::MoniLoggerExecutionContext
48+
{
49+
const pybind11::object get_foo() const { if (foo != nullptr) return pybind11::cast(*foo); else return pybind11::cast<pybind11::none>(Py_None); }
50+
// Declaring a getter for the 'bar' variable.
51+
std::vector<double> get_bar() const {return instance->bar;}
52+
53+
double *foo = nullptr;
54+
// Struct member storing the pointer to the object whose members are exposed.
55+
MyClass *instance = nullptr;
56+
57+
// Adapted struct constructor.
58+
MyExecutionContext(MyClass *instance, std::string name) : MoniLogExecutionContext(name), instance(instance) {}
59+
virtual ~MyExecutionContext() = default;
60+
};
61+
```
62+
63+
### **Exposing the context as a Python class**
64+
65+
```cpp
66+
// Initialization function for the interface module.
67+
std::function<void (pybind11::module_, pybind11::object)> interface_module_initializer = [](
68+
// Interface module containing the exposed execution contexts.
69+
pybind11::module_ interface_module,
70+
// MoniLoggerExecutionContext as a Python object for subclassing.
71+
pybind11::object context_class) {
72+
// Declaring a new execution context to be exposed as a Python class.
73+
pybind11::class_<MyExecutionContext, std::shared_ptr<MyExecutionContext>>(interface_module, "MyExecutionContext", context_class)
74+
// Declaring 'foo' as an exposed variable of the context, accessible through the 'get_foo' function.
75+
.def_property_readonly("foo", &MyExecutionContext::get_foo)
76+
.def_property_readonly("bar", &IterativeHeatEquationContext::get_u_n)
77+
/*
78+
Declare additional variables to be exposed here.
79+
*/
80+
// Providing some string representation for the context.
81+
.def("__str__", [](MyExecutionContext &self)
82+
{
83+
std::ostringstream oss;
84+
oss << self.name;
85+
return oss.str();
86+
})
87+
.def("__repr__", [](MyExecutionContext &self)
88+
{
89+
std::ostringstream oss;
90+
oss << self.name;
91+
return oss.str();
92+
});
93+
/*
94+
Register additional contexts here.
95+
*/
96+
};
97+
```
98+
99+
## Registering C++ classes as Python classes
100+
101+
```cpp
102+
// ...
103+
104+
pybind11::class_<BoundClass>(interface_module, "BoundClass");
105+
106+
// ...
107+
```
108+
109+
110+
## Triggering execution events
111+
112+
```cpp
113+
std::shared_ptr<MyExecutionContext> context(new MyExecutionContext(this, "MyCurrentContext"));
114+
MoniLog::trigger(0, context);
115+
MoniLog::trigger("SomeCompositeEvent", context);
116+
```

src/bindings/cpp/examples/simple-embedding/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
* Simple Embedding Example
1+
# Simple Embedding Example
22

33
This example illustrates how to integrate monilogger in your C++ application.
44

0 commit comments

Comments
 (0)