Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

LogStream.h

Go to the documentation of this file.
00001 // --------------------------------------------------------------------------
00002 //                   OpenMS -- Open-Source Mass Spectrometry
00003 // --------------------------------------------------------------------------
00004 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
00005 // ETH Zurich, and Freie Universitaet Berlin 2002-2012.
00006 //
00007 // This software is released under a three-clause BSD license:
00008 //  * Redistributions of source code must retain the above copyright
00009 //    notice, this list of conditions and the following disclaimer.
00010 //  * Redistributions in binary form must reproduce the above copyright
00011 //    notice, this list of conditions and the following disclaimer in the
00012 //    documentation and/or other materials provided with the distribution.
00013 //  * Neither the name of any author or any participating institution
00014 //    may be used to endorse or promote products derived from this software
00015 //    without specific prior written permission.
00016 // For a full list of authors, refer to the file AUTHORS.
00017 // --------------------------------------------------------------------------
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
00022 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00025 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00027 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00028 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // --------------------------------------------------------------------------
00031 // $Maintainer: Chris Bielow, Stephan Aiche $
00032 // $Authors: Chris Bielow, Stephan Aiche, Andreas Bertsch$
00033 // --------------------------------------------------------------------------
00034 
00035 #ifndef OPENMS_CONCEPT_LOGSTREAM_H
00036 #define OPENMS_CONCEPT_LOGSTREAM_H
00037 
00038 #include <OpenMS/DATASTRUCTURES/String.h>
00039 
00040 
00041 #include <sstream>
00042 #include <iostream>
00043 #include <list>
00044 #include <vector>
00045 #include <ctime>
00046 #include <map>
00047 
00048 namespace OpenMS
00049 {
00076   namespace Logger
00077   {
00078     // forward declarations
00079     class LogStream;
00080     class LogStreamNotifier;
00081 
00103     class OPENMS_DLLAPI LogStreamBuf :
00104       public std::streambuf
00105     {
00106 
00107       friend class LogStream;
00108 
00109 public:
00110 
00112 
00113       static const time_t MAX_TIME;
00114       static const std::string UNKNOWN_LOG_LEVEL;
00116 
00118 
00119 
00124       explicit LogStreamBuf(std::string log_level = UNKNOWN_LOG_LEVEL);
00125 
00129       virtual ~LogStreamBuf();
00130 
00132 
00134 
00135 
00146       virtual int sync();
00147 
00152       virtual int overflow(int c = -1);
00154 
00155 
00157 
00158 
00163       void setLevel(std::string level);
00164 
00165 
00169       std::string getLevel();
00171 
00177       OPENMS_DLLAPI struct StreamStruct
00178       {
00179         std::ostream * stream;
00180         std::string         prefix;
00181         LogStreamNotifier * target;
00182 
00183         StreamStruct() :
00184           stream(0),
00185           target(0)
00186         {}
00187 
00189         ~StreamStruct()
00190         {}
00191 
00192       };
00193 
00199       void clearCache();
00200 
00201 protected:
00202 
00204       void distribute_(std::string outstring);
00205 
00207       std::string expandPrefix_(const std::string & prefix, time_t time) const;
00208 
00209       char * pbuf_;
00210       std::string             level_;
00211       std::list<StreamStruct> stream_list_;
00212       std::string             incomplete_line_;
00213 
00215 
00216 
00220       struct LogCacheStruct
00221       {
00222         Size timestamp;
00223         int counter;
00224       };
00225 
00230       Size log_cache_counter_;
00231 
00233       std::map<std::string, LogCacheStruct> log_cache_;
00235       std::map<Size, std::string> log_time_cache_;
00236 
00238       bool isInCache_(std::string const & line);
00239 
00248       std::string addToCache_(std::string const & line);
00249 
00251       Size getNextLogCounter_();
00252 
00254     };
00255 
00257     class OPENMS_DLLAPI LogStreamNotifier
00258     {
00259 public:
00260 
00262       LogStreamNotifier();
00263 
00265       virtual ~LogStreamNotifier();
00266 
00268       virtual void logNotify();
00269 
00271       void registerAt(LogStream & log_stream);
00272 
00274       void unregister();
00275 
00276 protected:
00277       std::stringstream stream_;
00278 
00279       LogStream * registered_at_;
00280     };
00281 
00282 
00303     class OPENMS_DLLAPI LogStream :
00304       public std::ostream
00305     {
00306 public:
00307 
00309 
00310 
00320       LogStream(LogStreamBuf * buf = 0, bool delete_buf = true, std::ostream * stream = 0);
00321 
00323       virtual ~LogStream();
00325 
00327 
00328 
00335       LogStreamBuf * rdbuf();
00336 
00338       LogStreamBuf * operator->();
00340 
00341 
00343 
00344 
00350       void setLevel(std::string level);
00351 
00352 
00356       std::string getLevel();
00358 
00360 
00361 
00376       void insert(std::ostream & s);
00377 
00388       void remove(std::ostream & s);
00389 
00391       void insertNotification(std::ostream & s,
00392                               LogStreamNotifier & target);
00393 
00414       void setPrefix(const std::ostream & s, const std::string & prefix);
00415 
00416 
00418       void setPrefix(const std::string & prefix);
00419 
00421       void flush();
00423 private:
00424 
00425       typedef std::list<LogStreamBuf::StreamStruct>::iterator StreamIterator;
00426 
00427       StreamIterator findStream_(const std::ostream & stream);
00428       bool hasStream_(std::ostream & stream);
00429       bool bound_() const;
00430 
00435       bool delete_buffer_;
00436 
00437     }; //LogStream
00438 
00439   } // namespace Logger
00440 
00441 
00443 #define LOG_FATAL_ERROR \
00444   Log_fatal << __FILE__ << "(" << __LINE__ << "): "
00445 
00447 #define LOG_ERROR \
00448   Log_error
00449 
00451 #define LOG_WARN \
00452   Log_warn
00453 
00455 #define LOG_INFO \
00456   Log_info
00457 
00459 #define LOG_DEBUG \
00460   Log_debug << __FILE__ << "(" << __LINE__ << "): "
00461 
00462   OPENMS_DLLAPI extern Logger::LogStream Log_fatal; 
00463   OPENMS_DLLAPI extern Logger::LogStream Log_error; 
00464   OPENMS_DLLAPI extern Logger::LogStream Log_warn;  
00465   OPENMS_DLLAPI extern Logger::LogStream Log_info;  
00466   OPENMS_DLLAPI extern Logger::LogStream Log_debug; 
00467 
00468 } // namespace OpenMS
00469 
00470 #endif // OPENMS_CONCEPT_LOGSTREAM_H

OpenMS / TOPP release 1.10.0 Documentation generated on Thu Mar 7 2013 09:42:40 using doxygen 1.7.1