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/osfp_log.c

58 lines
1.8 KiB
C
Raw Normal View History

2023-09-27 11:45:26 +08:00
#include "osfp_common.h"
#include "osfp_log.h"
2023-09-16 10:43:06 +08:00
/* The maximum length of the log message */
2023-09-27 11:45:26 +08:00
#define OSFP_MAX_LOG_MSG_LEN 2048
2023-09-16 10:43:06 +08:00
2023-09-27 15:43:32 +08:00
unsigned int g_osfp_log_level = OSFP_LOG_LEVEL_WARNING;
2023-09-16 10:43:06 +08:00
2023-09-27 11:45:26 +08:00
void osfp_log_message(unsigned int x, const char *file, const int line, const char *func, const char *msg)
2023-09-16 10:43:06 +08:00
{
2023-09-27 11:45:26 +08:00
char buffer[OSFP_MAX_LOG_MSG_LEN] = "";
2023-09-16 10:43:06 +08:00
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) {
2023-09-27 11:45:26 +08:00
case OSFP_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;
2023-09-27 11:45:26 +08:00
case OSFP_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;
2023-09-27 11:45:26 +08:00
case OSFP_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;
2023-09-27 11:45:26 +08:00
case OSFP_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-27 15:43:32 +08:00
if (fprintf(stdout, "%s\n", buffer) < 0) {
printf("Error writing to stream using fprintf\n");
}
fflush(stdout);
2023-09-16 10:43:06 +08:00
}
2023-09-27 11:45:26 +08:00
void osfp_log(unsigned int x, const char *file, const int line, const char *func, const char *fmt, ...)
2023-09-16 10:43:06 +08:00
{
2023-09-27 15:43:32 +08:00
if (g_osfp_log_level <= x ) {
2023-09-27 11:45:26 +08:00
char msg[OSFP_MAX_LOG_MSG_LEN];
2023-09-16 10:43:06 +08:00
va_list ap;
va_start(ap, fmt);
vsnprintf(msg, sizeof(msg), fmt, ap);
va_end(ap);
2023-09-27 11:45:26 +08:00
osfp_log_message(x, file, line, func, msg);
2023-09-16 10:43:06 +08:00
}
}
2023-09-27 15:43:32 +08:00
void osfp_log_level_set(enum osfp_log_level level)
2023-09-16 10:43:06 +08:00
{
2023-09-27 15:43:32 +08:00
g_osfp_log_level = level;
2023-09-16 10:43:06 +08:00
}