Logging.h

00001 /*
00002  *  Phusion Passenger - http://www.modrails.com/
00003  *  Copyright (C) 2008  Phusion
00004  *
00005  *  Phusion Passenger is a trademark of Hongli Lai & Ninh Bui.
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; version 2 of the License.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License along
00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
00018  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00019  */
00020 #ifndef _PASSENGER_LOGGING_H_
00021 #define _PASSENGER_LOGGING_H_
00022 
00023 #include <sys/types.h>
00024 #include <sys/time.h>
00025 #include <unistd.h>
00026 #include <ostream>
00027 #include <ctime>
00028 
00029 namespace Passenger {
00030 
00031 using namespace std;
00032 
00033 extern unsigned int _logLevel;
00034 extern ostream *_logStream;
00035 extern ostream *_debugStream;
00036 
00037 unsigned int getLogLevel();
00038 void setLogLevel(unsigned int value);
00039 void setDebugFile(const char *logFile = NULL);
00040 
00041 /**
00042  * Write the given expression to the log stream.
00043  */
00044 #define P_LOG(expr) \
00045         do { \
00046                 if (Passenger::_logStream != 0) { \
00047                         time_t the_time = time(NULL); \
00048                         struct tm *the_tm = localtime(&the_time); \
00049                         char datetime_buf[60]; \
00050                         struct timeval tv; \
00051                         strftime(datetime_buf, sizeof(datetime_buf), "%x %H:%M:%S", the_tm); \
00052                         gettimeofday(&tv, NULL); \
00053                         *Passenger::_logStream << \
00054                                 "[ pid=" << getpid() << " file=" << __FILE__ << ":" << __LINE__ << \
00055                                 " time=" << datetime_buf << "." << (tv.tv_usec / 1000) << " ]:" << \
00056                                 "\n  " << expr << std::endl; \
00057                 } \
00058         } while (false)
00059 
00060 /**
00061  * Write the given expression, which represents a warning,
00062  * to the log stream.
00063  */
00064 #define P_WARN(expr) P_LOG(expr)
00065 
00066 /**
00067  * Write the given expression, which represents an error,
00068  * to the log stream.
00069  */
00070 #define P_ERROR(expr) P_LOG(expr)
00071 
00072 /**
00073  * Write the given expression, which represents a debugging message,
00074  * to the log stream.
00075  */
00076 #define P_DEBUG(expr) P_TRACE(1, expr)
00077 
00078 #ifdef PASSENGER_DEBUG
00079         #define P_TRACE(level, expr) \
00080                 do { \
00081                         if (Passenger::_logLevel >= level) { \
00082                                 if (Passenger::_debugStream != 0) { \
00083                                         time_t the_time = time(NULL); \
00084                                         struct tm *the_tm = localtime(&the_time); \
00085                                         char datetime_buf[60]; \
00086                                         struct timeval tv; \
00087                                         strftime(datetime_buf, sizeof(datetime_buf), "%x %H:%M:%S", the_tm); \
00088                                         gettimeofday(&tv, NULL); \
00089                                         *Passenger::_debugStream << \
00090                                                 "[ pid=" << getpid() << " file=" << __FILE__ << ":" << __LINE__ << \
00091                                                 " time=" << datetime_buf << "." << (tv.tv_usec / 1000) << " ]:" << \
00092                                                 "\n  " << expr << std::endl; \
00093                                 } \
00094                         } \
00095                 } while (false)
00096         
00097         #define P_ASSERT(expr, result_if_failed, message) \
00098                 do { \
00099                         if (!(expr)) { \
00100                                 P_ERROR("Assertion failed: " << message); \
00101                                 return result_if_failed; \
00102                         } \
00103                 } while (false)
00104 #else
00105         #define P_TRACE(level, expr) do { /* nothing */ } while (false)
00106         
00107         #define P_ASSERT(expr, result_if_failed, message) do { /* nothing */ } while (false)
00108 #endif
00109 
00110 } // namespace Passenger
00111 
00112 #endif /* _PASSENGER_LOGGING_H_ */
00113 

Generated on Fri Jan 23 08:28:57 2009 for Passenger by  doxygen 1.4.7