diff --git a/inc/hgl/LogInfo.h b/inc/hgl/log/LogInfo.h similarity index 100% rename from inc/hgl/LogInfo.h rename to inc/hgl/log/LogInfo.h diff --git a/inc/hgl/Logger.h b/inc/hgl/log/Logger.h similarity index 100% rename from inc/hgl/Logger.h rename to inc/hgl/log/Logger.h diff --git a/inc/hgl/plugin/PlugInManage.h b/inc/hgl/plugin/PlugInManage.h index 5255622..be25c41 100644 --- a/inc/hgl/plugin/PlugInManage.h +++ b/inc/hgl/plugin/PlugInManage.h @@ -25,20 +25,23 @@ namespace hgl /** * 插件注册模板 */ - template class RegistryPlugInProxy + template class RegistryPlugInProxy { - SharedPtr 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 class RegistryPlugInProxy /* @@ -52,10 +55,10 @@ namespace hgl */ #ifndef __MAKE_PLUGIN__ //内部插件 - #define REGISTRY_PLUG_IN(name,classname) static RegistryPlugInProxy plugin_proxy_##classname; + #define REGISTRY_PLUG_IN(name,classname) static RegistryPlugInProxy 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 plugin_proxy_##classname \ extern "C" void registry_plugin_##name(void) { diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b4c5e84..dc02a9d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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") diff --git a/src/Log/LogFile.cpp b/src/Log/LogFile.cpp new file mode 100644 index 0000000..d995f7c --- /dev/null +++ b/src/Log/LogFile.cpp @@ -0,0 +1,116 @@ +#include +#include +#include +#include +#include +#include + +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(llCreate(pc)) + return lf; + + delete lf; + return(nullptr); + } + }//namespace logger +}//namespace hgl diff --git a/src/Log/Loginfo.cpp b/src/Log/Loginfo.cpp new file mode 100644 index 0000000..bd1dbd3 --- /dev/null +++ b/src/Log/Loginfo.cpp @@ -0,0 +1,211 @@ +#include +#include +#include +#include + +namespace hgl +{ + namespace logger + { + RWLockObject> 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 + 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;iGetLevel()>=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;iClose(); + 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, + WriteLog + }; + + 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); + + /** + * 独立的日志系统初始化
+ * 供不整体使用SDK的应用程序使用 + */ + bool InitLogger(const OSString &app_name) + { + AddLogger(CreateLoggerConsole(app_name,llLog)); + AddLogger(CreateLoggerFile(app_name,llLog)); + + return InitLog(); + } + }//namespace logger +}//namespace hgl