log.hh

Go to the documentation of this file.
00001 /* vim: set sw=4 sts=4 et foldmethod=syntax : */
00002 
00003 /*
00004  * Copyright (c) 2006, 2007, 2008, 2009 Ciaran McCreesh
00005  *
00006  * This file is part of the Paludis package manager. Paludis is free software;
00007  * you can redistribute it and/or modify it under the terms of the GNU General
00008  * Public License version 2, as published by the Free Software Foundation.
00009  *
00010  * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
00011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00012  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00013  * details.
00014  *
00015  * You should have received a copy of the GNU General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 #ifndef PALUDIS_GUARD_PALUDIS_LOG_HH
00021 #define PALUDIS_GUARD_PALUDIS_LOG_HH 1
00022 
00023 #include <iosfwd>
00024 #include <string>
00025 #include <paludis/util/stringify.hh>
00026 #include <paludis/util/instantiation_policy.hh>
00027 #include <paludis/util/private_implementation_pattern.hh>
00028 
00029 /** \file
00030  * Declarations for Log and related classes.
00031  *
00032  * \ingroup g_log
00033  *
00034  * \section Examples
00035  *
00036  * - None at this time.
00037  */
00038 
00039 namespace paludis
00040 {
00041 #include <paludis/util/log-se.hh>
00042 
00043     class LogMessageHandler;
00044 
00045     /**
00046      * Singleton class that handles log messages.
00047      *
00048      * \ingroup g_log
00049      */
00050     class PALUDIS_VISIBLE Log :
00051         public InstantiationPolicy<Log, instantiation_method::SingletonTag>,
00052         private PrivateImplementationPattern<Log>
00053     {
00054         friend class InstantiationPolicy<Log, instantiation_method::SingletonTag>;
00055         friend class LogMessageHandler;
00056 
00057         private:
00058             Log();
00059 
00060             void _message(const std::string & id, const LogLevel, const LogContext, const std::string &);
00061 
00062         public:
00063             /**
00064              * Destructor, to be called only by our InstantiationPolicy.
00065              */
00066             ~Log();
00067 
00068             /**
00069              * Only display messages of at least this level.
00070              */
00071             void set_log_level(const LogLevel);
00072 
00073             /**
00074              * Fetch the current log level.
00075              */
00076             LogLevel log_level() const
00077                 PALUDIS_ATTRIBUTE((warn_unused_result));
00078 
00079             /**
00080              * Log a message.
00081              *
00082              * The return value can be appended to using
00083              * LogMessageHandler::operator<<(). When the return value is
00084              * destroyed (that is to say, at the end of the statement), the log
00085              * message is written.
00086              */
00087             LogMessageHandler message(const std::string & id,
00088                     const LogLevel, const LogContext) PALUDIS_ATTRIBUTE((warn_unused_result));
00089 
00090             /**
00091              * Change the log stream.
00092              */
00093             void set_log_stream(std::ostream * const);
00094 
00095             /**
00096              * Set our program name.
00097              */
00098             void set_program_name(const std::string &);
00099 
00100             /**
00101              * Finish any pending writes.
00102              */
00103             void complete_pending() const;
00104     };
00105 
00106     /**
00107      * Used by Log::message().
00108      *
00109      * \see Log
00110      * \ingroup g_log
00111      */
00112     class PALUDIS_VISIBLE LogMessageHandler
00113     {
00114         friend LogMessageHandler Log::message(const std::string &, const LogLevel, const LogContext);
00115 
00116         private:
00117             Log * _log;
00118             std::string _id;
00119             std::string _message;
00120             LogLevel _log_level;
00121             LogContext _log_context;
00122 
00123             LogMessageHandler(const LogMessageHandler &);
00124             LogMessageHandler(Log * const, const std::string &, const LogLevel, const LogContext);
00125             void operator= (const LogMessageHandler &);
00126 
00127             void _append(const std::string & s);
00128 
00129         public:
00130             ///\name Basic operations
00131             ///\{
00132 
00133             ~LogMessageHandler();
00134 
00135             ///\}
00136 
00137             /**
00138              * Append some text to our message.
00139              */
00140             template <typename T_>
00141             LogMessageHandler &
00142             operator<< (const T_ & t)
00143             {
00144                 _append(stringify(t));
00145                 return *this;
00146             }
00147     };
00148 }
00149 
00150 #endif

Generated on Mon Sep 21 10:36:08 2009 for paludis by  doxygen 1.5.4