Imported Upstream version 0.8~alpha1
[platform/upstream/syncevolution.git] / src / client-api / src / include / posix / base / posixlog.h
1 /*
2  * Funambol is a mobile platform developed by Funambol, Inc. 
3  * Copyright (C) 2003 - 2007 Funambol, Inc.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Affero General Public License version 3 as published by
7  * the Free Software Foundation with the addition of the following permission 
8  * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
9  * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE 
10  * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
11  * 
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  * 
17  * You should have received a copy of the GNU Affero General Public License 
18  * along with this program; if not, see http://www.gnu.org/licenses or write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20  * MA 02110-1301 USA.
21  * 
22  * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite 
23  * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
24  * 
25  * The interactive user interfaces in modified source and object code versions
26  * of this program must display Appropriate Legal Notices, as required under
27  * Section 5 of the GNU Affero General Public License version 3.
28  * 
29  * In accordance with Section 7(b) of the GNU Affero General Public License
30  * version 3, these Appropriate Legal Notices must retain the display of the
31  * "Powered by Funambol" logo. If the display of the logo is not reasonably 
32  * feasible for technical reasons, the Appropriate Legal Notices must display
33  * the words "Powered by Funambol".
34  */
35
36 #ifndef INCL_POSIX_LOG
37 # define INCL_POSIX_LOG
38 /** @cond DEV */
39
40 #include <base/fscapi.h>
41 #include <base/Log.h>
42 #include "base/util/StringBuffer.h"
43
44 #include <stdio.h>
45 #include <time.h>
46 #include "base/globalsdef.h"
47
48 BEGIN_NAMESPACE
49
50 /**
51  * extended API, can only be used if it is certain that
52  * Log::instance() returns a POSIXLog
53  */
54 class POSIXLog : public Log {
55  public:
56
57     POSIXLog();
58     ~POSIXLog();
59
60     /**
61      * Opens the specified file for logging of messages.
62      *
63      * By default the LOG instance of the Log class will
64      * create the file specified via its set methods
65      * as soon as the first message needs to be printed or
66      * when explicitly asking for a reset.
67      *
68      * By calling this function instead one gets more detailed
69      * control over logging and avoids the (currently) insecurely
70      * implemented handling of file name strings in the Log class.
71      *
72      * @param path            directory where file is to be created, can be NULL
73      * @param name            file name relative to path or "-" when asking for
74      *                        logging to stdout
75      * @param redirectStderr  if true, then file descriptor 2 (stderr)
76      *                        will also be redirected into the log file;
77      *                        the original stderr is preserved and will be
78      *                        restored when turning this redirection off
79      */
80     virtual void setLogFile(const char *path, const char* name, bool redirectStderr = false);
81
82     /**
83      * returns active log file or NULL if none set (e.g. if logging to stdout directly)
84      */
85     virtual FILE *getLogFile() { return logFile; }
86
87     /**
88      * if a client developer wants to ignore the prefix, he can
89      * derive his own Log implementation from POSIXLog, override this
90      * call and then install his implementation via Log::setLogger()
91      */
92     virtual void setPrefix(const char *prefix) { this->prefix = prefix ? prefix : ""; }
93     virtual const StringBuffer &getPrefix() const { return prefix; }
94
95     virtual void setLogPath(const char*  configLogPath);
96     virtual void setLogName(const char*  configLogName);
97     virtual void error(const char*  msg, ...);
98     virtual void info(const char*  msg, ...);
99     virtual void developer(const char*  msg, ...);
100     virtual void debug(const char*  msg, ...);
101     virtual void reset(const char* title = NULL);
102     virtual size_t getLogSize();
103
104  protected:
105     /**
106      * Prints a single line to the current log file.
107      * Can be overridden by derived class to also print
108      * in a different way.
109      *
110      * @param firstLine     true if this is the first line of a new message
111      * @param time          unformatted time stamp for line
112      * @param fullTime      a time string including date and GMT offset
113      * @param shortTime     a time string including just the local time of day in
114      *                      the preferred time format according to the current locale
115      * @param utcTime       a time string including just the UTC time of day in "hh:mm:ss UTC" format
116      * @param level         the severity of the report
117      * @param levelPrefix   a string representing the severity (may differ from level, e.g. for Log::developer())
118      * @param line          the actual message string
119      */
120     virtual void printLine(bool firstLine,
121                            time_t time,
122                            const char *fullTime,
123                            const char *shortTime,
124                            const char *utcTime,
125                            LogLevel level,
126                            const char *levelPrefix,
127                            const char *line);
128
129  private:
130     FILE* logFile;
131     bool logFileStdout;
132     StringBuffer logName;
133     StringBuffer logPath;
134     bool logRedirectStderr;
135
136     /** a copy of stderr before it was redirected */
137     int fderr;
138
139     /**
140      * additional prefix for each line
141      */
142     StringBuffer prefix;
143
144     void printMessage(LogLevel level, const char* levelPrefix, const char* msg, va_list argList);
145 };
146
147 #define POSIX_LOG ((POSIXLog &)Log::instance())
148
149
150 END_NAMESPACE
151
152 /** @endcond */
153 #endif