Orocos Real-Time Toolkit  2.6.0
Logger.hpp
00001 /***************************************************************************
00002   tag: Peter Soetens  Mon Jan 10 15:59:16 CET 2005  Logger.hpp
00003 
00004                         Logger.hpp -  description
00005                            -------------------
00006     begin                : Mon January 10 2005
00007     copyright            : (C) 2005 Peter Soetens
00008     email                : peter.soetens@mech.kuleuven.ac.be
00009 
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU General Public                   *
00013  *   License as published by the Free Software Foundation;                 *
00014  *   version 2 of the License.                                             *
00015  *                                                                         *
00016  *   As a special exception, you may use this file as part of a free       *
00017  *   software library without restriction.  Specifically, if other files   *
00018  *   instantiate templates or use macros or inline functions from this     *
00019  *   file, or you compile this file and link it with other files to        *
00020  *   produce an executable, this file does not by itself cause the         *
00021  *   resulting executable to be covered by the GNU General Public          *
00022  *   License.  This exception does not however invalidate any other        *
00023  *   reasons why the executable file might be covered by the GNU General   *
00024  *   Public License.                                                       *
00025  *                                                                         *
00026  *   This library is distributed in the hope that it will be useful,       *
00027  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00028  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00029  *   Lesser General Public License for more details.                       *
00030  *                                                                         *
00031  *   You should have received a copy of the GNU General Public             *
00032  *   License along with this library; if not, write to the Free Software   *
00033  *   Foundation, Inc., 59 Temple Place,                                    *
00034  *   Suite 330, Boston, MA  02111-1307  USA                                *
00035  *                                                                         *
00036  ***************************************************************************/
00037 
00038 
00039 #ifndef ORO_CORELIB_lOGGER_HPP
00040 #define ORO_CORELIB_lOGGER_HPP
00041 
00042 #include "rtt-config.h"
00043 #ifndef OROBLD_DISABLE_LOGGING
00044 #include <ostream>
00045 #include <sstream>
00046 #else
00047 #include <iosfwd>
00048 #endif
00049 #include <string>
00050 #ifndef OROSEM_PRINTF_LOGGING
00051 #include <iostream>            // for std::cerr
00052 #endif
00053 
00054 #include "os/TimeService.hpp"
00055 #include "os/Mutex.hpp"
00056 #include "os/MutexLock.hpp"
00057 
00058 namespace RTT
00059 {
00095     class RTT_API Logger
00096     {
00097         struct D;
00098         D* d;
00104         os::Mutex& inpguard;
00105         std::ostream& logline;
00106         std::ostream& fileline;
00107     public:
00108 
00112         os::TimeService::ticks getReferenceTime()const;
00113 
00121         enum LogLevel { Never = 0, Fatal, Critical, Error, Warning, Info, Debug, RealTime };
00122 
00126         void allowRealTime();
00127 
00131         void disallowRealTime();
00132 
00137         void mayLogStdOut(bool tf);
00138 
00143         void mayLogFile(bool tf);
00144 
00159         struct RTT_API In {
00160             In(const std::string& module);
00161             ~In();
00162             std::string oldmod;
00163         };
00164 
00169         Logger& in(const std::string& modname);
00170 
00175         Logger& out(const std::string& modname);
00176 
00180         std::string getLogModule() const;
00181 
00186         typedef std::ostream& (*LogFunction)(std::ostream&);
00187 
00191         static std::ostream& nl(std::ostream& __os);
00192 
00193         /*
00194          * Insert a newline '\n' in the ostream and flush. (Copy of the standard)
00195          */
00196         static std::ostream& endl(std::ostream& __os);
00197 
00201         static std::ostream& flush(std::ostream& __os);
00202 
00210         static Logger* Instance(std::ostream& str=std::cerr);
00211 
00215         static void Release();
00216 
00220         static Logger& log();
00221 
00225         static Logger& log(LogLevel ll);
00226 
00230         void startup();
00231 
00235         void shutdown();
00236 
00242         std::string getLogLine();
00243 
00247         void setStdStream( std::ostream& stdos  );
00248 
00255         template< class T>
00256         Logger& operator<<( T t );
00257 
00261         Logger& operator<<(LogLevel ll);
00262 
00268         Logger& operator<<(const std::string&);
00269 
00275         Logger& operator<<(const char*);
00276 
00280         Logger& operator<<(std::ostream& (*pf)(std::ostream&));
00281 
00289         void setLogLevel( LogLevel ll );
00290 
00294         LogLevel getLogLevel() const;
00295 
00299         void logflush();
00300 
00305         void logendl();
00306 
00312         void lognl();
00313 
00314     private:
00321         bool mayLog() const;
00322         bool mayLogStdOut() const;
00323         bool mayLogFile() const;
00324 
00325         Logger(std::ostream& str=std::cerr);
00326         ~Logger();
00327 
00328         static Logger* _instance;
00329     };
00330 
00338     enum LoggerLevel { Never = 0, Fatal, Critical, Error, Warning, Info, Debug, RealTime };
00339 
00343     static inline Logger& log() { return Logger::log(); }
00344 
00349     static inline Logger& log(LoggerLevel ll) { return Logger::log(Logger::LogLevel(ll)); }
00350 
00355     static inline Logger::LogFunction endlog() {return Logger::endl; }
00356 
00362     static inline Logger::LogFunction endlog(LoggerLevel ll) { log(ll); return Logger::endl; }
00363 
00368     static inline Logger::LogFunction nlog() {return Logger::nl; }
00369 
00374     static inline Logger::LogFunction flushlog() {return Logger::flush; }
00375 }
00376 
00377 #include "Logger.inl"
00378 
00379 #endif