source: XIOS/dev/dev_ym/XIOS_COUPLING/src/log.hpp @ 2341

Last change on this file since 2341 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
RevLine 
[380]1#ifndef __XIOS_LOG_HPP__
2#define __XIOS_LOG_HPP__
[300]3
[364]4#include <string>
[300]5#include <iostream>
[369]6#include <string>
[2341]7#include "log_type.hpp"
[300]8
[335]9namespace xios
[300]10{
11  using namespace std ;
12
13  class CLog : public ostream
14  {
15    public :
[523]16    CLog(const string& name_, std::streambuf* sBuff = cout.rdbuf())
17      : ostream(sBuff), level(0), name(name_), strBuf_(sBuff) {}
[490]18    CLog& operator()(int l)
19    {
20      if (l<=level)
[300]21      {
[490]22        rdbuf(strBuf_);
[347]23        *this<<"-> "<<name<<" : " ;
[300]24      }
25      else rdbuf(NULL) ;
26      return *this;
27    }
[2341]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       
[490]41    void setLevel(int l) {level=l; }
[300]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 ; }
[2341]45    bool isActive(CLogType& logType) {if (logSetting==nullptr) setLogSetting() ; if (logSetting->isSet(logType)) return true; else return false; }
[300]46
[490]47  public:
[523]48    //! Write log into a file with its streambuf
[490]49    void write2File(std::streambuf* sBuff) { changeStreamBuff(sBuff); }
50
[523]51    //! Write log into standard output
[490]52    void write2StdOut() { changeStreamBuff(cout.rdbuf()); }
[523]53
54    //! Write log into standard error output
55    void write2StdErr() { changeStreamBuff(cerr.rdbuf()); }
56
[490]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
[300]65    int level ;
[347]66    string name ;
[490]67    std::streambuf* strBuf_;
[2341]68   
69    void setLogSetting(void) {logSetting = new CSetLog ;}
70
71    CSetLog* logSetting = nullptr ;
[300]72  };
73
74  extern CLog info;
[347]75  extern CLog report;
[523]76  extern CLog error;
[300]77}
78#endif
Note: See TracBrowser for help on using the repository browser.