#ifdef _DEBUG
#pragma comment(lib, "aced.lib")
#else
#pragma comnent(lib, "ace.lib")
#endif
#include <ace/Log_Msg.h>
#include <ace/log_Msg_Callback.h>
#include <ace/Log_Record.h>
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
using namespace std;
class LogFile_Callback: public ACE_Log_Msg_Callback
{
public:
LogFile_Callback(string strInitLogFileName)
{
m_iFileCount = 0;
m_currSize = 0;
m_maxSize = 1024*1024;
m_strInitLogFileName = strInitLogFileName;
m_currFile = GetFile();
m_os.open(m_currFile.c_str());
}
void log(ACE_Log_Record &log_record);
private:
int m_iFileCount;
int m_currSize;
int m_maxSize;
string m_strInitLogFileName;
string m_currFile;
ofstream m_os;
private:
string GetFile()
{
//日志明增长操作
ostringstream out;
m_iFileCount+=1;
out<<m_strInitLogFileName<<m_iFileCount<<".log";
return out.str();
}
};
void LogFile_Callback::log(ACE_Log_Record &log_record)
{
ostringstream os;
os<< log_record.msg_data();
m_os.clear();
m_os << os.str();
m_os.flush();
// 记录成功再累积
if (m_os.good())
{
m_currSize += os.str().length(); // 同时计算写入长度
}
if(m_currSize >= m_maxSize)
{// 如果超出规定尺寸,更换文件
m_os.close();
m_currFile = GetFile();
m_os.open(m_currFile.c_str());
m_currSize = 0; // 当前尺寸归零
}
}
int main(int argc, char* argv[])
{
LogFile_Callback * pLogFile_Callback = new LogFile_Callback("testAceLog");
ACE_LOG_MSG->set_flags (ACE_Log_Msg::MSG_CALLBACK);
ACE_LOG_MSG->msg_callback (pLogFile_Callback);
ACE_DEBUG((LM_DEBUG, "hello world "));
return 0;
}
我的第一篇ace 学习笔记。就以acelog开始吧。
现在我没能实现直接的输出到流文件。只能曲线的用callback 实现了。这样也有他的优点。
希望谁用ofsteam 实现了回复一下。 急求。
用一个单模式 再加些临界区控制。就是个很好的西安城工具。