diff --git a/.gitignore b/.gitignore index bdea02f..9488149 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .vscode/** bin build +CMakeUserPresets.json diff --git a/CMakeLists.txt b/CMakeLists.txt index 904cd5d..1c53dbd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required( VERSION 3.28) project(Logging) + aux_source_directory(${CMAKE_SOURCE_DIR}/src SRC) set(LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib) @@ -17,3 +18,6 @@ else() endif() +add_executable(test_main ${CMAKE_SOURCE_DIR}/test_package/main.c ${SRC}) + + diff --git a/conanfile.py b/conanfile.py index 2700417..aff264f 100644 --- a/conanfile.py +++ b/conanfile.py @@ -49,7 +49,7 @@ class loggingRecipe(ConanFile): def build(self): cmake = CMake(self) cmake.configure() - cmake.build() + cmake.build(target="Logging") def package(self): copy(self, "LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses")) diff --git a/include/logging.h b/include/logging.h index f134595..8cb29e5 100644 --- a/include/logging.h +++ b/include/logging.h @@ -39,6 +39,10 @@ typedef struct Logger void (*warning)(const char* format, ...); void (*info)(const char* format, ...); void (*debug)(const char* format, ...); + + void (*addHandler)(log_Handler* handler); + // void (*addFormat)(const char* format); + // void (*addFilter)(const char* filter); } Logger; @@ -46,15 +50,13 @@ typedef struct Logger typedef struct Logging { Logger* (*getLogger)(const char* name, log_level level); log_status (*setLevel)(Logger* logger, log_level level); - // void (*addFormat)(const char* format); - // void (*addFilter)(const char* filter); - void (*addHandler)(log_Handler* handler); } Logging; -Logging* createLogging(); +Logging* createLogging(); //创建日志操作器 log_status destroyLogging(Logging* logging); +Logger* getCurrentLogger(void); log_Handler* fileHandler(const char* name); //文件处理器 diff --git a/src/logging-handler.c b/src/logging-handler.c index 0bd7018..711d3ae 100644 --- a/src/logging-handler.c +++ b/src/logging-handler.c @@ -1,4 +1,7 @@ - +/******************************************** +* @Date: 2024 08 12 +* @Description: 日志处理器 +********************************************/ #include "logging.h" diff --git a/src/logging.c b/src/logging.c index f507b10..672bb5c 100644 --- a/src/logging.c +++ b/src/logging.c @@ -1,3 +1,8 @@ +/******************************************** +* @Date: 2024 08 12 +* @Description: 日志模块 +********************************************/ + #include "logging.h" Logger* G_LOGGER = NULL; @@ -23,7 +28,9 @@ static void getTimeStr(char * timeStr){ strcpy(timeStr, _timeStr); } - +/** +* @description : 添加日志处理器 +*/ static void addHandler(log_Handler* handler){ if (G_LOGGER == NULL){ return; @@ -31,6 +38,9 @@ static void addHandler(log_Handler* handler){ G_LOGGER->handler = handler; } +/** +* @description : 内置日志记录函数 +*/ static void _builtin_log(char* level, const char *color, const char* format, ...){ if (G_LOGGER == NULL){ return; @@ -121,6 +131,8 @@ static Logger* getLogger(const char* name, log_level level){ logger->info = info; logger->debug = debug; + logger->addHandler = addHandler; + logger->level = level; logger->handler = NULL; logger->name = name; @@ -136,11 +148,12 @@ static Logger* getLogger(const char* name, log_level level){ Logging* createLogging(){ Logging* logging = (Logging*)malloc(sizeof(Logging)); logging->getLogger = getLogger; - logging->addHandler = addHandler; return logging; } - +/** +* @description :销毁日志对象 +*/ log_status destroyLogging(Logging* logging){ if (logging == NULL){ return L_ERROR; @@ -156,3 +169,15 @@ log_status destroyLogging(Logging* logging){ free(logging); return L_OK; } + + +/** +* @description :获取当前日志操作对象 +* @return 当前唯一的日志操作对象 +*/ +Logger* getCurrentLogger(void){ + if (G_LOGGER == NULL){ + return NULL; + } + return G_LOGGER; +} \ No newline at end of file diff --git a/main.c b/test_package/main.c similarity index 68% rename from main.c rename to test_package/main.c index 3e21df2..97dd020 100644 --- a/main.c +++ b/test_package/main.c @@ -2,17 +2,20 @@ int main() { - // Your code goes here Logging *log = createLogging(); Logger *logger = log->getLogger("testLogger",LOG_DEBUG); - // log->addHandler(consoleHandler("test")); - log->addHandler(fileHandler("test")); + // logger->addHandler(fileHandler("test")); + logger->addHandler(consoleHandler("test")); + logger->info("This is an info message"); logger->error("你好,这是一个错误消息%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"); + Logger *logger1 = getCurrentLogger(); + logger1->info("This is an info message from logger1"); + destroyLogging(log); return 0; } \ No newline at end of file