|
| 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