⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logger.h

📁 这是本人在工作中积累的VC++类库
💻 H
字号:
/************************************************************************
模快名:		moxu 公共类库
功能:		日志打印
完成日期:	2007-10-20
作者:		许 培 Xu Pei(Email/MSN: peimoxu@163.com)

本代码可以自由使用,但因使用本代码造成的后果,本人不承担任何责任
************************************************************************/

#pragma once

#include <vector>
#include <fstream>
#include "CriticalSection.h"
#include "Thread.h"
#include "TString.h"

namespace moxu
{


namespace detail
{

	class ILog
	{
	protected:
		TString m_caption;
		bool m_bCreated;

	public:
		ILog() : m_bCreated(false){ };
		virtual bool Create() = 0;
		virtual void Close() = 0;
		virtual void Print(const TString& str) = 0;
		virtual void SetCaption(const TString& str) = 0;
	};

	class FileLog : public ILog
	{
	private:
#ifdef _UNICODE
		std::wofstream m_file;
#else
		std::ofstream m_file;
#endif
		TString m_fileName;

	public:
		virtual bool Create();
		virtual void Close();
		virtual void Print(const TString& msg);
		virtual void SetCaption(const TString& str);
		TString GetFileName(){ return m_fileName; }
	};

	class WndLog : public ILog
	{
	private:
		HWND m_hWnd;
		HWND m_hList;

	public:
		virtual bool Create();
		virtual void Close();
		virtual void Print(const TString& msg);
		virtual void SetCaption(const TString& str);

	private:
		static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
	};

}

class Logger
{
private:
	moxu::CriticalSection m_cs;	//lock m_strList
	moxu::CriticalSection m_cs2;	//lock m_caption
	moxu::Thread m_thread;
	moxu::Semaphore m_semaphore;
	bool m_bFileLog, m_bWndLog;
	bool m_bInfo, m_bDebug, m_bError;
	detail::ILog *m_pFileLog, *m_pWndLog;
	std::vector<TString> m_strList;
	TString m_caption;

public:
	Logger(void);
	~Logger(void);

	bool Create();
	void Close();

	void Info(LPCTSTR str, ...);
	void Debug(LPCTSTR str, ...);
	void Error(LPCTSTR str, ...);

	bool IsAddInfo(){ return m_bInfo; }
	bool IsAddDebug(){ return m_bDebug; }
	bool IsAddError(){ return m_bError; }

	void SetCaption(const TString& str);

private:
	void RunThread();
	void AddLog(LPCTSTR str, va_list args);
	void Print();
	void CheckConfig();
};

};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -