nux-1.14.0
|
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 00051 // than debug. 00052 #define LOG_TRACE_BLOCK(logger) ::nux::logging::BlockTracer _block_tracer_ ## __LINE__ (logger, ::nux::logging::Trace, __PRETTY_FUNCTION__, __FILE__, __LINE__) 00053 #define LOG_DEBUG_BLOCK(logger) ::nux::logging::BlockTracer _block_tracer_ ## __LINE__ (logger, ::nux::logging::Debug, __PRETTY_FUNCTION__, __FILE__, __LINE__) 00054 00055 00056 namespace nux { 00057 namespace logging { 00058 00059 enum Level 00060 { 00061 NotSpecified, 00062 Trace, 00063 Debug, 00064 Info, 00065 Warning, 00066 Error, 00067 Critical, 00068 }; 00069 00070 // Convert a string representation of a logging level into the enum value. 00071 Level get_logging_level(std::string level); 00072 00090 void configure_logging(const char* config_string); 00091 std::string backtrace(int levels = -1); 00092 00093 std::string dump_logging_levels(std::string const& prefix = ""); 00094 00095 class LogStream : public std::ostream 00096 { 00097 public: 00098 LogStream(Level severity, 00099 std::string const& module, 00100 std::string const& filename, 00101 int line_number); 00102 ~LogStream(); 00103 00104 std::ostream& stream() { return *this; } 00105 }; 00106 00107 00108 class LoggerModule; 00109 typedef boost::shared_ptr<LoggerModule> LoggerModulePtr; 00110 00111 class Logger 00112 { 00113 public: 00114 explicit Logger(std::string const& module); 00115 00116 std::string const& module() const; 00117 00118 bool IsErrorEnabled() const; 00119 bool IsWarningEnabled() const; 00120 bool IsInfoEnabled() const; 00121 bool IsDebugEnabled() const; 00122 bool IsTraceEnabled() const; 00123 00124 void SetLogLevel(Level level); 00125 Level GetLogLevel() const; 00126 Level GetEffectiveLogLevel() const; 00127 00128 private: 00129 LoggerModulePtr pimpl; 00130 }; 00131 00146 class BlockTracer 00147 { 00148 public: 00149 BlockTracer(Logger& logger, 00150 Level level, 00151 std::string const& function_name, 00152 std::string const& filename, 00153 int line_number); 00154 ~BlockTracer(); 00155 private: 00156 Logger& logger_; 00157 Level level_; 00158 std::string function_name_; 00159 std::string filename_; 00160 int line_number_; 00161 }; 00162 00163 } 00164 } 00165 00166 00167 #endif