libyui
3.0.10
|
00001 /* 00002 Copyright (C) 2000-2012 Novell, Inc 00003 This library is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU Lesser General Public License as 00005 published by the Free Software Foundation; either version 2.1 of the 00006 License, or (at your option) version 3.0 of the License. This library 00007 is distributed in the hope that it will be useful, but WITHOUT ANY 00008 WARRANTY; without even the implied warranty of MERCHANTABILITY or 00009 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 00010 License for more details. You should have received a copy of the GNU 00011 Lesser General Public License along with this library; if not, write 00012 to the Free Software Foundation, Inc., 51 Franklin Street, Fifth 00013 Floor, Boston, MA 02110-1301 USA 00014 */ 00015 00016 00017 /*-/ 00018 00019 File: YUILog.h 00020 00021 Author: Stefan Hundhammer <sh@suse.de> 00022 00023 /-*/ 00024 00025 #ifndef YUILog_h 00026 00027 #ifndef YUILogComponent 00028 #error Missing #define YUILogComponent "myComponent" before #include "YUILog.h" 00029 #endif 00030 00031 #include <iostream> 00032 #include <string> 00033 00034 #include "ImplPtr.h" 00035 00036 00037 // 00038 // UI Logging: Macros for Application use. 00039 // 00040 // They all return a std::ostream & for use with operator<<(). 00041 // #define YUILogComponent before including this header file 00042 // to identify what subsystem ("my-ui" etc.) this log line belongs to. 00043 // 00044 // #define YUILogComponent "myComponent" 00045 // #include <YUILog.h> 00046 // 00047 // ... 00048 // yuiDebug() << "Creating widget" << widget << std::endl; 00049 // yuiError() << "No widget with ID " << id << std::endl; 00050 // 00051 // Unless the underlying logger function handles this differently, 00052 // Milestone, Warning and Error are always logged, Debug only when enabled. 00053 // 00054 00055 #define yuiDebug() YUILog::debug ( YUILogComponent, __FILE__, __LINE__, __FUNCTION__ ) 00056 #define yuiMilestone() YUILog::milestone( YUILogComponent, __FILE__, __LINE__, __FUNCTION__ ) 00057 #define yuiWarning() YUILog::warning ( YUILogComponent, __FILE__, __LINE__, __FUNCTION__ ) 00058 #define yuiError() YUILog::error ( YUILogComponent, __FILE__, __LINE__, __FUNCTION__ ) 00059 00060 00061 // 00062 // ------ End of user relevant part ------ 00063 // 00064 00065 00066 00067 class YUILogPrivate; 00068 00069 enum YUILogLevel_t 00070 { 00071 YUI_LOG_DEBUG = 0, 00072 YUI_LOG_MILESTONE, 00073 YUI_LOG_WARNING, 00074 YUI_LOG_ERROR 00075 }; 00076 00077 00078 /** 00079 * Logger function. 00080 * 00081 * All const char pointer parameters might be 0. 00082 **/ 00083 typedef void (*YUILoggerFunction)( YUILogLevel_t, // logLevel 00084 const char *, // logComponent 00085 const char *, // sourceFileName 00086 int, // sourceLineNo 00087 const char *, // sourceFunctionName 00088 const char * ); // message 00089 00090 typedef void (*YUIEnableDebugLoggingFunction)( bool ); 00091 typedef bool (*YUIDebugLoggingEnabledFunction)(); 00092 00093 00094 /** 00095 * UI logging. 00096 **/ 00097 class YUILog 00098 { 00099 public: 00100 00101 /** 00102 * Logging functions for each log level. They all access the singleton object for this class. 00103 * This means that the first call to any of those functions will create the singleton YUILog object. 00104 **/ 00105 static std::ostream & debug ( const char * logComponent, const char * sourceFileName, int lineNo, const char * functionName ); 00106 static std::ostream & milestone( const char * logComponent, const char * sourceFileName, int lineNo, const char * functionName ); 00107 static std::ostream & warning ( const char * logComponent, const char * sourceFileName, int lineNo, const char * functionName ); 00108 static std::ostream & error ( const char * logComponent, const char * sourceFileName, int lineNo, const char * functionName ); 00109 00110 /** 00111 * Generic log function. debug(), milestone() etc. ultimately all call this function. 00112 **/ 00113 std::ostream & log( YUILogLevel_t logLevel, 00114 const char * logComponent, 00115 const char * sourceFileName, 00116 int lineNo, 00117 const char * functionName ); 00118 00119 /** 00120 * Return the singleton object for this class. 00121 * This will create the singleton if it doesn't exist yet. 00122 **/ 00123 static YUILog * instance(); 00124 00125 /** 00126 * Enable or disable debug logging. 00127 **/ 00128 static void enableDebugLogging( bool debugLogging = true ); 00129 00130 /** 00131 * Return 'true' if debug logging is enabled, 'false' if not. 00132 **/ 00133 static bool debugLoggingEnabled(); 00134 00135 /** 00136 * Set the log file name to be used with the standard logger function. 00137 * Output will be appended to this file. 00138 * 00139 * Until this file name is set, the standard logger function logs to stderr. 00140 * Set the log file name to an empty string to log to stderr again. 00141 * 00142 * This returns 'true' upon success (opening the file was successful), 00143 *'false' upon error. 00144 * 00145 * 00146 * Notice: 00147 * 00148 * (1) This file name is only relevant as long as the standard logger 00149 * function is used. Custom logger functions may or may not use this 00150 * file name. 00151 * 00152 * (2) No attempt is made to do anything fancy with the log file like log 00153 * file rotation when a certain file size is reached. Applications that 00154 * need this should use a custom logger function. 00155 * See also setLoggerFunction(). 00156 **/ 00157 static bool setLogFileName( const std::string & logFileName ); 00158 00159 /** 00160 * Return the current log file name or an empty string if stderr is used. 00161 * Notice that this information is only relevant as long as the standard 00162 * logger function is used. 00163 **/ 00164 static std::string logFileName(); 00165 00166 /** 00167 * Set the UI logger function. This is the function that will ultimately 00168 * receive all UI log output (except debug logging if debug logging is 00169 * disabled). 00170 * 00171 * By default, all logging is output to stderr. This behaviour can be 00172 * restored if 0 is passed as a function pointer here. 00173 **/ 00174 static void setLoggerFunction( YUILoggerFunction loggerFunction ); 00175 00176 /** 00177 * Return the UI logger function. 00178 * 00179 * If stderr is used for logging (i.e. no logger function set), 0 is 00180 * returned (unless 'returnStdLogger' is 'true', in which case the 00181 * internally used stderr-logger is returned). 00182 **/ 00183 static YUILoggerFunction loggerFunction( bool returnStdLogger = false ); 00184 00185 /** 00186 * Set the hook functions to enable/disable debug logging and to query if 00187 * debug logging is enabled: 00188 * 00189 * void enableDebugLogging( bool enable ); 00190 * bool debugLoggingEnabled(); 00191 * 00192 * If those functions are set, they will be used instead of the internal 00193 * "debugLogging" flag. 00194 **/ 00195 static void setEnableDebugLoggingHooks( YUIEnableDebugLoggingFunction enableFunction, 00196 YUIDebugLoggingEnabledFunction isEnabledFunction ); 00197 00198 /** 00199 * Return the hook function that enables or disables debug logging 00200 * or 0 if no such hook function is set. 00201 **/ 00202 static YUIEnableDebugLoggingFunction enableDebugLoggingHook(); 00203 00204 /** 00205 * Return the hook function that checks if debug logging is enabled 00206 * or 0 if no such hook function is set. 00207 **/ 00208 static YUIDebugLoggingEnabledFunction debugLoggingEnabledHook(); 00209 00210 /** 00211 * Return the base name without path from a file name with path. 00212 **/ 00213 static std::string basename( const std::string & fileNameWithPath ); 00214 00215 00216 private: 00217 /** 00218 * Constructor. 00219 * 00220 * Not for application use - use one of the static functions above instead. 00221 * They all access the singleton object for this class. 00222 **/ 00223 YUILog(); 00224 00225 /** 00226 * Destructor. 00227 **/ 00228 ~YUILog(); 00229 00230 // 00231 // Data 00232 // 00233 00234 ImplPtr<YUILogPrivate> priv; 00235 }; 00236 00237 00238 #define YUILog_h 00239 00240 #endif // YUILog_h