Chinaunix首页 | 论坛 | 博客
  • 博客访问: 56562
  • 博文数量: 16
  • 博客积分: 306
  • 博客等级: 二等列兵
  • 技术积分: 162
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-20 15:08
文章分类

全部博文(16)

文章存档

2013年(1)

2012年(15)

我的朋友

分类: C/C++

2012-12-04 20:02:35

在 Singleton 模式中,通过维护一个 static 的成员变量来记录这个唯一的对象实例。通过提供一个 staitc 的接口 instance 来获得这个唯一的实例。


点击(此处)折叠或打开

  1. /*
  2. * Logger.h
  3. */
  4. #include <iostream>
  5. #include <fstream>
  6. #include <vector>

  7. using namespace std ;

  8. class Logger {
  9. public:
  10.      static const string kLogLevelDebug ;
  11.      static const string kLogLevelInfo ;
  12.      static const string kLogLeveleError ;

  13.      //Log a single message
  14.      /* static void log( const string& inMessage , */
  15.      /* const string& inLogLevel ) ; */
  16.      /* static void log( const vector<string>& inMessage , */
  17.      /* const string& inLogLevel ) ; */
  18.      // static void teardown() ;

  19.      void log( const string& inMessage ,
  20.                const string& inLogLevel ) ;
  21.      void log( const vector<string>& inMessage ,
  22.                const string& inLogLevel ) ;
  23.      //return a reference to the singleton object
  24.      static Logger& instance() ;

  25. protected:
  26.      //static void init() ;
  27.      //static bool isInited ;
  28.      static Logger sInstance ;

  29.      static const char* const kLogFileName ;
  30.      //Data member for the output stream
  31.      ofstream sOutputStream ;

  32. private:
  33.      Logger() ;
  34.      ~Logger() ;
  35. } ;

 点击(此处)折叠或打开

  1. /*
  2. * Logger.cpp
  3. */

  4. #include <string>
  5. #include "Logger.h"

  6. using namespace std ;


  7. const string Logger::kLogLevelDebug = "DEBUG" ;
  8. const string Logger::kLogLevelInfo = "INFO" ;
  9. const string Logger::kLogLeveleError = "ERROR" ;

  10. const char* const Logger::kLogFileName = "log.out" ;

  11. Logger Logger::sInstance ;

  12. void Logger::log( const string& inMessage ,
  13.                   const string& inLogLevel ) {
  14.     // if ( !isInited ) {
  15.     // init() ;
  16.     // }
  17.     sOutputStream << inLogLevel << " " << inMessage << endl ;
  18. }

  19. void Logger::log( const vector<string>& inMessage ,
  20.                   const string& inLogLevel ){
  21.     for ( size_t i = 0 ; i < inMessage.size() ; i  ) {
  22.         log( inMessage[i] , inLogLevel ) ;
  23.     }
  24. }

  25. // void Logger::teardown() {
  26. // if (isInited ) {
  27. // sOutputStream.close() ;
  28. // isInited = false ;
  29. // }
  30. // }


  31. // void Logger::init() {
  32. // if ( !isInited ) {
  33. // sOutputStream.open(kLogFileName , ios_base::app ) ;
  34. // if ( !sOutputStream.good() ) {
  35. // cerr << "Unable to initialied the Logger !" << endl ;
  36. // return ;
  37. // }
  38. // isInited = true ;
  39. // }
  40. // }


  41. Logger& Logger::instance() {
  42.     return sInstance ;
  43. }

  44. Logger::~Logger() {
  45.     sOutputStream.close() ;
  46. }

  47. Logger::Logger() {
  48.     sOutputStream.open( kLogFileName , ios_base::app) ;
  49.     if ( !sOutputStream.good() ) {
  50.         cerr << "Unable to initialied the Logger !" << endl ;
  51.     }
  52. }

点击(此处)折叠或打开

  1. /*
  2. * Test.cpp
  3. */

  4. #include "Logger.h"
  5. #include <vector>
  6. #include <string>
  7. #include <fstream>
  8. using namespace std ;


  9. int main( int agrc , char** argv ) {
  10.     Logger::instance().log("Test Message" , Logger::kLogLevelDebug) ;
  11.     vector<string> items ;
  12.     items.push_back( "item1" ) ;
  13.     items.push_back( "item2" ) ;

  14.     Logger::instance().log( items , Logger::kLogLeveleError ) ;
  15.     return 0 ;
  16. }

阅读(818) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~