CMCore/src/Log/Loginfo.cpp

217 lines
5.7 KiB
C++
Raw Normal View History

2019-08-26 21:19:13 +08:00
#include<hgl/plugin/PlugIn.h>
#include<hgl/log/Logger.h>
2019-08-23 17:54:42 +08:00
#include<hgl/type/DateTime.h>
2019-08-26 21:19:13 +08:00
#include<hgl/type/List.h>
2019-08-23 17:54:42 +08:00
#include<hgl/thread/RWLock.h>
#include<hgl/filesystem/FileSystem.h>
2019-08-23 17:54:42 +08:00
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;
2019-08-27 20:26:44 +08:00
ToDateTime(d,t);
OSString cur_path;
OSString cur_program;
filesystem::GetCurrentPath(cur_path);
filesystem::GetCurrentProgram(cur_program);
const OSString str= OS_TEXT("Starting Log at ")+
OSString::numberOf(d.GetYear ())+OS_TEXT("-")+
OSString::numberOf(d.GetMonth ())+OS_TEXT("-")+
OSString::numberOf(d.GetDay ())+OS_TEXT(" ")+
OSString::numberOf(t.GetHour ())+OS_TEXT(":")+
OSString::numberOf(t.GetMinute())+OS_TEXT(":")+
OSString::numberOf(t.GetSecond())+OS_TEXT("\n")+
OS_TEXT("Current program: ") + cur_program.c_str() + OS_TEXT("\n")+
OS_TEXT("Current path: ") + cur_path.c_str() + OS_TEXT("\n");
2019-08-23 17:54:42 +08:00
WriteLog(LogLevel::Log,str.c_str(),str.Length());
2019-08-23 17:54:42 +08:00
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->Free();
2019-08-23 17:54:42 +08:00
}
}//namespace logger
namespace logger
{
/**
*
*/
struct LogInterface
{
bool (*Add)(Logger *); ///<增加一个日志输出
bool (*Init)(); ///<初始化日志输出
void (*Close)(); ///<关闭所有日志输出
void (*WriteUTF16)(LogLevel,const u16char *,int); ///<输出一行日志
2020-07-07 19:14:42 +08:00
void (*WriteUTF8)(LogLevel,const u8char *,int); ///<输出一行日志
2019-08-23 17:54:42 +08:00
};//struct LogInterface
static LogInterface LogInterface3=
{
AddLogger,
PutLogHeader,
CloseAllLog,
WriteLog<u16char>,
2020-07-07 19:14:42 +08:00
WriteLog<u8char>
2019-08-23 17:54:42 +08:00
};
/**
2019-08-27 20:26:44 +08:00
*
2019-08-23 17:54:42 +08:00
*/
2019-08-26 21:19:13 +08:00
class LogPlugIn:public PlugIn ///日志插件
2019-08-23 17:54:42 +08:00
{
public:
LogPlugIn()
{
2019-08-27 20:26:44 +08:00
ver=1;
name=OS_TEXT("Log");
intro=U16_TEXT("unicode text log module.");
2019-08-23 17:54:42 +08:00
2019-08-27 20:26:44 +08:00
filename=OS_TEXT("Loginfo.cpp");
}
2019-08-23 17:54:42 +08:00
2019-08-27 20:26:44 +08:00
bool GetInterface(uint ver,void *data) override
{
if(ver!=3||!data)
return(false);
memcpy(data,&LogInterface3,sizeof(LogInterface));
return(true);
2019-08-23 17:54:42 +08:00
}
};//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;
return(nullptr);
}
void CloseLog()
{
2019-08-29 15:28:49 +08:00
if(!li)return;
li->Close();
delete li;
li=nullptr;
2019-08-23 17:54:42 +08:00
}
void Log(LogLevel level,const u16char *str,int size)
{
if(li)
li->WriteUTF16(level,str,size==-1?hgl::strlen(str):size);
}
2020-07-07 19:14:42 +08:00
void Log(LogLevel level,const u8char *str,int size)
2019-08-23 17:54:42 +08:00
{
if(li)
li->WriteUTF8(level,str,size==-1?hgl::strlen(str):size);
}
}//namespace logger
namespace logger
{
2019-08-29 15:55:18 +08:00
Logger *CreateLoggerConsole (LogLevel);
2019-08-23 17:54:42 +08:00
Logger *CreateLoggerFile (const OSString &,LogLevel);
/**
* <br>
* 使SDK的应用程序使用
*/
bool InitLogger(const OSString &app_name)
{
AddLogger(CreateLoggerConsole(LogLevel::Log));
AddLogger(CreateLoggerFile(app_name,LogLevel::Log));
2019-08-23 17:54:42 +08:00
return InitLog();
}
}//namespace logger
}//namespace hgl