Davide Pesavento | 01cea50 | 2021-07-31 19:25:42 -0400 | [diff] [blame] | 1 | ndn-cxx logging |
| 2 | =============== |
| 3 | |
| 4 | Description |
| 5 | ----------- |
dmcoomes | 57e238f | 2017-09-11 22:52:32 -0500 | [diff] [blame] | 6 | |
| 7 | The ndn-cxx logging facility exposes the internal state of NDN libraries and |
| 8 | applications so the user can investigate internal interactions, such as interest |
| 9 | dispatch inside ndn::Face, Data and Interest validation in the security framework, |
| 10 | sync and recovery Interest processing inside ChronoSync, etc. During runtime, the |
| 11 | user is able to specify the types of messages he or she would like to receive from |
| 12 | a set of logging modules pre-configured by library and application developers. |
| 13 | |
Davide Pesavento | 01cea50 | 2021-07-31 19:25:42 -0400 | [diff] [blame] | 14 | Environment |
| 15 | ----------- |
dmcoomes | 57e238f | 2017-09-11 22:52:32 -0500 | [diff] [blame] | 16 | |
| 17 | One way to control ndn-cxx logging facility is through the environment variable |
| 18 | ``NDN_LOG``. Using this variable, one can set the log levels for the available logging |
| 19 | modules. Logging modules within different libraries and applications usually have |
| 20 | distinguishing prefixes (``ndn.``, ``sync.``, etc.), which can be specified when |
| 21 | setting the environment variable. Wildcards can be used to enable logging for all |
| 22 | modules (just ``*``) or all modules under a selected prefix (e.g., ``ndn.*``). |
| 23 | |
Alexander Afanasyev | 75c3af8 | 2020-06-10 14:06:46 -0400 | [diff] [blame] | 24 | If an additional environment variable ``NDN_LOG_NOFLUSH`` is set, the automatic flushing |
| 25 | after each log record will be disabled. This can improve logging performance but may |
| 26 | cause the log records to appear delayed or, in case of application crash, the last |
| 27 | few log records may be lost. |
| 28 | |
dmcoomes | 57e238f | 2017-09-11 22:52:32 -0500 | [diff] [blame] | 29 | ndn-cxx logging facility provides a mechanism to manage the type of log messages |
| 30 | that are written by classifying log messages by severity levels. Listed below |
Davide Pesavento | 487cc9a | 2025-05-01 23:32:05 -0400 | [diff] [blame^] | 31 | are the available log levels: |
dmcoomes | 57e238f | 2017-09-11 22:52:32 -0500 | [diff] [blame] | 32 | |
Davide Pesavento | 487cc9a | 2025-05-01 23:32:05 -0400 | [diff] [blame^] | 33 | * TRACE |
| 34 | * DEBUG |
| 35 | * INFO |
| 36 | * WARN |
| 37 | * ERROR |
| 38 | * FATAL |
dmcoomes | 57e238f | 2017-09-11 22:52:32 -0500 | [diff] [blame] | 39 | |
| 40 | A message's severity level will determine whether the log is written. For instance, |
| 41 | if an application sets its log severity level to DEBUG, all messages marked with |
| 42 | DEBUG, or any of those below that level, are written. FATAL level logs are always |
| 43 | written. |
| 44 | |
Davide Pesavento | 487cc9a | 2025-05-01 23:32:05 -0400 | [diff] [blame^] | 45 | Setting ``NDN_LOG`` requires the following syntax with as many prefixes and |
dmcoomes | 57e238f | 2017-09-11 22:52:32 -0500 | [diff] [blame] | 46 | corresponding loglevels as the user desires: |
| 47 | |
Davide Pesavento | 487cc9a | 2025-05-01 23:32:05 -0400 | [diff] [blame^] | 48 | .. code-block:: shell |
Davide Pesavento | 933a567 | 2020-07-03 22:32:43 -0400 | [diff] [blame] | 49 | |
dmcoomes | 57e238f | 2017-09-11 22:52:32 -0500 | [diff] [blame] | 50 | export NDN_LOG="<prefix1>=<loglevel1>:<prefix2>=<loglevel2>" |
| 51 | |
Davide Pesavento | 933a567 | 2020-07-03 22:32:43 -0400 | [diff] [blame] | 52 | *Example:* |
dmcoomes | 57e238f | 2017-09-11 22:52:32 -0500 | [diff] [blame] | 53 | |
Davide Pesavento | 487cc9a | 2025-05-01 23:32:05 -0400 | [diff] [blame^] | 54 | .. code-block:: shell |
dmcoomes | 57e238f | 2017-09-11 22:52:32 -0500 | [diff] [blame] | 55 | |
| 56 | export NDN_LOG="ndn.*=DEBUG" |
| 57 | export NDN_LOG="ndn.UnixTransport=INFO" |
| 58 | export NDN_LOG="sync.Logic=ERROR" |
| 59 | export NDN_LOG="*=DEBUG:ndn.UnixTransport=INFO:sync.Logic=ERROR" |
| 60 | |
| 61 | **Note:** |
| 62 | |
Davide Pesavento | 933a567 | 2020-07-03 22:32:43 -0400 | [diff] [blame] | 63 | The loglevel assignments in ``NDN_LOG`` are processed left-to-right. Thus, shorter |
| 64 | (more general) prefixes should be listed before longer (more specific) prefixes. |
| 65 | Otherwise, the loglevel setting of a more specific prefix may be overwritten by a |
| 66 | more general assignment appearing later in the string. For example: |
| 67 | |
Davide Pesavento | 487cc9a | 2025-05-01 23:32:05 -0400 | [diff] [blame^] | 68 | .. code-block:: shell |
Davide Pesavento | 933a567 | 2020-07-03 22:32:43 -0400 | [diff] [blame] | 69 | |
| 70 | export NDN_LOG="ndn.UnixTransport=TRACE:ndn.*=ERROR:*=INFO" |
| 71 | |
| 72 | will set all modules to INFO. To obtain the desired effect, it should instead be |
| 73 | written as: |
| 74 | |
Davide Pesavento | 487cc9a | 2025-05-01 23:32:05 -0400 | [diff] [blame^] | 75 | .. code-block:: shell |
Davide Pesavento | 933a567 | 2020-07-03 22:32:43 -0400 | [diff] [blame] | 76 | |
| 77 | export NDN_LOG="*=INFO:ndn.*=ERROR:ndn.UnixTransport=TRACE" |
dmcoomes | 57e238f | 2017-09-11 22:52:32 -0500 | [diff] [blame] | 78 | |
| 79 | **Note:** |
| 80 | |
| 81 | Setting the environment variable with sudo requires the application to be run |
| 82 | in the same command. |
| 83 | |
Davide Pesavento | 487cc9a | 2025-05-01 23:32:05 -0400 | [diff] [blame^] | 84 | .. code-block:: shell |
dmcoomes | 57e238f | 2017-09-11 22:52:32 -0500 | [diff] [blame] | 85 | |
Davide Pesavento | 933a567 | 2020-07-03 22:32:43 -0400 | [diff] [blame] | 86 | # Correct |
| 87 | sudo env NDN_LOG=logLevel ./ndn-application |
dmcoomes | 57e238f | 2017-09-11 22:52:32 -0500 | [diff] [blame] | 88 | |
Davide Pesavento | 933a567 | 2020-07-03 22:32:43 -0400 | [diff] [blame] | 89 | # Also correct |
| 90 | sudo -s |
| 91 | export NDN_LOG=logLevel |
| 92 | ./ndn-application |
dmcoomes | 57e238f | 2017-09-11 22:52:32 -0500 | [diff] [blame] | 93 | |
Davide Pesavento | 933a567 | 2020-07-03 22:32:43 -0400 | [diff] [blame] | 94 | # Incorrect |
| 95 | sudo export NDN_LOG=logLevel |
| 96 | sudo ./ndn-application |
dmcoomes | 57e238f | 2017-09-11 22:52:32 -0500 | [diff] [blame] | 97 | |
Davide Pesavento | 933a567 | 2020-07-03 22:32:43 -0400 | [diff] [blame] | 98 | # Incorrect |
| 99 | NDN_LOG=logLevel sudo ./ndn-application |