1 /* 2 * Copyright (c) 2015, Xilinx Inc. and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 /* 8 * @file log.h 9 * @brief Logging support for libmetal. 10 */ 11 12 #ifndef __METAL_METAL_LOG__H__ 13 #define __METAL_METAL_LOG__H__ 14 15 #ifdef __cplusplus 16 extern "C" { 17 #endif 18 19 /** \defgroup logging Library Logging Interfaces 20 * @{ 21 */ 22 23 /** Log message priority levels for libmetal. */ 24 enum metal_log_level { 25 METAL_LOG_EMERGENCY, /**< system is unusable. */ 26 METAL_LOG_ALERT, /**< action must be taken immediately. */ 27 METAL_LOG_CRITICAL, /**< critical conditions. */ 28 METAL_LOG_ERROR, /**< error conditions. */ 29 METAL_LOG_WARNING, /**< warning conditions. */ 30 METAL_LOG_NOTICE, /**< normal but significant condition. */ 31 METAL_LOG_INFO, /**< informational messages. */ 32 METAL_LOG_DEBUG, /**< debug-level messages. */ 33 }; 34 35 /** Log message handler type. */ 36 typedef void (*metal_log_handler)(enum metal_log_level level, 37 const char *format, ...); 38 39 /** 40 * @brief Set libmetal log handler. 41 * @param[in] handler log message handler. 42 * @return 0 on success, or -errno on failure. 43 */ 44 void metal_set_log_handler(metal_log_handler handler); 45 46 /** 47 * @brief Get the current libmetal log handler. 48 * @return Current log handler. 49 */ 50 metal_log_handler metal_get_log_handler(void); 51 52 /** 53 * @brief Set the level for libmetal logging. 54 * @param[in] level log message level. 55 */ 56 void metal_set_log_level(enum metal_log_level level); 57 58 /** 59 * @brief Get the current level for libmetal logging. 60 * @return Current log level. 61 */ 62 enum metal_log_level metal_get_log_level(void); 63 64 /** 65 * @brief Default libmetal log handler. This handler prints libmetal log 66 * messages to stderr. 67 * @param[in] level log message level. 68 * @param[in] format log message format string. 69 * @return 0 on success, or -errno on failure. 70 */ 71 void metal_default_log_handler(enum metal_log_level level, 72 const char *format, ...); 73 74 /** 75 * @internal 76 * 77 * @brief used by the metal_log() macro to update the format string 78 * 79 * If ML_FUNC_LINE is defined this macro generates a unified format 80 * string for metal_log() and its convenience metal_*() macros, i.e. it 81 * adds function-name:line-number prefix to all log messages. 82 * 83 * @param[in] fmt format string passed from the metal_log() macro 84 */ 85 #if defined(ML_FUNC_LINE) 86 #define metal_fmt(fmt) "%s:%u " fmt, __func__, __LINE__ 87 #else /* ML_FUNC_LINE */ 88 #define metal_fmt(fmt) fmt 89 #endif /* ML_FUNC_LINE */ 90 91 /** 92 * @brief Emit a log message if the log level permits. 93 * 94 * @param level Log level. 95 * @param fmt Format string. 96 * @param ... Variable number of arguments. 97 */ 98 #define metal_log(level, fmt, ...) ({ \ 99 if (_metal.common.log_handler && level <= _metal.common.log_level) \ 100 _metal.common.log_handler(level, metal_fmt(fmt), ## __VA_ARGS__); \ 101 }) 102 103 #define metal_err(fmt, args...) metal_log(METAL_LOG_ERROR, fmt, ##args) 104 #define metal_warn(fmt, args...) metal_log(METAL_LOG_WARNING, fmt, ##args) 105 #define metal_info(fmt, args...) metal_log(METAL_LOG_INFO, fmt, ##args) 106 #define metal_dbg(fmt, args...) metal_log(METAL_LOG_DEBUG, fmt, ##args) 107 108 /** @} */ 109 110 #ifdef __cplusplus 111 } 112 #endif 113 114 #include <metal/system/@PROJECT_SYSTEM@/log.h> 115 116 #endif /* __METAL_METAL_LOG__H__ */ 117