增加Log通用部分代码
This commit is contained in:
parent
c5dca40260
commit
0552be7754
@ -25,20 +25,23 @@ namespace hgl
|
||||
/**
|
||||
* 插件注册模板
|
||||
*/
|
||||
template<typename T> class RegistryPlugInProxy
|
||||
template<typename T,typename CN> class RegistryPlugInProxy
|
||||
{
|
||||
SharedPtr<T> plugin;
|
||||
T *plugin;
|
||||
|
||||
public:
|
||||
|
||||
RegistryPlugInProxy()
|
||||
{
|
||||
plugin=new T;
|
||||
plugin=new CN;
|
||||
}
|
||||
|
||||
virtual ~RegistryPlugInProxy()=default;
|
||||
virtual ~RegistryPlugInProxy()
|
||||
{
|
||||
delete plugin;
|
||||
}
|
||||
|
||||
T *get(){return *plugin;}
|
||||
T *get(){return plugin;}
|
||||
};//template<typename T> class RegistryPlugInProxy
|
||||
|
||||
/*
|
||||
@ -52,10 +55,10 @@ namespace hgl
|
||||
*/
|
||||
|
||||
#ifndef __MAKE_PLUGIN__ //内部插件
|
||||
#define REGISTRY_PLUG_IN(name,classname) static RegistryPlugInProxy<classname> plugin_proxy_##classname;
|
||||
#define REGISTRY_PLUG_IN(name,classname) static RegistryPlugInProxy<name,classname> plugin_proxy_##classname; \
|
||||
extern "C" void registry_plugin_##classname;
|
||||
#else //外部插件
|
||||
#define REGISTRY_PLUG_IN(name,classname)
|
||||
static
|
||||
#define REGISTRY_PLUG_IN(name,classname) static RegistryPlugInProxy<name,classname> plugin_proxy_##classname \
|
||||
extern "C" void registry_plugin_##name(void)
|
||||
{
|
||||
|
||||
|
@ -49,6 +49,12 @@ file(GLOB BASE_PLUG_IN_SOURCE PlugIn/*.cpp)
|
||||
SOURCE_GROUP("PlugIn\\Header Files" FILES ${BASE_PLUG_IN_HEADER})
|
||||
SOURCE_GROUP("PlugIn\\Source Files" FILES ${BASE_PLUG_IN_SOURCE})
|
||||
|
||||
file(GLOB BASE_LOG_HEADER ${CMCORE_ROOT_INCLUDE_PATH}/hgl/log/*.h)
|
||||
file(GLOB BASE_LOG_SOURCE Log/*.*)
|
||||
|
||||
SOURCE_GROUP("Log\\Header Files" FILES ${BASE_LOG_HEADER})
|
||||
SOURCE_GROUP("Log\\Source Files" FILES ${BASE_LOG_SOURCE})
|
||||
|
||||
add_library(CMCore STATIC #${SYSTEM_INFO_SOURCE}
|
||||
${TYPE_TEMPLATE_HEADER}
|
||||
${BASE_TYPE_SOURCE}
|
||||
@ -57,6 +63,10 @@ add_library(CMCore STATIC #${SYSTEM_INFO_SOURCE}
|
||||
${BASE_OTHER_SOURCE}
|
||||
|
||||
${BASE_PLUG_IN_HEADER}
|
||||
${BASE_PLUG_IN_SOURCE})
|
||||
${BASE_PLUG_IN_SOURCE}
|
||||
|
||||
${BASE_LOG_HEADER}
|
||||
${BASE_LOG_SOURCE}
|
||||
)
|
||||
|
||||
set_property(TARGET CMCore PROPERTY FOLDER "CM")
|
||||
|
116
src/Log/LogFile.cpp
Normal file
116
src/Log/LogFile.cpp
Normal file
@ -0,0 +1,116 @@
|
||||
#include<hgl/Logger.h>
|
||||
#include<hgl/io/FileSystem.h>
|
||||
#include<hgl/thread/ThreadMutex.h>
|
||||
#include<hgl/io/FileOutputStream.h>
|
||||
#include<hgl/io/DataOutputStream.h>
|
||||
#include<hgl/io/TextOutputStream.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
using namespace filesystem;
|
||||
|
||||
namespace logger
|
||||
{
|
||||
io::TextOutputStream *CreateTextOutputStream(io::OutputStream *);
|
||||
|
||||
class LogFile:public Logger
|
||||
{
|
||||
ThreadMutex lock;
|
||||
|
||||
io::FileOutputStream fos;
|
||||
io::TextOutputStream *tos;
|
||||
|
||||
public:
|
||||
|
||||
LogFile(LogLevel ll):Logger(ll)
|
||||
{
|
||||
tos=nullptr;
|
||||
}
|
||||
|
||||
~LogFile()
|
||||
{
|
||||
SAFE_CLEAR(tos);
|
||||
}
|
||||
|
||||
bool Create(const OSString &project_code)
|
||||
{
|
||||
os_char filename[HGL_MAX_PATH];
|
||||
os_char fn[HGL_MAX_PATH];
|
||||
os_char num[16]={'.',0};
|
||||
|
||||
GetLocalAppdataPath(fn);
|
||||
|
||||
strcat(fn,HGL_MAX_PATH,HGL_DIRECTORY_SEPARATOR);
|
||||
strcat(fn,HGL_MAX_PATH,OS_TEXT(".cmgdk"),6);
|
||||
filesystem::MakePath(fn);
|
||||
|
||||
strcat(fn,HGL_MAX_PATH,HGL_DIRECTORY_SEPARATOR);
|
||||
|
||||
strcat(fn,HGL_MAX_PATH,project_code);
|
||||
|
||||
for(uint i=0;i<=0xFFFF;i++)
|
||||
{
|
||||
strcpy(filename,HGL_MAX_PATH,fn);
|
||||
|
||||
if(i)
|
||||
{
|
||||
utos(num+1,14,i);
|
||||
strcat(filename,HGL_MAX_PATH,num,sizeof(num));
|
||||
}
|
||||
|
||||
strcat(filename,HGL_MAX_PATH,OS_TEXT(".Loginfo"),8);
|
||||
|
||||
if(fos.CreateTrunc(filename))//创建成功
|
||||
{
|
||||
tos=CreateTextOutputStream(&fos);
|
||||
tos->WriteBOM();
|
||||
|
||||
return(true);
|
||||
}
|
||||
}
|
||||
|
||||
return(false);
|
||||
}
|
||||
|
||||
void Close()
|
||||
{
|
||||
SAFE_CLEAR(tos);
|
||||
fos.Close();
|
||||
}
|
||||
|
||||
void Write(const u16char *str,int size)
|
||||
{
|
||||
if(tos&&str&&*str&&size>0)
|
||||
{
|
||||
// lock.Lock();
|
||||
tos->WriteLine(str,size);
|
||||
// lock.Unlock();
|
||||
}
|
||||
}
|
||||
|
||||
void Write(const char *str,int size)
|
||||
{
|
||||
if(tos&&str&&*str&&size>0)
|
||||
{
|
||||
// lock.Lock();
|
||||
tos->WriteLine(str,size);
|
||||
// lock.Unlock();
|
||||
}
|
||||
}
|
||||
};//class LogFile
|
||||
|
||||
Logger *CreateLoggerFile(const OSString &pc,LogLevel ll)
|
||||
{
|
||||
if(ll<llError)
|
||||
return(nullptr);
|
||||
|
||||
LogFile *lf=new LogFile(ll);
|
||||
|
||||
if(lf->Create(pc))
|
||||
return lf;
|
||||
|
||||
delete lf;
|
||||
return(nullptr);
|
||||
}
|
||||
}//namespace logger
|
||||
}//namespace hgl
|
211
src/Log/Loginfo.cpp
Normal file
211
src/Log/Loginfo.cpp
Normal file
@ -0,0 +1,211 @@
|
||||
#include<hgl/PlugIn.h>
|
||||
#include<hgl/Logger.h>
|
||||
#include<hgl/type/DateTime.h>
|
||||
#include<hgl/thread/RWLock.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
namespace logger
|
||||
{
|
||||
RWLockObject<List<Logger *>> log_list; //记录器列表
|
||||
|
||||
bool AddLogger(Logger *log)
|
||||
{
|
||||
if(!log)
|
||||
return(false);
|
||||
|
||||
OnlyWriteLock owl(log_list);
|
||||
|
||||
if(log_list->IsExist(log)) //重复添加
|
||||
return(false);
|
||||
|
||||
log_list->Add(log);
|
||||
return(true);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void WriteLog(LogLevel level,const T *str,int size)
|
||||
{
|
||||
OnlyReadLock orl(log_list);
|
||||
|
||||
const int n=log_list->GetCount();
|
||||
|
||||
if(n<=0)return;
|
||||
|
||||
Logger **log=log_list->GetData();
|
||||
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
if((*log)->GetLevel()>=level)
|
||||
(*log)->Write(str,size);
|
||||
|
||||
++log;
|
||||
}
|
||||
}
|
||||
|
||||
bool PutLogHeader()
|
||||
{
|
||||
OnlyReadLock orl(log_list);
|
||||
|
||||
if(log_list->GetCount()<=0)
|
||||
return(false);
|
||||
|
||||
Date d;
|
||||
Time t;
|
||||
|
||||
d.Sync();
|
||||
t.Sync();
|
||||
|
||||
const OSString str= OS_TEXT("Create Log Date/Time: ")+
|
||||
OSString(d.GetYear ())+OS_TEXT("-")+
|
||||
OSString(d.GetMonth ())+OS_TEXT("-")+
|
||||
OSString(d.GetDay ())+OS_TEXT(" ")+
|
||||
OSString(t.GetHour ())+OS_TEXT(":")+
|
||||
OSString(t.GetMinute())+OS_TEXT(":")+
|
||||
OSString(t.GetSecond())+OS_TEXT("\n");
|
||||
|
||||
WriteLog(llLog,str.c_str(),str.Length());
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
void CloseAllLog()
|
||||
{
|
||||
OnlyWriteLock owl(log_list);
|
||||
|
||||
const int n=log_list->GetCount();
|
||||
|
||||
if(n<=0)return;
|
||||
|
||||
Logger **log=log_list->GetData();
|
||||
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
(*log)->Close();
|
||||
delete *log;
|
||||
|
||||
++log;
|
||||
}
|
||||
|
||||
log_list->Clear();
|
||||
}
|
||||
}//namespace logger
|
||||
|
||||
namespace logger
|
||||
{
|
||||
/**
|
||||
* 日志插件接口
|
||||
*/
|
||||
struct LogInterface
|
||||
{
|
||||
bool (*Add)(Logger *); ///<增加一个日志输出
|
||||
|
||||
bool (*Init)(); ///<初始化日志输出
|
||||
void (*Close)(); ///<关闭所有日志输出
|
||||
|
||||
void (*WriteUTF16)(LogLevel,const u16char *,int); ///<输出一行日志
|
||||
void (*WriteUTF8)(LogLevel,const char *,int); ///<输出一行日志
|
||||
};//struct LogInterface
|
||||
|
||||
static LogInterface LogInterface3=
|
||||
{
|
||||
AddLogger,
|
||||
|
||||
PutLogHeader,
|
||||
CloseAllLog,
|
||||
|
||||
WriteLog<u16char>,
|
||||
WriteLog<char>
|
||||
};
|
||||
|
||||
bool GetLogInterface(uint32 ver,void *data)
|
||||
{
|
||||
if(ver!=3)
|
||||
return(false);
|
||||
|
||||
memcpy(data,&LogInterface3,sizeof(LogInterface));
|
||||
return(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 日志插件,一个模拟的接口
|
||||
*/
|
||||
class LogPlugIn:public PlugIn ///日志插件
|
||||
{
|
||||
public:
|
||||
|
||||
LogPlugIn()
|
||||
{
|
||||
type=pitLog;
|
||||
|
||||
name=OS_TEXT("unicode text log module.");
|
||||
filename=OS_TEXT("LogInfo.cpp");
|
||||
|
||||
GetInterface=GetLogInterface;
|
||||
}
|
||||
};//class LogPlugIn
|
||||
}//namespace logger
|
||||
|
||||
namespace logger
|
||||
{
|
||||
static LogInterface *li=nullptr;
|
||||
|
||||
PlugIn *InitLog()
|
||||
{
|
||||
PlugIn *pi=new LogPlugIn;
|
||||
|
||||
li=new LogInterface;
|
||||
|
||||
if(pi->GetInterface(3,li))
|
||||
if(li->Init())
|
||||
return(pi);
|
||||
|
||||
delete li;
|
||||
li=nullptr;
|
||||
|
||||
UnloadPlugIn(pi);
|
||||
|
||||
return(nullptr);
|
||||
}
|
||||
|
||||
void CloseLog()
|
||||
{
|
||||
if(li)
|
||||
{
|
||||
li->Close();
|
||||
delete li;
|
||||
li=nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void Log(LogLevel level,const u16char *str,int size)
|
||||
{
|
||||
if(li)
|
||||
li->WriteUTF16(level,str,size==-1?hgl::strlen(str):size);
|
||||
}
|
||||
|
||||
void Log(LogLevel level,const char *str,int size)
|
||||
{
|
||||
if(li)
|
||||
li->WriteUTF8(level,str,size==-1?hgl::strlen(str):size);
|
||||
}
|
||||
}//namespace logger
|
||||
|
||||
namespace logger
|
||||
{
|
||||
Logger *CreateLoggerConsole (const OSString &,LogLevel);
|
||||
Logger *CreateLoggerFile (const OSString &,LogLevel);
|
||||
|
||||
/**
|
||||
* 独立的日志系统初始化<br>
|
||||
* 供不整体使用SDK的应用程序使用
|
||||
*/
|
||||
bool InitLogger(const OSString &app_name)
|
||||
{
|
||||
AddLogger(CreateLoggerConsole(app_name,llLog));
|
||||
AddLogger(CreateLoggerFile(app_name,llLog));
|
||||
|
||||
return InitLog();
|
||||
}
|
||||
}//namespace logger
|
||||
}//namespace hgl
|
Loading…
x
Reference in New Issue
Block a user