source: XIOS3/branches/xios-3.0-beta/src/log.hpp @ 2525

Last change on this file since 2525 was 2341, checked in by ymipsl, 2 years ago

Implement new info log method.
Log must be now activated more selectivelly the by level using variable "log_type" key in the xios context.
ex :

<variable id="log_type" type="string">log_protocol</variable>

In code new object CLogType must be created and will be feed as argument to the info object :
ex :
static CLogType logProtocol("log_protocol") ;
...
info(logProtocol)<<"...."

info can be also tested to know if specific key to output log is active :
ex :

if (info.isActive(logProtocol)) ....

Previous method using integer level is still available.

YM

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
File size: 2.0 KB
Line 
1#ifndef __XIOS_LOG_HPP__
2#define __XIOS_LOG_HPP__
3
4#include <string>
5#include <iostream>
6#include <string>
7#include "log_type.hpp"
8
9namespace xios
10{
11  using namespace std ;
12
13  class CLog : public ostream
14  {
15    public :
16    CLog(const string& name_, std::streambuf* sBuff = cout.rdbuf())
17      : ostream(sBuff), level(0), name(name_), strBuf_(sBuff) {}
18    CLog& operator()(int l)
19    {
20      if (l<=level)
21      {
22        rdbuf(strBuf_);
23        *this<<"-> "<<name<<" : " ;
24      }
25      else rdbuf(NULL) ;
26      return *this;
27    }
28
29    CLog& operator()(CLogType& logType)
30    {
31      if (logSetting==nullptr) setLogSetting() ;
32      if (logSetting->isSet(logType))
33      {
34        rdbuf(strBuf_);
35        *this<<"-> "<<name<<" : " ;
36      }
37      else rdbuf(NULL) ;
38      return *this;
39    }
40       
41    void setLevel(int l) {level=l; }
42    int getLevel() {return level ;}
43    bool isActive(void) { if (rdbuf()==NULL) return true ; else return false ;}
44    bool isActive(int l) {if (l<=level) return true ; else return false ; }
45    bool isActive(CLogType& logType) {if (logSetting==nullptr) setLogSetting() ; if (logSetting->isSet(logType)) return true; else return false; }
46
47  public:
48    //! Write log into a file with its streambuf
49    void write2File(std::streambuf* sBuff) { changeStreamBuff(sBuff); }
50
51    //! Write log into standard output
52    void write2StdOut() { changeStreamBuff(cout.rdbuf()); }
53
54    //! Write log into standard error output
55    void write2StdErr() { changeStreamBuff(cerr.rdbuf()); }
56
57  private:
58    /*!
59     * \brief Change current streambuf (by default std::cout) to new one
60     * This function associates a new streambuf to the current log object
61     * \param [in] pointer to new streambuf
62    */
63    void changeStreamBuff(std::streambuf* sBuff) { strBuf_ = sBuff; rdbuf(sBuff); }
64
65    int level ;
66    string name ;
67    std::streambuf* strBuf_;
68   
69    void setLogSetting(void) {logSetting = new CSetLog ;}
70
71    CSetLog* logSetting = nullptr ;
72  };
73
74  extern CLog info;
75  extern CLog report;
76  extern CLog error;
77}
78#endif
Note: See TracBrowser for help on using the repository browser.