This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
zhuzhenjun-libosfp/src/libosfp_log.c

54 lines
1.7 KiB
C
Raw Normal View History

2023-09-22 15:59:40 +08:00
#include "libosfp_common.h"
2023-09-16 10:43:06 +08:00
#include "libosfp_log.h"
/* The maximum length of the log message */
#define LIBOSFP_MAX_LOG_MSG_LEN 2048
unsigned int libosfp_log_level = LIBOSFP_LOG_LEVEL_INFO;
void libosfp_log_message(unsigned int x, const char *file, const int line, const char *func, const char *msg)
{
char buffer[LIBOSFP_MAX_LOG_MSG_LEN] = "";
char log_time_buf[128];
time_t now;
struct tm tm_now;
time(&now);
localtime_r(&now, &tm_now);
strftime(log_time_buf, sizeof(log_time_buf), "%Y-%m-%d %T", &tm_now);
switch (x) {
case LIBOSFP_LOG_LEVEL_DEBUG:
2023-09-22 15:59:40 +08:00
snprintf(buffer, sizeof(buffer), "[%s][DEBUG][%s:%d %s] %s\n", log_time_buf, file, line, func, msg);
2023-09-16 10:43:06 +08:00
break;
case LIBOSFP_LOG_LEVEL_INFO:
2023-09-22 15:59:40 +08:00
snprintf(buffer, sizeof(buffer), "[%s][INFO][%s:%d %s] %s\n", log_time_buf, file, line, func, msg);
2023-09-16 10:43:06 +08:00
break;
case LIBOSFP_LOG_LEVEL_WARNING:
2023-09-22 15:59:40 +08:00
snprintf(buffer, sizeof(buffer), "[%s][WARN][%s:%d %s] %s\n", log_time_buf, file, line, func, msg);
2023-09-16 10:43:06 +08:00
break;
case LIBOSFP_LOG_LEVEL_ERROR:
2023-09-22 15:59:40 +08:00
snprintf(buffer, sizeof(buffer), "[%s][ERROR][%s:%d %s] %s\n", log_time_buf, file, line, func, msg);
2023-09-16 10:43:06 +08:00
break;
}
}
2023-09-22 15:59:40 +08:00
void libosfp_log(unsigned int x, const char *file, const int line, const char *func, const char *fmt, ...)
2023-09-16 10:43:06 +08:00
{
if (libosfp_log_level >= x ) {
char msg[LIBOSFP_MAX_LOG_MSG_LEN];
va_list ap;
va_start(ap, fmt);
vsnprintf(msg, sizeof(msg), fmt, ap);
va_end(ap);
libosfp_log_message(x, file, line, func, msg);
}
}
void libosfp_log_level_set(libosfp_log_level_t level)
{
libosfp_log_level = level;
}