Skip to content

Commit 6af11b1

Browse files
Add python distributed logger example (#648)
1 parent 738312b commit 6af11b1

File tree

5 files changed

+112
-13
lines changed

5 files changed

+112
-13
lines changed

examples/connext_dds/distributed_logger/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ For example, you may find RTI Spy in your RTI Connext DDS installation
2323
and run it from a terminal as follows:
2424

2525
```sh
26-
cd rti_connext_dds-7.2.0/bin
27-
./rtiddsspy -printSample
26+
<install dir>/bin/rtiddsspy -printSample
2827
```
2928

3029
Once the Distributed Logger example is running in a different terminal,

examples/connext_dds/distributed_logger/c++11/DistLoggerExample.cxx

+4-5
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,13 @@ void distlogger_example_main(
4141
// Instantiate Distributed Logger
4242
DistLogger dist_logger = DistLogger::get_instance();
4343

44-
// RTI Distributed Logger provides the ability to interact with its
45-
// topics directly. However, for the sake of simplicity in this example,
46-
// you may use RTI Tools such as RTI Spy or RTI Admin Console to visualize
47-
// the logging.
44+
45+
// The log messages are published as DDS topics, which allows your DDS
46+
// applications to subscribe to them. You can also run rtiddsspy or
47+
// RTI Admin Console to visualize the logs.
4848
for (uint i = 1; i <= iterations; ++i) {
4949
cout << "\nIteration #" << i << endl;
5050

51-
// Log messages using the appropiate log levels for your messages.
5251
dist_logger.debug("This is a debug message");
5352
dist_logger.warning("This is a warning message");
5453
dist_logger.error("This is an error message");

examples/connext_dds/distributed_logger/c++11/README.md

+1-6
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,7 @@ on the terminal.
114114
### Visualizing the log messages
115115

116116
Once the example application is running, open RTI Spy or
117-
RTI Admin Console.
118-
You should be able to visualize the logging messages being sent
119-
by the application.
120-
121-
To learn more about RTI Tools, refer to their section in the
122-
[Connext DDS documentation](https://community.rti.com/documentation).
117+
RTI Admin Console as described in the top-level [README](../README.md) file.
123118

124119
## Customizing the Build
125120

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Example Code: Python Distributed Logger Application
2+
3+
See the example description in the top-level [README](../README.md) file.
4+
5+
## Running the Example
6+
7+
### Example Application
8+
9+
Run the application in a terminal as follows:
10+
11+
```sh
12+
python dist_logger_example.py [options]
13+
```
14+
15+
Run with ``--help`` to see the available options.
16+
17+
You should see the messages that are being logged on each iteration printed
18+
on the terminal.
19+
20+
### Visualizing the log messages
21+
22+
Once the example application is running, open RTI Spy or
23+
RTI Admin Console as described in the top-level [README](../README.md) file.
24+
25+
You should be able to visualize the logging messages being sent
26+
by the application.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#
2+
# (c) 2024 Copyright, Real-Time Innovations, Inc. All rights reserved.
3+
#
4+
# RTI grants Licensee a license to use, modify, compile, and create derivative
5+
# works of the Software solely for use with RTI products. The Software is
6+
# provided "as is", with no warranty of any type, including any warranty for
7+
# fitness for any purpose. RTI is under no obligation to maintain or support
8+
# the Software. RTI shall not be liable for any incidental or consequential
9+
# damages arising out of the use or inability to use the software.
10+
#
11+
12+
import time
13+
import argparse
14+
import rti.connextdds as dds
15+
import rti.logging.distlog as distlog
16+
17+
18+
def distlogger_example(application_kind, domain_id, sleep, iterations):
19+
# First, create the options to personalize Distributed Logger.
20+
# If no options are provided, default ones will be created.
21+
options = distlog.LoggerOptions()
22+
options.domain_id = domain_id
23+
options.application_kind = application_kind
24+
25+
# Then, set the created options. The method init must be called before
26+
# using the Logger instance.
27+
distlog.Logger.init(options)
28+
29+
# The log messages are published as DDS topics, which allows your DDS
30+
# applications to subscribe to them. You can also run rtiddsspy or
31+
# RTI Admin Console to visualize the logs.
32+
for i in range(1, iterations + 1):
33+
print(f"\nIteration #{i}")
34+
35+
distlog.Logger.debug("This is a debug message")
36+
distlog.Logger.warning("This is a warning message")
37+
distlog.Logger.error("This is an error message")
38+
39+
time.sleep(sleep)
40+
41+
distlog.Logger.finalize()
42+
43+
44+
def main():
45+
# Parse command line arguments
46+
parser = argparse.ArgumentParser()
47+
parser.add_argument(
48+
"-d", "--domain_id", type=int, default=0, help="Domain ID (default: 0)"
49+
)
50+
parser.add_argument(
51+
"-s",
52+
"--sleep",
53+
type=int,
54+
default=1,
55+
help="Seconds between iterations (default: 1)",
56+
)
57+
parser.add_argument(
58+
"-i",
59+
"--iterations",
60+
type=int,
61+
default=50,
62+
help="Number of iterations (default: 50)",
63+
)
64+
args = parser.parse_args()
65+
66+
try:
67+
distlogger_example(
68+
"Python Distributed Logger Example",
69+
args.domain_id,
70+
args.sleep,
71+
args.iterations,
72+
)
73+
except Exception as ex:
74+
print(f"Exception in distlogger_example: {ex}")
75+
76+
dds.DomainParticipant.finalize_participant_factory()
77+
78+
79+
if __name__ == "__main__":
80+
main()

0 commit comments

Comments
 (0)