The instance of singleton in application of Tracing-Files
来源:互联网 发布:csgo手机菠菜软件 编辑:程序博客网 时间:2024/04/29 16:00
在写 MANET Simulator 时,为了记录 trace 文件,我用了一个 LogFile 的类,这个类为了使用简便,而不必在每个使用日志的类中都建立一个LogFile对象,最好的办法就是把这个LogFile类设计成 Singleton.
具体如下:
1)--------------------------------------- 头文件:
#ifndef _LOG_H_
#define _LOG_H_
#include <iostream>
#include <fstream>
class LogFile
{
private:
LogFile();
~LogFile();
LogFile( const LogFile& ); // Prevent copy-construction.
public:
static LogFile* instance();
private:
static LogFile* pObjLog;
void CloseFiles();
public:
// 1 Error log file, record the errors and warnings.
static std::ofstream m_ofErrLog;
// 2 Running log file, record the running results and hints.
static std::ofstream m_ofRunLog;
// 3 Forwarding log file, record the forwarding actions.
static std::ofstream m_ofForwardLog;
// 4 Location log file, record the nodes' moving actions and locations.
static std::ofstream m_ofLocLog;
// 5 MN's SIR_state log file, record the MN' SIR states.
static std::ofstream m_ofSIRLog;
// 6 Record the Percolation Probability of all MNs.
static std::ofstream m_ofPPLog;
};
#endif
2)--------------------------------------- cpp文件:
#include "..\Log\Log.h"
std::ofstream LogFile::m_ofErrLog("NS_ErrLog.txt");
std::ofstream LogFile::m_ofRunLog("NS_RunLog.txt");
std::ofstream LogFile::m_ofForwardLog("NS_ForwardingLog.txt");
std::ofstream LogFile::m_ofLocLog("NS_LocationLog.txt");
std::ofstream LogFile::m_ofSIRLog("NS_SIRLog.txt");
std::ofstream LogFile::m_ofPPLog( "Trace\\PercoPr.tr", std::ios::app );
LogFile* LogFile::pObjLog = new LogFile();
LogFile::LogFile()
{
}
LogFile::~LogFile()
{
CloseFiles();
if ( NULL != pObjLog )
{
delete pObjLog;
pObjLog = NULL;
}
}
LogFile* LogFile::instance()
{
if ( NULL != pObjLog )
{
return pObjLog;
}
return NULL;
}
void LogFile::CloseFiles()
{
m_ofErrLog.close();
m_ofRunLog.close();
m_ofForwardLog.close();
m_ofLocLog.close();
m_ofSIRLog.close();
m_ofPPLog.close();
}
这样,只要包含了头文件,就可以在其他地方使用静态trace文件记录trace信息了,如:
LogFile::instance()->m_ofErrLog << "Error: the simulate time can not be negative.\n";
或 LogFile::instance()->m_ofRunLog << "\n ----------- The NS begins running now. ----------\n";
整理至此,以便备忘。
Davy 2012_03_15_21:10
- The instance of singleton in application of Tracing-Files
- detect the encoding of files in Python
- The instance of keyword
- An easy instance of using the template class in C++
- The study of Class and Instance in Python(20170918)
- Main application must be in the list of application paths
- Three ways of implementing the Singleton Pattern in Java
- customize the template of new files in vs.net 2003
- the application of pointer
- THE compile of serveral files
- 学习记录: Server Error in '/' Application. Object reference not set to an instance of an object
- the diary for one command application of pipe in Linux
- The application of the static
- Talk of computer technology in the enterprise management of the existing documents and file the application of three phase
- which returns the pathnames of the files (or links) which would be executed in the currente
- Resist the Temptation of the Singleton Pattern
- Resist the Temptation of the Singleton Pattern
- How many files in the "res" of the famous IM app.
- 测试 PSeq
- 消息队列
- Webconfig配置和读取
- 轻松解决FirePHP乱码问题
- iPhone程序开发 图形和动画入门指南
- The instance of singleton in application of Tracing-Files
- SQL1015N The database is in an inconsistent state. SQLSTATE=55025
- Undefined exploded archive location 项目不能部署
- RMQ求区间最值
- MDA开发三个阶段
- Android fill_parent和wrap_content分析
- 1101 The Game
- 无法解决 equal to 操作中 "SQL_Latin1_General_CP1_CI_AS" 和 "Chinese_PRC_CI_AS" 之间
- ubuntu apache2 修改网页端口