OutputAgentObject:用于Log輸出的對(duì)象
Log的輸出問題是程序設(shè)計(jì)需要在設(shè)計(jì)初期就要計(jì)劃的一個(gè)模塊,其接口和功能的設(shè)計(jì),會(huì)直接影響整體程序的調(diào)試和信息輸出等方面。
wxDeMPQ在初期,計(jì)劃通過其StatusBar作為L(zhǎng)og輸出,并簡(jiǎn)單封裝了一個(gè)函數(shù),但在實(shí)際使用中,由于急于實(shí)現(xiàn)功能和DLL等的引入,加上VC方便的調(diào)試,導(dǎo)致完全廢棄了當(dāng)初的想法,這也是的wxDeMPQ在后期實(shí)現(xiàn)上沒有任何的Log輸出,這對(duì)于Debug的程序來說,是--災(zāi)難和活該。
對(duì)于類似Log輸出的模塊,在實(shí)現(xiàn)時(shí)需要考慮的是使用時(shí)的易融合性和靈活性。簡(jiǎn)單地說,一來此模塊的引入不應(yīng)影響原代碼的執(zhí)行;二來要考慮信息輸出的不確定性,就是說你可能無法預(yù)知程序會(huì)最終通過什么方式輸出,文件?Console?或者一個(gè)EditCtrl?這兩點(diǎn)我認(rèn)為是需要在初期著重考慮的。
下面是一個(gè)簡(jiǎn)單Log輸出模塊的實(shí)現(xiàn),通過Adapter方式,連接輸入和輸出。
下面是一段測(cè)試代碼,用于展示如何使用。
簡(jiǎn)單實(shí)現(xiàn),很多因素并不考慮,如Thread Safe問題等,源碼和測(cè)試代碼在這里。測(cè)試程序模樣如下:

wxDeMPQ在初期,計(jì)劃通過其StatusBar作為L(zhǎng)og輸出,并簡(jiǎn)單封裝了一個(gè)函數(shù),但在實(shí)際使用中,由于急于實(shí)現(xiàn)功能和DLL等的引入,加上VC方便的調(diào)試,導(dǎo)致完全廢棄了當(dāng)初的想法,這也是的wxDeMPQ在后期實(shí)現(xiàn)上沒有任何的Log輸出,這對(duì)于Debug的程序來說,是--災(zāi)難和活該。
對(duì)于類似Log輸出的模塊,在實(shí)現(xiàn)時(shí)需要考慮的是使用時(shí)的易融合性和靈活性。簡(jiǎn)單地說,一來此模塊的引入不應(yīng)影響原代碼的執(zhí)行;二來要考慮信息輸出的不確定性,就是說你可能無法預(yù)知程序會(huì)最終通過什么方式輸出,文件?Console?或者一個(gè)EditCtrl?這兩點(diǎn)我認(rèn)為是需要在初期著重考慮的。
下面是一個(gè)簡(jiǎn)單Log輸出模塊的實(shí)現(xiàn),通過Adapter方式,連接輸入和輸出。
#pragma once
#include <string>
#ifdef OUTPUTAGENTLIBRARY_EXPORTS
#define OUTPUTAGENTLIBRARY_API __declspec(dllexport)
#else
#define OUTPUTAGENTLIBRARY_API __declspec(dllimport)
#endif
namespace OUTPUT
{
class OUTPUTAGENTLIBRARY_API COutputBase
{
public:
COutputBase() {}
virtual ~COutputBase() {}
virtual void Output(const std::wstring& info) = 0;
};
const unsigned int LEVEL_DEBUG = 1;
const unsigned int LEVEL_INFO = 2;
const unsigned int LEVEL_WARN = 4;
const unsigned int LEVEL_ERROR = 8;
const unsigned int LEVEL_ALL = 0xFFFFFFFF;
class OUTPUTAGENTLIBRARY_API COutputAgentObject
{
public:
COutputAgentObject();
virtual ~COutputAgentObject();
void SetLevel(unsigned int level);
unsigned int GetLevel() const;
void SetOutput(COutputBase* output);
COutputBase * GetOutput() const;
void Output(unsigned int level, const std::wstring& info);
protected:
COutputBase* _output;
unsigned int _level;
};
}
//#define OUTPUT(outputptr, level, info) \
//{\
// if(outputptr != NULL) \
// {\
// std::wostringstream ostr; \
// ostr << info; \
// outputptr->Output(level, ostr.c_str()); \
// }\
//}
#include <string>
#ifdef OUTPUTAGENTLIBRARY_EXPORTS
#define OUTPUTAGENTLIBRARY_API __declspec(dllexport)
#else
#define OUTPUTAGENTLIBRARY_API __declspec(dllimport)
#endif
namespace OUTPUT
{
class OUTPUTAGENTLIBRARY_API COutputBase
{
public:
COutputBase() {}
virtual ~COutputBase() {}
virtual void Output(const std::wstring& info) = 0;
};
const unsigned int LEVEL_DEBUG = 1;
const unsigned int LEVEL_INFO = 2;
const unsigned int LEVEL_WARN = 4;
const unsigned int LEVEL_ERROR = 8;
const unsigned int LEVEL_ALL = 0xFFFFFFFF;
class OUTPUTAGENTLIBRARY_API COutputAgentObject
{
public:
COutputAgentObject();
virtual ~COutputAgentObject();
void SetLevel(unsigned int level);
unsigned int GetLevel() const;
void SetOutput(COutputBase* output);
COutputBase * GetOutput() const;
void Output(unsigned int level, const std::wstring& info);
protected:
COutputBase* _output;
unsigned int _level;
};
}
//#define OUTPUT(outputptr, level, info) \
//{\
// if(outputptr != NULL) \
// {\
// std::wostringstream ostr; \
// ostr << info; \
// outputptr->Output(level, ostr.c_str()); \
// }\
//}
#include "stdafx.h"
#include "OutputAgentObject.h"
namespace OUTPUT
{
COutputAgentObject::COutputAgentObject()
: _output(NULL), _level(LEVEL_ALL)
{
}
COutputAgentObject::~COutputAgentObject()
{
}
void COutputAgentObject::SetLevel(unsigned int level)
{
_level = level;
}
unsigned int COutputAgentObject::GetLevel() const
{
return _level;
}
void COutputAgentObject::SetOutput(COutputBase* output)
{
_output = output;
}
COutputBase* COutputAgentObject::GetOutput() const
{
return _output;
}
void COutputAgentObject::Output(unsigned int level, const std::wstring& info)
{
if(_output == NULL)
return;
if((level & _level) == 0)
return;
_output->Output(info);
}
}
#include "OutputAgentObject.h"
namespace OUTPUT
{
COutputAgentObject::COutputAgentObject()
: _output(NULL), _level(LEVEL_ALL)
{
}
COutputAgentObject::~COutputAgentObject()
{
}
void COutputAgentObject::SetLevel(unsigned int level)
{
_level = level;
}
unsigned int COutputAgentObject::GetLevel() const
{
return _level;
}
void COutputAgentObject::SetOutput(COutputBase* output)
{
_output = output;
}
COutputBase* COutputAgentObject::GetOutput() const
{
return _output;
}
void COutputAgentObject::Output(unsigned int level, const std::wstring& info)
{
if(_output == NULL)
return;
if((level & _level) == 0)
return;
_output->Output(info);
}
}
下面是一段測(cè)試代碼,用于展示如何使用。
#pragma once
#include <string>
#include <sstream>
#include "wx/wx.h"
#include "OutputAgentObject.h"
class COutput : public OUTPUT::COutputBase
{
public:
COutput(wxTextCtrl* text)
:_text(text)
{
}
virtual ~COutput() {}
virtual void Output(const std::wstring& info)
{
if(_text != NULL)
{
_text->AppendText(WString2wxString(info));
}
}
protected:
const wxString WString2wxString(const std::wstring& str)
{
return wxString(str.c_str(), wxConvISO8859_1);
}
private:
wxTextCtrl* _text;
};
OUTPUT::COutputAgentObject g_stOutputAgent;
#define OUTPUT(level, info) \
{\
std::wostringstream ostr; \
ostr << info; \
g_stOutputAgent.Output(level, ostr.str());\
}
#include <string>
#include <sstream>
#include "wx/wx.h"
#include "OutputAgentObject.h"
class COutput : public OUTPUT::COutputBase
{
public:
COutput(wxTextCtrl* text)
:_text(text)
{
}
virtual ~COutput() {}
virtual void Output(const std::wstring& info)
{
if(_text != NULL)
{
_text->AppendText(WString2wxString(info));
}
}
protected:
const wxString WString2wxString(const std::wstring& str)
{
return wxString(str.c_str(), wxConvISO8859_1);
}
private:
wxTextCtrl* _text;
};
OUTPUT::COutputAgentObject g_stOutputAgent;
#define OUTPUT(level, info) \
{\
std::wostringstream ostr; \
ostr << info; \
g_stOutputAgent.Output(level, ostr.str());\
}
_output = new COutput(m_textOutput);
g_stOutputAgent.SetOutput(_output);
OUTPUT(OUTPUT::LEVEL_INFO, L"Level:" << g_stOutputAgent.GetLevel() << std::endl);
g_stOutputAgent.SetOutput(_output);
OUTPUT(OUTPUT::LEVEL_INFO, L"Level:" << g_stOutputAgent.GetLevel() << std::endl);
簡(jiǎn)單實(shí)現(xiàn),很多因素并不考慮,如Thread Safe問題等,源碼和測(cè)試代碼在這里。測(cè)試程序模樣如下:
posted on 2009-06-09 10:52 codejie 閱讀(260) 評(píng)論(0) 編輯 收藏 引用 所屬分類: C++