The instance of singleton in application of Tracing-Files

本文介绍了一种在MANETSimulator中使用Singleton设计模式来创建LogFile类,以简化日志记录过程并避免重复实例化。通过静态成员变量实现日志文件的共享,使得在不同部分调用日志记录功能时,无需多次创建LogFile对象。举例展示了如何在程序中使用此设计模式进行错误日志、运行日志等记录。

      在写 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


: Connection released: [id: 0][route: {}->http://192.168.12.59:6041][total kept alive: 1; route allocated: 1 of 20; total allocated: 1 of 200] 2025-08-29 09:54:39.783 DEBUG 21556 --- [ main] org.mybatis.spring.SqlSessionUtils : Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@68212585] 2025-08-29 09:54:39.925 DEBUG 21556 --- [ main] o.s.b.f.xml.XmlBeanDefinitionReader : Loaded 11 bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml] 2025-08-29 09:54:39.933 DEBUG 21556 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'DB2' 2025-08-29 09:54:39.965 DEBUG 21556 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'Derby' 2025-08-29 09:54:39.968 DEBUG 21556 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'H2' 2025-08-29 09:54:39.970 DEBUG 21556 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'HDB' 2025-08-29 09:54:39.977 DEBUG 21556 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'HSQL' 2025-08-29 09:54:39.979 DEBUG 21556 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'Informix' 2025-08-29 09:54:39.980 DEBUG 21556 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'MS-SQL' 2025-08-29 09:54:39.983 DEBUG 21556 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'MySQL' 2025-08-29 09:54:39.985 DEBUG 21556 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'Oracle' 2025-08-29 09:54:39.988 DEBUG 21556 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'PostgreSQL' 2025-08-29 09:54:39.991 DEBUG 21556 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'Sybase' 2025-08-29 09:54:39.994 DEBUG 21556 --- [ main] o.s.jdbc.support.SQLErrorCodesFactory : Looking up default SQLErrorCodes for DataSource [com.depower.datasource.config.DynamicDataSource@3596a9d5] 2025-08-29 09:54:39.994 DEBUG 21556 --- [ main] o.s.jdbc.datasource.DataSourceUtils : Fetching JDBC Connection from DataSource 2025-08-29 09:54:39.997 DEBUG 21556 --- [ main] o.s.jdbc.support.SQLErrorCodesFactory : SQL error codes for 'TDengine' not found 2025-08-29 09:54:39.998 DEBUG 21556 --- [ main] o.s.jdbc.support.SQLErrorCodesFactory : Caching SQL error codes for DataSource [com.depower.datasource.config.DynamicDataSource@3596a9d5]: database product name is 'TDengine' 2025-08-29 09:54:40.007 DEBUG 21556 --- [ main] s.j.s.SQLErrorCodeSQLExceptionTranslator : Unable to translate SQLException with Error code '537', will now try the fallback translator 2025-08-29 09:54:40.020 DEBUG 21556 --- [ main] c.d.datasource.aspect.DataSourceAspect : clean datasource
08-30
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值