采用多态方式封装LogC库
This commit is contained in:
parent
6d886d26eb
commit
29ef5292c0
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -3,4 +3,3 @@ cmake-shell-linux/
|
|||
external/gtest/googletest-release-1.11.0/
|
||||
external/libconfig/libconfig-1.7.3/
|
||||
out/
|
||||
build/
|
57
build/cmake_install.cmake
Normal file
57
build/cmake_install.cmake
Normal file
|
@ -0,0 +1,57 @@
|
|||
# Install script for directory: /home/ljx/work/ipc
|
||||
|
||||
# Set the install prefix
|
||||
if(NOT DEFINED CMAKE_INSTALL_PREFIX)
|
||||
set(CMAKE_INSTALL_PREFIX "/usr/local")
|
||||
endif()
|
||||
string(REGEX REPLACE "/$" "" CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
|
||||
|
||||
# Set the install configuration name.
|
||||
if(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)
|
||||
if(BUILD_TYPE)
|
||||
string(REGEX REPLACE "^[^A-Za-z0-9_]+" ""
|
||||
CMAKE_INSTALL_CONFIG_NAME "${BUILD_TYPE}")
|
||||
else()
|
||||
set(CMAKE_INSTALL_CONFIG_NAME "")
|
||||
endif()
|
||||
message(STATUS "Install configuration: \"${CMAKE_INSTALL_CONFIG_NAME}\"")
|
||||
endif()
|
||||
|
||||
# Set the component getting installed.
|
||||
if(NOT CMAKE_INSTALL_COMPONENT)
|
||||
if(COMPONENT)
|
||||
message(STATUS "Install component: \"${COMPONENT}\"")
|
||||
set(CMAKE_INSTALL_COMPONENT "${COMPONENT}")
|
||||
else()
|
||||
set(CMAKE_INSTALL_COMPONENT)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Install shared libraries without execute permission?
|
||||
if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)
|
||||
set(CMAKE_INSTALL_SO_NO_EXE "1")
|
||||
endif()
|
||||
|
||||
# Is this installation the result of a crosscompile?
|
||||
if(NOT DEFINED CMAKE_CROSSCOMPILING)
|
||||
set(CMAKE_CROSSCOMPILING "FALSE")
|
||||
endif()
|
||||
|
||||
if(NOT CMAKE_INSTALL_LOCAL_ONLY)
|
||||
# Include the install script for each subdirectory.
|
||||
include("/home/ljx/work/ipc/build/hal/cmake_install.cmake")
|
||||
include("/home/ljx/work/ipc/build/utils/cmake_install.cmake")
|
||||
include("/home/ljx/work/ipc/build/test/cmake_install.cmake")
|
||||
|
||||
endif()
|
||||
|
||||
if(CMAKE_INSTALL_COMPONENT)
|
||||
set(CMAKE_INSTALL_MANIFEST "install_manifest_${CMAKE_INSTALL_COMPONENT}.txt")
|
||||
else()
|
||||
set(CMAKE_INSTALL_MANIFEST "install_manifest.txt")
|
||||
endif()
|
||||
|
||||
string(REPLACE ";" "\n" CMAKE_INSTALL_MANIFEST_CONTENT
|
||||
"${CMAKE_INSTALL_MANIFEST_FILES}")
|
||||
file(WRITE "/home/ljx/work/ipc/build/${CMAKE_INSTALL_MANIFEST}"
|
||||
"${CMAKE_INSTALL_MANIFEST_CONTENT}")
|
|
@ -4,6 +4,7 @@ set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
|
|||
include_directories(
|
||||
${UTILS_SOURCE_PATH}/LogC/include
|
||||
${UTILS_SOURCE_PATH}/LogC/src
|
||||
${UTILS_SOURCE_PATH}/ReturnCode/include
|
||||
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
|
||||
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
|
||||
)
|
||||
|
@ -25,7 +26,8 @@ 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)
|
||||
target_link_libraries(${TARGET_NAME} gtest gmock pthread ReturnCode LogCAbstract)
|
||||
# target_link_libraries(${TARGET_NAME} gtest gmock pthread ReturnCode LogCAbstract LogCUb)
|
||||
if(${COVERAGE_ON} MATCHES "true")
|
||||
target_link_libraries(${TARGET_NAME} gcov)
|
||||
endif()
|
|
@ -1,5 +1,4 @@
|
|||
#include "iLog.h"
|
||||
#include "logUb.h"
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
@ -8,20 +7,14 @@ namespace LogCTest
|
|||
// ../out/test/bin/LogCTest --gtest_filter=LogCTest.Demo
|
||||
TEST(LogCTest, Demo)
|
||||
{
|
||||
ILog *log = new_Log_abs();
|
||||
setILog(log);
|
||||
create_log_module();
|
||||
i_log_init();
|
||||
|
||||
LogInfo("hello world.");
|
||||
LogErr("create ... failed.");
|
||||
LogError("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);
|
||||
|
||||
i_log_unInit();
|
||||
destroy_log_module();
|
||||
}
|
||||
}
|
|
@ -5,14 +5,24 @@ set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH})
|
|||
include_directories(
|
||||
./src
|
||||
./include
|
||||
${UTILS_SOURCE_PATH}/ReturnCode/include
|
||||
)
|
||||
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
|
||||
aux_source_directory(./src SRC_FILES)
|
||||
aux_source_directory(./src IMPL_SRC_FILES)
|
||||
aux_source_directory(./abstract ABSTRACT_FILES)
|
||||
|
||||
# set(TARGET_NAME LogC)
|
||||
add_library(LogCAbstract STATIC ${ABSTRACT_FILES})
|
||||
add_library(LogCUb STATIC ${SRC_FILES})
|
||||
# # set(TARGET_NAME LogC)
|
||||
# add_library(LogCAbstract STATIC ${ABSTRACT_FILES})
|
||||
# target_link_libraries(${ABSTRACT_TARGET} ReturnCode)
|
||||
# add_library(LogCUb STATIC ${SRC_FILES})
|
||||
# target_link_libraries(${ABSTRACT_TARGET} ReturnCode)
|
||||
|
||||
set(ABSTRACT_TARGET LogCAbstract)
|
||||
set(IMPL_TARGET LogCUb)
|
||||
add_library(${ABSTRACT_TARGET} STATIC ${ABSTRACT_FILES})
|
||||
target_link_libraries(${ABSTRACT_TARGET} ReturnCode)
|
||||
add_library(${IMPL_TARGET} STATIC ${IMPL_SRC_FILES})
|
||||
target_link_libraries(${IMPL_TARGET} ${ABSTRACT_TARGET} ReturnCode)
|
||||
|
|
|
@ -1,42 +1,58 @@
|
|||
#include "iLog.h"
|
||||
|
||||
#include <stddef.h>
|
||||
static ILog *instance = NULL;
|
||||
#include <string.h>
|
||||
|
||||
ILog *getInstance(void)
|
||||
static RETURN_CODE_C def_log_init(ILog *impl)
|
||||
{
|
||||
return CreateReturnCode(C_RETURN_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
||||
|
||||
static RETURN_CODE_C def_log_unInit(ILog *impl)
|
||||
{
|
||||
return CreateReturnCode(C_RETURN_CODE_VIRTUAL_FUNCTION);
|
||||
}
|
||||
|
||||
static void def_free(ILog *impl)
|
||||
{}
|
||||
|
||||
static void def_log_fmt(ILog *log, const LogLeveL level, const char *fmt, ...)
|
||||
{}
|
||||
|
||||
|
||||
static ILog default_log = {
|
||||
.init = def_log_init,
|
||||
.log_fmt = def_log_fmt,
|
||||
.free = def_free,
|
||||
.unInit = def_log_unInit,
|
||||
};
|
||||
static ILog *instance = &default_log;
|
||||
|
||||
ILog *get_log_instance(void)
|
||||
{
|
||||
return instance;
|
||||
}
|
||||
|
||||
void setILog(ILog *impl)
|
||||
void reset_log_impl(ILog *impl)
|
||||
{
|
||||
instance->free(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)
|
||||
RETURN_CODE_C new_i_Log(ILog **impl)
|
||||
{
|
||||
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;
|
||||
if (!impl || !(*impl)) {
|
||||
return CreateReturnCode(C_RETURN_CODE_NOT_OK);
|
||||
}
|
||||
memcpy(*impl, &default_log, sizeof(ILog));
|
||||
return CreateReturnCode(C_RETURN_CODE_OK);
|
||||
}
|
||||
|
||||
RETURN_CODE_C create_log_module(void)
|
||||
{
|
||||
return CreateReturnCode(C_RETURN_CODE_OK);
|
||||
}
|
||||
RETURN_CODE_C destroy_log_module(void)
|
||||
{
|
||||
return CreateReturnCode(C_RETURN_CODE_OK);
|
||||
}
|
|
@ -4,20 +4,28 @@
|
|||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "ReturnCode.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__)
|
||||
#define LogVerbose(...) get_log_instance()->log_fmt(get_log_instance(), LOG_TYPE_VERBOSE, __VA_ARGS__)
|
||||
#define LogInfo(...) get_log_instance()->log_fmt(get_log_instance(), LOG_TYPE_INFORMATION, __VA_ARGS__)
|
||||
#define LogDebug(...) get_log_instance()->log_fmt(get_log_instance(), LOG_TYPE_DEBUG, __VA_ARGS__)
|
||||
#define LogError(...) get_log_instance()->log_fmt(get_log_instance(), LOG_TYPE_ERROR, __VA_ARGS__)
|
||||
#define LogWarning(...) get_log_instance()->log_fmt(get_log_instance(), LOG_TYPE_WARNING, __VA_ARGS__)
|
||||
#define LogTrace(...) get_log_instance()->log_fmt(get_log_instance(), LOG_TYPE_TRACE, __VA_ARGS__)
|
||||
|
||||
typedef enum LogLeveL {
|
||||
LOG_TYPE_INFO = 0,
|
||||
LOG_TYPE_VERBOSE = 0,
|
||||
LOG_TYPE_DEBUG,
|
||||
LOG_TYPE_INFORMATION,
|
||||
LOG_TYPE_WARNING,
|
||||
LOG_TYPE_ERROR,
|
||||
LOG_TYPE_WARR,
|
||||
LOG_TYPE_TRACE,
|
||||
LOG_TYPE_TEST_TIPS,
|
||||
LOG_TYPE_END
|
||||
}LogLeveL;
|
||||
|
||||
typedef enum LogMode {
|
||||
|
@ -28,26 +36,28 @@ typedef enum LogMode {
|
|||
|
||||
typedef struct iLog ILog;
|
||||
struct iLog {
|
||||
void (*init)(ILog *);
|
||||
void (*unInit)(ILog *);
|
||||
RETURN_CODE_C (*init)(ILog *);
|
||||
RETURN_CODE_C (*unInit)(ILog *);
|
||||
void (*free)(ILog *);
|
||||
void (*log_fmt)(ILog *, const LogLeveL level, const char *fmt, ...);
|
||||
};
|
||||
ILog* getInstance(void);
|
||||
void setILog(ILog *impl);
|
||||
ILog* get_log_instance(void);
|
||||
void reset_log_impl(ILog *impl);
|
||||
RETURN_CODE_C new_i_Log(ILog **impl);
|
||||
|
||||
ILog *new_Log_abs(void);
|
||||
void del_Log_abs(ILog *impl);
|
||||
|
||||
static void f_init(ILog *impl)
|
||||
static RETURN_CODE_C i_log_init()
|
||||
{
|
||||
getInstance()->init(getInstance());
|
||||
return get_log_instance()->init(get_log_instance());
|
||||
}
|
||||
|
||||
static void f_unInit(ILog *impl)
|
||||
static RETURN_CODE_C i_log_unInit()
|
||||
{
|
||||
getInstance()->unInit(getInstance());
|
||||
return get_log_instance()->unInit(get_log_instance());
|
||||
}
|
||||
|
||||
RETURN_CODE_C create_log_module(void);
|
||||
RETURN_CODE_C destroy_log_module(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
54
utils/LogC/src/logMakePtr.c
Normal file
54
utils/LogC/src/logMakePtr.c
Normal file
|
@ -0,0 +1,54 @@
|
|||
#include "logMakePtr.h"
|
||||
#include "logUb.h"
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
RETURN_CODE_C create_log_module(void)
|
||||
{
|
||||
LogUbuntu *log = NULL;
|
||||
RETURN_CODE_C code = create_log_instance(&log);
|
||||
if (C_RETURN_CODE_OK == code.mCode) {
|
||||
LogInfo("Create log instance ok.\n");
|
||||
reset_log_impl((ILog *)log);
|
||||
return code;
|
||||
}
|
||||
return CreateReturnCode(C_RETURN_CODE_NOT_OK);
|
||||
}
|
||||
|
||||
RETURN_CODE_C destroy_log_module(void)
|
||||
{
|
||||
reset_log_impl(NULL);
|
||||
return CreateReturnCode(C_RETURN_CODE_OK);
|
||||
}
|
||||
|
||||
static RETURN_CODE_C create_log_instance_ptr(LogMakePtr *object, LogUbuntu **log)
|
||||
{
|
||||
return new_log(log);
|
||||
}
|
||||
|
||||
static void log_make_ptr_free(LogMakePtr *object)
|
||||
{
|
||||
}
|
||||
|
||||
static LogMakePtr default_log_make_ptr = {
|
||||
.init = NULL,
|
||||
.create_log_instance = create_log_instance_ptr,
|
||||
.free = log_make_ptr_free,
|
||||
.unInit = NULL,
|
||||
};
|
||||
static LogMakePtr *log_make_ptr_instance = &default_log_make_ptr;
|
||||
|
||||
LogMakePtr *get_log_make_ptr_instance(void)
|
||||
{
|
||||
return log_make_ptr_instance;
|
||||
}
|
||||
|
||||
void log_make_ptr_object_init(LogMakePtr *object)
|
||||
{
|
||||
memcpy(object, &default_log_make_ptr, sizeof(LogMakePtr));
|
||||
}
|
||||
void reset_log_make_ptr_impl(LogMakePtr *impl)
|
||||
{
|
||||
log_make_ptr_instance->free(log_make_ptr_instance);
|
||||
log_make_ptr_instance = impl;
|
||||
}
|
42
utils/LogC/src/logMakePtr.h
Normal file
42
utils/LogC/src/logMakePtr.h
Normal file
|
@ -0,0 +1,42 @@
|
|||
#ifndef _IPC_LOG_MAKE_PTR_H_
|
||||
#define _IPC_LOG_MAKE_PTR_H_
|
||||
|
||||
#include "ReturnCode.h"
|
||||
#include "logUb.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct log_make_ptr LogMakePtr;
|
||||
typedef struct log_make_ptr {
|
||||
RETURN_CODE_C (*init)(LogMakePtr *);
|
||||
RETURN_CODE_C (*create_log_instance)(LogMakePtr *, LogUbuntu **);
|
||||
void (*free)(LogMakePtr *);
|
||||
RETURN_CODE_C (*unInit)(LogMakePtr *);
|
||||
} LogMakePtr;
|
||||
|
||||
LogMakePtr *get_log_make_ptr_instance(void);
|
||||
void log_make_ptr_object_init(LogMakePtr *object);
|
||||
void reset_log_make_ptr_impl(LogMakePtr *impl);
|
||||
|
||||
static inline RETURN_CODE_C log_make_ptr_init(void)
|
||||
{
|
||||
return get_log_make_ptr_instance()->init(get_log_make_ptr_instance());
|
||||
}
|
||||
|
||||
static inline RETURN_CODE_C log_make_ptr_un_init(void)
|
||||
{
|
||||
return get_log_make_ptr_instance()->unInit(get_log_make_ptr_instance());
|
||||
}
|
||||
|
||||
static inline RETURN_CODE_C create_log_instance(LogUbuntu **log)
|
||||
{
|
||||
return get_log_make_ptr_instance()->create_log_instance(get_log_make_ptr_instance(), log);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif //_IPC_LOG_MAKE_PTR_H_
|
|
@ -6,29 +6,19 @@
|
|||
#include <time.h>
|
||||
|
||||
static const char *LogLevelStr[] = {
|
||||
"VERBOSE",
|
||||
"DEBUG",
|
||||
"INFO",
|
||||
"Debug",
|
||||
"WARR",
|
||||
"ERROR",
|
||||
"WARR"
|
||||
"TRACE",
|
||||
"TEST_TIPS",
|
||||
};
|
||||
|
||||
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)
|
||||
void ub_log_free(ILog *impl)
|
||||
{
|
||||
if (impl != NULL) {
|
||||
free(impl);
|
||||
setILog(NULL);
|
||||
impl = NULL;
|
||||
}
|
||||
}
|
||||
|
@ -54,17 +44,46 @@ static void ub_log_fmt(ILog *log, const LogLeveL level, const char *fmt, ...)
|
|||
va_end(args);
|
||||
}
|
||||
|
||||
void ub_log_init(ILog *impl)
|
||||
RETURN_CODE_C ub_log_init(ILog *impl)
|
||||
{
|
||||
return CreateReturnCode(C_RETURN_CODE_OK);
|
||||
}
|
||||
|
||||
void ub_log_unInit(ILog *impl)
|
||||
RETURN_CODE_C ub_log_unInit(ILog *impl)
|
||||
{
|
||||
return CreateReturnCode(C_RETURN_CODE_OK);
|
||||
}
|
||||
|
||||
void init_Log_Ubuntu(LogUbuntu *lu)
|
||||
{
|
||||
((ILog*)lu)->log_fmt = ub_log_fmt;
|
||||
((ILog*)lu)->init = ub_log_init;
|
||||
((ILog*)lu)->free = ub_log_free;
|
||||
((ILog*)lu)->unInit = ub_log_unInit;
|
||||
}
|
||||
|
||||
RETURN_CODE_C new_log(LogUbuntu **log_ub)
|
||||
{
|
||||
if (!log_ub) {
|
||||
printf("C_RETURN_CODE_INVALID_PARAMENTER\n");
|
||||
return CreateReturnCode(C_RETURN_CODE_INVALID_PARAMENTER);
|
||||
}
|
||||
// log_ub为NULL
|
||||
if (!(*log_ub)) {
|
||||
*log_ub = (LogUbuntu *)malloc(sizeof(LogUbuntu));
|
||||
// malloc success
|
||||
if (*log_ub) {
|
||||
printf("new_log succeed.\n");
|
||||
new_i_Log((ILog **)log_ub);
|
||||
init_Log_Ubuntu(*log_ub);
|
||||
return CreateReturnCode(C_RETURN_CODE_OK);
|
||||
}
|
||||
// malloc failed
|
||||
// init_Log_Ubuntu(*log_ub);
|
||||
printf("new_log failed.\n");
|
||||
return CreateReturnCode(C_RETURN_CODE_NOT_OK);
|
||||
}
|
||||
// log_ub 不为NULL
|
||||
init_Log_Ubuntu(*log_ub);
|
||||
return CreateReturnCode(C_RETURN_CODE_OK);
|
||||
}
|
|
@ -2,7 +2,6 @@
|
|||
#define _IPC_LOG_UBUNTU_H_
|
||||
|
||||
#include "iLog.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
@ -12,9 +11,7 @@ struct LogUbuntu {
|
|||
ILog base;
|
||||
};
|
||||
|
||||
void init_Log_Ubuntu(LogUbuntu *lu);
|
||||
LogUbuntu *new_Log_Ubuntu(void);
|
||||
void del_Log_Ubuntu(LogUbuntu *impl);
|
||||
RETURN_CODE_C new_log(LogUbuntu **log_ub);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user