libyui  3.4.2
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 
37 //
38 // UI Logging: Macros for Application use.
39 //
40 // They all return a std::ostream & for use with operator<<().
41 // #define YUILogComponent before including this header file
42 // to identify what subsystem ("my-ui" etc.) this log line belongs to.
43 //
44 // #define YUILogComponent "myComponent"
45 // #include <YUILog.h>
46 //
47 // ...
48 // yuiDebug() << "Creating widget" << widget << std::endl;
49 // yuiError() << "No widget with ID " << id << std::endl;
50 //
51 // Unless the underlying logger function handles this differently,
52 // Milestone, Warning and Error are always logged, Debug only when enabled.
53 //
54 
55 #define yuiDebug() YUILog::debug ( YUILogComponent, __FILE__, __LINE__, __FUNCTION__ )
56 #define yuiMilestone() YUILog::milestone( YUILogComponent, __FILE__, __LINE__, __FUNCTION__ )
57 #define yuiWarning() YUILog::warning ( YUILogComponent, __FILE__, __LINE__, __FUNCTION__ )
58 #define yuiError() YUILog::error ( YUILogComponent, __FILE__, __LINE__, __FUNCTION__ )
59 
60 
61 //
62 // ------ End of user relevant part ------
63 //
64 
65 
66 
67 class YUILogPrivate;
68 
69 enum YUILogLevel_t
70 {
71  YUI_LOG_DEBUG = 0,
72  YUI_LOG_MILESTONE,
73  YUI_LOG_WARNING,
74  YUI_LOG_ERROR
75 };
76 
77 
78 /**
79  * Logger function.
80  *
81  * All const char pointer parameters might be 0.
82  **/
83 typedef void (*YUILoggerFunction)( YUILogLevel_t, // logLevel
84  const char *, // logComponent
85  const char *, // sourceFileName
86  int, // sourceLineNo
87  const char *, // sourceFunctionName
88  const char * ); // message
89 
90 typedef void (*YUIEnableDebugLoggingFunction)( bool );
91 typedef bool (*YUIDebugLoggingEnabledFunction)();
92 
93 
94 /**
95  * UI logging.
96  **/
97 class YUILog
98 {
99 public:
100 
101  /**
102  * Logging functions for each log level. They all access the singleton object for this class.
103  * This means that the first call to any of those functions will create the singleton YUILog object.
104  **/
105  static std::ostream & debug ( const char * logComponent, const char * sourceFileName, int lineNo, const char * functionName );
106  static std::ostream & milestone( const char * logComponent, const char * sourceFileName, int lineNo, const char * functionName );
107  static std::ostream & warning ( const char * logComponent, const char * sourceFileName, int lineNo, const char * functionName );
108  static std::ostream & error ( const char * logComponent, const char * sourceFileName, int lineNo, const char * functionName );
109 
110  /**
111  * Generic log function. debug(), milestone() etc. ultimately all call this function.
112  **/
113  std::ostream & log( YUILogLevel_t logLevel,
114  const char * logComponent,
115  const char * sourceFileName,
116  int lineNo,
117  const char * functionName );
118 
119  /**
120  * Return the singleton object for this class.
121  * This will create the singleton if it doesn't exist yet.
122  **/
123  static YUILog * instance();
124 
125  /**
126  * Enable or disable debug logging.
127  **/
128  static void enableDebugLogging( bool debugLogging = true );
129 
130  /**
131  * Return 'true' if debug logging is enabled, 'false' if not.
132  **/
133  static bool debugLoggingEnabled();
134 
135  /**
136  * Set the log file name to be used with the standard logger function.
137  * Output will be appended to this file.
138  *
139  * Until this file name is set, the standard logger function logs to stderr.
140  * Set the log file name to an empty string to log to stderr again.
141  *
142  * This returns 'true' upon success (opening the file was successful),
143  *'false' upon error.
144  *
145  *
146  * Notice:
147  *
148  * (1) This file name is only relevant as long as the standard logger
149  * function is used. Custom logger functions may or may not use this
150  * file name.
151  *
152  * (2) No attempt is made to do anything fancy with the log file like log
153  * file rotation when a certain file size is reached. Applications that
154  * need this should use a custom logger function.
155  * See also setLoggerFunction().
156  **/
157  static bool setLogFileName( const std::string & logFileName );
158 
159  /**
160  * Return the current log file name or an empty string if stderr is used.
161  * Notice that this information is only relevant as long as the standard
162  * logger function is used.
163  **/
164  static std::string logFileName();
165 
166  /**
167  * Set the UI logger function. This is the function that will ultimately
168  * receive all UI log output (except debug logging if debug logging is
169  * disabled).
170  *
171  * By default, all logging is output to stderr. This behaviour can be
172  * restored if 0 is passed as a function pointer here.
173  **/
174  static void setLoggerFunction( YUILoggerFunction loggerFunction );
175 
176  /**
177  * Return the UI logger function.
178  *
179  * If stderr is used for logging (i.e. no logger function set), 0 is
180  * returned (unless 'returnStdLogger' is 'true', in which case the
181  * internally used stderr-logger is returned).
182  **/
183  static YUILoggerFunction loggerFunction( bool returnStdLogger = false );
184 
185  /**
186  * Set the hook functions to enable/disable debug logging and to query if
187  * debug logging is enabled:
188  *
189  * void enableDebugLogging( bool enable );
190  * bool debugLoggingEnabled();
191  *
192  * If those functions are set, they will be used instead of the internal
193  * "debugLogging" flag.
194  **/
195  static void setEnableDebugLoggingHooks( YUIEnableDebugLoggingFunction enableFunction,
196  YUIDebugLoggingEnabledFunction isEnabledFunction );
197 
198  /**
199  * Return the hook function that enables or disables debug logging
200  * or 0 if no such hook function is set.
201  **/
202  static YUIEnableDebugLoggingFunction enableDebugLoggingHook();
203 
204  /**
205  * Return the hook function that checks if debug logging is enabled
206  * or 0 if no such hook function is set.
207  **/
208  static YUIDebugLoggingEnabledFunction debugLoggingEnabledHook();
209 
210  /**
211  * Return the base name without path from a file name with path.
212  **/
213  static std::string basename( const std::string & fileNameWithPath );
214 
215 
216 private:
217  /**
218  * Constructor.
219  *
220  * Not for application use - use one of the static functions above instead.
221  * They all access the singleton object for this class.
222  **/
223  YUILog();
224 
225  /**
226  * Destructor.
227  **/
228  ~YUILog();
229 
230  //
231  // Data
232  //
233 
235 };
236 
237 
238 #define YUILog_h
239 
240 #endif // YUILog_h
UI logging.
Definition: YUILog.h:97
static void setLoggerFunction(YUILoggerFunction loggerFunction)
Set the UI logger function.
Definition: YUILog.cc:407
std::ostream & log(YUILogLevel_t logLevel, const char *logComponent, const char *sourceFileName, int lineNo, const char *functionName)
Generic log function.
Definition: YUILog.cc:452
static std::ostream & debug(const char *logComponent, const char *sourceFileName, int lineNo, const char *functionName)
Logging functions for each log level.
Definition: YUILog.cc:483
static std::string basename(const std::string &fileNameWithPath)
Return the base name without path from a file name with path.
Definition: YUILog.cc:512
static bool setLogFileName(const std::string &logFileName)
Set the log file name to be used with the standard logger function.
Definition: YUILog.cc:344
static std::string logFileName()
Return the current log file name or an empty string if stderr is used.
Definition: YUILog.cc:380
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:445
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:429
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:438
static YUILog * instance()
Return the singleton object for this class.
Definition: YUILog.cc:329
static bool debugLoggingEnabled()
Return &#39;true&#39; if debug logging is enabled, &#39;false&#39; if not.
Definition: YUILog.cc:397
static void enableDebugLogging(bool debugLogging=true)
Enable or disable debug logging.
Definition: YUILog.cc:387
static YUILoggerFunction loggerFunction(bool returnStdLogger=false)
Return the UI logger function.
Definition: YUILog.cc:417