log.hpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #ifndef __XIOS_LOG_HPP__
  2. #define __XIOS_LOG_HPP__
  3. #include <string>
  4. #include <iostream>
  5. #include <string>
  6. namespace xios
  7. {
  8. using namespace std ;
  9. class CLog : public ostream
  10. {
  11. public :
  12. CLog(const string& name_, std::streambuf* sBuff = cout.rdbuf())
  13. : ostream(sBuff), level(0), name(name_), strBuf_(sBuff) {}
  14. CLog& operator()(int l)
  15. {
  16. if (l<=level)
  17. {
  18. rdbuf(strBuf_);
  19. *this<<"-> "<<name<<" : " ;
  20. }
  21. else rdbuf(NULL) ;
  22. return *this;
  23. }
  24. void setLevel(int l) {level=l; }
  25. int getLevel() {return level ;}
  26. bool isActive(void) { if (rdbuf()==NULL) return true ; else return false ;}
  27. bool isActive(int l) {if (l<=level) return true ; else return false ; }
  28. public:
  29. //! Write log into a file with its streambuf
  30. void write2File(std::streambuf* sBuff) { changeStreamBuff(sBuff); }
  31. //! Write log into standard output
  32. void write2StdOut() { changeStreamBuff(cout.rdbuf()); }
  33. //! Write log into standard error output
  34. void write2StdErr() { changeStreamBuff(cerr.rdbuf()); }
  35. private:
  36. /*!
  37. * \brief Change current streambuf (by default std::cout) to new one
  38. * This function associates a new streambuf to the current log object
  39. * \param [in] pointer to new streambuf
  40. */
  41. void changeStreamBuff(std::streambuf* sBuff) { strBuf_ = sBuff; rdbuf(sBuff); }
  42. int level ;
  43. string name ;
  44. std::streambuf* strBuf_;
  45. };
  46. extern CLog info;
  47. extern CLog report;
  48. extern CLog error;
  49. }
  50. #endif