Home C-Bits Package Documentation Project Page

Main Page   Namespace List   Compound List   File List   Namespace Members   Compound Members   File Members  

LogStream.h

Go to the documentation of this file.
00001 /* ====================================================================
00002  *              The CBITS Software License, Version 1.0
00003  *
00004  *               Copyright (c) 2002-2003 Bruce Lowery
00005  *                       All rights reserved
00006  *
00007  * Redistribution and use of this software, in source and binary forms, 
00008  * with or without modification, are permitted provided that the above 
00009  * copyright notice, this paragraph, and the following paragraph are 
00010  * retained in each source code file.
00011  *
00012  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
00013  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00014  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00015  * DISCLAIMED.  IN NO EVENT SHALL BRUCE LOWERY OR OTHER CONTRIBUTORS 
00016  * TO THE CBITS LIBRARY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00017  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00018  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
00019  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00020  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00021  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00022  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00023  * SUCH DAMAGE.
00024  * ====================================================================
00025  */
00026 #ifndef LogStream_H
00027 #define LogStream_H
00028 
00029 #include <iostream>
00030 #include <string>
00031 #include <map>
00032 
00033 #include <cbits/LogLevel.h>
00034 #include <cbits/LogStreamExceptions.h>
00035 #include <cbits/NullStream.h>
00036 
00037 
00038 #ifdef WIN32
00039 #include <winnt.h>
00040 #else
00041 #include <pthread.h>
00042 #endif
00043 
00044 
00063 namespace cbits
00064 {
00065 
00184 class LogStream 
00185 {
00186 
00187 public:
00188 
00195     LogStream
00196     ( 
00197         const std::string& path,    
00201         const LogLevel::Level level,
00206         const bool appnd = true     
00209     )
00210     throw( LogStreamException );
00211 
00212 
00220     LogStream
00221     ( 
00222         std::ostream* os,           
00225         const LogLevel::Level level 
00229     )
00230     throw( LogStreamException );
00231 
00232 
00238     LogStream();
00239 
00240 
00257     inline const char*  get_file_path()  const
00258         { 
00259         if( _path )
00260         {
00261             return _path->c_str();
00262         }
00263         else
00264         {
00265             return 0;
00266         }
00267         }
00268 
00269 
00279     inline const LogLevel::Level get_max_level()  const
00280         { 
00281            return _max_level; 
00282         }
00283 
00284 
00292     LogLevel::Level reset_max_level
00293     ( 
00294         LogLevel::Level new_level 
00303     );
00304 
00305 
00310     void increaseLevel();
00311 
00312 
00317     void decreaseLevel();
00318 
00319 
00326     void setTrace
00327     (
00328         const bool on 
00329     )
00330     {
00331         _trace = on;
00332     }
00333 
00334 
00340     const bool isTraceOn() const
00341     {
00342         return _trace;
00343     }
00344 
00345 
00355     virtual ~LogStream( void );
00356 
00357 
00368     std::ostream* replace_stream
00369     ( 
00370         std::ostream*  
00371     ) 
00372         throw( LogStreamException);
00373 
00374 
00381     std::ostream* get_logstream() const;
00382 
00383 
00388     inline std::ostream* get_nullstream() const { return &_nullstream; }
00389 
00390     
00398     inline std::ostream* get_ostream()    const { return _ostream; }
00399 
00404     void lock() {
00405 #ifdef WIN32
00406         EnterCriticalSection(&_lock);
00407 #else
00408         pthread_mutex_lock(&_lock);
00409 #endif
00410     }
00411 
00415     void unlock() {
00416 #ifdef WIN32
00417         LeaveCriticalSection(&_lock);
00418 #else
00419         pthread_mutex_unlock(&_lock);
00420 #endif
00421     }
00422 
00423 private:
00424 
00425     std::string*    _path;         
00426     LogLevel::Level _max_level;    
00427     mutable std::ostream* _ostream;
00428     bool            _own_stream;   
00429     mutable NullStream _nullstream;
00430     bool _trace;                   
00432     // COPY PROTECTION
00433     LogStream( const LogStream& );
00434     LogStream& operator=( const LogStream& );
00435 
00436     // Lock
00437 #ifdef WIN32
00438     critical_section_t _lock; 
00439 #else
00440     pthread_mutex_t _lock;    
00441 #endif
00442 
00443 
00444 };
00445 
00446 }; /* END OF namespace cbits */
00447 
00448 
00452 std::ostream& operator<<
00453 ( 
00454     cbits::LogStream&, 
00458     cbits::LogLevel::Level 
00462 );
00463 
00464 #endif /* LogStream_H */
00465 
00466 
00467 /*
00468  * $Id: LogStream.h,v 1.10 2002/10/12 02:19:56 brulow Exp $
00469  * 
00470  * History: (Add nothing manually below)
00471  * -----------------------------------------------------------------------
00472  *
00473  * $Log: LogStream.h,v $
00474  * Revision 1.10  2002/10/12 02:19:56  brulow
00475  * Add socket pkg unit tests
00476  *
00477  * Revision 1.9  2002/10/11 15:58:25  brulow
00478  * Improve LogStream documentation
00479  *
00480  * Revision 1.8  2002/10/11 05:06:59  brulow
00481  * Clarify 'tracing' with LogStream.
00482  *
00483  * Revision 1.7  2002/10/11 01:47:29  brulow
00484  * Add logstream unit tests
00485  *
00486  * Revision 1.6  2002/10/10 01:16:22  brulow
00487  * Improve documentation
00488  *
00489  * Revision 1.5  2002/10/09 02:02:26  brulow
00490  * Improve package documentation
00491  *
00492  * Revision 1.4  2002/10/08 17:11:20  brulow
00493  * Improve documentation
00494  *
00495  * Revision 1.3  2002/09/28 00:08:50  brulow
00496  * Add lock/unlock methods to support thread safety.
00497  *
00498  * Revision 1.2  2002/09/27 22:04:16  brulow
00499  * Add history footer to all .h .cpp files
00500  *
00501  *
00502  */

Generated by
doxygen
Hosted by
SourceForge