1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_log: 2*61c4878aSAndroid Build Coastguard Worker 3*61c4878aSAndroid Build Coastguard Worker====== 4*61c4878aSAndroid Build Coastguard Workerpw_log 5*61c4878aSAndroid Build Coastguard Worker====== 6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module:: 7*61c4878aSAndroid Build Coastguard Worker :name: pw_log 8*61c4878aSAndroid Build Coastguard Worker 9*61c4878aSAndroid Build Coastguard WorkerPigweed's logging module provides facilities for applications to log 10*61c4878aSAndroid Build Coastguard Workerinformation about the execution of their application. The module is split into 11*61c4878aSAndroid Build Coastguard Workertwo components: 12*61c4878aSAndroid Build Coastguard Worker 13*61c4878aSAndroid Build Coastguard Worker1. The facade (this module) which is only a macro interface layer 14*61c4878aSAndroid Build Coastguard Worker2. The backend, provided elsewhere, that implements the low level logging 15*61c4878aSAndroid Build Coastguard Worker 16*61c4878aSAndroid Build Coastguard Worker``pw_log`` also defines a logging protobuf, helper utilities, and an RPC 17*61c4878aSAndroid Build Coastguard Workerservice for efficiently storing and transmitting log messages. See 18*61c4878aSAndroid Build Coastguard Worker:ref:`module-pw_log-protobuf` for details. 19*61c4878aSAndroid Build Coastguard Worker 20*61c4878aSAndroid Build Coastguard Worker-------------- 21*61c4878aSAndroid Build Coastguard WorkerUsage examples 22*61c4878aSAndroid Build Coastguard Worker-------------- 23*61c4878aSAndroid Build Coastguard WorkerHere is a typical usage example, showing setting the module name, and using the 24*61c4878aSAndroid Build Coastguard Workerlong-form names. 25*61c4878aSAndroid Build Coastguard Worker 26*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp 27*61c4878aSAndroid Build Coastguard Worker 28*61c4878aSAndroid Build Coastguard Worker #define PW_LOG_MODULE_NAME "BLE" 29*61c4878aSAndroid Build Coastguard Worker 30*61c4878aSAndroid Build Coastguard Worker #include "pw_log/log.h" 31*61c4878aSAndroid Build Coastguard Worker 32*61c4878aSAndroid Build Coastguard Worker int main() { 33*61c4878aSAndroid Build Coastguard Worker PW_LOG_INFO("Booting..."); 34*61c4878aSAndroid Build Coastguard Worker PW_LOG_DEBUG("CPU temp: %.2f", cpu_temperature); 35*61c4878aSAndroid Build Coastguard Worker if (BootFailed()) { 36*61c4878aSAndroid Build Coastguard Worker PW_LOG_CRITICAL("Had trouble booting due to error %d", GetErrorCode()); 37*61c4878aSAndroid Build Coastguard Worker ReportErrorsAndHalt(); 38*61c4878aSAndroid Build Coastguard Worker } 39*61c4878aSAndroid Build Coastguard Worker PW_LOG_INFO("Successfully booted"); 40*61c4878aSAndroid Build Coastguard Worker } 41*61c4878aSAndroid Build Coastguard Worker 42*61c4878aSAndroid Build Coastguard WorkerIn ``.cc`` files, it is possible to dispense with the ``PW_`` part of the log 43*61c4878aSAndroid Build Coastguard Workernames and go for shorter log macros. Include ``pw_log/short.h`` or 44*61c4878aSAndroid Build Coastguard Worker``pw_log/shorter.h`` for shorter versions of the macros. 45*61c4878aSAndroid Build Coastguard Worker 46*61c4878aSAndroid Build Coastguard Worker.. warning:: 47*61c4878aSAndroid Build Coastguard Worker The shorter log macros collide with `Abseil's logging API 48*61c4878aSAndroid Build Coastguard Worker <https://abseil.io/docs/cpp/guides/logging>`_. Do not use them in upstream 49*61c4878aSAndroid Build Coastguard Worker Pigweed modules, or any code that may depend on Abseil. 50*61c4878aSAndroid Build Coastguard Worker 51*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp 52*61c4878aSAndroid Build Coastguard Worker 53*61c4878aSAndroid Build Coastguard Worker #define PW_LOG_MODULE_NAME "BLE" 54*61c4878aSAndroid Build Coastguard Worker 55*61c4878aSAndroid Build Coastguard Worker #include "pw_log/shorter.h" 56*61c4878aSAndroid Build Coastguard Worker 57*61c4878aSAndroid Build Coastguard Worker int main() { 58*61c4878aSAndroid Build Coastguard Worker INF("Booting..."); 59*61c4878aSAndroid Build Coastguard Worker DBG("CPU temp: %.2f", cpu_temperature); 60*61c4878aSAndroid Build Coastguard Worker if (BootFailed()) { 61*61c4878aSAndroid Build Coastguard Worker CRT("Had trouble booting due to error %d", GetErrorCode()); 62*61c4878aSAndroid Build Coastguard Worker ReportErrorsAndHalt(); 63*61c4878aSAndroid Build Coastguard Worker } 64*61c4878aSAndroid Build Coastguard Worker INF("Successfully booted"); 65*61c4878aSAndroid Build Coastguard Worker } 66*61c4878aSAndroid Build Coastguard Worker 67*61c4878aSAndroid Build Coastguard WorkerThe ``pw_log`` facade also exposes a handful of macros that only apply 68*61c4878aSAndroid Build Coastguard Workerspecifically to tokenized logging. See :ref:`module-pw_log-tokenized-args` for 69*61c4878aSAndroid Build Coastguard Workerdetails. 70*61c4878aSAndroid Build Coastguard Worker 71*61c4878aSAndroid Build Coastguard WorkerLayer diagram example: ``stm32f429i-disc1`` 72*61c4878aSAndroid Build Coastguard Worker=========================================== 73*61c4878aSAndroid Build Coastguard WorkerBelow is an example diagram showing how the modules connect together for the 74*61c4878aSAndroid Build Coastguard Worker``stm32f429i-disc1`` target, where the ``pw_log`` backend is ``pw_log_basic``. 75*61c4878aSAndroid Build Coastguard Worker``pw_log_basic`` uses the ``pw_sys_io`` module to log in plaintext, which in 76*61c4878aSAndroid Build Coastguard Workerturn outputs to the STM32F429 bare metal backend for ``pw_sys_io``, which is 77*61c4878aSAndroid Build Coastguard Worker``pw_sys_io_baremetal_stm32f429i``. 78*61c4878aSAndroid Build Coastguard Worker 79*61c4878aSAndroid Build Coastguard Worker.. image:: example_layer_diagram.svg 80*61c4878aSAndroid Build Coastguard Worker 81*61c4878aSAndroid Build Coastguard Worker.. _module-pw_log-macros: 82*61c4878aSAndroid Build Coastguard Worker 83*61c4878aSAndroid Build Coastguard WorkerLogging macros 84*61c4878aSAndroid Build Coastguard Worker============== 85*61c4878aSAndroid Build Coastguard WorkerThese are the primary macros for logging information about the functioning of a 86*61c4878aSAndroid Build Coastguard Workersystem, intended to be used directly. 87*61c4878aSAndroid Build Coastguard Worker 88*61c4878aSAndroid Build Coastguard Worker.. c:macro:: PW_LOG(level, verbosity, module, flags, fmt, ...) 89*61c4878aSAndroid Build Coastguard Worker 90*61c4878aSAndroid Build Coastguard Worker This is the primary mechanism for logging. 91*61c4878aSAndroid Build Coastguard Worker 92*61c4878aSAndroid Build Coastguard Worker *level* - An integer level as defined by ``pw_log/levels.h`` for this log. 93*61c4878aSAndroid Build Coastguard Worker 94*61c4878aSAndroid Build Coastguard Worker *verbosity* - An integer level as defined by ``pw_log/levels.h`` which is the 95*61c4878aSAndroid Build Coastguard Worker minimum level which is enabled. 96*61c4878aSAndroid Build Coastguard Worker 97*61c4878aSAndroid Build Coastguard Worker *module* - A string literal for the module name. 98*61c4878aSAndroid Build Coastguard Worker 99*61c4878aSAndroid Build Coastguard Worker *flags* - Arbitrary flags the backend can leverage. The semantics of these 100*61c4878aSAndroid Build Coastguard Worker flags are not defined in the facade, but are instead meant as a general 101*61c4878aSAndroid Build Coastguard Worker mechanism for communication bits of information to the logging backend. 102*61c4878aSAndroid Build Coastguard Worker ``pw_log`` reserves 2 flag bits by default, but log backends may provide for 103*61c4878aSAndroid Build Coastguard Worker more or fewer flag bits. 104*61c4878aSAndroid Build Coastguard Worker 105*61c4878aSAndroid Build Coastguard Worker Here are some ideas for what a backend might use flags for: 106*61c4878aSAndroid Build Coastguard Worker 107*61c4878aSAndroid Build Coastguard Worker - Example: ``HAS_PII`` - A log has personally-identifying data 108*61c4878aSAndroid Build Coastguard Worker - Example: ``HAS_DII`` - A log has device-identifying data 109*61c4878aSAndroid Build Coastguard Worker - Example: ``RELIABLE_DELIVERY`` - Ask the backend to ensure the log is 110*61c4878aSAndroid Build Coastguard Worker delivered; this may entail blocking other logs. 111*61c4878aSAndroid Build Coastguard Worker - Example: ``BEST_EFFORT`` - Don't deliver this log if it would mean blocking 112*61c4878aSAndroid Build Coastguard Worker or dropping important-flagged logs 113*61c4878aSAndroid Build Coastguard Worker 114*61c4878aSAndroid Build Coastguard Worker *fmt* - The message to log, which may contain format specifiers like ``%d`` 115*61c4878aSAndroid Build Coastguard Worker or ``%0.2f``. 116*61c4878aSAndroid Build Coastguard Worker 117*61c4878aSAndroid Build Coastguard Worker Example: 118*61c4878aSAndroid Build Coastguard Worker 119*61c4878aSAndroid Build Coastguard Worker .. code-block:: cpp 120*61c4878aSAndroid Build Coastguard Worker 121*61c4878aSAndroid Build Coastguard Worker PW_LOG(PW_LOG_LEVEL_INFO, PW_LOG_LEVEL_DEBUG, PW_LOG_MODULE_NAME, PW_LOG_FLAGS, "Temp is %d degrees", temp); 122*61c4878aSAndroid Build Coastguard Worker PW_LOG(PW_LOG_LEVEL_ERROR, PW_LOG_LEVEL_DEBUG, PW_LOG_MODULE_NAME, UNRELIABLE_DELIVERY, "It didn't work!"); 123*61c4878aSAndroid Build Coastguard Worker 124*61c4878aSAndroid Build Coastguard Worker .. note:: 125*61c4878aSAndroid Build Coastguard Worker 126*61c4878aSAndroid Build Coastguard Worker ``PW_LOG()`` should not be used frequently; typically only when adding 127*61c4878aSAndroid Build Coastguard Worker flags to a particular message to mark PII or to indicate delivery 128*61c4878aSAndroid Build Coastguard Worker guarantees. For most cases, prefer to use the direct ``PW_LOG_INFO`` or 129*61c4878aSAndroid Build Coastguard Worker ``PW_LOG_DEBUG`` style macros, which are often implemented more efficiently 130*61c4878aSAndroid Build Coastguard Worker in the backend. 131*61c4878aSAndroid Build Coastguard Worker 132*61c4878aSAndroid Build Coastguard Worker.. _module-pw_log-levels: 133*61c4878aSAndroid Build Coastguard Worker 134*61c4878aSAndroid Build Coastguard Worker.. c:macro:: PW_LOG_DEBUG(fmt, ...) 135*61c4878aSAndroid Build Coastguard Worker.. c:macro:: PW_LOG_INFO(fmt, ...) 136*61c4878aSAndroid Build Coastguard Worker.. c:macro:: PW_LOG_WARN(fmt, ...) 137*61c4878aSAndroid Build Coastguard Worker.. c:macro:: PW_LOG_ERROR(fmt, ...) 138*61c4878aSAndroid Build Coastguard Worker.. c:macro:: PW_LOG_CRITICAL(fmt, ...) 139*61c4878aSAndroid Build Coastguard Worker 140*61c4878aSAndroid Build Coastguard Worker Shorthand for ``PW_LOG(<level>, PW_LOG_MODULE_NAME, PW_LOG_FLAGS, fmt, ...)``. 141*61c4878aSAndroid Build Coastguard Worker 142*61c4878aSAndroid Build Coastguard Worker.. c:macro:: PW_LOG_EVERY_N(level, rate, ...) 143*61c4878aSAndroid Build Coastguard Worker 144*61c4878aSAndroid Build Coastguard Worker A simple rate limit logger which will simply log one out of every N logs. 145*61c4878aSAndroid Build Coastguard Worker 146*61c4878aSAndroid Build Coastguard Worker *level* - An integer level as defined by ``pw_log/levels.h``. 147*61c4878aSAndroid Build Coastguard Worker 148*61c4878aSAndroid Build Coastguard Worker *rate* - Rate to reduce logs to, every ``rate``-th log will complete, others 149*61c4878aSAndroid Build Coastguard Worker will be suppressed. 150*61c4878aSAndroid Build Coastguard Worker 151*61c4878aSAndroid Build Coastguard Worker.. c:macro:: PW_LOG_EVERY_N_DURATION(level, min_interval_between_logs, msg, ...) 152*61c4878aSAndroid Build Coastguard Worker 153*61c4878aSAndroid Build Coastguard Worker This is a rate-limited form of logging, especially useful for progressive 154*61c4878aSAndroid Build Coastguard Worker or chatty logs that should be logged periodically, but not on each instance 155*61c4878aSAndroid Build Coastguard Worker of the logger being called. 156*61c4878aSAndroid Build Coastguard Worker 157*61c4878aSAndroid Build Coastguard Worker *level* - An integer level as defined by ``pw_log/levels.h``. 158*61c4878aSAndroid Build Coastguard Worker 159*61c4878aSAndroid Build Coastguard Worker *min_interval_between_logs* - A ``std::chrono::duration`` of the minimum time 160*61c4878aSAndroid Build Coastguard Worker between logs. Logs attempted before this time duration will be completely 161*61c4878aSAndroid Build Coastguard Worker dropped. 162*61c4878aSAndroid Build Coastguard Worker Dropped logs will be counted to add a drop count and calculated rate of the 163*61c4878aSAndroid Build Coastguard Worker logs. 164*61c4878aSAndroid Build Coastguard Worker 165*61c4878aSAndroid Build Coastguard Worker *msg* - Formattable log message, as you would pass to the above ``PW_LOG`` 166*61c4878aSAndroid Build Coastguard Worker macro. 167*61c4878aSAndroid Build Coastguard Worker 168*61c4878aSAndroid Build Coastguard Worker .. note:: 169*61c4878aSAndroid Build Coastguard Worker 170*61c4878aSAndroid Build Coastguard Worker ``PW_LOG_EVERY_N`` is simpler, if you simply need to reduce uniformly 171*61c4878aSAndroid Build Coastguard Worker periodic logs by a fixed or variable factor not based explicitly on a 172*61c4878aSAndroid Build Coastguard Worker duration. Each call to the macro will incur a static ``uint32_t`` within 173*61c4878aSAndroid Build Coastguard Worker the calling context. 174*61c4878aSAndroid Build Coastguard Worker 175*61c4878aSAndroid Build Coastguard Worker ``PW_LOG_EVERY_N_DURATION`` is able to suppress all logs based on a time 176*61c4878aSAndroid Build Coastguard Worker interval, suppressing logs logging faster than the desired time interval. 177*61c4878aSAndroid Build Coastguard Worker Each call to the duration macro will incur a static 16 byte object to 178*61c4878aSAndroid Build Coastguard Worker track the time interval within the calling context. 179*61c4878aSAndroid Build Coastguard Worker 180*61c4878aSAndroid Build Coastguard Worker Example: 181*61c4878aSAndroid Build Coastguard Worker 182*61c4878aSAndroid Build Coastguard Worker .. code-block:: cpp 183*61c4878aSAndroid Build Coastguard Worker 184*61c4878aSAndroid Build Coastguard Worker // Ensure at least 500ms between transfer parameter logs. 185*61c4878aSAndroid Build Coastguard Worker chrono::SystemClock::duration rate_limit_ = 186*61c4878aSAndroid Build Coastguard Worker chrono::SystemClock::for_at_least(std::chrono::milliseconds(500)); 187*61c4878aSAndroid Build Coastguard Worker 188*61c4878aSAndroid Build Coastguard Worker PW_LOG_EVERY_N_DURATION(PW_LOG_LEVEL_INFO, 189*61c4878aSAndroid Build Coastguard Worker rate_limit_, 190*61c4878aSAndroid Build Coastguard Worker "Transfer %u sending transfer parameters!" 191*61c4878aSAndroid Build Coastguard Worker static_cast<unsigned>(session_id_)); 192*61c4878aSAndroid Build Coastguard Worker 193*61c4878aSAndroid Build Coastguard Worker-------------------- 194*61c4878aSAndroid Build Coastguard WorkerModule configuration 195*61c4878aSAndroid Build Coastguard Worker-------------------- 196*61c4878aSAndroid Build Coastguard WorkerThis module has configuration options that globally affect the behavior of 197*61c4878aSAndroid Build Coastguard Workerpw_log via compile-time configuration of this module, see the 198*61c4878aSAndroid Build Coastguard Worker:ref:`module documentation <module-structure-compile-time-configuration>` for 199*61c4878aSAndroid Build Coastguard Workermore details. 200*61c4878aSAndroid Build Coastguard Worker 201*61c4878aSAndroid Build Coastguard Worker.. c:macro:: PW_LOG_LEVEL_DEFAULT 202*61c4878aSAndroid Build Coastguard Worker 203*61c4878aSAndroid Build Coastguard Worker Controls the default value of ``PW_LOG_LEVEL``. Setting 204*61c4878aSAndroid Build Coastguard Worker ``PW_LOG_LEVEL_DEFAULT`` will change the behavior of all source files that 205*61c4878aSAndroid Build Coastguard Worker have not explicitly set ``PW_LOG_LEVEL``. Defaults to ``PW_LOG_LEVEL_DEBUG``. 206*61c4878aSAndroid Build Coastguard Worker 207*61c4878aSAndroid Build Coastguard Worker.. c:macro:: PW_LOG_FLAGS_DEFAULT 208*61c4878aSAndroid Build Coastguard Worker 209*61c4878aSAndroid Build Coastguard Worker Controls the default value of ``PW_LOG_FLAGS``. Setting 210*61c4878aSAndroid Build Coastguard Worker ``PW_LOG_FLAGS_DEFAULT`` will change the behavior of all source files that 211*61c4878aSAndroid Build Coastguard Worker have not explicitly set ``PW_LOG_FLAGS``. Defaults to ``0``. 212*61c4878aSAndroid Build Coastguard Worker 213*61c4878aSAndroid Build Coastguard Worker.. c:macro:: PW_LOG_ENABLE_IF(level, verbosity, flags) 214*61c4878aSAndroid Build Coastguard Worker 215*61c4878aSAndroid Build Coastguard Worker Filters logs by an arbitrary expression based on ``level``, ``verbosity``, 216*61c4878aSAndroid Build Coastguard Worker and ``flags``. Source files that define 217*61c4878aSAndroid Build Coastguard Worker ``PW_LOG_ENABLE_IF(level, verbosity, flags)`` will display if the given 218*61c4878aSAndroid Build Coastguard Worker expression evaluates true. Defaults to 219*61c4878aSAndroid Build Coastguard Worker ``((int32_t)(level) >= (int32_t)(verbosity))``. 220*61c4878aSAndroid Build Coastguard Worker 221*61c4878aSAndroid Build Coastguard Worker.. attention:: 222*61c4878aSAndroid Build Coastguard Worker 223*61c4878aSAndroid Build Coastguard Worker At this time, only compile time filtering is supported. In the future, we 224*61c4878aSAndroid Build Coastguard Worker plan to add support for runtime filtering. 225*61c4878aSAndroid Build Coastguard Worker 226*61c4878aSAndroid Build Coastguard Worker 227*61c4878aSAndroid Build Coastguard WorkerPer-source file configuration 228*61c4878aSAndroid Build Coastguard Worker============================= 229*61c4878aSAndroid Build Coastguard WorkerThis module defines macros that can be overridden to independently control the 230*61c4878aSAndroid Build Coastguard Workerbehavior of ``pw_log`` statements for each C or C++ source file. To override 231*61c4878aSAndroid Build Coastguard Workerthese macros, add ``#define`` statements for them before including headers. 232*61c4878aSAndroid Build Coastguard Worker 233*61c4878aSAndroid Build Coastguard WorkerThe option macro definitions must be visible to ``pw_log/log.h`` the first time 234*61c4878aSAndroid Build Coastguard Workerit is included. To handle potential transitive includes, place these 235*61c4878aSAndroid Build Coastguard Worker``#defines`` before all ``#include`` statements. This should only be done in 236*61c4878aSAndroid Build Coastguard Workersource files, not headers. For example: 237*61c4878aSAndroid Build Coastguard Worker 238*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp 239*61c4878aSAndroid Build Coastguard Worker 240*61c4878aSAndroid Build Coastguard Worker // Set the pw_log option macros here, before ALL of the #includes. 241*61c4878aSAndroid Build Coastguard Worker #define PW_LOG_MODULE_NAME "Calibration" 242*61c4878aSAndroid Build Coastguard Worker #define PW_LOG_LEVEL PW_LOG_LEVEL_WARN 243*61c4878aSAndroid Build Coastguard Worker 244*61c4878aSAndroid Build Coastguard Worker #include <array> 245*61c4878aSAndroid Build Coastguard Worker #include <random> 246*61c4878aSAndroid Build Coastguard Worker 247*61c4878aSAndroid Build Coastguard Worker #include "devices/hal9000.h" 248*61c4878aSAndroid Build Coastguard Worker #include "pw_log/log.h" 249*61c4878aSAndroid Build Coastguard Worker #include "pw_rpc/server.h" 250*61c4878aSAndroid Build Coastguard Worker 251*61c4878aSAndroid Build Coastguard Worker int MyFunction() { 252*61c4878aSAndroid Build Coastguard Worker PW_LOG_INFO("hello???"); 253*61c4878aSAndroid Build Coastguard Worker } 254*61c4878aSAndroid Build Coastguard Worker 255*61c4878aSAndroid Build Coastguard Worker.. c:macro:: PW_LOG_MODULE_NAME 256*61c4878aSAndroid Build Coastguard Worker 257*61c4878aSAndroid Build Coastguard Worker A string literal module name to use in logs. Log backends may attach this 258*61c4878aSAndroid Build Coastguard Worker name to log messages or use it for runtime filtering. Defaults to ``""``. 259*61c4878aSAndroid Build Coastguard Worker 260*61c4878aSAndroid Build Coastguard Worker.. c:macro:: PW_LOG_FLAGS 261*61c4878aSAndroid Build Coastguard Worker 262*61c4878aSAndroid Build Coastguard Worker Log flags to use for the ``PW_LOG_<level>`` macros. Different flags may be 263*61c4878aSAndroid Build Coastguard Worker applied when using the ``PW_LOG`` macro directly. 264*61c4878aSAndroid Build Coastguard Worker 265*61c4878aSAndroid Build Coastguard Worker Log backends use flags to change how they handle individual log messages. 266*61c4878aSAndroid Build Coastguard Worker Potential uses include assigning logs priority or marking them as containing 267*61c4878aSAndroid Build Coastguard Worker personal information. Defaults to ``PW_LOG_FLAGS_DEFAULT``. 268*61c4878aSAndroid Build Coastguard Worker 269*61c4878aSAndroid Build Coastguard Worker.. c:macro:: PW_LOG_LEVEL 270*61c4878aSAndroid Build Coastguard Worker 271*61c4878aSAndroid Build Coastguard Worker Filters logs by level. Source files that define ``PW_LOG_LEVEL`` will display 272*61c4878aSAndroid Build Coastguard Worker only logs at or above the chosen level. Log statements below this level will 273*61c4878aSAndroid Build Coastguard Worker be compiled out of optimized builds. Defaults to ``PW_LOG_LEVEL_DEFAULT``. 274*61c4878aSAndroid Build Coastguard Worker 275*61c4878aSAndroid Build Coastguard Worker Example: 276*61c4878aSAndroid Build Coastguard Worker 277*61c4878aSAndroid Build Coastguard Worker .. code-block:: cpp 278*61c4878aSAndroid Build Coastguard Worker 279*61c4878aSAndroid Build Coastguard Worker #define PW_LOG_LEVEL PW_LOG_LEVEL_INFO 280*61c4878aSAndroid Build Coastguard Worker 281*61c4878aSAndroid Build Coastguard Worker #include "pw_log/log.h" 282*61c4878aSAndroid Build Coastguard Worker 283*61c4878aSAndroid Build Coastguard Worker void DoSomething() { 284*61c4878aSAndroid Build Coastguard Worker PW_LOG_DEBUG("This won't be logged at all"); 285*61c4878aSAndroid Build Coastguard Worker PW_LOG_INFO("This is INFO level, and will display"); 286*61c4878aSAndroid Build Coastguard Worker PW_LOG_WARN("This is above INFO level, and will display"); 287*61c4878aSAndroid Build Coastguard Worker } 288*61c4878aSAndroid Build Coastguard Worker 289*61c4878aSAndroid Build Coastguard Worker.. _module-pw_log-logging_attributes: 290*61c4878aSAndroid Build Coastguard Worker 291*61c4878aSAndroid Build Coastguard Worker------------------ 292*61c4878aSAndroid Build Coastguard WorkerLogging attributes 293*61c4878aSAndroid Build Coastguard Worker------------------ 294*61c4878aSAndroid Build Coastguard WorkerThe logging facade in Pigweed is designed to facilitate the capture of at least 295*61c4878aSAndroid Build Coastguard Workerthe following attributes: 296*61c4878aSAndroid Build Coastguard Worker 297*61c4878aSAndroid Build Coastguard Worker- *Level* - The log level; for example, INFO, DEBUG, ERROR, etc. Typically an 298*61c4878aSAndroid Build Coastguard Worker integer 299*61c4878aSAndroid Build Coastguard Worker- *Flags* - Bitset for e.g. RELIABLE_DELIVERY, or HAS_PII, or BEST_EFFORT 300*61c4878aSAndroid Build Coastguard Worker- *File* - The file where the log was triggered 301*61c4878aSAndroid Build Coastguard Worker- *Line* - The line number in the file where the log line occured 302*61c4878aSAndroid Build Coastguard Worker- *Function* - What function the log comes from. This is expensive in binary 303*61c4878aSAndroid Build Coastguard Worker size to use! 304*61c4878aSAndroid Build Coastguard Worker- *Module* - The user-defined module name for the log statement; e.g. “BLE” or 305*61c4878aSAndroid Build Coastguard Worker “BAT” 306*61c4878aSAndroid Build Coastguard Worker- *Message* - The message itself; with % format arguments 307*61c4878aSAndroid Build Coastguard Worker- *Arguments* - The format arguments to message 308*61c4878aSAndroid Build Coastguard Worker- *Thread* - For devices running with an RTOS, capturing the thread is very 309*61c4878aSAndroid Build Coastguard Worker useful 310*61c4878aSAndroid Build Coastguard Worker- *Others* - Processor security level? Maybe Thread is a good proxy for this 311*61c4878aSAndroid Build Coastguard Worker 312*61c4878aSAndroid Build Coastguard WorkerEach backend may decide to capture different attributes to balance the tradeoff 313*61c4878aSAndroid Build Coastguard Workerbetween call site code size, call site run time, wire format size, logging 314*61c4878aSAndroid Build Coastguard Workercomplexity, and more. 315*61c4878aSAndroid Build Coastguard Worker 316*61c4878aSAndroid Build Coastguard Worker.. _module-pw_log-circular-deps: 317*61c4878aSAndroid Build Coastguard Worker 318*61c4878aSAndroid Build Coastguard Worker---------------------------------------------- 319*61c4878aSAndroid Build Coastguard WorkerAvoiding circular dependencies with ``PW_LOG`` 320*61c4878aSAndroid Build Coastguard Worker---------------------------------------------- 321*61c4878aSAndroid Build Coastguard WorkerBecause logs are so widely used, including in low-level libraries, it is 322*61c4878aSAndroid Build Coastguard Workercommon for the ``pw_log`` backend to cause circular dependencies. Because of 323*61c4878aSAndroid Build Coastguard Workerthis, log backends may avoid declaring explicit dependencies, instead relying 324*61c4878aSAndroid Build Coastguard Workeron include paths to access header files. 325*61c4878aSAndroid Build Coastguard Worker 326*61c4878aSAndroid Build Coastguard WorkerGN 327*61c4878aSAndroid Build Coastguard Worker== 328*61c4878aSAndroid Build Coastguard WorkerIn GN, the ``pw_log`` backend's full implementation with true dependencies is 329*61c4878aSAndroid Build Coastguard Workermade available through the ``$dir_pw_log:impl`` group. When ``pw_log_BACKEND`` 330*61c4878aSAndroid Build Coastguard Workeris set, ``$dir_pw_log:impl`` must be listed in the ``pw_build_LINK_DEPS`` 331*61c4878aSAndroid Build Coastguard Workervariable. See :ref:`module-pw_build-link-deps`. 332*61c4878aSAndroid Build Coastguard Worker 333*61c4878aSAndroid Build Coastguard WorkerIn the ``pw_log``, the backend's full implementation is placed in the 334*61c4878aSAndroid Build Coastguard Worker``$pw_log_BACKEND.impl`` target. ``$dir_pw_log:impl`` depends on this 335*61c4878aSAndroid Build Coastguard Workerbackend target. The ``$pw_log_BACKEND.impl`` target may be an empty group if 336*61c4878aSAndroid Build Coastguard Workerthe backend target can use its dependencies directly without causing circular 337*61c4878aSAndroid Build Coastguard Workerdependencies. 338*61c4878aSAndroid Build Coastguard Worker 339*61c4878aSAndroid Build Coastguard WorkerIn order to break dependency cycles, the ``pw_log_BACKEND`` target may need 340*61c4878aSAndroid Build Coastguard Workerto directly provide dependencies through include paths only, rather than GN 341*61c4878aSAndroid Build Coastguard Worker``public_deps``. In this case, GN header checking can be disabled with 342*61c4878aSAndroid Build Coastguard Worker``check_includes = false``. 343*61c4878aSAndroid Build Coastguard Worker 344*61c4878aSAndroid Build Coastguard Worker.. _module-pw_log-bazel-backend_impl: 345*61c4878aSAndroid Build Coastguard Worker 346*61c4878aSAndroid Build Coastguard WorkerBazel 347*61c4878aSAndroid Build Coastguard Worker===== 348*61c4878aSAndroid Build Coastguard WorkerIn Bazel, log backends may avoid cyclic dependencies by placing the full 349*61c4878aSAndroid Build Coastguard Workerimplementation in an ``impl`` target, like ``//pw_log_tokenized:impl``. The 350*61c4878aSAndroid Build Coastguard Worker``//pw_log:backend_impl`` label flag should be set to the ``impl`` target 351*61c4878aSAndroid Build Coastguard Workerrequired by the log backend used by the platform. 352*61c4878aSAndroid Build Coastguard Worker 353*61c4878aSAndroid Build Coastguard WorkerYou must add a dependency on the ``@pigweed//pw_log:backend_impl`` target to 354*61c4878aSAndroid Build Coastguard Workerany binary using ``pw_log``. 355*61c4878aSAndroid Build Coastguard Worker 356*61c4878aSAndroid Build Coastguard WorkerSee :ref:`docs-build_system-bazel_link-extra-lib` for a general discussion of 357*61c4878aSAndroid Build Coastguard Workercyclic dependencies in low-level libraries in Bazel. 358*61c4878aSAndroid Build Coastguard Worker 359*61c4878aSAndroid Build Coastguard Worker---------------------- 360*61c4878aSAndroid Build Coastguard WorkerGoogle Logging Adapter 361*61c4878aSAndroid Build Coastguard Worker---------------------- 362*61c4878aSAndroid Build Coastguard WorkerPigweed provides a minimal C++ stream-style Google Log set of adapter 363*61c4878aSAndroid Build Coastguard Workermacros around PW_LOG under ``pw_log/glog_adapter.h`` for compatibility with 364*61c4878aSAndroid Build Coastguard Workernon-embedded code. While it is effective for porting server code to 365*61c4878aSAndroid Build Coastguard Workermicrocontrollers quickly, we do not advise embedded projects use that approach 366*61c4878aSAndroid Build Coastguard Workerunless absolutely necessary. 367*61c4878aSAndroid Build Coastguard Worker 368*61c4878aSAndroid Build Coastguard WorkerConfiguration 369*61c4878aSAndroid Build Coastguard Worker============== 370*61c4878aSAndroid Build Coastguard Worker 371*61c4878aSAndroid Build Coastguard Worker.. c:macro:: PW_LOG_CFG_GLOG_BUFFER_SIZE_BYTES 372*61c4878aSAndroid Build Coastguard Worker 373*61c4878aSAndroid Build Coastguard Worker The size of the stack-allocated buffer used by the Google Logging (glog) 374*61c4878aSAndroid Build Coastguard Worker macros. This only affects the glog macros provided through pw_log/glog.h. 375*61c4878aSAndroid Build Coastguard Worker 376*61c4878aSAndroid Build Coastguard Worker Pigweed strongly recommends sticking to printf-style logging instead 377*61c4878aSAndroid Build Coastguard Worker of C++ stream-style Google Log logging unless absolutely necessary. The glog 378*61c4878aSAndroid Build Coastguard Worker macros are only provided for compatibility with non-embedded code. See 379*61c4878aSAndroid Build Coastguard Worker :ref:`module-pw_log-design-discussion` for more details. 380*61c4878aSAndroid Build Coastguard Worker 381*61c4878aSAndroid Build Coastguard Worker Undersizing this buffer will result in truncated log messages. 382*61c4878aSAndroid Build Coastguard Worker 383*61c4878aSAndroid Build Coastguard Worker----------------- 384*61c4878aSAndroid Build Coastguard WorkerDesign discussion 385*61c4878aSAndroid Build Coastguard Worker----------------- 386*61c4878aSAndroid Build Coastguard Worker 387*61c4878aSAndroid Build Coastguard Worker.. _module-pw_log-design-discussion: 388*61c4878aSAndroid Build Coastguard Worker 389*61c4878aSAndroid Build Coastguard WorkerWhy not use C++ style stream logging operators like Google Log? 390*61c4878aSAndroid Build Coastguard Worker=============================================================== 391*61c4878aSAndroid Build Coastguard WorkerThere are multiple reasons to avoid the C++ stream logging style in embedded, 392*61c4878aSAndroid Build Coastguard Workerbut the biggest reason is that C++ stream logging defeats log tokenization. By 393*61c4878aSAndroid Build Coastguard Workerhaving the string literals broken up between ``<<`` operators, tokenization 394*61c4878aSAndroid Build Coastguard Workerbecomes impossible with current language features. 395*61c4878aSAndroid Build Coastguard Worker 396*61c4878aSAndroid Build Coastguard WorkerConsider this example use of Google Log: 397*61c4878aSAndroid Build Coastguard Worker 398*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp 399*61c4878aSAndroid Build Coastguard Worker 400*61c4878aSAndroid Build Coastguard Worker LOG(INFO) << "My temperature is " << temperature << ". State: " << state; 401*61c4878aSAndroid Build Coastguard Worker 402*61c4878aSAndroid Build Coastguard WorkerThis log statement has two string literals. It might seem like one could convert 403*61c4878aSAndroid Build Coastguard Workermove to tokenization: 404*61c4878aSAndroid Build Coastguard Worker 405*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp 406*61c4878aSAndroid Build Coastguard Worker 407*61c4878aSAndroid Build Coastguard Worker LOG(INFO) << TOKEN("My temperature is ") << temperature << TOKEN(". State: ") << state; 408*61c4878aSAndroid Build Coastguard Worker 409*61c4878aSAndroid Build Coastguard WorkerHowever, this doesn't work. The key problem is that the tokenization system 410*61c4878aSAndroid Build Coastguard Workerneeds to allocate the string in a linker section that is excluded from the 411*61c4878aSAndroid Build Coastguard Workerfinal binary, but is in the final ELF executable (and so can be extracted). 412*61c4878aSAndroid Build Coastguard WorkerSince there is no way to declare a string or array in a different section in 413*61c4878aSAndroid Build Coastguard Workerthe middle of an experession in C++, it is not possible to tokenize an 414*61c4878aSAndroid Build Coastguard Workerexpression like the above. 415*61c4878aSAndroid Build Coastguard Worker 416*61c4878aSAndroid Build Coastguard WorkerIn contrast, the ``printf``-style version is a single statement with a single 417*61c4878aSAndroid Build Coastguard Workerstring constant, which can be expanded by the preprocessor (as part of 418*61c4878aSAndroid Build Coastguard Worker``pw_tokenizer``) into a constant array in a special section. 419*61c4878aSAndroid Build Coastguard Worker 420*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp 421*61c4878aSAndroid Build Coastguard Worker 422*61c4878aSAndroid Build Coastguard Worker // Note: LOG_INFO can be tokenized behind the macro; transparent to users. 423*61c4878aSAndroid Build Coastguard Worker PW_LOG_INFO("My temperature is %d. State: %s", temperature, state); 424*61c4878aSAndroid Build Coastguard Worker 425*61c4878aSAndroid Build Coastguard WorkerAdditionally, while Pigweed is mostly C++, it a practical reality that at times 426*61c4878aSAndroid Build Coastguard Workerprojects using Pigweed will need to log from third-party libraries written in 427*61c4878aSAndroid Build Coastguard WorkerC. Thus, we also wanted to retain C compatibility. 428*61c4878aSAndroid Build Coastguard Worker 429*61c4878aSAndroid Build Coastguard WorkerIn summary, printf-style logging is better for Pigweed's target audience 430*61c4878aSAndroid Build Coastguard Workerbecause it: 431*61c4878aSAndroid Build Coastguard Worker 432*61c4878aSAndroid Build Coastguard Worker- works with tokenization 433*61c4878aSAndroid Build Coastguard Worker- is C compatibile 434*61c4878aSAndroid Build Coastguard Worker- has smaller call sites 435*61c4878aSAndroid Build Coastguard Worker 436*61c4878aSAndroid Build Coastguard WorkerSee also :ref:`module-pw_log_tokenized` for details on leveraging Pigweed's 437*61c4878aSAndroid Build Coastguard Workertokenizer module for logging. 438*61c4878aSAndroid Build Coastguard Worker 439*61c4878aSAndroid Build Coastguard WorkerSee also :ref:`module-pw_tokenizer` for details on Pigweed's tokenizer, 440*61c4878aSAndroid Build Coastguard Workerwhich is useful for more than just logging. 441*61c4878aSAndroid Build Coastguard Worker 442*61c4878aSAndroid Build Coastguard WorkerWhy does the facade use header redirection instead of C functions? 443*61c4878aSAndroid Build Coastguard Worker================================================================== 444*61c4878aSAndroid Build Coastguard WorkerWithout header redirection, it is not possible to do sophisticated macro 445*61c4878aSAndroid Build Coastguard Workertransforms in the backend. For example, to apply tokenization to log strings, 446*61c4878aSAndroid Build Coastguard Workerthe backend must define the handling macros. Additionally, compile-time 447*61c4878aSAndroid Build Coastguard Workerfiltering by log level or flags is not possible without header redirection. 448*61c4878aSAndroid Build Coastguard WorkerWhile it may be possible to do the filtering in the facade, that would imply 449*61c4878aSAndroid Build Coastguard Workerhaving the same filtering implementation for all log handling, which is a 450*61c4878aSAndroid Build Coastguard Workerrestriction we want to avoid. 451*61c4878aSAndroid Build Coastguard Worker 452*61c4878aSAndroid Build Coastguard WorkerWhy is the module name done as a preprocessor define rather than an argument? 453*61c4878aSAndroid Build Coastguard Worker============================================================================= 454*61c4878aSAndroid Build Coastguard WorkerAPIs are a balance between power and ease of use. In the practical cases we 455*61c4878aSAndroid Build Coastguard Workerhave seen over the years, most translation units only need to log to one 456*61c4878aSAndroid Build Coastguard Workermodule, like ``"BLE"``, ``"PWR"``, ``"BAT"`` and so on. Thus, adding the 457*61c4878aSAndroid Build Coastguard Workerargument to each macro call seemed like too much. On the other hand, flags are 458*61c4878aSAndroid Build Coastguard Workersomething that are typically added on a per-log-statement basis, and is why the 459*61c4878aSAndroid Build Coastguard Workerflags are added on a per-call basis (though hidden through the high-level 460*61c4878aSAndroid Build Coastguard Workermacros). 461*61c4878aSAndroid Build Coastguard Worker 462*61c4878aSAndroid Build Coastguard Worker-------------- 463*61c4878aSAndroid Build Coastguard Workerpw_log in Java 464*61c4878aSAndroid Build Coastguard Worker-------------- 465*61c4878aSAndroid Build Coastguard Worker``pw_log`` provides a thin Java logging class that uses Google's `Flogger 466*61c4878aSAndroid Build Coastguard Worker<https://google.github.io/flogger/>`_ API. The purpose of this wrapper is to 467*61c4878aSAndroid Build Coastguard Workersupport logging on platforms that do not support Flogger. The main 468*61c4878aSAndroid Build Coastguard Workerimplementation in ``pw_log/java/main`` simply wraps a 469*61c4878aSAndroid Build Coastguard Worker``com.google.common.flogger.FluentLogger``. An implementation that logs to 470*61c4878aSAndroid Build Coastguard WorkerAndroid's ``android.util.Log`` instead is provided in 471*61c4878aSAndroid Build Coastguard Worker``pw_log/java/android_main``. 472*61c4878aSAndroid Build Coastguard Worker 473*61c4878aSAndroid Build Coastguard Worker---------------- 474*61c4878aSAndroid Build Coastguard Workerpw_log in Python 475*61c4878aSAndroid Build Coastguard Worker---------------- 476*61c4878aSAndroid Build Coastguard Worker``pw_log`` provides utilities to decode ``LogEntries`` and the encapsulated 477*61c4878aSAndroid Build Coastguard Worker``LogEntry`` proto messages. 478*61c4878aSAndroid Build Coastguard Worker 479*61c4878aSAndroid Build Coastguard WorkerThe ``Log`` class represents a decoded ``LogEntry`` in a human-readable and 480*61c4878aSAndroid Build Coastguard Workerconsumable fashion. 481*61c4878aSAndroid Build Coastguard Worker 482*61c4878aSAndroid Build Coastguard WorkerThe ``LogStreamDecoder`` offers APIs to decode ``LogEntries`` and ``LogEntry`` 483*61c4878aSAndroid Build Coastguard Workerwhile tracking and logging log drops. It requires a ``decoded_log_handler`` to 484*61c4878aSAndroid Build Coastguard Workerpass decoded logs to. This class can also be customized to use an optional token 485*61c4878aSAndroid Build Coastguard Workerdatabase if the message, module and thread names are tokenized; a custom 486*61c4878aSAndroid Build Coastguard Workertimestamp parser; and optional message parser for any extra message parsing. 487*61c4878aSAndroid Build Coastguard Worker``pw_log`` includes examples for customizing the ``LogStreamDecoder``: 488*61c4878aSAndroid Build Coastguard Worker``timestamp_parser_ns_since_boot`` parses the timestamp number from nanoseconds 489*61c4878aSAndroid Build Coastguard Workersince boot to an HH:MM::SS string, ``log_decoded_log`` emits a decoded ``Log`` 490*61c4878aSAndroid Build Coastguard Workerto the provided logger in a format known to ``pw console``, and 491*61c4878aSAndroid Build Coastguard Worker``pw_status_code_to_name`` searches the decoded log message for a matching 492*61c4878aSAndroid Build Coastguard Workerpattern encapsulating the status code number and replaces it with the status 493*61c4878aSAndroid Build Coastguard Workername. 494*61c4878aSAndroid Build Coastguard Worker 495*61c4878aSAndroid Build Coastguard WorkerPython API 496*61c4878aSAndroid Build Coastguard Worker========== 497*61c4878aSAndroid Build Coastguard Worker 498*61c4878aSAndroid Build Coastguard Workerpw_log.log_decoder 499*61c4878aSAndroid Build Coastguard Worker------------------ 500*61c4878aSAndroid Build Coastguard Worker.. automodule:: pw_log.log_decoder 501*61c4878aSAndroid Build Coastguard Worker :members: Log, LogStreamDecoder 502*61c4878aSAndroid Build Coastguard Worker :undoc-members: 503*61c4878aSAndroid Build Coastguard Worker :show-inheritance: 504*61c4878aSAndroid Build Coastguard Worker 505*61c4878aSAndroid Build Coastguard Worker.. toctree:: 506*61c4878aSAndroid Build Coastguard Worker :hidden: 507*61c4878aSAndroid Build Coastguard Worker :maxdepth: 1 508*61c4878aSAndroid Build Coastguard Worker 509*61c4878aSAndroid Build Coastguard Worker protobuf 510*61c4878aSAndroid Build Coastguard Worker tokenized_args 511*61c4878aSAndroid Build Coastguard Worker Backends <backends> 512