Commit a8e4e971 authored by Fabio Utzig's avatar Fabio Utzig Committed by Fabio Utzig
Browse files

Update logging to use per platform log macros

Now bootutil_log.h was converted to use per platform logging primitives
defined according to documentation established on the template file.

Some "null" macros were left to avoid breaking per source logging level
definition when MCUBOOT_HAVE_LOGGING was not defined.

Signed-off-by: default avatarFabio Utzig <utzig@apache.org>
parent 9a4b9ba1
Loading
Loading
Loading
Loading
+15 −163
Changes for boot/bootutil/include/bootutil/bootutil_log.h: 15 added lines, 163 removed lines.
Original line number Diff line number Diff line
@@ -23,183 +23,35 @@
extern "C" {
#endif

/*
 * When building for targets running Zephyr, delegate to its native
 * logging subsystem.
 *
 * In this case:
 *
 * - BOOT_LOG_LEVEL determines SYS_LOG_LEVEL,
 * - BOOT_LOG_ERR() and friends are SYS_LOG_ERR() etc.
 * - SYS_LOG_DOMAIN is unconditionally set to "MCUBOOT"
 */
#ifdef __ZEPHYR__
#include <mcuboot_config/mcuboot_config.h>

#define BOOT_LOG_LEVEL_OFF      SYS_LOG_LEVEL_OFF
#define BOOT_LOG_LEVEL_ERROR    SYS_LOG_LEVEL_ERROR
#define BOOT_LOG_LEVEL_WARNING  SYS_LOG_LEVEL_WARNING
#define BOOT_LOG_LEVEL_INFO     SYS_LOG_LEVEL_INFO
#define BOOT_LOG_LEVEL_DEBUG    SYS_LOG_LEVEL_DEBUG
#ifdef MCUBOOT_HAVE_LOGGING

/* Treat BOOT_LOG_LEVEL equivalently to SYS_LOG_LEVEL. */
#ifndef BOOT_LOG_LEVEL
#define BOOT_LOG_LEVEL CONFIG_SYS_LOG_DEFAULT_LEVEL
#elif (BOOT_LOG_LEVEL < CONFIG_SYS_LOG_OVERRIDE_LEVEL)
#undef BOOT_LOG_LEVEL
#define BOOT_LOG_LEVEL CONFIG_SYS_LOG_OVERRIDE_LEVEL
#ifdef BOOT_LOG_LEVEL
#define MCUBOOT_LOG_LEVEL BOOT_LOG_LEVEL
#endif

#define SYS_LOG_LEVEL BOOT_LOG_LEVEL
#define BOOT_LOG_LEVEL_OFF     MCUBOOT_LOG_LEVEL_OFF
#define BOOT_LOG_LEVEL_ERROR   MCUBOOT_LOG_LEVEL_ERROR
#define BOOT_LOG_LEVEL_WARNING MCUBOOT_LOG_LEVEL_WARNING
#define BOOT_LOG_LEVEL_INFO    MCUBOOT_LOG_LEVEL_INFO
#define BOOT_LOG_LEVEL_DEBUG   MCUBOOT_LOG_LEVEL_DEBUG

#undef SYS_LOG_DOMAIN
#define SYS_LOG_DOMAIN "MCUBOOT"

#define BOOT_LOG_ERR(...) SYS_LOG_ERR(__VA_ARGS__)
#define BOOT_LOG_WRN(...) SYS_LOG_WRN(__VA_ARGS__)
#define BOOT_LOG_INF(...) SYS_LOG_INF(__VA_ARGS__)
#define BOOT_LOG_DBG(...) SYS_LOG_DBG(__VA_ARGS__)

#include <logging/sys_log.h>

/*
 * When built on the simulator, just use printf().
 */
#elif defined(__BOOTSIM__)      /* !defined(__ZEPHYR__) */

#include <stdio.h>

#define BOOT_LOG_LEVEL_OFF      0
#define BOOT_LOG_LEVEL_ERROR    1
#define BOOT_LOG_LEVEL_WARNING  2
#define BOOT_LOG_LEVEL_INFO     3
#define BOOT_LOG_LEVEL_DEBUG    4

/*
 * The compiled log level determines the maximum level that can be
 * printed.  Messages at or below this level can be printed, provided
 * they are also enabled through the Rust logging system, such as by
 * setting RUST_LOG to bootsim::api=info.
 */
#ifndef BOOT_LOG_LEVEL
#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
#endif
#include <mcuboot_config/mcuboot_logging.h>

int sim_log_enabled(int level);
#define BOOT_LOG_ERR(...) MCUBOOT_LOG_ERR(__VA_ARGS__)
#define BOOT_LOG_WRN(...) MCUBOOT_LOG_WRN(__VA_ARGS__)
#define BOOT_LOG_INF(...) MCUBOOT_LOG_INF(__VA_ARGS__)
#define BOOT_LOG_DBG(...) MCUBOOT_LOG_DBG(__VA_ARGS__)

#if BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_ERROR
#define BOOT_LOG_ERR(_fmt, ...)                                         \
    do {                                                                \
        if (sim_log_enabled(BOOT_LOG_LEVEL_ERROR)) {                    \
            fprintf(stderr, "[ERR] " _fmt "\n", ##__VA_ARGS__);         \
        }                                                               \
    } while (0)
#else
#define BOOT_LOG_ERR(...) IGNORE(__VA_ARGS__)
#endif

#if BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_WARNING
#define BOOT_LOG_WRN(_fmt, ...)                                         \
    do {                                                                \
        if (sim_log_enabled(BOOT_LOG_LEVEL_WARNING)) {                  \
            fprintf(stderr, "[WRN] " _fmt "\n", ##__VA_ARGS__);         \
        }                                                               \
    } while (0)
#else
#define BOOT_LOG_WRN(...) IGNORE(__VA_ARGS__)
#endif

#if BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_INFO
#define BOOT_LOG_INF(_fmt, ...)                                         \
    do {                                                                \
        if (sim_log_enabled(BOOT_LOG_LEVEL_INFO)) {                     \
            fprintf(stderr, "[INF] " _fmt "\n", ##__VA_ARGS__);         \
        }                                                               \
    } while (0)
#else
#define BOOT_LOG_INF(...) IGNORE(__VA_ARGS__)
#endif

#if BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_DEBUG
#define BOOT_LOG_DBG(_fmt, ...)                                         \
    do {                                                                \
        if (sim_log_enabled(BOOT_LOG_LEVEL_DEBUG)) {                    \
            fprintf(stderr, "[DBG] " _fmt "\n", ##__VA_ARGS__);         \
        }                                                               \
    } while (0)
#else
#define BOOT_LOG_DBG(...) IGNORE(__VA_ARGS__)
#endif

/*
 * When built on Mynewt, just use printf().
 */
#elif defined(MCUBOOT_MYNEWT)

#include <stdio.h>

#define BOOT_LOG_LEVEL_OFF      0
#define BOOT_LOG_LEVEL_ERROR    1
#define BOOT_LOG_LEVEL_WARNING  2
#define BOOT_LOG_LEVEL_INFO     3
#define BOOT_LOG_LEVEL_DEBUG    4

#ifndef BOOT_LOG_LEVEL
#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
#endif

#if BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_ERROR
#define BOOT_LOG_ERR(_fmt, ...)                                         \
    do {                                                                \
        printf("[ERR] " _fmt "\n", ##__VA_ARGS__);                      \
    } while (0)
#else
#define BOOT_LOG_ERR(...) IGNORE(__VA_ARGS__)
#endif

#if BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_WARNING
#define BOOT_LOG_WRN(_fmt, ...)                                         \
    do {                                                                \
        printf("[WRN] " _fmt "\n", ##__VA_ARGS__);                      \
    } while (0)
#else
#define BOOT_LOG_WRN(...) IGNORE(__VA_ARGS__)
#endif

#if BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_INFO
#define BOOT_LOG_INF(_fmt, ...)                                         \
    do {                                                                \
        printf("[INF] " _fmt "\n", ##__VA_ARGS__);                      \
    } while (0)
#else
#define BOOT_LOG_INF(...) IGNORE(__VA_ARGS__)
#endif

#if BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_DEBUG
#define BOOT_LOG_DBG(_fmt, ...)                                         \
    do {                                                                \
        printf("[DBG] " _fmt "\n", ##__VA_ARGS__);                      \
    } while (0)
#else
#define BOOT_LOG_DBG(...) IGNORE(__VA_ARGS__)
#endif

/*
 * In other environments, logging calls are no-ops.
 */
#else  /* !defined(__BOOTSIM__) */

#define BOOT_LOG_LEVEL_OFF      0
#define BOOT_LOG_LEVEL_ERROR    1
#define BOOT_LOG_LEVEL_WARNING  2
#define BOOT_LOG_LEVEL_INFO     3
#define BOOT_LOG_LEVEL_DEBUG    4

#define BOOT_LOG_ERR(...) IGNORE(__VA_ARGS__)
#define BOOT_LOG_WRN(...) IGNORE(__VA_ARGS__)
#define BOOT_LOG_INF(...) IGNORE(__VA_ARGS__)
#define BOOT_LOG_DBG(...) IGNORE(__VA_ARGS__)

#endif
#endif /* MCUBOOT_HAVE_LOGGING */

#ifdef __cplusplus
}