libyui  3.10.0
YUILog.h
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YUILog.h
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 #ifndef YUILog_h
26 
27 #ifndef YUILogComponent
28 #error Missing #define YUILogComponent "myComponent" before #include "YUILog.h"
29 #endif
30 
31 #include <iostream>
32 #include <string>
33 
34 #include "ImplPtr.h"
35 
36 using std::endl;
37 
38 
39 //
40 // UI Logging: Macros for Application use.
41 //
42 // They all return a std::ostream & for use with operator<<().
43 // #define YUILogComponent before including this header file
44 // to identify what subsystem ("my-ui" etc.) this log line belongs to.
45 //
46 // #define YUILogComponent "myComponent"
47 // #include <YUILog.h>
48 //
49 // ...
50 // yuiDebug() << "Creating widget" << widget << endl;
51 // yuiError() << "No widget with ID " << id << endl;
52 //
53 // Unless the underlying logger function handles this differently,
54 // Milestone, Warning and Error are always logged, Debug only when enabled.
55 //
56 
57 #define yuiDebug() YUILog::debug ( YUILogComponent, __FILE__, __LINE__, __FUNCTION__ )
58 #define yuiMilestone() YUILog::milestone( YUILogComponent, __FILE__, __LINE__, __FUNCTION__ )
59 #define yuiWarning() YUILog::warning ( YUILogComponent, __FILE__, __LINE__, __FUNCTION__ )
60 #define yuiError() YUILog::error ( YUILogComponent, __FILE__, __LINE__, __FUNCTION__ )
61 
62 
63 //
64 // ------ End of user relevant part ------
65 //
66 
67 
68 
69 class YUILogPrivate;
70 
71 enum YUILogLevel_t
72 {
73  YUI_LOG_DEBUG = 0,
74  YUI_LOG_MILESTONE,
75  YUI_LOG_WARNING,
76  YUI_LOG_ERROR
77 };
78 
79 
80 /**
81  * Logger function.
82  *
83  * All const char pointer parameters might be 0.
84  **/
85 typedef void (*YUILoggerFunction)( YUILogLevel_t, // logLevel
86  const char *, // logComponent
87  const char *, // sourceFileName
88  int, // sourceLineNo
89  const char *, // sourceFunctionName
90  const char * ); // message
91 
92 typedef void (*YUIEnableDebugLoggingFunction)( bool );
93 typedef bool (*YUIDebugLoggingEnabledFunction)();
94 
95 
96 /**
97  * UI logging.
98  **/
99 class YUILog
100 {
101 public:
102 
103  /**
104  * Logging functions for each log level. They all access the singleton object for this class.
105  * This means that the first call to any of those functions will create the singleton YUILog object.
106  **/
107  static std::ostream & debug ( const char * logComponent, const char * sourceFileName, int lineNo, const char * functionName );
108  static std::ostream & milestone( const char * logComponent, const char * sourceFileName, int lineNo, const char * functionName );
109  static std::ostream & warning ( const char * logComponent, const char * sourceFileName, int lineNo, const char * functionName );
110  static std::ostream & error ( const char * logComponent, const char * sourceFileName, int lineNo, const char * functionName );
111 
112  /**
113  * Generic log function. debug(), milestone() etc. ultimately all call this function.
114  **/
115  std::ostream & log( YUILogLevel_t logLevel,
116  const char * logComponent,
117  const char * sourceFileName,
118  int lineNo,
119  const char * functionName );
120 
121  /**
122  * Return the singleton object for this class.
123  * This will create the singleton if it doesn't exist yet.
124  **/
125  static YUILog * instance();
126 
127  /**
128  * Enable or disable debug logging.
129  **/
130  static void enableDebugLogging( bool debugLogging = true );
131 
132  /**
133  * Return 'true' if debug logging is enabled, 'false' if not.
134  **/
135  static bool debugLoggingEnabled();
136 
137  /**
138  * Set the log file name to be used with the standard logger function.
139  * Output will be appended to this file.
140  *
141  * Until this file name is set, the standard logger function logs to stderr.
142  * Set the log file name to an empty string to log to stderr again.
143  *
144  * This returns 'true' upon success (opening the file was successful),
145  *'false' upon error.
146  *
147  *
148  * Notice:
149  *
150  * (1) This file name is only relevant as long as the standard logger
151  * function is used. Custom logger functions may or may not use this
152  * file name.
153  *
154  * (2) No attempt is made to do anything fancy with the log file like log
155  * file rotation when a certain file size is reached. Applications that
156  * need this should use a custom logger function.
157  * See also setLoggerFunction().
158  **/
159  static bool setLogFileName( const std::string & logFileName );
160 
161  /**
162  * Return the current log file name or an empty string if stderr is used.
163  * Notice that this information is only relevant as long as the standard
164  * logger function is used.
165  **/
166  static std::string logFileName();
167 
168  /**
169  * Set the UI logger function. This is the function that will ultimately
170  * receive all UI log output (except debug logging if debug logging is
171  * disabled).
172  *
173  * By default, all logging is output to stderr. This behaviour can be
174  * restored if 0 is passed as a function pointer here.
175  **/
176  static void setLoggerFunction( YUILoggerFunction loggerFunction );
177 
178  /**
179  * Return the UI logger function.
180  *
181  * If stderr is used for logging (i.e. no logger function set), 0 is
182  * returned (unless 'returnStdLogger' is 'true', in which case the
183  * internally used stderr-logger is returned).
184  **/
185  static YUILoggerFunction loggerFunction( bool returnStdLogger = false );
186 
187  /**
188  * Set the hook functions to enable/disable debug logging and to query if
189  * debug logging is enabled:
190  *
191  * void enableDebugLogging( bool enable );
192  * bool debugLoggingEnabled();
193  *
194  * If those functions are set, they will be used instead of the internal
195  * "debugLogging" flag.
196  **/
197  static void setEnableDebugLoggingHooks( YUIEnableDebugLoggingFunction enableFunction,
198  YUIDebugLoggingEnabledFunction isEnabledFunction );
199 
200  /**
201  * Return the hook function that enables or disables debug logging
202  * or 0 if no such hook function is set.
203  **/
204  static YUIEnableDebugLoggingFunction enableDebugLoggingHook();
205 
206  /**
207  * Return the hook function that checks if debug logging is enabled
208  * or 0 if no such hook function is set.
209  **/
210  static YUIDebugLoggingEnabledFunction debugLoggingEnabledHook();
211 
212  /**
213  * Return the base name without path from a file name with path.
214  **/
215  static std::string basename( const std::string & fileNameWithPath );
216 
217 
218 private:
219  /**
220  * Constructor.
221  *
222  * Not for application use - use one of the static functions above instead.
223  * They all access the singleton object for this class.
224  **/
225  YUILog();
226 
227  /**
228  * Destructor.
229  **/
230  ~YUILog();
231 
232  //
233  // Data
234  //
235 
237 };
238 
239 
240 #define YUILog_h
241 
242 #endif // YUILog_h
UI logging.
Definition: YUILog.h:100
static std::ostream & debug(const char *logComponent, const char *sourceFileName, int lineNo, const char *functionName)
Logging functions for each log level.
Definition: YUILog.cc:487
static bool debugLoggingEnabled()
Return 'true' if debug logging is enabled, 'false' if not.
Definition: YUILog.cc:401
static YUIEnableDebugLoggingFunction enableDebugLoggingHook()
Return the hook function that enables or disables debug logging or 0 if no such hook function is set.
Definition: YUILog.cc:442
static std::string basename(const std::string &fileNameWithPath)
Return the base name without path from a file name with path.
Definition: YUILog.cc:516
std::ostream & log(YUILogLevel_t logLevel, const char *logComponent, const char *sourceFileName, int lineNo, const char *functionName)
Generic log function.
Definition: YUILog.cc:456
static void setLoggerFunction(YUILoggerFunction loggerFunction)
Set the UI logger function.
Definition: YUILog.cc:411
static YUIDebugLoggingEnabledFunction debugLoggingEnabledHook()
Return the hook function that checks if debug logging is enabled or 0 if no such hook function is set...
Definition: YUILog.cc:449
static void setEnableDebugLoggingHooks(YUIEnableDebugLoggingFunction enableFunction, YUIDebugLoggingEnabledFunction isEnabledFunction)
Set the hook functions to enable/disable debug logging and to query if debug logging is enabled:
Definition: YUILog.cc:433
static void enableDebugLogging(bool debugLogging=true)
Enable or disable debug logging.
Definition: YUILog.cc:391
static std::string logFileName()
Return the current log file name or an empty string if stderr is used.
Definition: YUILog.cc:384
static YUILoggerFunction loggerFunction(bool returnStdLogger=false)
Return the UI logger function.
Definition: YUILog.cc:421
static bool setLogFileName(const std::string &logFileName)
Set the log file name to be used with the standard logger function.
Definition: YUILog.cc:348
static YUILog * instance()
Return the singleton object for this class.
Definition: YUILog.cc:333