Add new log module.

This commit is contained in:
xiaojiazhu 2023-09-08 22:44:13 -07:00
parent 3daf05fa02
commit a80dbac00e
11 changed files with 169 additions and 45 deletions

View File

@ -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,\

View File

@ -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"
{

View File

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

View File

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

View File

@ -8,7 +8,13 @@ public:
ILogCpp() = default;
virtual ~ILogCpp() = default;
static std::shared_ptr<ILogCpp> &GetInstance(std::shared_ptr<ILogCpp> *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

View File

@ -3,12 +3,19 @@
#include "ILogCpp.h"
#include "LogEasylogging.h"
#include "LogImpl.h"
#include "Log.h"
#include <memory>
void CreateLogModule(void)
{
std::shared_ptr<ILogCpp> 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<ILogCpp> ILogMakePtr::MakeLogImplPtr(void)
{
@ -20,3 +27,7 @@ std::shared_ptr<ILogCpp> ILogMakePtr::MakeLogEasylogging(const LogSetting *setti
std::shared_ptr<ILogCpp> logImpl = std::make_shared<LogEasylogging>(setting);
return logImpl;
}
void ILogMakePtr::CreateLogPtr(ILog **log)
{
NewLog((Log **)log);
}

View File

@ -1,6 +1,7 @@
#ifndef ILOG_MAKE_PTR_H
#define ILOG_MAKE_PTR_H
#include "ILogCpp.h"
#include "ILog.h"
#include <iostream>
#include <memory>
class ILogMakePtr
@ -19,5 +20,6 @@ public:
virtual ~ILogMakePtr() = default;
virtual std::shared_ptr<ILogCpp> MakeLogImplPtr(void);
virtual std::shared_ptr<ILogCpp> MakeLogEasylogging(const LogSetting *setting);
virtual void CreateLogPtr(ILog **log);
};
#endif

87
utils/Log/src/Log.cpp Normal file
View File

@ -0,0 +1,87 @@
#include "Log.h"
#include "ILogCpp.h"
#include <stdarg.h>
#include <stdio.h>
#include <iostream>
#include <memory>
#include <string.h>
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;
}
}

17
utils/Log/src/Log.h Normal file
View File

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

View File

@ -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;
// }
int LogEasylogging::Trace(const char *buff)
{
LOG(TRACE) << buff;
return 0;
}

View File

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