#include "stdafx.h"
#include "LLog.h"
#include "process.h"
#include <io.h>
#include <iostream>
namespace LTM {
/*-----------------------------------------------------------------------
* 功能: 得到指定文件的大小
*
* 参数: [szPath] 文件路径
*
* 返回:
*
* 备注:
-----------------------------------------------------------------------*/
LONGLONG GetFileSize(const std::string szPath)
{
WIN32_FIND_DATAA fileInfo;
HANDLE hFind;
LONGLONG llFileSize;
LARGE_INTEGER TotalFileSize;
hFind = FindFirstFileA(szPath.c_str() ,&fileInfo);
if (hFind != INVALID_HANDLE_VALUE)
{
TotalFileSize.LowPart = fileInfo.nFileSizeLow;
TotalFileSize.HighPart = fileInfo.nFileSizeHigh;
llFileSize = TotalFileSize.QuadPart;
}
else
llFileSize = 0 ;
FindClose(hFind);
return llFileSize;
}
/*-----------------------------------------------------------------------
* 功能: 判断指定路径的文件是否存在
*
* 参数: [strPath] 文件路径
*
* 返回: 存在返回TRUE,反之返回FALSE
*
* 备注:
-----------------------------------------------------------------------*/
bool CheckFileExist(const std::string strPath)
{
return (_access(strPath.c_str(),0) == 0);
}
/*-----------------------------------------------------------------------
* 功能: 得到当前系统时间
*
* 参数:
*
* 返回:
*
* 备注: 北京时区
-----------------------------------------------------------------------*/
std::string GetLocalTimeString()
{
char szTime[255] = {0};
SYSTEMTIME st;
GetLocalTime(&st);
wsprintfA(szTime,"%04d-%02d-%02d %02d:%02d:%02d",\
st.wYear,st.wMonth,st.wDay,\
st.wHour,st.wMinute,\
st.wSecond);
return szTime;
}
/*-----------------------------------------------------------------------
* 功能: 得到安装路径
*
* 参数:
*
* 返回: 安装路径字符串
*
* 备注:
-----------------------------------------------------------------------*/
std::string GetSetupPath()
{
char szFilePath[512] = {0};
::GetModuleFileNameA(NULL, szFilePath, 512);
std::string strFile = szFilePath;
/*int nCount = strFile.Find(_T('.'));
strFile = strFile.Left(nCount);*/
// 从后向前扫描,当发现\停止
int i = 0;
for(i = strFile.length() - 1; i >=0; i--)
{
if (strFile.at(i) == _T('.'))
break;
}
strFile = strFile.substr(0,i);
i = 0;
for(i = strFile.length() - 1; i >= 0 ; i--)
{
if (strFile.at(i) == _T('\\'))
break;
}
strFile = strFile.substr(0,i);
return strFile;
}
//////////////////////////////////////////////////////////////////////////
//unsigned long LLog::ulIncrease = 0;
//boost::mutex LLog::m_mutex;
LLog::LLog(int level_)
{
memset(m_szContext,0,LOG_CONTEXT_LENGTH);
memset(m_szDateTime,0,LOG_DATE_LENGTH);
SYSTEMTIME st;
GetLocalTime(&st);
#ifdef USE_WCHAR
GetLocalTime(&st);
wsprintfW(m_szDateTime,_T("%04d-%02d-%02d %02d:%02d:%02d:%04d"),\
st.wYear,st.wMonth,st.wDay,\
st.wHour,st.wMinute,\
st.wSecond,\
st.wMilliseconds);
#else
wsprintfA(m_szDateTime,("%04d-%02d-%02d %02d:%02d:%02d:%04d"),\
st.wYear,st.wMonth,st.wDay,\
st.wHour,st.wMinute,\
st.wSecond,\
st.wMilliseconds);
#endif
m_nThreadID = GetCurrentThreadId();
m_nLevel = level_;
m_nID = 0;
}
LLog::~LLog(void)
{
/*char buf[255] = {0};
wsprintfA(buf,"~LLog : %x,ID : %d\n",this,m_nID);
OutputDebugStringA(buf);*/
}
std::string LLog::toString()
{
char buf[512] = {0};
wsprintfA(buf,"%s [%05d] %s",m_szDateTime,m_nThreadID,m_szContext);
std::string str = buf;
return str;
}
//////////////////////////////////////////////////////////////////////////
LLogger* LLogger::pInstance = NULL;
unsigned long LLogger::m_ulIncrease = 0;
/*-----------------------------------------------------------------------
* 功能: 初始化日志系统
*
* 参数: [param_] 相关日志参数
*
* 返回:
*
* 备注:
-----------------------------------------------------------------------*/
void LLogger::init( log_param* param_ /*= NULL*/ )
{
if (param_ != NULL)
{
m_param.log_file_size = param_->log_file_size;
m_param.log_max_store_days = param_->log_max_store_days;
m_param.log_watermark_high = param_->log_watermark_high;
m_param.bAllDestStore_ = param_->bAllDestStore_;
m_param.bOpenConsole_ = param_->bOpenConsole_;
m_param.bOpenDebugout_ = param_->bOpenDebugout_;
#ifdef USE_WCHAR
_wcsncpy_l(m_param.log_filename_prefix,param_->log_filename_prefix);
_wcsncpy_l(m_param.log_file_ext,param_->log_file_ext);
#else
strcpy_s(m_param.log_filename_prefix,param_->log_filename_prefix);
strcpy_s(m_param.log_file_ext,param_->log_file_ext);
#endif
}
// 避免其自适应大小而影响效率
m_vcLogList.reserve(m_param.log_watermark_high + 10);
//m_Assistant = (HANDLE)_beginthreadex(NULL,0,assistant,PVOID(this),0,NULL);
m_Assistant = LThreadPool::GetInstance()->GetFreeThread();
if(m_Assistant)
{
m_Assistant->m_pFun = assistant;
m_Assistant->m_pParam = (LPVOID)this;
m_Assistant->Start();
}
OutputDebugStringA("[LTools]==> LLogger is initialization success!\n");
/*try
{
m_Assistant.detach();
}
catch (std::exception* e)
{
OutputDebugStringA(e->what());
}
*/
}
/*-----------------------------------------------------------------------
* 功能: 删除过期的日志文件
*
* 参数:
*
* 返回:
*
* 备注:
-----------------------------------------------------------------------*/
void LLogger::deleteLogs()
{
// 处理保存的最大天数
int nLogMaxCount = m_param.log_max_store_days;
if (nLogMaxCount < 0 || nLogMaxCount > 31)
return;
int nCurCount = scanfLogs();
std::string strPath = GetSetupPath();
strPath += "\\log\\";
// 以今天为最后一天向前推算到最大保存天数的那天
for(int i = 0; i < nCurCount - nLogMaxCount; i++)
{
std::string strLogFilepath = strPath + m_vcLogFileList.at(i);
DeleteFileA(strLogFilepath.c_str());
}
// 重新扫描日志文件
m_vcLogFileList.clear();
scanfLogs();
}
/*-----------------------------------------------------------------------
* 功能: 扫描日志文件
*
* 参数:
*
* 返回:
*
* 备注:
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Logger.zip (2个子文件)
LLog.h 5KB
LLog.cpp 13KB
共 2 条
- 1
李天明
- 粉丝: 2
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页