HPM SDK
HPMicro Software Development Kit
hpm_log.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2025 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef HPM_LOG_H
9 #define HPM_LOG_H
10 
11 #include <stdint.h>
12 #include <string.h>
13 #include <stdio.h>
14 
22 #ifndef HPM_LOG_CONFIG_MAX_ENGINE_NUM
23 #define HPM_LOG_CONFIG_MAX_ENGINE_NUM 1
24 #endif
25 
33 #ifndef HPM_LOG_CONFIG_MAX_MESSAGE_LEN
34 #define HPM_LOG_CONFIG_MAX_MESSAGE_LEN 256
35 #endif
36 
45 #ifndef HPM_LOG_CONFIG_ENABLE_TIMESTAMP
46 #define HPM_LOG_CONFIG_ENABLE_TIMESTAMP 1
47 #endif
48 
57 #ifndef HPM_LOG_CONFIG_ENABLE_FUNC_NAME
58 #define HPM_LOG_CONFIG_ENABLE_FUNC_NAME 1
59 #endif
60 
69 #ifndef HPM_LOG_CONFIG_LEVEL
70 #define HPM_LOG_CONFIG_LEVEL HPM_LOG_LEVEL_DEBUG
71 #endif
72 
79 #define HPM_LOG_FAST_FUNC
80 
87 #define HPM_LOG_LEVEL_DEBUG 4
88 
95 #define HPM_LOG_LEVEL_INFO 3
96 
103 #define HPM_LOG_LEVEL_WARN 2
104 
111 #define HPM_LOG_LEVEL_ERR 1
112 
117 typedef struct hpm_log_engine hpm_log_engine_t;
118 
125 typedef struct hpm_log_engine_config {
133  void (*transfer_start)(const char *buf, uint32_t len);
141  void (*transfer_block)(const char *buf, uint32_t len);
159  char *fifo_buf;
165  uint32_t fifo_buf_size;
167 
174 typedef struct hpm_log_config {
181  uint64_t (*get_timestamp)(void);
188  long (*critical_enter)(void);
195  void (*critical_exit)(long val);
197 
198 #ifdef __cplusplus
199 extern "C" {
200 #endif
201 
210 
219 
227 
235 
244 
252 
259 void hpm_log_init(hpm_log_config_t *cfg);
260 
268 void hpm_log_printf(const char *fmt, ...);
269 
281 void hpm_log_write(int level, const char *func, const int line, const char *fmt, ...);
282 
283 #ifdef __cplusplus
284 }
285 #endif
286 
295 #define HPM_LOG_PRINTF(fmt, ...) hpm_log_printf(fmt, ##__VA_ARGS__);
296 
305 #if HPM_LOG_CONFIG_LEVEL >= HPM_LOG_LEVEL_DEBUG
306 #define HPM_LOG_DEBUG(fmt, ...) ({\
307  hpm_log_write(HPM_LOG_LEVEL_DEBUG, __func__, __LINE__, fmt, ##__VA_ARGS__);\
308 })
309 #else
310 #define HPM_LOG_DEBUG(fmt, ...)
311 #endif
312 
321 #if HPM_LOG_CONFIG_LEVEL >= HPM_LOG_LEVEL_INFO
322 #define HPM_LOG_INFO(fmt, ...) ({\
323  hpm_log_write(HPM_LOG_LEVEL_INFO, __func__, __LINE__, fmt, ##__VA_ARGS__);\
324 })
325 #else
326 #define HPM_LOG_INFO(fmt, ...)
327 #endif
328 
337 #if HPM_LOG_CONFIG_LEVEL >= HPM_LOG_LEVEL_WARN
338 #define HPM_LOG_WARN(fmt, ...) ({\
339  hpm_log_write(HPM_LOG_LEVEL_WARN, __func__, __LINE__, fmt, ##__VA_ARGS__);\
340 })
341 #else
342 #define HPM_LOG_WARN(fmt, ...)
343 #endif
344 
353 #if HPM_LOG_CONFIG_LEVEL >= HPM_LOG_LEVEL_ERR
354 #define HPM_LOG_ERR(fmt, ...) ({\
355  hpm_log_write(HPM_LOG_LEVEL_ERR, __func__, __LINE__, fmt, ##__VA_ARGS__);\
356 })
357 #else
358 #define HPM_LOG_ERR(fmt, ...)
359 #endif
360 
361 #endif
void hpm_log_engine_dump(hpm_log_engine_t *engine)
Dump all log messages in the FIFO buffer.
Definition: hpm_log.c:234
void hpm_log_init(hpm_log_config_t *cfg)
Initialize the log module.
Definition: hpm_log.c:371
hpm_log_engine_t * hpm_log_engine_create(hpm_log_engine_config_t *cfg)
Create a log engine.
Definition: hpm_log.c:104
void hpm_log_printf(const char *fmt,...)
Print a formatted log message.
Definition: hpm_log.c:319
int hpm_log_engine_destroy(hpm_log_engine_t *engine)
Destroy a log engine.
Definition: hpm_log.c:140
struct hpm_log_config hpm_log_config_t
#define HPM_LOG_FAST_FUNC
Indicate fast functions.
Definition: hpm_log.h:79
void hpm_log_engine_enable(hpm_log_engine_t *engine)
Enable a log engine.
Definition: hpm_log.c:157
struct hpm_log_engine_config hpm_log_engine_config_t
void hpm_log_write(int level, const char *func, const int line, const char *fmt,...)
Write a log message with a specified level.
Definition: hpm_log.c:271
void hpm_log_engine_disable(hpm_log_engine_t *engine)
Disable a log engine.
Definition: hpm_log.c:172
void hpm_log_engine_transfer_finish(hpm_log_engine_t *engine)
Indicate that the transfer of a log message is finished.
Definition: hpm_log.c:211
Configuration structure for the log module.
Definition: hpm_log.h:174
void(* critical_exit)(long val)
Function pointer to exit a critical section.
Definition: hpm_log.h:195
long(* critical_enter)(void)
Function pointer to enter a critical section.
Definition: hpm_log.h:188
uint64_t(* get_timestamp)(void)
Function pointer to get the current timestamp.
Definition: hpm_log.h:181
Configuration structure for the log engine.
Definition: hpm_log.h:125
uint32_t max_transfer_size
Maximum size of the transfer buffer.
Definition: hpm_log.h:153
void(* transfer_block)(const char *buf, uint32_t len)
Function pointer to transfer a block of data.
Definition: hpm_log.h:141
void(* transfer_start)(const char *buf, uint32_t len)
Function pointer to start data transfer.
Definition: hpm_log.h:133
char * fifo_buf
Pointer to the FIFO buffer.
Definition: hpm_log.h:159
uint32_t fifo_buf_size
Size of the FIFO buffer.
Definition: hpm_log.h:165
char * transfer_buf
Pointer to the transfer buffer.
Definition: hpm_log.h:147
Definition: hpm_log.c:28