2 * Funambol is a mobile platform developed by Funambol, Inc.
3 * Copyright (C) 2003 - 2007 Funambol, Inc.
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.
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
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,
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.
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.
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".
37 #include "base/globalsdef.h"
38 #include "base/posixlog.h"
39 #include "base/fscapi.h"
40 #include "base/util/utils.h"
48 POSIXLog::POSIXLog() :
52 logRedirectStderr(false),
58 void POSIXLog::setLogPath(const char* configLogPath) {
59 logPath.sprintf("%s/", configLogPath ? configLogPath : "./");
62 void POSIXLog::setLogName(const char* configLogName) {
63 logName.sprintf("%s", configLogName ? configLogName : LOG_NAME);
66 void POSIXLog::setLogFile(const char *path, const char* name, bool redirectStderr) {
67 if (path != logPath.c_str()) {
70 if (name != logName.c_str()) {
73 logRedirectStderr = redirectStderr;
75 if (logFile != NULL) {
79 logFileStdout = false;
81 if (!strcmp(name, "-")) {
85 char *filename = new char[strlen(path) + strlen(name) + 3];
87 sprintf(filename, "%s/%s", path, name);
88 logFile = fopen(filename, "a+" );
91 logFile = fopen(name, "a+" );
97 time_t t = time(NULL);
99 // We log UTC at the start of each line.
100 // Log the current user's time offset.
101 localtime_r(&t, &tm);
102 strftime(buffer, sizeof(buffer),
103 "local timezone: %Z = GMT %z",
105 developer("%s", buffer);
106 asctime_r(&tm, buffer);
107 developer("local time: %s", buffer);
109 asctime_r(&tm, buffer);
110 developer("world time: %s", buffer);
113 if (redirectStderr && logFile) {
115 // remember original stderr
118 // overwrite stderr with log file fd,
119 // closing the current stderr if necessary
120 dup2(fileno(logFile), 2);
123 // restore original stderr
129 POSIXLog::~POSIXLog() {
130 if (logFile != NULL) {
135 void POSIXLog::error(const char* msg, ...) {
137 va_start (argList, msg);
138 printMessage(LOG_LEVEL_NONE, LOG_ERROR, msg, argList);
142 void POSIXLog::info(const char* msg, ...) {
143 if (isLoggable(LOG_LEVEL_INFO)) {
145 va_start (argList, msg);
146 printMessage(LOG_LEVEL_INFO, LOG_INFO, msg, argList);
151 void POSIXLog::developer(const char* msg, ...) {
152 if (isLoggable(LOG_LEVEL_INFO)) {
154 va_start (argList, msg);
155 printMessage(LOG_LEVEL_DEBUG, LOG_DEBUG, msg, argList);
160 void POSIXLog::debug(const char* msg, ...) {
161 if (isLoggable(LOG_LEVEL_DEBUG)) {
163 va_start (argList, msg);
164 printMessage(LOG_LEVEL_DEBUG, LOG_DEBUG, msg, argList);
169 void POSIXLog::printLine(bool firstLine,
171 const char *fullTime,
172 const char *shortTime,
175 const char *levelPrefix,
178 FILE *out = getLogFile();
183 fprintf(out, "%s [%s] %s%s\n",
184 logFile ? utcTime : shortTime,
189 fprintf(out, "[%s] %s%s\n",
197 void POSIXLog::printMessage(LogLevel level, const char* levelPrefix, const char* msg, va_list argList) {
198 time_t t = time(NULL);
201 char fullTime[64], shortTime[32];
204 localtime_r(&t, &sys_time);
205 gmtime_r(&t, &utc_time);
207 strftime(fullTime, sizeof(fullTime), "%F %T GMT %z", &sys_time);
208 strftime(shortTime, sizeof(shortTime), "%T", &sys_time);
209 sprintf(utcTime, "%02d:%02d:%02d GMT",
214 if (!logFileStdout && !logFile) {
219 buffer.vsprintf(msg, argList);
220 const char *start = buffer.c_str();
221 const char *eol = strchr(start, '\n');
222 bool firstLine = true;
224 /* hack: StringBuffer does not really allow write access, but do it anyway */
237 eol = strchr(start, '\n');
250 void POSIXLog::reset(const char* title) {
251 setLogFile(logPath, logName, logRedirectStderr);
254 ftruncate(fileno(logFile), 0);
259 size_t POSIXLog::getLogSize() {
263 ret = fgetsize(logFile);
272 Log &Log::instance() {
274 logger = new POSIXLog();