一个写日志的类
来源:互联网 发布:雷查尔斯 知乎 编辑:程序博客网 时间:2024/04/29 23:35
使用方法,创建一个CMyLog的对象,调用WriteLog函数,即可进行日志记录工作
头文件
#pragma once
class CMyLog
{
public:
CMyLog(void);
~CMyLog(void);
// 默认的日志文件路径
private:
CString m_strLogPath;
// 获得日志文件路进
void GetLogPath(void);
public:
void WriteLog(CString LogInfo, CString strLogFilePath = NULL);
};
实现文件
#include "StdAfx.h"
#include "./mylog.h"
const unsigned int PATH_LENGTH = 512;
CMyLog::CMyLog(void)
: m_strLogPath(_T(""))
{
GetLogPath();
}
CMyLog::~CMyLog(void)
{
}
// 获得日志文件路进
void CMyLog::GetLogPath(void)
{
TCHAR path[PATH_LENGTH] = {0};
HMODULE hModule = ::GetModuleHandle(NULL);
int length = ::GetModuleFileName(hModule,path,sizeof(path)/sizeof(TCHAR));
m_strLogPath = path;
int Index = m_strLogPath.ReverseFind('//');
m_strLogPath = m_strLogPath.Left(Index);
Index = m_strLogPath.ReverseFind('//');
m_strLogPath = m_strLogPath.Left(Index);
m_strLogPath += "//Log";
CFileFind fileFind;
if(fileFind.FindFile(m_strLogPath)== 0)
{
::CreateDirectory(m_strLogPath,NULL);
}
m_strLogPath = m_strLogPath + "//log.txt";
}
void CMyLog::WriteLog(const CString LogInfo, const CString strLogFilePath)
{
CStdioFile file;
CString strLogInfo;
CTime time = CTime::GetCurrentTime();
strLogInfo.Format("%04d-%02d-%02d %02d:%02d:%02d --- ",time.GetYear(),time.GetMonth(),time.GetDay(),time.GetHour(),time.GetMinute(),time.GetSecond());
if(strLogFilePath.IsEmpty())
{
file.Open(m_strLogPath,CStdioFile::modeNoTruncate|CStdioFile::modeCreate|CStdioFile::modeReadWrite|CStdioFile::shareDenyWrite);
}
else
{
file.Open(strLogFilePath,CStdioFile::modeNoTruncate|CStdioFile::modeCreate|CStdioFile::modeReadWrite|CStdioFile::shareDenyWrite);
}
strLogInfo = "/r/n" + strLogInfo + LogInfo;
file.SeekToEnd();
file.WriteString(strLogInfo);
file.Close();
}
- 一个写日志的类
- 一个写日志的类
- 一个简单的c++写日志类
- 一个简单的c++写日志类
- 一个写日志的方法
- C#2005 一个简单的写日志类
- 自己写的一个C#日志管理类
- 重新写了一个简单的日志类
- 一个简单的.net写日志方法
- c++ 多线程写日志的一个很实用的日志类源码(支持 c++ builder)
- 自己写的写日志类
- 自己写的一个基于C++的跨平台的轻量级日志类
- C++写的一个小的日志系统
- 写一个Windows(可扩展LINUX)下记录本地日志的类
- 一个简单的日志类,将打印信息写到文件
- 一个用hibernate+struts写的简单日志系统
- delphi怎样写一个程序的日志啊?
- linux写一个监控日志,发邮件的shell
- RED-BLACK TREES
- 嵌入式设备上的 Linux 系统开发
- java的static用法
- Linux 安装软件方法
- 关于eMule服务器
- 一个写日志的类
- 一个高人的自述
- CodeSmith使用基础教程 一 — 模板
- CodeSmith使用基础教程 二 — 语法、标签
- 学习下Django Step by Step (七)
- 反射
- CodeSmith使用基础教程 三 — 语法与对象
- 我今天对那个定餐系统做的分析
- 嵌入式linux启动信息完全注释