00001 /* 00002 * Funambol is a mobile platform developed by Funambol, Inc. 00003 * Copyright (C) 2003 - 2007 Funambol, Inc. 00004 * 00005 * This program is free software; you can redistribute it and/or modify it under 00006 * the terms of the GNU Affero General Public License version 3 as published by 00007 * the Free Software Foundation with the addition of the following permission 00008 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED 00009 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE 00010 * WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. 00011 * 00012 * This program is distributed in the hope that it will be useful, but WITHOUT 00013 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00014 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 00015 * details. 00016 * 00017 * You should have received a copy of the GNU Affero General Public License 00018 * along with this program; if not, see http://www.gnu.org/licenses or write to 00019 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00020 * MA 02110-1301 USA. 00021 * 00022 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite 00023 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com. 00024 * 00025 * The interactive user interfaces in modified source and object code versions 00026 * of this program must display Appropriate Legal Notices, as required under 00027 * Section 5 of the GNU Affero General Public License version 3. 00028 * 00029 * In accordance with Section 7(b) of the GNU Affero General Public License 00030 * version 3, these Appropriate Legal Notices must retain the display of the 00031 * "Powered by Funambol" logo. If the display of the logo is not reasonably 00032 * feasible for technical reasons, the Appropriate Legal Notices must display 00033 * the words "Powered by Funambol". 00034 */ 00035 00036 #ifndef INCL_LOG 00037 #define INCL_LOG 00038 /** @cond DEV */ 00039 00040 #include "base/fscapi.h" 00041 #include "base/util/StringBuffer.h" 00042 00043 /** prefix for error messages */ 00044 #define LOG_ERROR "ERROR" 00045 /** prefix for informational messages */ 00046 #define LOG_INFO "INFO" 00047 /** prefix for debug or developer messages */ 00048 #define LOG_DEBUG "DEBUG" 00049 00050 /** default is to create this file in the current directory */ 00051 #define LOG_NAME "synclog.txt" 00052 #include "base/globalsdef.h" 00053 00054 BEGIN_NAMESPACE 00055 00056 /** 00057 * valid parameters for setLevel() 00058 */ 00059 typedef enum { 00060 /** 00061 * log level not configured: if used in setLevel(), then only 00062 * error messages will be printed 00063 */ 00064 LOG_LEVEL_NONE = 0, 00065 /** 00066 * errors and info messages for users and developers 00067 * will be printed: use this to keep the log consise and 00068 * small 00069 */ 00070 LOG_LEVEL_INFO = 1, 00071 /** 00072 * all messages will be printed: the log can become very large! 00073 */ 00074 LOG_LEVEL_DEBUG = 2, 00075 } LogLevel; 00076 00077 class Log { 00078 00079 private: 00080 00081 /** 00082 * Which log level is set? 00083 */ 00084 LogLevel logLevel; 00085 00086 /** 00087 * the singleton implementing logging 00088 */ 00089 static Log *logger; 00090 00091 public: 00092 00093 Log() : logLevel(LOG_LEVEL_INFO) {} 00094 virtual ~Log() {} 00095 00096 /** 00097 * Grants access to the singleton which implements logging. 00098 * The implementation of this function and thus the Log 00099 * class itself is platform specific: if no Log instance 00100 * has been set yet, then this call has to create one. 00101 */ 00102 static Log &instance(); 00103 00104 /** 00105 * Overrides the default Log implementation. The Log class 00106 * itself will never delete the active logger. 00107 * 00108 * @param logger will be used for all future logging activities; 00109 * NULL is allowed and implies that the default 00110 * Log implementation will be created if needed 00111 */ 00112 static void setLogger(Log *logger) { Log::logger = logger; } 00113 00114 /** clients can use #ifdef to detect this new feature */ 00115 # define LOG_HAVE_SET_LOGGER 1 00116 00117 /** 00118 * Sets the directory where the log file will be created, 00119 * which is done in reset() or any of the logging functions. 00120 */ 00121 virtual void setLogPath(const char* configLogPath) = 0; 00122 00123 /** 00124 * Sets the file name of the log file without creating it; 00125 * that is done in reset() or any of the logging functions. 00126 */ 00127 virtual void setLogName(const char* configLogName) = 0; 00128 00129 /** 00130 * creates the log file under the selected name and path, 00131 * optionally logging the given title 00132 */ 00133 virtual void reset(const char* title = NULL) = 0; 00134 00135 virtual void setLevel(LogLevel level) { logLevel = level; } 00136 virtual LogLevel getLevel() { return logLevel; } 00137 virtual bool isLoggable(LogLevel level) { return level <= logLevel; } 00138 00139 /** 00140 * error(), info(), developer(), debug() all print one message, 00141 * using printf() style formatting. Whether the message is really 00142 * written into the log file depends on the current log level 00143 * (see LogLevel above). 00144 * 00145 * Which of these calls is the right one for a certain message 00146 * is a somewhat subjective choice. Here is a definition how they 00147 * are supposed to be used: 00148 * - error: severe problem which the user and developer have to 00149 * know about 00150 * - info: information about a sync session which the user 00151 * will want to read during/after each sync session 00152 * - developer: information about a sync session that is not 00153 * interesting for a user (for example, because it 00154 * is constant and already known) but which should 00155 * be in each log because developers need to know 00156 * it. Messages logged with this calls will be included 00157 * at LOG_LEVEL_INFO, therefore messages should be small and 00158 * not recur so that the log file size remains small. 00159 * - debug: most detailed logging, messages may be arbitrarily large 00160 * 00161 * Here is a decision tree which helps to pick the right level: 00162 * - an error: => error() 00163 * - it changes during each sync or marks important steps 00164 * in the sync: info() 00165 * - small, non-recurring message which is important for developers 00166 * who read a log produced at LOG_LEVEL_INFO: developer() 00167 * - everything else: debug() 00168 */ 00169 virtual void error(const char* msg, ...) = 0; 00170 virtual void info(const char* msg, ...) = 0; 00171 virtual void developer(const char* msg, ...) = 0; 00172 virtual void debug(const char* msg, ...) = 0; 00173 00174 /** clients can use #ifdef to detect this new feature */ 00175 # define LOG_HAVE_DEVELOPER 1 00176 00177 /** 00178 * Adds a fixed string to each following line of output. NULL 00179 * removes the prefix again. Some logging implementations 00180 * might ignore the prefix. The prefix is copied by the 00181 * implementation, i.e. the caller can free it after this 00182 * call. 00183 */ 00184 virtual void setPrefix(const char *prefix) { 00185 // Avoid compiler warning 00186 prefix = NULL; 00187 } 00188 00189 /** 00190 * Returns the log file size [bytes]. 00191 */ 00192 virtual size_t getLogSize() = 0; 00193 }; 00194 00195 # define LOG Log::instance() 00196 00197 00198 END_NAMESPACE 00199 00200 /** @endcond */ 00201 #endif