From 019007436794a7b4614d8cb72158dcd932ba5788 Mon Sep 17 00:00:00 2001 From: ljx Date: Sun, 13 Aug 2023 01:28:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0LogC=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- external/gtest/build_gtest.sh | 0 test/utils/CMakeLists.txt | 1 + test/utils/LogC/CMakeLists.txt | 31 ++++++++++++++ test/utils/LogC/mainTest.cpp | 9 ++++ test/utils/LogC/src/LogCTest.cpp | 27 ++++++++++++ utils/CMakeLists.txt | 3 +- utils/LogC/CMakeLists.txt | 18 ++++++++ utils/LogC/abstract/iLog.c | 42 +++++++++++++++++++ utils/LogC/include/iLog.h | 55 +++++++++++++++++++++++++ utils/LogC/src/logUb.c | 70 ++++++++++++++++++++++++++++++++ utils/LogC/src/logUb.h | 23 +++++++++++ utils/ReturnCodeC | 0 13 files changed, 280 insertions(+), 2 deletions(-) mode change 100644 => 100755 external/gtest/build_gtest.sh create mode 100644 test/utils/LogC/CMakeLists.txt create mode 100644 test/utils/LogC/mainTest.cpp create mode 100644 test/utils/LogC/src/LogCTest.cpp create mode 100644 utils/LogC/CMakeLists.txt create mode 100644 utils/LogC/abstract/iLog.c create mode 100644 utils/LogC/include/iLog.h create mode 100644 utils/LogC/src/logUb.c create mode 100644 utils/LogC/src/logUb.h create mode 100644 utils/ReturnCodeC diff --git a/.gitignore b/.gitignore index d0c7251f..e75dfbc6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ cmake-shell-linux/ external/gtest/googletest-release-1.11.0/ external/libconfig/libconfig-1.7.3/ -out/ \ No newline at end of file +out/ +build/ \ No newline at end of file diff --git a/external/gtest/build_gtest.sh b/external/gtest/build_gtest.sh old mode 100644 new mode 100755 diff --git a/test/utils/CMakeLists.txt b/test/utils/CMakeLists.txt index a93dc76e..495f1913 100644 --- a/test/utils/CMakeLists.txt +++ b/test/utils/CMakeLists.txt @@ -2,5 +2,6 @@ # cmake_minimum_required(VERSION 2.8.0) #Compile gtest for test code. add_subdirectory(ReturnCode) +add_subdirectory(LogC) diff --git a/test/utils/LogC/CMakeLists.txt b/test/utils/LogC/CMakeLists.txt new file mode 100644 index 00000000..9387b945 --- /dev/null +++ b/test/utils/LogC/CMakeLists.txt @@ -0,0 +1,31 @@ +# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake) +set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin) + +include_directories( + ${UTILS_SOURCE_PATH}/LogC/include + ${UTILS_SOURCE_PATH}/LogC/src + ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include + ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include +) + +link_directories( + ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/lib + ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/lib + ${LIBS_OUTPUT_PATH} +) + +set(CMAKE_AUTOMOC ON) +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(CMAKE_AUTOMOC ON) +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +aux_source_directory(./ SRC_FILES) +aux_source_directory(./src SRC_FILES) + +set(TARGET_NAME LogCTest) +add_executable(${TARGET_NAME} ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} gtest gmock pthread LogCAbstract LogCUb) +if(${COVERAGE_ON} MATCHES "true") + target_link_libraries(${TARGET_NAME} gcov) +endif() \ No newline at end of file diff --git a/test/utils/LogC/mainTest.cpp b/test/utils/LogC/mainTest.cpp new file mode 100644 index 00000000..fb1d91b4 --- /dev/null +++ b/test/utils/LogC/mainTest.cpp @@ -0,0 +1,9 @@ +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/test/utils/LogC/src/LogCTest.cpp b/test/utils/LogC/src/LogCTest.cpp new file mode 100644 index 00000000..6e08b3e5 --- /dev/null +++ b/test/utils/LogC/src/LogCTest.cpp @@ -0,0 +1,27 @@ +#include "iLog.h" +#include "logUb.h" + +#include +#include +namespace LogCTest +{ + // ../out/test/bin/LogCTest --gtest_filter=LogCTest.Demo + TEST(LogCTest, Demo) + { + ILog *log = new_Log_abs(); + setILog(log); + LogInfo("hello world."); + LogErr("create ... failed."); + LogDebug("a = %d b = %s", 124, "apple"); + del_Log_abs(log); + } + TEST(LogCTest, Demo2) + { + LogUbuntu *lu = new_Log_Ubuntu(); + setILog((iLog*)lu); + LogInfo("hello world."); + LogErr("create ... failed."); + LogDebug("a = %d b = %s", 124, "apple"); + del_Log_Ubuntu(lu); + } +} \ No newline at end of file diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index e8914d0e..931451d0 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -1,4 +1,5 @@ # cmake_minimum_required(VERSION 2.8.0) add_subdirectory(ReturnCode) -add_subdirectory(Log) \ No newline at end of file +add_subdirectory(Log) +add_subdirectory(LogC) \ No newline at end of file diff --git a/utils/LogC/CMakeLists.txt b/utils/LogC/CMakeLists.txt new file mode 100644 index 00000000..e4458542 --- /dev/null +++ b/utils/LogC/CMakeLists.txt @@ -0,0 +1,18 @@ +include(${CMAKE_SOURCE_DIR}/build/global_config.cmake) +set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH}) +set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH}) + +include_directories( + ./src + ./include +) + +set(CMAKE_AUTOMOC ON) +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +aux_source_directory(./src SRC_FILES) +aux_source_directory(./abstract ABSTRACT_FILES) + +# set(TARGET_NAME LogC) +add_library(LogCAbstract STATIC ${ABSTRACT_FILES}) +add_library(LogCUb STATIC ${SRC_FILES}) diff --git a/utils/LogC/abstract/iLog.c b/utils/LogC/abstract/iLog.c new file mode 100644 index 00000000..d5b3996d --- /dev/null +++ b/utils/LogC/abstract/iLog.c @@ -0,0 +1,42 @@ +#include "iLog.h" + +#include +static ILog *instance = NULL; + +ILog *getInstance(void) +{ + return instance; +} + +void setILog(ILog *impl) +{ + instance = impl; +} + +static void log_init_abs(ILog *impl) +{} + +static void log_unInit_abs(ILog *impl) +{} + +static void ub_log_fmt_abs(ILog *log, const LogLeveL level, const char *fmt, ...) +{} + +void init_Log_abs(ILog *log) +{ + log->log_fmt = ub_log_fmt_abs; + log->init = log_init_abs; + log->unInit = log_unInit_abs; +} +ILog *new_Log_abs(void) +{ + ILog *impl = malloc(sizeof(ILog)); + init_Log_abs(impl); +} +void del_Log_abs(ILog *impl) +{ + if (impl) { + free(impl); + impl = NULL; + } +} \ No newline at end of file diff --git a/utils/LogC/include/iLog.h b/utils/LogC/include/iLog.h new file mode 100644 index 00000000..0f3ea88c --- /dev/null +++ b/utils/LogC/include/iLog.h @@ -0,0 +1,55 @@ +#ifndef _IPC_I_LOG_H_ +#define _IPC_I_LOG_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define LogInfo(...) getInstance()->log_fmt(getInstance(), LOG_TYPE_INFO, __VA_ARGS__) +#define LogDebug(...) getInstance()->log_fmt(getInstance(), LOG_TYPE_DEBUG, __VA_ARGS__) +#define LogErr(...) getInstance()->log_fmt(getInstance(), LOG_TYPE_ERROR, __VA_ARGS__) +#define LogWarr(...) getInstance()->log_fmt(getInstance(), LOG_TYPE_WARR, __VA_ARGS__) + +typedef enum LogLeveL { + LOG_TYPE_INFO = 0, + LOG_TYPE_DEBUG, + LOG_TYPE_ERROR, + LOG_TYPE_WARR, +}LogLeveL; + +typedef enum LogMode { + LOG_MODE_TERMINAL = 0, + LOG_MODE_LOGFILE, + LOG_MODE_BOTH, +}LogMode; + +typedef struct iLog ILog; +struct iLog { + void (*init)(ILog *); + void (*unInit)(ILog *); + void (*log_fmt)(ILog *, const LogLeveL level, const char *fmt, ...); +}; +ILog* getInstance(void); +void setILog(ILog *impl); + +ILog *new_Log_abs(void); +void del_Log_abs(ILog *impl); + +static void f_init(ILog *impl) +{ + getInstance()->init(getInstance()); +} + +static void f_unInit(ILog *impl) +{ + getInstance()->unInit(getInstance()); +} + +#ifdef __cplusplus +} +#endif + +#endif //_IPC_I_LOG_H_ \ No newline at end of file diff --git a/utils/LogC/src/logUb.c b/utils/LogC/src/logUb.c new file mode 100644 index 00000000..70a58edd --- /dev/null +++ b/utils/LogC/src/logUb.c @@ -0,0 +1,70 @@ +#include "logUb.h" + +#include +#include +#include +#include + +static const char *LogLevelStr[] = { + "INFO", + "Debug", + "ERROR", + "WARR" +}; + +LogUbuntu *new_Log_Ubuntu(void) +{ + LogUbuntu *impl = (LogUbuntu *)malloc(sizeof(LogUbuntu)); + if (impl) { + printf("new_log_ubuntu succeed.\n"); + init_Log_Ubuntu(impl); + return impl; + } + printf("new_log_ubuntu failed.\n"); + return NULL; +} + +void del_Log_Ubuntu(LogUbuntu *impl) +{ + if (impl != NULL) { + free(impl); + setILog(NULL); + impl = NULL; + } +} + +#define LOG_BUFF_SIZE (256) +#define LOG_TIME_BUF_SIZE (32) +static void ub_log_fmt(ILog *log, const LogLeveL level, const char *fmt, ...) +{ + time_t rawtime; + time(&rawtime); + char ubt_time_buf[LOG_TIME_BUF_SIZE] = {0}; + struct tm *info = localtime(&rawtime); + strftime(ubt_time_buf, LOG_TIME_BUF_SIZE, "%y-%m-%d %H:%M:%S", info); + + va_list args; + size_t length; + static char ubt_log_buf[LOG_BUFF_SIZE]; + va_start(args, fmt); + length = vsnprintf(ubt_log_buf, sizeof(ubt_log_buf) - 1, fmt, args); + if (length > LOG_BUFF_SIZE - 1) + length = LOG_BUFF_SIZE - 1; + printf("[%s][%s] %s\n", ubt_time_buf, LogLevelStr[level], ubt_log_buf); + va_end(args); +} + +void ub_log_init(ILog *impl) +{ +} + +void ub_log_unInit(ILog *impl) +{ +} + +void init_Log_Ubuntu(LogUbuntu *lu) +{ + ((ILog*)lu)->log_fmt = ub_log_fmt; + ((ILog*)lu)->init = ub_log_init; + ((ILog*)lu)->unInit = ub_log_unInit; +} \ No newline at end of file diff --git a/utils/LogC/src/logUb.h b/utils/LogC/src/logUb.h new file mode 100644 index 00000000..df9d6890 --- /dev/null +++ b/utils/LogC/src/logUb.h @@ -0,0 +1,23 @@ +#ifndef _IPC_LOG_UBUNTU_H_ +#define _IPC_LOG_UBUNTU_H_ + +#include "iLog.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct LogUbuntu LogUbuntu; +struct LogUbuntu { + ILog base; +}; + +void init_Log_Ubuntu(LogUbuntu *lu); +LogUbuntu *new_Log_Ubuntu(void); +void del_Log_Ubuntu(LogUbuntu *impl); + +#ifdef __cplusplus +} +#endif + +#endif //_IPC_LOG_UBUNTU_H_ \ No newline at end of file diff --git a/utils/ReturnCodeC b/utils/ReturnCodeC new file mode 100644 index 00000000..e69de29b