- Main Page
- Modules
- Classes
- Files

- File List

# 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 <sstream>
    00028 #include <ctime>
    00029
    00030 namespace Passenger {
    00031
    00032 using namespace std;
    00033
    00034 extern unsigned int _logLevel;
    00035 extern ostream *_logStream;
    00036 extern ostream *_debugStream;
    00037
    00038 unsigned int getLogLevel();
    00039 void setLogLevel(unsigned int value);
    00040 void setDebugFile(const char *logFile = NULL);
    00041
    00042 /**
    00043  * Write the given expression to the given stream.
    00044  *
    00045  * @param expr The expression to write.
    00046  * @param stream A pointer to an object that accepts the '<<' operator.
    00047  */
    00048 #define P_LOG_TO(expr, stream) \
    00049         do { \
    00050                 if (stream != 0) { \
    00051                         time_t the_time;                        \
    00052                         struct tm *the_tm;                      \
    00053                         char datetime_buf[60];                  \
    00054                         struct timeval tv;                      \
    00055                         std::stringstream sstream;              \
    00056                                                                 \
    00057                         the_time = time(NULL);                  \
    00058                         the_tm = localtime(&the_time);          \
    00059                         strftime(datetime_buf, sizeof(datetime_buf), "%F %H:%M:%S", the_tm); \
    00060                         gettimeofday(&tv, NULL); \
    00061                         sstream << \
    00062                                 "[ pid=" << getpid() << " file=" << __FILE__ << ":" << __LINE__ << \
    00063                                 " time=" << datetime_buf << "." << (tv.tv_usec / 1000) << " ]:" << \
    00064                                 "\n  " << expr << std::endl;    \
    00065                         *stream << sstream.str();               \
    00066                         stream->flush();                        \
    00067                 } \
    00068         } while (false)
    00069
    00070 /**
    00071  * Write the given expression to the log stream.
    00072  */
    00073 #define P_LOG(expr) P_LOG_TO(expr, Passenger::_logStream)
    00074
    00075 /**
    00076  * Write the given expression, which represents a warning,
    00077  * to the log stream.
    00078  */
    00079 #define P_WARN(expr) P_LOG(expr)
    00080
    00081 /**
    00082  * Write the given expression, which represents an error,
    00083  * to the log stream.
    00084  */
    00085 #define P_ERROR(expr) P_LOG(expr)
    00086
    00087 /**
    00088  * Write the given expression, which represents a debugging message,
    00089  * to the log stream.
    00090  */
    00091 #define P_DEBUG(expr) P_TRACE(1, expr)
    00092
    00093 #ifdef PASSENGER_DEBUG
    00094         #define P_TRACE(level, expr) \
    00095                 do { \
    00096                         if (Passenger::_logLevel >= level) { \
    00097                                 P_LOG_TO(expr, Passenger::_debugStream); \
    00098                         } \
    00099                 } while (false)
    00100
    00101         #define P_ASSERT(expr, result_if_failed, message) \
    00102                 do { \
    00103                         if (!(expr)) { \
    00104                                 P_ERROR("Assertion failed: " << message); \
    00105                                 return result_if_failed; \
    00106                         } \
    00107                 } while (false)
    00108 #else
    00109         #define P_TRACE(level, expr) do { /* nothing */ } while (false)
    00110
    00111         #define P_ASSERT(expr, result_if_failed, message) do { /* nothing */ } while (false)
    00112 #endif
    00113
    00114 } // namespace Passenger
    00115
    00116 #endif /* _PASSENGER_LOGGING_H_ */
    00117