Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

LogStream.h

Go to the documentation of this file.
00001 /* SimData: Data Infrastructure for Simulations
00002  * Copyright (C) 2002, 2003 Mark Rose <tm2@stm.lbl.gov>
00003  * 
00004  * This file is part of SimData.
00005  * 
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  * 
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019  */
00020 
00021 
00038 #ifndef __SIMDATA_LOGSTREAM_H__
00039 #define __SIMDATA_LOGSTREAM_H__
00040 
00041 #include <iostream>
00042 #include <fstream>
00043 #include <string>
00044 
00045 #include <SimData/Namespace.h>
00046 #include <SimData/Export.h>
00047 
00048 NAMESPACE_SIMDATA
00049 
00050 using std::ostream;
00051 
00052 //
00053 // TODO:
00054 //
00055 // 1. Change output destination. Done.
00056 // 2. Make logbuf thread safe.
00057 // 3. Read environment for default debugClass and debugPriority.
00058 //
00059 
00066 class SIMDATA_EXPORT logbuf : public std::streambuf
00067 {
00068 public:
00069 
00071         logbuf();
00072 
00074         ~logbuf();
00075 
00080         bool enabled() { return logging_enabled; }
00081 
00087         void set_log_state(int c, int p);
00088 
00094         void set_log_level(int c, int p);
00095 
00096 
00101         void set_log_classes(int c);
00102 
00103 
00107         int get_log_classes ();
00108 
00109 
00114         void set_log_priority(int p);
00115 
00116 
00121         int get_log_priority ();
00122 
00123 
00128         void set_sb( std::streambuf* sb );
00129 
00130 protected:
00131 
00133         inline virtual int sync();
00134 
00136         int overflow( int ch );
00137 
00138 private:
00139 
00140         // The streambuf used for actual output. Defaults to cerr.rdbuf().
00141         std::streambuf* sbuf;
00142 
00143         bool logging_enabled;
00144         int logClass;
00145         int logPriority;
00146 
00147 private:
00148 
00149         // Not defined.
00150         logbuf( const logbuf& );
00151         void operator= ( const logbuf& );
00152 };
00153 
00154 inline int
00155 logbuf::sync()
00156 {
00157         if (!sbuf) return -1;
00158         return sbuf->pubsync();
00159 }
00160 
00161 inline void
00162 logbuf::set_log_state(int c, int p)
00163 {
00164         logging_enabled = ((c & logClass) != 0 && p >= logPriority);
00165 }
00166 
00167 //inline logbuf::int_type
00168 inline int
00169 logbuf::overflow( int c )
00170 {
00171         return logging_enabled ? sbuf->sputc(c) : (EOF == 0 ? 1: 0);
00172 }
00173 
00176 struct loglevel
00177 {
00178         loglevel(int c, int p)
00179                 : logClass(c), logPriority(p) {}
00180 
00181         int logClass;
00182         int logPriority;
00183 };
00184 
00193 struct SIMDATA_EXPORT logstream_base
00194 {
00195         logstream_base() {}
00196         logbuf lbuf;
00197 };
00198  
00201 class SIMDATA_EXPORT logstream : private logstream_base, public std::ostream
00202 {
00203         std::ofstream *m_out;
00204 public:
00209         logstream(std::ostream& out_)
00210                 : logstream_base(),
00211                 ostream(&lbuf), // msvc6 accepts ostream(&lbuf) _using std::ostream_, but not std::ostream(&lbuf) ...
00212                   m_out(NULL)
00213         { 
00214                 lbuf.set_sb(out_.rdbuf());
00215         }
00216 
00217         ~logstream() {
00218                 _close();
00219         }
00220 
00221         void _close() {
00222                 if (m_out != NULL) {
00223                         m_out->close();
00224                         delete m_out;
00225                         m_out = NULL;
00226                 }
00227         }
00228 
00233         void setOutput(std::ostream& out_) { 
00234                 _close();
00235                 lbuf.set_sb(out_.rdbuf() ); 
00236         }
00237 
00242         void setOutput(std::string const &fn) { 
00243                 _close();
00244                 m_out = new std::ofstream(fn.c_str());
00245                 lbuf.set_sb( m_out->rdbuf() ); 
00246         }
00247 
00253         void setLogLevels(int c, int p);
00254 
00259         void setLogClasses(int c);
00260 
00264         inline std::ostream& operator<< ( const loglevel& l );
00265 };
00266 
00267 inline std::ostream&
00268 logstream::operator<< ( const loglevel& l )
00269 {
00270         lbuf.set_log_state( l.logClass, l.logPriority );
00271         return *this;
00272 }
00273 
00274 NAMESPACE_SIMDATA_END
00275 
00276 
00277 #endif // __SIMDATA_LOGSTREAM_H__
00278 
00279 

SimData version pre-0.4.0. For more information on SimData, visit the SimData Homepage.

Generated on Tue Oct 14 12:06:38 2003, using Doxygen 1.2.18.

[SF.net]