Back to index

nux  3.0.0
Logger.h
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003  * Copyright 2011 Inalogic® Inc.
00004  *
00005  * This program is free software: you can redistribute it and/or modify it
00006  * under the terms of the GNU Lesser General Public License, as
00007  * published by the  Free Software Foundation; either version 2.1 or 3.0
00008  * of the License.
00009  *
00010  * This program is distributed in the hope that it will be useful, but
00011  * WITHOUT ANY WARRANTY; without even the implied warranties of
00012  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00013  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00014  * License for more details.
00015  *
00016  * You should have received a copy of both the GNU Lesser General Public
00017  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00018  *
00019  * Authored by: Tim Penhey <tim.penhey@canonical.com>
00020  *
00021  */
00022 #ifndef NUX_CORE_LOGGER_H
00023 #define NUX_CORE_LOGGER_H
00024 
00025 #include <ostream>
00026 #include <string>
00027 #include <boost/shared_ptr.hpp>
00028 
00029 #if defined(NUX_OS_WINDOWS)
00030   #define __func__ __FUNCTION__
00031 #endif
00032 
00033 #define LOG_TRACE(logger) \
00034   if (!logger.IsTraceEnabled()) {} \
00035   else ::nux::logging::LogStream(::nux::logging::Trace, logger.module(), __FILE__, __LINE__).stream()
00036 #define LOG_DEBUG(logger) \
00037   if (!logger.IsDebugEnabled()) {} \
00038   else ::nux::logging::LogStream(::nux::logging::Debug, logger.module(), __FILE__, __LINE__).stream()
00039 #define LOG_INFO(logger) \
00040   if (!logger.IsInfoEnabled()) {} \
00041   else ::nux::logging::LogStream(::nux::logging::Info, logger.module(), __FILE__, __LINE__).stream()
00042 #define LOG_WARN(logger) LOG_WARNING(logger)
00043 #define LOG_WARNING(logger) \
00044   if (!logger.IsWarningEnabled()) {} \
00045   else ::nux::logging::LogStream(::nux::logging::Warning, logger.module(), __FILE__, __LINE__).stream()
00046 #define LOG_ERROR(logger) \
00047   if (!logger.IsErrorEnabled()) {} \
00048   else ::nux::logging::LogStream(::nux::logging::Error, logger.module(), __FILE__, __LINE__).stream()
00049 
00050 // We shouldn't really be logging block level information at anything higher than debug.
00051 #if defined(NUX_OS_WINDOWS)
00052   #define LOG_TRACE_BLOCK(logger)
00053   #define LOG_DEBUG_BLOCK(logger)
00054 #else
00055   #define LOG_TRACE_BLOCK(logger) ::nux::logging::BlockTracer _block_tracer_ ## __LINE__ (logger, ::nux::logging::Trace, __PRETTY_FUNCTION__, __FILE__, __LINE__)
00056   #define LOG_DEBUG_BLOCK(logger) ::nux::logging::BlockTracer _block_tracer_ ## __LINE__ (logger, ::nux::logging::Debug, __PRETTY_FUNCTION__, __FILE__, __LINE__)
00057 #endif
00058 
00059 namespace nux {
00060 namespace logging {
00061 
00062 enum Level
00063 {
00064   NotSpecified,
00065   Trace,
00066   Debug,
00067   Info,
00068   Warning,
00069   Error,
00070   Critical,
00071 };
00072 
00073 // Convert a string representation of a logging level into the enum value.
00074 Level get_logging_level(std::string level);
00075 
00093 void configure_logging(const char* config_string);
00094 std::string Backtrace(int levels = -1);
00095 
00096 void reset_logging();
00097 std::string dump_logging_levels(std::string const& prefix = "");
00098 
00099 class LogStream : public std::ostream
00100 {
00101 public:
00102   LogStream(Level severity,
00103             std::string const& module,
00104             std::string const& filename,
00105             int line_number);
00106   ~LogStream();
00107 
00108   std::ostream& stream() { return *this; }
00109 };
00110 
00111 
00112 class LoggerModule;
00113 typedef boost::shared_ptr<LoggerModule> LoggerModulePtr;
00114 
00115 class Logger
00116 {
00117 public:
00118   explicit Logger(std::string const& module);
00119 
00120   std::string const& module() const;
00121 
00122   bool IsErrorEnabled() const;
00123   bool IsWarningEnabled() const;
00124   bool IsInfoEnabled() const;
00125   bool IsDebugEnabled() const;
00126   bool IsTraceEnabled() const;
00127 
00128   void SetLogLevel(Level level);
00129   Level GetLogLevel() const;
00130   Level GetEffectiveLogLevel() const;
00131 
00132 private:
00133   LoggerModulePtr pimpl;
00134 };
00135 
00150 class BlockTracer
00151 {
00152 public:
00153   BlockTracer(Logger& logger,
00154               Level level,
00155               std::string const& function_name,
00156               std::string const& filename,
00157               int line_number);
00158   ~BlockTracer();
00159 private:
00160   Logger& logger_;
00161   Level level_;
00162   std::string function_name_;
00163   std::string filename_;
00164   int line_number_;
00165 };
00166 
00167 }
00168 }
00169 
00170 
00171 #endif