|
| 1 | +--- |
| 2 | +title: Log Functions |
| 3 | +description: How use log functions. |
| 4 | +keywords: logging, log, functions, cmake |
| 5 | +author: RSP Systems A/S |
| 6 | +--- |
| 7 | + |
| 8 | +# Log Functions |
| 9 | + |
| 10 | +[TOC] |
| 11 | + |
| 12 | +## `emergency()` |
| 13 | + |
| 14 | +Logs an "emergency" level message. |
| 15 | + |
| 16 | +```cmake |
| 17 | +emergency("External power supply is unavailable") |
| 18 | +``` |
| 19 | + |
| 20 | +The following parameters are supported: |
| 21 | + |
| 22 | +* < message > |
| 23 | +* < mode >: (_option_) |
| 24 | +* `CONTEXT`: (_optional_) |
| 25 | +* `OUTPUT`: (_optional_) |
| 26 | +* `LIST_SEPARATOR`: (_optional_) |
| 27 | + |
| 28 | +_See [`log()`](#log) function for parameter descriptions and examples._ |
| 29 | + |
| 30 | +## `alert()` |
| 31 | + |
| 32 | +Logs an "alert" level message. |
| 33 | + |
| 34 | +```cmake |
| 35 | +alert("Storage disk is above 90% full, cleanup is required") |
| 36 | +``` |
| 37 | + |
| 38 | +The following parameters are supported: |
| 39 | + |
| 40 | +* < message > |
| 41 | +* < mode >: (_option_) |
| 42 | +* `CONTEXT`: (_optional_) |
| 43 | +* `OUTPUT`: (_optional_) |
| 44 | +* `LIST_SEPARATOR`: (_optional_) |
| 45 | + |
| 46 | +_See [`log()`](#log) function for parameter descriptions and examples._ |
| 47 | + |
| 48 | +## `critical()` |
| 49 | + |
| 50 | +Logs a "critical" level message. |
| 51 | + |
| 52 | +```cmake |
| 53 | +critical("Acme 3.7V Li-Po Battery Driver is unavailable, unable to continue build") |
| 54 | +``` |
| 55 | + |
| 56 | +The following parameters are supported: |
| 57 | + |
| 58 | +* < message > |
| 59 | +* < mode >: (_option_) |
| 60 | +* `CONTEXT`: (_optional_) |
| 61 | +* `OUTPUT`: (_optional_) |
| 62 | +* `LIST_SEPARATOR`: (_optional_) |
| 63 | + |
| 64 | +_See [`log()`](#log) function for parameter descriptions and examples._ |
| 65 | + |
| 66 | +## `error()` |
| 67 | + |
| 68 | +Log an "error" level message. |
| 69 | + |
| 70 | +```cmake |
| 71 | +error("Acme LDSv6 Driver failed loading device.ini") |
| 72 | +``` |
| 73 | + |
| 74 | +The following parameters are supported: |
| 75 | + |
| 76 | +* < message > |
| 77 | +* < mode >: (_option_) |
| 78 | +* `CONTEXT`: (_optional_) |
| 79 | +* `OUTPUT`: (_optional_) |
| 80 | +* `LIST_SEPARATOR`: (_optional_) |
| 81 | + |
| 82 | +_See [`log()`](#log) function for parameter descriptions and examples._ |
| 83 | + |
| 84 | +## `warning()` |
| 85 | + |
| 86 | +Logs a "warning" level message. |
| 87 | + |
| 88 | +```cmake |
| 89 | +warning("No configuration found for Acme VCMx Driver, using driver defaults") |
| 90 | +``` |
| 91 | + |
| 92 | +The following parameters are supported: |
| 93 | + |
| 94 | +* < message > |
| 95 | +* < mode >: (_option_) |
| 96 | +* `CONTEXT`: (_optional_) |
| 97 | +* `OUTPUT`: (_optional_) |
| 98 | +* `LIST_SEPARATOR`: (_optional_) |
| 99 | + |
| 100 | +_See [`log()`](#log) function for parameter descriptions and examples._ |
| 101 | + |
| 102 | +## `notice()` |
| 103 | + |
| 104 | +Logs a "notice" level message. |
| 105 | + |
| 106 | +```cmake |
| 107 | +notice("Acme CPU32v6xx Power Control Driver build completed") |
| 108 | +``` |
| 109 | + |
| 110 | +The following parameters are supported: |
| 111 | + |
| 112 | +* < message > |
| 113 | +* < mode >: (_option_) |
| 114 | +* `CONTEXT`: (_optional_) |
| 115 | +* `OUTPUT`: (_optional_) |
| 116 | +* `LIST_SEPARATOR`: (_optional_) |
| 117 | + |
| 118 | +_See [`log()`](#log) function for parameter descriptions and examples._ |
| 119 | + |
| 120 | +## `info()` |
| 121 | + |
| 122 | +Log an "info" level message. |
| 123 | + |
| 124 | +```cmake |
| 125 | +info("Started building external system assets") |
| 126 | +``` |
| 127 | + |
| 128 | +The following parameters are supported: |
| 129 | + |
| 130 | +* < message > |
| 131 | +* < mode >: (_option_) |
| 132 | +* `CONTEXT`: (_optional_) |
| 133 | +* `OUTPUT`: (_optional_) |
| 134 | +* `LIST_SEPARATOR`: (_optional_) |
| 135 | + |
| 136 | +_See [`log()`](#log) function for parameter descriptions and examples._ |
| 137 | + |
| 138 | +## `debug()` |
| 139 | + |
| 140 | +Log a "debug" level message. |
| 141 | + |
| 142 | +```cmake |
| 143 | +debug("Network: eth4, via pci@0004:01:00.0 (serial: e4:1d:2d:67:83:56)") |
| 144 | +``` |
| 145 | + |
| 146 | +The following parameters are supported: |
| 147 | + |
| 148 | +* < message > |
| 149 | +* < mode >: (_option_) |
| 150 | +* `CONTEXT`: (_optional_) |
| 151 | +* `OUTPUT`: (_optional_) |
| 152 | +* `LIST_SEPARATOR`: (_optional_) |
| 153 | + |
| 154 | +_See [`log()`](#log) function for parameter descriptions and examples._ |
| 155 | + |
| 156 | +## `log()` |
| 157 | + |
| 158 | +Log a message with an arbitrary level. |
| 159 | + |
| 160 | +```cmake |
| 161 | +log(INFO_LEVEL "Removing expired tmp files from cache storage") |
| 162 | +``` |
| 163 | + |
| 164 | +Behind the scene, `log()` uses the [`output()`](../output/helpers.md#output) function to print messages to the console. |
| 165 | +It supports the following parameters: |
| 166 | + |
| 167 | +* < level >: _The PSR log level (see [`RSP_LOG_LEVELS`](./01_levels.md#psr-log-levels))_ |
| 168 | +* < message >: _`string`, `variable` or `list` message to output._ |
| 169 | +* < mode >: (_option_), _cmake [message mode](https://cmake.org/cmake/help/latest/command/message.html#general-messages), e.g. `WARNING`, `NOTICE`, `STATUS`, ...etc._ |
| 170 | + _Defaults to the mode that is associated with the given log level (see [`RSP_LOG_LEVELS_CMAKE`](./01_levels.md#cmake-message-modes))._ |
| 171 | +* `CONTEXT`: (_optional_), _Evt. variables to output in a "context" associated with the log message._ |
| 172 | +* `OUTPUT`: (_optional_), _output variable. If specified, message is assigned to variable, instead of being printed to `stdout` or `stderr`._ |
| 173 | +* `LIST_SEPARATOR`: (_optional_), _Separator to used, if a list variable is given as message. Defaults to `\n` (newline)._ |
| 174 | + |
| 175 | +### Mode |
| 176 | + |
| 177 | +Unless you specify the cmake [message mode](https://cmake.org/cmake/help/latest/command/message.html#general-messages), `log()` will automatically apply the mode that is associated with |
| 178 | +the specified PSR log level (_defined by the [`RSP_LOG_LEVELS_CMAKE`](./01_levels.md#cmake-message-modes) property_). |
| 179 | + |
| 180 | + |
| 181 | +Consequently, in situations when you need to deviate from the default associated mode, simply specify the desired |
| 182 | +message mode. |
| 183 | + |
| 184 | +```cmake |
| 185 | +# Log a "warning" level message, but use a STATUS cmake message mode |
| 186 | +log(WARNING_LEVEL "Config NOT found for VCMx Driver, using defaults" STATUS) |
| 187 | +``` |
| 188 | + |
| 189 | +The above example will print a message similar to this: |
| 190 | + |
| 191 | +```txt |
| 192 | +-- warning: Config NOT found for VCMx Driver, using defaults |
| 193 | + Timestamp: 2025-02-05 11:33:32.190620 |
| 194 | +``` |
| 195 | + |
| 196 | +### Context |
| 197 | + |
| 198 | +The `CONTEXT` parameter allows you to associate one or more variables with the given log entry. |
| 199 | + |
| 200 | +```cmake |
| 201 | +log( |
| 202 | + NOTICE_LEVEL "Assets build completed" |
| 203 | + CONTEXT |
| 204 | + assets_dir |
| 205 | + my_assets_list |
| 206 | +) |
| 207 | +``` |
| 208 | + |
| 209 | +The above shown example will print a message similar to this: |
| 210 | + |
| 211 | +```txt |
| 212 | +notice: Assets build completed |
| 213 | + Context: |
| 214 | + [ |
| 215 | + assets_dir = build/resources/config |
| 216 | + my_assets_list = graft.conf;ports.init;power.json |
| 217 | + ] |
| 218 | + Timestamp: 2025-02-05 11:08:40.912747 |
| 219 | +``` |
| 220 | + |
| 221 | +### Timestamp |
| 222 | + |
| 223 | +By default, a timestamp is appended at the end of each logged message. |
| 224 | +You can modify this behaviour, and the timestamp format by changing the following predefined properties: |
| 225 | + |
| 226 | +| Property | Default | Description | |
| 227 | +|----------------------------|------------------------|-----------------------------------------------------------------------------------------------------------------------------| |
| 228 | +| `RSP_LOG_SHOW_TIMESTAMP` | `true` | State whether to append timestamp for log entries, or not | |
| 229 | +| `RSP_LOG_TIMESTAMP_FORMAT` | `%Y-%m-%d %H:%M:%S.%f` | Timestamp format. See [CMake documentation](https://cmake.org/cmake/help/latest/command/string.html#timestamp) for details. | |
| 230 | +| `RSP_LOG_TIMESTAMP_UTC` | `false` | True if timestamp is UTC, false if timestamp is local | |
| 231 | + |
| 232 | +!!! note "Note" |
| 233 | + Properties should be set before you include the `rsp/logging` module or by [force](https://cmake.org/cmake/help/latest/command/set.html#set-cache-entry) caching the property. |
| 234 | + |
| 235 | +### Output |
| 236 | + |
| 237 | +See ["Capture Output"](../output/helpers.md#capture-output) for additional information about the `OUTPUT` parameter. |
| 238 | + |
| 239 | +### Lists |
| 240 | + |
| 241 | +See [output "Lists"](../output/helpers.md#lists) for additional information about the `LIST_SEPARATOR` parameter. |
0 commit comments