Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | /* * Copyright (c) 2018 Nordic Semiconductor ASA * * SPDX-License-Identifier: Apache-2.0 */ #ifndef ZEPHYR_INCLUDE_LOGGING_LOG_BACKEND_H_ #define ZEPHYR_INCLUDE_LOGGING_LOG_BACKEND_H_ #include <logging/log_msg.h> #include <stdarg.h> #include <sys/__assert.h> #ifdef __cplusplus extern "C" { #endif /** * @brief Logger backend interface * @defgroup log_backend Logger backend interface * @ingroup logger * @{ */ /* Forward declaration of the log_backend type. */ struct log_backend; /** * @brief Logger backend API. */ struct log_backend_api { void (*put)(const struct log_backend *const backend, struct log_msg *msg); void (*put_sync_string)(const struct log_backend *const backend, struct log_msg_ids src_level, u32_t timestamp, const char *fmt, va_list ap); void (*put_sync_hexdump)(const struct log_backend *const backend, struct log_msg_ids src_level, u32_t timestamp, const char *metadata, const u8_t *data, u32_t len); void (*dropped)(const struct log_backend *const backend, u32_t cnt); void (*panic)(const struct log_backend *const backend); void (*init)(void); }; /** * @brief Logger backend control block. */ struct log_backend_control_block { void *ctx; u8_t id; bool active; }; /** * @brief Logger backend structure. */ struct log_backend { const struct log_backend_api *api; struct log_backend_control_block *cb; const char *name; bool autostart; }; extern const struct log_backend __log_backends_start[0]; extern const struct log_backend __log_backends_end[0]; /** * @brief Macro for creating a logger backend instance. * * @param _name Name of the backend instance. * @param _api Logger backend API. * @param _autostart If true backend is initialized and activated together * with the logger subsystem. */ #define LOG_BACKEND_DEFINE(_name, _api, _autostart) \ static struct log_backend_control_block UTIL_CAT(backend_cb_, _name) = \ { \ .active = false, \ .id = 0, \ }; \ static const Z_STRUCT_SECTION_ITERABLE(log_backend, _name) = \ { \ .api = &_api, \ .cb = &UTIL_CAT(backend_cb_, _name), \ .name = STRINGIFY(_name), \ .autostart = _autostart \ } /** * @brief Put message with log entry to the backend. * * @param[in] backend Pointer to the backend instance. * @param[in] msg Pointer to message with log entry. */ static inline void log_backend_put(const struct log_backend *const backend, struct log_msg *msg) { __ASSERT_NO_MSG(backend != NULL); __ASSERT_NO_MSG(msg != NULL); backend->api->put(backend, msg); } /** * @brief Synchronously process log message. * * @param[in] backend Pointer to the backend instance. * @param[in] src_level Message details. * @param[in] timestamp Timestamp. * @param[in] fmt Log string. * @param[in] ap Log string arguments. */ static inline void log_backend_put_sync_string( const struct log_backend *const backend, struct log_msg_ids src_level, u32_t timestamp, const char *fmt, va_list ap) { __ASSERT_NO_MSG(backend != NULL); if (backend->api->put_sync_string) { backend->api->put_sync_string(backend, src_level, timestamp, fmt, ap); } } /** * @brief Synchronously process log hexdump_message. * * @param[in] backend Pointer to the backend instance. * @param[in] src_level Message details. * @param[in] timestamp Timestamp. * @param[in] metadata Raw string associated with the data. * @param[in] data Data. * @param[in] len Data length. */ static inline void log_backend_put_sync_hexdump( const struct log_backend *const backend, struct log_msg_ids src_level, u32_t timestamp, const char *metadata, const u8_t *data, u32_t len) { __ASSERT_NO_MSG(backend != NULL); if (backend->api->put_sync_hexdump) { backend->api->put_sync_hexdump(backend, src_level, timestamp, metadata, data, len); } } /** * @brief Notify backend about dropped log messages. * * Function is optional. * * @param[in] backend Pointer to the backend instance. * @param[in] cnt Number of dropped logs since last notification. */ static inline void log_backend_dropped(const struct log_backend *const backend, u32_t cnt) { __ASSERT_NO_MSG(backend != NULL); if (backend->api->dropped != NULL) { backend->api->dropped(backend, cnt); } } /** * @brief Reconfigure backend to panic mode. * * @param[in] backend Pointer to the backend instance. */ static inline void log_backend_panic(const struct log_backend *const backend) { __ASSERT_NO_MSG(backend != NULL); backend->api->panic(backend); } /** * @brief Set backend id. * * @note It is used internally by the logger. * * @param backend Pointer to the backend instance. * @param id ID. */ static inline void log_backend_id_set(const struct log_backend *const backend, u8_t id) { __ASSERT_NO_MSG(backend != NULL); backend->cb->id = id; } /** * @brief Get backend id. * * @note It is used internally by the logger. * * @param[in] backend Pointer to the backend instance. * @return Id. */ static inline u8_t log_backend_id_get(const struct log_backend *const backend) { __ASSERT_NO_MSG(backend != NULL); return backend->cb->id; } /** * @brief Get backend. * * @param[in] idx Pointer to the backend instance. * * @return Pointer to the backend instance. */ static inline const struct log_backend *log_backend_get(u32_t idx) { return &__log_backends_start[idx]; } /** * @brief Get number of backends. * * @return Number of backends. */ static inline int log_backend_count_get(void) { return __log_backends_end - __log_backends_start; } /** * @brief Activate backend. * * @param[in] backend Pointer to the backend instance. * @param[in] ctx User context. */ static inline void log_backend_activate(const struct log_backend *const backend, void *ctx) { __ASSERT_NO_MSG(backend != NULL); backend->cb->ctx = ctx; backend->cb->active = true; } /** * @brief Deactivate backend. * * @param[in] backend Pointer to the backend instance. */ static inline void log_backend_deactivate( const struct log_backend *const backend) { __ASSERT_NO_MSG(backend != NULL); backend->cb->active = false; } /** * @brief Check state of the backend. * * @param[in] backend Pointer to the backend instance. * * @return True if backend is active, false otherwise. */ static inline bool log_backend_is_active( const struct log_backend *const backend) { __ASSERT_NO_MSG(backend != NULL); return backend->cb->active; } /** * @} */ #ifdef __cplusplus } #endif #endif /* ZEPHYR_INCLUDE_LOGGING_LOG_BACKEND_H_ */ |