Merge branch 'master-develop' of https://gitee.com/shenzhen-jiuyilian/ipc into master-develop

This commit is contained in:
xiaojiazhu 2023-08-13 01:21:32 -07:00
commit 5b3152355e
13 changed files with 280 additions and 2 deletions

3
.gitignore vendored
View File

@ -2,4 +2,5 @@
cmake-shell-linux/
external/gtest/googletest-release-1.11.0/
external/libconfig/libconfig-1.7.3/
out/
out/
build/

0
external/gtest/build_gtest.sh vendored Normal file → Executable file
View File

View File

@ -2,5 +2,6 @@
# cmake_minimum_required(VERSION 2.8.0)
#Compile gtest for test code.
add_subdirectory(ReturnCode)
add_subdirectory(LogC)

View File

@ -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()

View File

@ -0,0 +1,9 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,27 @@
#include "iLog.h"
#include "logUb.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
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);
}
}

View File

@ -1,4 +1,5 @@
# cmake_minimum_required(VERSION 2.8.0)
add_subdirectory(ReturnCode)
add_subdirectory(Log)
add_subdirectory(Log)
add_subdirectory(LogC)

18
utils/LogC/CMakeLists.txt Normal file
View File

@ -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})

View File

@ -0,0 +1,42 @@
#include "iLog.h"
#include <stddef.h>
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;
}
}

55
utils/LogC/include/iLog.h Normal file
View File

@ -0,0 +1,55 @@
#ifndef _IPC_I_LOG_H_
#define _IPC_I_LOG_H_
#include <stdarg.h>
#include <stdio.h>
#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_

70
utils/LogC/src/logUb.c Normal file
View File

@ -0,0 +1,70 @@
#include "logUb.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
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;
}

23
utils/LogC/src/logUb.h Normal file
View File

@ -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_

0
utils/ReturnCodeC Normal file
View File