/*************************
*file: JW_Log_Msg.h
*autor: Clark/陈泽丹
*time: 20120624
*describe: 日志类
*************************/
#pragma once
#include <string.h>
#define START_JW_LOG_MSG JW_LOG_NAMESPACE::JW_Log_Msg::Init()
#define JW_LOG_MSG JW_LOG_NAMESPACE::JW_Log_Msg::instance()
#define END_JW_LOG_MSG JW_LOG_NAMESPACE::JW_Log_Msg::UnInit()
#define JW_MSG(X) JW_LOG_MSG->log(JW_LOG_NAMESPACE::JW_LM_MSG, X)
#define JW_TRACE(X) JW_LOG_MSG->log(JW_LOG_NAMESPACE::JW_LM_TRACE, X)
#define JW_DEBUG(X) JW_LOG_MSG->log(JW_LOG_NAMESPACE::JW_LM_DEBUG, X)
#define JW_ERROR(X) JW_LOG_MSG->log(JW_LOG_NAMESPACE::JW_LM_ERROR, X)
namespace JW_LOG_NAMESPACE
{
enum JW_Log_Priority
{
JW_LM_NO = 0,
JW_LM_MSG = 1,
JW_LM_TRACE = 2,
JW_LM_DEBUG = 4,
JW_LM_ERROR = 8,
};
enum JW_Log_Result
{
JW_S_OK = 0,
JW_E_FAILT = 1,
};
//日志信息
class JW_Log_Record
{
public:
JW_Log_Record(JW_Log_Priority _lm, const char* _msg);
JW_Log_Priority m_lm;
enum { BUF_LEN = 2048};
char m_msg[BUF_LEN];
private:
JW_Log_Record(const JW_Log_Record& _r){}
JW_Log_Record& operator =(JW_Log_Record& _r){ return *this; }
};
//日志反应器
class JW_Log_Msg_Callback
{
public:
virtual ~JW_Log_Msg_Callback (void){}
private:
//日志处理函数
virtual void log(JW_Log_Record &log_record) = 0;
friend class JW_Log_Msg;
};
//日志分离器
class JW_Log_Msg
{
public:
static void Init();
static JW_Log_Msg* instance();
static void UnInit();
public:
//设置日志信息开关
int set_priority(int _log_priority);
//设置日志处理器
JW_Log_Msg_Callback* set_msg_callback(JW_Log_Msg_Callback* _p_msg_callback);
//记录日志信息
JW_Log_Result log(JW_Log_Priority _log_priority, const char* _str);
private:
JW_Log_Msg();
virtual ~JW_Log_Msg();
static JW_Log_Msg* g_p_jw_log_msg;
int m_log_priority;
JW_Log_Msg_Callback* m_p_msg_callback;
};
}
#include "stdafx.h"
#include "JW_Log_Msg.h"
using namespace JW_LOG_NAMESPACE;
JW_Log_Record::JW_Log_Record(JW_Log_Priority _lm, const char* _msg)
{
m_lm = _lm;
strcpy(m_msg, "");
int len = strlen(_msg);
len = (len >= BUF_LEN) ? BUF_LEN-1 : len;
strncpy(m_msg, _msg, len);
m_msg[len+1] = '\0';
}
JW_Log_Msg::JW_Log_Msg()
{
m_log_priority = JW_LM_NO;
m_p_msg_callback = 0;
}
JW_Log_Msg::~JW_Log_Msg()
{
m_log_priority = JW_LM_NO;
m_p_msg_callback = 0;
}
int JW_Log_Msg::set_priority(int _log_priority)
{
int old = this->m_log_priority;
this->m_log_priority = _log_priority;
return old;
}
JW_Log_Msg_Callback* JW_Log_Msg::set_msg_callback(JW_Log_Msg_Callback* _p_msg_callback)
{
JW_Log_Msg_Callback *old = this->m_p_msg_callback;
this->m_p_msg_callback = _p_msg_callback;
return old;
}
JW_Log_Result JW_Log_Msg::log(JW_Log_Priority _log_priority, const char* _str)
{
if( (_log_priority & this->m_log_priority) && 0 != this->m_p_msg_callback )
{
JW_Log_Record record(_log_priority, _str);
this->m_p_msg_callback->log(record);
return JW_S_OK;
}
return JW_E_FAILT;
}
JW_Log_Msg* JW_Log_Msg::g_p_jw_log_msg = 0;
void JW_Log_Msg::Init()
{
g_p_jw_log_msg = new JW_Log_Msg();
}
JW_Log_Msg* JW_Log_Msg::instance()
{
return g_p_jw_log_msg;
}
void JW_Log_Msg::UnInit()
{
delete g_p_jw_log_msg;
}