diff --git a/build/global_config.cmake b/build/global_config.cmake index ebe12d3d..b9c096ff 100755 --- a/build/global_config.cmake +++ b/build/global_config.cmake @@ -14,7 +14,7 @@ set(EXTERNAL_SOURCE_PATH "${CMAKE_SOURCE_DIR_IPCSDK}/external") set(CLANG_TIDY_CHECKS "-*,\ llvm-else-after-return,\ -llvm-include-order,\ +-llvm-include-order,\ llvm-namespace-comment,\ llvm-prefer-isa-or-dyn-cast-in-conditionals,\ llvm-prefer-register-over-unsigned,\ diff --git a/hal/src/Hal.h b/hal/src/Hal.h index 4c4d5953..2cb10598 100644 --- a/hal/src/Hal.h +++ b/hal/src/Hal.h @@ -2,9 +2,6 @@ #define HAL_H #include "StatusCode.h" #include "IHal.h" -/* -** Make sure we can call this stuff from C++. -*/ #ifdef __cplusplus extern "C" { diff --git a/utils/Log/abstract/ILog.cpp b/utils/Log/abstract/ILog.cpp index ae9860aa..a59ad506 100644 --- a/utils/Log/abstract/ILog.cpp +++ b/utils/Log/abstract/ILog.cpp @@ -8,7 +8,7 @@ static void ILogInitCallBack(ILog *object, const enum LogInstance log) static void ILogFree(ILog *object) { } -static int ILogPrintf(ILog *object, const char *function, const int line, const int type, const char *format, ...) +static int ILogPrintf(ILog *object, const char *function, const int line, const enum LogType type, const char *format, ...) { return 0; } diff --git a/utils/Log/include/ILog.h b/utils/Log/include/ILog.h index 659bcd75..9576fe6e 100644 --- a/utils/Log/include/ILog.h +++ b/utils/Log/include/ILog.h @@ -37,7 +37,7 @@ extern "C" { void (*init)(ILog *, const enum LogInstance); void (*free)(ILog *); - int (*printf)(ILog *, const char *, const int, const int, const char *, ...); + int (*printf)(ILog *, const char *, const int, const enum LogType, const char *, ...); void (*un_init)(ILog *); } ILog; ILog *GetLogIntance(void); @@ -51,6 +51,10 @@ extern "C" { return GetLogIntance()->un_init(GetLogIntance()); } + static inline void ILogFree(void) + { + GetLogIntance()->free(GetLogIntance()); + } void CreateLogModule(void); void DestroyLogModule(void); #ifdef __cplusplus diff --git a/utils/Log/include/ILogCpp.h b/utils/Log/include/ILogCpp.h index e8b1c1fa..7b1b264f 100644 --- a/utils/Log/include/ILogCpp.h +++ b/utils/Log/include/ILogCpp.h @@ -8,7 +8,13 @@ public: ILogCpp() = default; virtual ~ILogCpp() = default; static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); - virtual void Init(const int &log) {} + virtual void Init(const enum LogInstance &log) {} virtual void UnInit(void) {} + virtual int Log(const char *buff) { return 0; } + virtual int InFo(const char *buff) { return 0; } + virtual int Warning(const char *buff) { return 0; } + virtual int Error(const char *buff) { return 0; } + virtual int Trace(const char *buff) { return 0; } + virtual int Debug(const char *buff) { return 0; } }; #endif \ No newline at end of file diff --git a/utils/Log/src/ILogMakePtr.cpp b/utils/Log/src/ILogMakePtr.cpp index 7c48c1ed..a1859b23 100644 --- a/utils/Log/src/ILogMakePtr.cpp +++ b/utils/Log/src/ILogMakePtr.cpp @@ -3,12 +3,19 @@ #include "ILogCpp.h" #include "LogEasylogging.h" #include "LogImpl.h" +#include "Log.h" #include void CreateLogModule(void) { + std::shared_ptr logImpl = ILogMakePtr::GetInstance()->MakeLogEasylogging(nullptr); + ILogCpp::GetInstance(&logImpl); + ILog *log = nullptr; + ILogMakePtr::GetInstance()->CreateLogPtr(&log); + ResetLogImpl((ILog *)log); } void DestroyLogModule(void) { + ILogFree(); } std::shared_ptr ILogMakePtr::MakeLogImplPtr(void) { @@ -19,4 +26,8 @@ std::shared_ptr ILogMakePtr::MakeLogEasylogging(const LogSetting *setti { std::shared_ptr logImpl = std::make_shared(setting); return logImpl; +} +void ILogMakePtr::CreateLogPtr(ILog **log) +{ + NewLog((Log **)log); } \ No newline at end of file diff --git a/utils/Log/src/ILogMakePtr.h b/utils/Log/src/ILogMakePtr.h index 80a0aad4..c48a839d 100644 --- a/utils/Log/src/ILogMakePtr.h +++ b/utils/Log/src/ILogMakePtr.h @@ -1,6 +1,7 @@ #ifndef ILOG_MAKE_PTR_H #define ILOG_MAKE_PTR_H #include "ILogCpp.h" +#include "ILog.h" #include #include class ILogMakePtr @@ -19,5 +20,6 @@ public: virtual ~ILogMakePtr() = default; virtual std::shared_ptr MakeLogImplPtr(void); virtual std::shared_ptr MakeLogEasylogging(const LogSetting *setting); + virtual void CreateLogPtr(ILog **log); }; #endif \ No newline at end of file diff --git a/utils/Log/src/Log.cpp b/utils/Log/src/Log.cpp new file mode 100644 index 00000000..b080378e --- /dev/null +++ b/utils/Log/src/Log.cpp @@ -0,0 +1,87 @@ +#include "Log.h" +#include "ILogCpp.h" +#include +#include +#include +#include +#include +static void LogFree(ILog *object) +{ + printf("log instance free.\n"); + if (object) + { + free(object); + } +} +static int LogPrintf(ILog *object, const char *function, const int line, const enum LogType type, const char *format, ...) +{ + // TODO: + // LogTypeToString(type); + constexpr int SEND_TRACE_BUFF_SIZE = 2048; + char buff[SEND_TRACE_BUFF_SIZE] = {0}; + snprintf(buff, SEND_TRACE_BUFF_SIZE, "[%s][line:%d]:", function, line); + // ILog::GetInstance()->Log(buff); + const int headLen = strlen(buff); + va_list vargs; + va_start(vargs, format); + int len = vsnprintf(buff + headLen, SEND_TRACE_BUFF_SIZE - headLen, format, vargs); + va_end(vargs); + switch (type) + { + case LOG_TYPE_INFORMATION: + ILogCpp::GetInstance()->InFo(buff); + break; + + case LOG_TYPE_WARNING: + ILogCpp::GetInstance()->Warning(buff); + break; + + case LOG_TYPE_ERROR: + ILogCpp::GetInstance()->Error(buff); + break; + + case LOG_TYPE_DEBUG: + ILogCpp::GetInstance()->Debug(buff); + break; + + case LOG_TYPE_TRACE: + ILogCpp::GetInstance()->Trace(buff); + break; + + default: + break; + } + return len; +} +static void LogImplInit(Log *log) +{ + printf("LogImplInit\n"); + ((ILog *)log)->printf = LogPrintf; + ((ILog *)log)->free = LogFree; +} +void NewLog(Log **log) +{ + if (!log) + { + printf("STATUS_CODE_INVALID_PARAMENTER\n"); + return; + } + if (!(*log)) + { + *log = (Log *)malloc(sizeof(Log)); + if (*log) + { + printf("NewLog succeed.\n"); + NewILog((ILog **)log); + LogImplInit(*log); + return; + } + LogError("NewLog failed.\n"); + return; + } + // else + { + LogImplInit(*log); + return; + } +} \ No newline at end of file diff --git a/utils/Log/src/Log.h b/utils/Log/src/Log.h new file mode 100644 index 00000000..91558019 --- /dev/null +++ b/utils/Log/src/Log.h @@ -0,0 +1,17 @@ +#ifndef LOG_H +# define LOG_H +#include "ILog.h" +#ifdef __cplusplus +extern "C" +{ +#endif + typedef struct log Log; + typedef struct log + { + ILog base; + } Log; + void NewLog(Log **log); +#ifdef __cplusplus +} +#endif +#endif \ No newline at end of file diff --git a/utils/Log/src/LogEasylogging.cpp b/utils/Log/src/LogEasylogging.cpp index 4d6831ca..6ed5c967 100644 --- a/utils/Log/src/LogEasylogging.cpp +++ b/utils/Log/src/LogEasylogging.cpp @@ -21,7 +21,7 @@ LogEasylogging::LogEasylogging(const LogSetting *setting) mMaxSize = setting->maxSize; } } -void LogEasylogging::Init(const int &log) +void LogEasylogging::Init(const enum LogInstance &log) { #if 0 el::Configurations conf("/home/xiaojiazhu/project/OS/OSThings/test/out/bin/default-logger.conf"); @@ -51,38 +51,38 @@ void LogEasylogging::UnInit(void) // { // return true; // } -// int LogEasylogging::Log(const char *buff) -// { -// // LOG(INFO) << buff; -// return 0; -// } +int LogEasylogging::Log(const char *buff) +{ + // LOG(INFO) << buff; + return 0; +} -// int LogEasylogging::InFo(const char *buff) -// { -// LOG(INFO) << buff; -// return 0; -// } +int LogEasylogging::InFo(const char *buff) +{ + LOG(INFO) << buff; + return 0; +} -// int LogEasylogging::Warning(const char *buff) -// { -// LOG(WARNING) << buff; -// return 0; -// } +int LogEasylogging::Warning(const char *buff) +{ + LOG(WARNING) << buff; + return 0; +} -// int LogEasylogging::Error(const char *buff) -// { -// LOG(ERROR) << buff; -// return 0; -// } +int LogEasylogging::Error(const char *buff) +{ + LOG(ERROR) << buff; + return 0; +} -// int LogEasylogging::Debug(const char *buff) -// { -// LOG(DEBUG) << buff; -// return 0; -// } +int LogEasylogging::Debug(const char *buff) +{ + LOG(DEBUG) << buff; + return 0; +} -// int LogEasylogging::Trace(const char *buff) -// { -// LOG(TRACE) << buff; -// return 0; -// } \ No newline at end of file +int LogEasylogging::Trace(const char *buff) +{ + LOG(TRACE) << buff; + return 0; +} \ No newline at end of file diff --git a/utils/Log/src/LogEasylogging.h b/utils/Log/src/LogEasylogging.h index c5826092..5f5e46df 100644 --- a/utils/Log/src/LogEasylogging.h +++ b/utils/Log/src/LogEasylogging.h @@ -6,15 +6,15 @@ class LogEasylogging : public ILogCpp public: LogEasylogging(const LogSetting *setting); virtual ~LogEasylogging() = default; - void Init(const int &log) override; + void Init(const enum LogInstance &log) override; void UnInit(void) override; // bool IsWorking(); // override; - // int Log(const char *buff); // override; - // int InFo(const char *buff); // override; - // int Warning(const char *buff); // override; - // int Error(const char *buff); // override; - // int Trace(const char *buff); // override; - // int Debug(const char *buff); // override; + int Log(const char *buff); // override; + int InFo(const char *buff); // override; + int Warning(const char *buff); // override; + int Error(const char *buff); // override; + int Trace(const char *buff); // override; + int Debug(const char *buff); // override; private: std::string mFileName; // File name of saving log.