UniSet  2.0.0
DebugStream.h
00001 // -*- C++ -*-
00002 
00003 // Created by Lars Gullik BjЬnnes
00004 // Copyright 1999 Lars Gullik BjЬnnes (larsbj@lyx.org)
00005 // Released into the public domain.
00006 
00007 // Implemented and tested on g++ 2.7.2.3
00008 
00009 // Primarily developed for use in the LyX Project http://www.lyx.org/
00010 // but should be adaptable to any project.
00011 
00012 // (c) 2002 adapted for UniSet by Lav, GNU GPL license
00013 // Modify for UniSet by pv@eterspft.ru, GNU GPL license
00014 
00015 #ifndef DEBUGSTREAM_H
00016 #define DEBUGSTREAM_H
00017 
00018 //#ifdef __GNUG__
00019 //#pragma interface
00020 //#endif
00021 
00022 #include <iostream>
00023 #include <string>
00024 #include <sigc++/sigc++.h>
00025 #include "Debug.h"
00026 
00027 #ifdef TEST_DEBUGSTREAM
00028 #include <string>
00029 struct Debug {
00030     enum type {
00031         NONE = 0,
00032         INFO       = (1 << 0),   // 1
00033         WARN       = (1 << 1),   // 2
00034         CRIT       = (1 << 2)   // 4
00035     };
00036     static const type ANY = type(INFO | WARN | CRIT);
00037     static Debug::type value(std::string const & val) {
00038         if (val == "NONE") return Debug::NONE;
00039         if (val == "INFO") return Debug::INFO;
00040         if (val == "WARN") return Debug::WARN;
00041         if (val == "CRIT") return Debug::CRIT;
00042         return Debug::NONE;
00043     }
00044 };
00045 #endif
00046 
00084 class DebugStream : public std::ostream
00085 {
00086 public:
00088     explicit DebugStream(Debug::type t = Debug::NONE);
00089 
00091     explicit
00092     DebugStream(char const * f, Debug::type t = Debug::NONE, bool truncate=false );
00093 
00095     virtual ~DebugStream();
00096 
00097     typedef sigc::signal<void,const std::string&> StreamEvent_Signal;
00098     StreamEvent_Signal signal_stream_event();
00099 
00101     void level(Debug::type t) {
00102         dt = Debug::type(t & Debug::ANY);
00103     }
00104 
00106     Debug::type level() const {
00107         return dt;
00108     }
00109 
00111     void addLevel(Debug::type t) {
00112         dt = Debug::type(dt | t);
00113     }
00114 
00116     void delLevel(Debug::type t) {
00117         dt = Debug::type(dt & ~t);
00118     }
00119 
00121     virtual void logFile( const std::string& f, bool truncate=false );
00122 
00123     inline std::string getLogFile(){ return fname; }
00124 
00126     inline bool debugging(Debug::type t = Debug::ANY) const
00127     {  return (dt & t); }
00128 
00133     std::ostream & debug(Debug::type t = Debug::ANY);
00134 //        if (dt & t) return *this;
00135 //        return nullstream;
00136 //    }
00137 
00138 
00143     std::ostream & operator[](Debug::type t) {
00144         return debug(t);
00145     }
00146 
00150     inline std::ostream& to_end(Debug::type t)
00151     { return this->operator()(t); }
00152 
00156     std::ostream& operator()(Debug::type t);
00157 
00158     inline void showDateTime(bool s)
00159     { show_datetime = s; }
00160 
00161 // короткие функции (для удобства)
00162 // log.level1()  - вывод с датой и временем  "date time [LEVEL] ...",
00163 //    если вывод даты и времени не выключен при помощи showDateTime(false)
00164 // if( log.is_level1() ) - проверка включён ли лог.."
00165 
00166 #define DMANIP(FNAME,LEVEL) \
00167     inline std::ostream& FNAME( bool showdatetime=true ) \
00168     {\
00169         if( showdatetime )\
00170             return operator[](Debug::LEVEL); \
00171         return  operator()(Debug::LEVEL); \
00172     } \
00173 \
00174     inline bool is_##FNAME() \
00175     { return debugging(Debug::LEVEL); }
00176 
00177     DMANIP(level1,LEVEL1)
00178     DMANIP(level2,LEVEL2)
00179     DMANIP(level3,LEVEL3)
00180     DMANIP(level4,LEVEL4)
00181     DMANIP(level5,LEVEL5)
00182     DMANIP(level6,LEVEL6)
00183     DMANIP(level7,LEVEL7)
00184     DMANIP(level8,LEVEL8)
00185     DMANIP(level9,LEVEL9)
00186     DMANIP(info,INFO)
00187     DMANIP(warn,WARN)
00188     DMANIP(crit,CRIT)
00189     DMANIP(repository,REPOSITORY)
00190     DMANIP(system,SYSTEM)
00191     DMANIP(exception,EXCEPTION)
00192     DMANIP(any,ANY)
00193 #undef DMANIP
00194 
00195     std::ostream& printDate(Debug::type t, char brk='/');
00196     std::ostream& printTime(Debug::type t, char brk=':');
00197     std::ostream& printDateTime(Debug::type t);
00198 
00199     std::ostream& pos(int x, int y);
00200 
00201     const DebugStream &operator=(const DebugStream& r);
00202 
00203     inline void setLogName( const std::string& n ){ logname = n; }
00204     inline std::string  getLogName(){ return logname; }
00205 
00206 protected:
00207     void sbuf_overflow( const std::string& s );
00208 
00209 // private:
00211     Debug::type dt;
00213     std::ostream nullstream;
00215     struct debugstream_internal;
00217     debugstream_internal * internal;
00218     bool show_datetime;
00219     std::string fname;
00220 
00221     StreamEvent_Signal s_stream;
00222     std::string logname; 
00223 };
00224 
00225 #endif