Skip to content

Logging - MikroSDK.Log API

Introduction

This example demonstrates how to utilize the logging system of the MikroSDK.Log API within the NECTO Studio IDE. The logging API enables developers to efficiently output diagnostic and status messages from embedded applications, helping with debugging, monitoring, and troubleshooting. This is particularly useful in resource-constrained environments where real-time feedback is essential for understanding system behavior.

By using the MikroSDK.Log API, developers can log messages at different levels (e.g., debug, info, warning, error) and redirect them to various output targets, such as UART, file systems, or the NECTO Studio terminal.

API Name

  • MikroSDK.Log

The MikroSDK.Log header provides a set of functions and macros for structured logging, enabling developers to output log messages with different severity levels and easily manage log output across their embedded systems.

Location of Files and Directories

Prerequisites

  • Library Manager:

    • To add the MikroSDK.Log library to your project, follow these steps:
      • Open NECTO Studio and navigate to the Library Manager.
      • Select the MikroSDK.Log library from the library list.
  • Headers:

    • Include the following header in your source files to access the logging functionality:
      • #include "MikroSDK.Log"
  • CMakeLists:

    • No manual configuration of the CMake file is required, as this is automatically handled when adding the library via the Library Manager.
    • If needed, ensure your CMakeLists.txt includes the following configuration to link the MikroSDK.Log library:

      find_package(MikroSDK.Log REQUIRED)
      target_link_libraries(${PROJECT_NAME} PUBLIC MikroSDK.Log)
      

Code Examples

  • Logging Example - MikroSDK.Log API
/******************************************************************************
 * Project name: Logging Example - `Log` API
 * Copyright:
 *   (c) MIKROE, 2024.
 * Description:
 *   - This example demonstrates the usage of MikroSDK.Log API.
 *   - It shows how to configure and initialize the logging system, and log messages
 *     with different levels (debug, info, warning, fatal).
 * Library dependencies:
 *   - Make sure `Log` library is enabled in NECTO's Library Manager to
 *     ensure a successful build.
 * How to test this code example:
 *   - Connect the MCU to your development board.
 *   - Compile the code in NECTO Studio.
 *   - Run Debug Mode (press F9 on your keyboard).
 *   - Run the example (press F6 on your keyboard).
 ******************************************************************************/

/* ---------------------------------------------------------------- INCLUDES */
/**
 * Include necessary headers for logging and UART functionalities.
 * log.h handles all the logging functionality, while UART is used for output.
 */
#ifdef PREINIT_SUPPORTED
#include "preinit.h"  // Optional, if pre-initialization is supported
#endif

#include "log.h"    // Include mikroSDK v2.0 Log API for UART-based logging

/* ------------------------------------------------------------- USER CODE  */
int main() {
    /* Ensure any necessary pre-initialization is executed to set up clocks or other peripherals. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif

    /**
     * Logger initialization.
     * Default baud rate: 115200
     * Default log level: LOG_LEVEL_DEBUG
     * @note If USB_UART_RX and USB_UART_TX
     * are defined as HAL_PIN_NC, you will
     * need to define them manually for log to work.
     * See @b LOG_MAP_USB_UART macro definition for detailed explanation.
     */
    log_cfg_t log_cfg;  // Define configuration structure for logging
    log_t logger;       // Define the logger instance
    LOG_MAP_USB_UART( log_cfg );

    /* Initialize logging with the defined configuration. */
    log_init(&logger, &log_cfg);

    /* Print an informational message indicating that logging has started. */
    log_info(&logger, "---- Application Started ----\n");

    /* Log a debug message to assist with detailed system diagnostics.
     * Typically used during development for tracking code execution paths.
     */
    log_debug(&logger, "Debug message: System init complete.\n");

    /* Log an informational message.
     * This message level is used to indicate general system status.
     */
    log_printf(&logger, "Info message: System is running.\n");

    /* Log a warning message.
     * Warnings are used to indicate that something unexpected occurred,
     * but the system can still function.
     */
    log_warning(&logger, "Warning message: Low memory detected.\n");

    /* Log an error message.
     * Errors are used for critical issues where the system cannot function correctly.
     */
    log_fatal(&logger, "Error message: Critical failure!\n");

    /* End of the main function, where the logging system will continue to run,
     * allowing the capture and display of any future log events.
     */
    return 0;
}

/* ----------------------------------------------------------------------- END */