source: XIOS/dev/branch_yushan/src/log.hpp @ 1128

Last change on this file since 1128 was 1128, checked in by yushan, 7 years ago

log OK with threads

  • 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: 1.7 KB
Line 
1#ifndef __XIOS_LOG_HPP__
2#define __XIOS_LOG_HPP__
3
4#include <string>
5#include <iostream>
6#include <string>
7#include <stdio.h>
8#include <omp.h>
9
10namespace xios
11{
12  using namespace std ;
13
14  class CLog : public ostream
15  {
16    public :
17    CLog(const string& name_, std::streambuf* sBuff = cout.rdbuf())
18      : ostream(cout.rdbuf()), level(0), name(name_), strBuf_(sBuff) 
19    {
20      omp_init_lock( &mutex );
21      for(int i=0; i<10; i++)
22        strBuf_array[i] = sBuff;
23    }
24
25    ~CLog()
26    {
27      omp_destroy_lock( &mutex );
28    }
29
30
31    CLog& operator()(int l);
32    void setLevel(int l) {level=l; }
33    int  getLevel() {return level ;}
34    bool isActive(void) { if (rdbuf()==NULL) return true ; else return false ;}
35    bool isActive(int l) {if (l<=level) return true ; else return false ; }
36
37  public:
38    //! Write log into a file with its streambuf
39    void write2File(std::streambuf* sBuff) { changeStreamBuff(sBuff); }
40
41    //! Write log into standard output
42    void write2StdOut() { changeStreamBuff(cout.rdbuf()); }
43
44    //! Write log into standard error output
45    void write2StdErr() { changeStreamBuff(cerr.rdbuf()); }
46
47  private:
48    /*!
49     * \brief Change current streambuf (by default std::cout) to new one
50     * This function associates a new streambuf to the current log object
51     * \param [in] pointer to new streambuf
52    */
53    void changeStreamBuff(std::streambuf* sBuff) 
54    { 
55      strBuf_ = sBuff; 
56      strBuf_array[omp_get_thread_num()] = sBuff;
57      rdbuf(sBuff);
58    }
59
60    int level ;
61    string name ;
62    std::streambuf* strBuf_;
63    std::streambuf* strBuf_array[10];
64    omp_lock_t mutex;
65  };
66
67  extern CLog info;
68  extern CLog report;
69  extern CLog error;
70
71
72  extern std::filebuf* info_FB[10];
73
74
75}
76#endif
Note: See TracBrowser for help on using the repository browser.