diff --git a/include/logging.h b/include/logging.h index 7f2d8c8..387b071 100644 --- a/include/logging.h +++ b/include/logging.h @@ -8,7 +8,7 @@ #include "logging/logging-handler.h" #include "logging/logging-interceptor.h" -// 日志操作器 + typedef struct Logger { log_level level; log_Handler *handler; @@ -24,7 +24,7 @@ typedef struct Logger { void (*addInterceptor)(log_Interceptor *Interceptor); } Logger; -// 日志类对象 + typedef struct Logging { Logger *(*getLogger)(const char *name, log_level level); log_status (*setLevel)(Logger *logger, log_level level); @@ -32,6 +32,6 @@ typedef struct Logging { log_status (*destroyLogging)(struct Logging *logging); } Logging; -Logging *newLogging(); // 创建日志类对象 +Logging *newLogging(); #endif // __LOGGING_H__ \ No newline at end of file diff --git a/include/logging/logging-core.h b/include/logging/logging-core.h index 3f8008c..25e8f6a 100644 --- a/include/logging/logging-core.h +++ b/include/logging/logging-core.h @@ -14,5 +14,4 @@ typedef enum { L_OK, } log_status; - #endif // __LOGGING_CORE_H__ diff --git a/include/logging/logging-handler.h b/include/logging/logging-handler.h index 06c1dd7..9254266 100644 --- a/include/logging/logging-handler.h +++ b/include/logging/logging-handler.h @@ -4,14 +4,13 @@ #include typedef struct log_Handler { - void* stream; - bool apply_color; - void (*_free)(struct log_Handler* handler);//释放资源 - void (*output)(struct log_Handler* handler,const char* message); + void *stream; + bool apply_color; + void (*_free)(struct log_Handler *handler); + void (*output)(struct log_Handler *handler, const char *message); } log_Handler; +log_Handler *loggingFileHandler(const char *name); +log_Handler *loggingConsoleHandler(); -log_Handler* loggingFileHandler(const char* name); -log_Handler* loggingConsoleHandler(); - -#endif //__LOGGING_HANDLER_H__ \ No newline at end of file +#endif //__LOGGING_HANDLER_H__ \ No newline at end of file diff --git a/include/logging/logging-interceptor.h b/include/logging/logging-interceptor.h index a8f2fd6..a365027 100644 --- a/include/logging/logging-interceptor.h +++ b/include/logging/logging-interceptor.h @@ -5,10 +5,10 @@ #include "logging-handler.h" typedef struct log_Interceptor { - log_level level; // 拦截级别 - log_Handler *handler; // 拦截目标处理器 - bool (*_dispose)(char *level, const char *message, ...); // 拦截触发器 - void (*_free)(struct log_Interceptor *Interceptor); // 释放资源 + log_level level; + log_Handler *handler; + bool (*_dispose)(char *level, const char *message, ...); + void (*_free)(struct log_Interceptor *Interceptor); } log_Interceptor; diff --git a/src/handler/logging-handler-console.c b/src/handler/logging-handler-console.c index f8e9482..a71c99d 100644 --- a/src/handler/logging-handler-console.c +++ b/src/handler/logging-handler-console.c @@ -1,29 +1,13 @@ -/******************************************** - * @Date: 2024 09 18 - * @Description: 控制台日志处理器 - ********************************************/ #include "logging/logging-handler.h" #include #include -/** - * @brief 释放组件 - * @param handler 处理器 - */ static void __freeConsoleHandler(log_Handler *handler) { free(handler); } -/** - * @brief 输出组件 - * @param handler 处理器 - * @param message 消息 - */ static void outputConsoleHandler(log_Handler *handler, const char *message) { fputs(message, handler->stream); } -/** - * @brief :控制台日志处理器 - */ log_Handler *loggingConsoleHandler() { log_Handler *handler = (log_Handler *)malloc(sizeof(log_Handler)); diff --git a/src/handler/logging-handler-file.c b/src/handler/logging-handler-file.c index 58b1d51..b2bbc2a 100644 --- a/src/handler/logging-handler-file.c +++ b/src/handler/logging-handler-file.c @@ -1,33 +1,16 @@ -/******************************************** - * @Date: 2024 09 18 - * @Description: 文件日志处理器 - ********************************************/ #include "logging/logging-handler.h" #include #include -/** - * @brief 文件日志处理器释放组件 - */ static void __freeFileHandler(log_Handler *handler) { fclose(handler->stream); free(handler); } -/** - * @brief 文件日志处理器输出组件 - * @param handler 文件日志处理器 - * @param message 消息 - */ static void outputFileHandler(log_Handler *handler, const char *message) { fputs(message, handler->stream); } -/** - * @brief 文件日志处理器 - * @param name 文件名 - * @return log_Handler * - */ log_Handler *loggingFileHandler(const char *name) { char new_file_name[100]; sprintf(new_file_name, "%s.log", name); diff --git a/src/interceptor/logging-interceptor-substr.c b/src/interceptor/logging-interceptor-substr.c index 7610ca9..f21dc10 100644 --- a/src/interceptor/logging-interceptor-substr.c +++ b/src/interceptor/logging-interceptor-substr.c @@ -1,8 +1,3 @@ -/******************************************** - * @Date: 2024 08 12 - * @Description: 子串拦截器 - *********************************************/ - #include "logging/logging-interceptor.h" #include #include @@ -54,11 +49,6 @@ static bool kmp_search(char *substr, char *master) { return false; } -/** - * @description 处理 - * @param - * @return - */ static bool _disposeSubstring(char *level, const char *message, ...) { int count = 0; @@ -76,11 +66,8 @@ static bool _disposeSubstring(char *level, const char *message, ...) { return false; } -/** - * @description : 完成拦截器自我释放内存 - */ static void _freeSubstring(log_Interceptor *interceptor) { - if (G_keywords!=NULL) { + if (G_keywords != NULL) { int sum = 0; while (G_keywords[sum] != NULL) { free(G_keywords[sum]); @@ -94,13 +81,11 @@ static void _freeSubstring(log_Interceptor *interceptor) { if (interceptor->handler != NULL) { interceptor->handler->_free(interceptor->handler); } - - if(interceptor!=NULL)free(interceptor); + + if (interceptor != NULL) + free(interceptor); } -/** - * @description : 子字符串拦截器 - */ log_Interceptor *loggingSubStringInterceptor(char *keywords[], int count, log_level level, @@ -112,7 +97,7 @@ log_Interceptor *loggingSubStringInterceptor(char *keywords[], interceptor->level = level; interceptor->_free = _freeSubstring; - G_keywords = (char **)malloc((sizeof(G_keywords) * count) + 1); + G_keywords = (char **)malloc((sizeof(G_keywords) * (count + 1))); for (int i = 0; i < count; i++) { G_keywords[i] = (char *)malloc(strlen(keywords[i]) + 1); diff --git a/src/logging.c b/src/logging.c index 471c8a2..6b7c455 100644 --- a/src/logging.c +++ b/src/logging.c @@ -1,7 +1,3 @@ -/******************************************** - * @Date: 2024 08 12 - * @Description: 日志模块 - ********************************************/ #include "logging.h" #include "logging/logging-handler.h" #include @@ -9,7 +5,6 @@ #include #include - #define RED "\033[0;31m" #define RED_B "\033[0;41m" #define GREEN "\033[0;32m" @@ -18,7 +13,6 @@ #define RESET "\033[0m" #define CYAN "\033[0;36m" - #define LOG_BUFFER_SIZE 1024 Logger *G_LOGGER = NULL; @@ -31,9 +25,6 @@ static void getTimeStr(char *timeStr) { strcpy(timeStr, _timeStr); } -/** - * @description : 添加日志处理器 - */ static void addHandler(log_Handler *handler) { if (G_LOGGER == NULL) { return; @@ -48,11 +39,6 @@ static void addHandler(log_Handler *handler) { G_LOGGER->handler = handler; } -/** - * @description : 添加日志拦截器 - * @param - * @return - */ void addInterceptor(log_Interceptor *Interceptor) { if (G_LOGGER == NULL) { return; @@ -67,9 +53,6 @@ void addInterceptor(log_Interceptor *Interceptor) { G_LOGGER->interceptor = Interceptor; } -/** - * @description : 内置日志记录函数 - */ static void _builtin_log(char *level, const char *color, const char *message, ...) { if (G_LOGGER == NULL) { @@ -84,7 +67,6 @@ _builtin_log(char *level, const char *color, const char *message, ...) { log_Handler *handler = G_LOGGER->handler; - // 通过拦截器 if (G_LOGGER->interceptor != NULL) { if (G_LOGGER->interceptor->_dispose(level, message)) { if (G_LOGGER->interceptor->handler != NULL) { @@ -93,7 +75,6 @@ _builtin_log(char *level, const char *color, const char *message, ...) { } } - // 判断处理器是否应用颜色 if (handler->apply_color) sprintf(logStr, "%s: %s %s%s%s %s\n", @@ -110,7 +91,6 @@ _builtin_log(char *level, const char *color, const char *message, ...) { handler->output(handler, logStr); } -//*************************记录日志******************************* */ static void fatal(const char *message, ...) { if (G_LOGGER->level >= LOG_ERROR) { char logStr[LOG_BUFFER_SIZE]; @@ -165,13 +145,7 @@ static void debug(const char *message, ...) { _builtin_log("Debug", CYAN, logStr, args); } } -//*************************记录日志******************************* */ -/** - * @description :获取一个日志操作对象 - * @param - * @return - */ static Logger *getLogger(const char *name, log_level level) { if (G_LOGGER != NULL) { G_LOGGER->name = name; @@ -180,7 +154,7 @@ static Logger *getLogger(const char *name, log_level level) { } Logger *logger = (Logger *)malloc(sizeof(Logger)); - // 方法 + logger->fatal = fatal; logger->error = error; logger->warning = warning; @@ -190,7 +164,6 @@ static Logger *getLogger(const char *name, log_level level) { logger->addHandler = addHandler; logger->addInterceptor = addInterceptor; - // 属性 logger->level = level; logger->handler = loggingConsoleHandler(); logger->name = name; @@ -223,10 +196,6 @@ log_status destroyLogging(Logging *logging) { return L_OK; } -/** - * @description :获取当前日志操作对象 - * @return 当前唯一的日志操作对象 - */ Logger *getCurrentLogger(void) { if (G_LOGGER == NULL) { return NULL; @@ -234,10 +203,6 @@ Logger *getCurrentLogger(void) { return G_LOGGER; } -/** - * @description :创建一个日志对象 - * @return :Logging* 返回一个日志对象 - */ Logging *newLogging() { Logging *logging = (Logging *)malloc(sizeof(Logging)); logging->getLogger = getLogger; diff --git a/tests/test_interceptor.c b/tests/test_interceptor.c index 8ba1541..08889b3 100644 --- a/tests/test_interceptor.c +++ b/tests/test_interceptor.c @@ -6,15 +6,15 @@ int main() { Logger *logger = log->getLogger("testLogger", LOG_DEBUG); logger->info("This is an info message"); - logger->error("你好,这是一个错误消息%s", "123"); + logger->error("This is an error message%s", "123"); logger->fatal("This is an fatal message"); logger->debug("This is a debug message"); logger->warning("This is a warning message%s", "123"); - char *test1[] = {"123", "你好"}; // 要拦截的字符串 - // 添加拦截器,将拦截到的日志重定向到拦截器的专属处理器中 + char *test1[] = {"123", "tt"}; + log_Interceptor *tint = - loggingSubStringInterceptor(test1, 2, LOG_DEBUG, loggingFileHandler("被拦截")); + loggingSubStringInterceptor(test1, 2, LOG_DEBUG, loggingFileHandler("test_interceptor")); logger->addInterceptor(tint); @@ -23,7 +23,7 @@ int main() { printf("\n"); logger->info("This is an info message"); - logger->error("你好,这是一个错误消息%s", "123"); + logger->error("This is an error message%s", "123"); logger->fatal("This is an fatal message"); logger->debug("This is a debug message"); logger->warning("This is a warning message%s", "123"); diff --git a/tests/test_simple.c b/tests/test_simple.c index 71628ba..9abc796 100644 --- a/tests/test_simple.c +++ b/tests/test_simple.c @@ -5,7 +5,7 @@ int main() { Logger *logger = log->getLogger("testLogger", LOG_DEBUG); logger->info("This is an info message"); - logger->error("你好,这是一个错误消息%s", "123"); + logger->error("This is an error message%s", "123"); logger->fatal("This is an fatal message"); logger->debug("This is a debug message"); logger->warning("This is a warning message%s", "123");