Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add python distributed logger example #648

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions examples/connext_dds/distributed_logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ For example, you may find RTI Spy in your RTI Connext DDS installation
and run it from a terminal as follows:

```sh
cd rti_connext_dds-7.2.0/bin
./rtiddsspy -printSample
<install dir>/bin/rtiddsspy -printSample
```

Once the Distributed Logger example is running in a different terminal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,13 @@ void distlogger_example_main(
// Instantiate Distributed Logger
DistLogger dist_logger = DistLogger::get_instance();

// RTI Distributed Logger provides the ability to interact with its
// topics directly. However, for the sake of simplicity in this example,
// you may use RTI Tools such as RTI Spy or RTI Admin Console to visualize
// the logging.

// The log messages are published as DDS topics, which allows your DDS
// applications to subscribe to them. You can also run rtiddsspy or
// RTI Admin Console to visualize the logs.
for (uint i = 1; i <= iterations; ++i) {
cout << "\nIteration #" << i << endl;

// Log messages using the appropiate log levels for your messages.
dist_logger.debug("This is a debug message");
dist_logger.warning("This is a warning message");
dist_logger.error("This is an error message");
Expand Down
7 changes: 1 addition & 6 deletions examples/connext_dds/distributed_logger/c++11/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,7 @@ on the terminal.
### Visualizing the log messages

Once the example application is running, open RTI Spy or
RTI Admin Console.
You should be able to visualize the logging messages being sent
by the application.

To learn more about RTI Tools, refer to their section in the
[Connext DDS documentation](https://community.rti.com/documentation).
RTI Admin Console as described in the top-level [README](../README.md) file.

## Customizing the Build

Expand Down
26 changes: 26 additions & 0 deletions examples/connext_dds/distributed_logger/py/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Example Code: Python Distributed Logger Application

See the example description in the top-level [README](../README.md) file.

## Running the Example

### Example Application

Run the application in a terminal as follows:

```sh
python dist_logger_example.py [options]
```

Run with ``--help`` to see the available options.

You should see the messages that are being logged on each iteration printed
on the terminal.

### Visualizing the log messages

Once the example application is running, open RTI Spy or
RTI Admin Console as described in the top-level [README](../README.md) file.

You should be able to visualize the logging messages being sent
by the application.
80 changes: 80 additions & 0 deletions examples/connext_dds/distributed_logger/py/dist_logger_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#
# (c) 2024 Copyright, Real-Time Innovations, Inc. All rights reserved.
#
# RTI grants Licensee a license to use, modify, compile, and create derivative
# works of the Software solely for use with RTI products. The Software is
# provided "as is", with no warranty of any type, including any warranty for
# fitness for any purpose. RTI is under no obligation to maintain or support
# the Software. RTI shall not be liable for any incidental or consequential
# damages arising out of the use or inability to use the software.
#

import time
import argparse
import rti.connextdds as dds
import rti.logging.distlog as distlog


def distlogger_example(application_kind, domain_id, sleep, iterations):
# First, create the options to personalize Distributed Logger.
# If no options are provided, default ones will be created.
options = distlog.LoggerOptions()
options.domain_id = domain_id
options.application_kind = application_kind

# Then, set the created options. The method init must be called before
# using the Logger instance.
distlog.Logger.init(options)

# The log messages are published as DDS topics, which allows your DDS
# applications to subscribe to them. You can also run rtiddsspy or
# RTI Admin Console to visualize the logs.
for i in range(1, iterations + 1):
print(f"\nIteration #{i}")

distlog.Logger.debug("This is a debug message")
distlog.Logger.warning("This is a warning message")
distlog.Logger.error("This is an error message")

time.sleep(sleep)

distlog.Logger.finalize()


def main():
# Parse command line arguments
parser = argparse.ArgumentParser()
parser.add_argument(
"-d", "--domain_id", type=int, default=0, help="Domain ID (default: 0)"
)
parser.add_argument(
"-s",
"--sleep",
type=int,
default=1,
help="Seconds between iterations (default: 1)",
)
parser.add_argument(
"-i",
"--iterations",
type=int,
default=50,
help="Number of iterations (default: 50)",
)
args = parser.parse_args()

try:
distlogger_example(
"Python Distributed Logger Example",
args.domain_id,
args.sleep,
args.iterations,
)
except Exception as ex:
print(f"Exception in distlogger_example: {ex}")

dds.DomainParticipant.finalize_participant_factory()


if __name__ == "__main__":
main()
Loading