Back to index

php5  5.3.10
zlog.c
Go to the documentation of this file.
00001 
00002        /* $Id: zlog.c,v 1.7 2008/05/22 21:08:32 anight Exp $ */
00003        /* (c) 2004-2007 Andrei Nigmatulin */
00004 
00005 #include "fpm_config.h"
00006 
00007 #include <stdio.h>
00008 #include <unistd.h>
00009 #include <time.h>
00010 #include <string.h>
00011 #include <stdarg.h>
00012 #include <sys/time.h>
00013 #include <errno.h>
00014 
00015 #include "php_syslog.h"
00016 
00017 #include "zlog.h"
00018 #include "fpm.h"
00019 
00020 #define MAX_LINE_LENGTH 1024
00021 
00022 static int zlog_fd = -1;
00023 static int zlog_level = ZLOG_NOTICE;
00024 static int launched = 0;
00025 
00026 static const char *level_names[] = {
00027        [ZLOG_DEBUG]   = "DEBUG",
00028        [ZLOG_NOTICE]  = "NOTICE",
00029        [ZLOG_WARNING] = "WARNING",
00030        [ZLOG_ERROR]   = "ERROR",
00031        [ZLOG_ALERT]   = "ALERT",
00032 };
00033 
00034 #ifdef HAVE_SYSLOG_H
00035 const int syslog_priorities[] = {
00036        [ZLOG_DEBUG]   = LOG_DEBUG,
00037        [ZLOG_NOTICE]  = LOG_NOTICE,
00038        [ZLOG_WARNING] = LOG_WARNING,
00039        [ZLOG_ERROR]   = LOG_ERR,
00040        [ZLOG_ALERT]   = LOG_ALERT,
00041 };
00042 #endif
00043 
00044 const char *zlog_get_level_name(int log_level) /* {{{ */
00045 {
00046        if (log_level < 0) {
00047               log_level = zlog_level;
00048        } else if (log_level < ZLOG_DEBUG || log_level > ZLOG_ALERT) {
00049               return "unknown value";
00050        }
00051 
00052        return level_names[log_level];
00053 }
00054 /* }}} */
00055 
00056 void zlog_set_launched(void) {
00057        launched = 1;
00058 }
00059 
00060 size_t zlog_print_time(struct timeval *tv, char *timebuf, size_t timebuf_len) /* {{{ */
00061 {
00062        struct tm t;
00063        size_t len;
00064 
00065        len = strftime(timebuf, timebuf_len, "[%d-%b-%Y %H:%M:%S", localtime_r((const time_t *) &tv->tv_sec, &t));
00066        if (zlog_level == ZLOG_DEBUG) {
00067               len += snprintf(timebuf + len, timebuf_len - len, ".%06d", (int) tv->tv_usec);
00068        }
00069        len += snprintf(timebuf + len, timebuf_len - len, "] ");
00070        return len;
00071 }
00072 /* }}} */
00073 
00074 int zlog_set_fd(int new_fd) /* {{{ */
00075 {
00076        int old_fd = zlog_fd;
00077 
00078        zlog_fd = new_fd;
00079        return old_fd;
00080 }
00081 /* }}} */
00082 
00083 int zlog_set_level(int new_value) /* {{{ */
00084 {
00085        int old_value = zlog_level;
00086 
00087        if (new_value < ZLOG_DEBUG || new_value > ZLOG_ALERT) return old_value;
00088 
00089        zlog_level = new_value;
00090        return old_value;
00091 }
00092 /* }}} */
00093 
00094 void zlog_ex(const char *function, int line, int flags, const char *fmt, ...) /* {{{ */
00095 {
00096        struct timeval tv;
00097        char buf[MAX_LINE_LENGTH];
00098        const size_t buf_size = MAX_LINE_LENGTH;
00099        va_list args;
00100        size_t len = 0;
00101        int truncated = 0;
00102        int saved_errno;
00103 
00104        if ((flags & ZLOG_LEVEL_MASK) < zlog_level) {
00105               return;
00106        }
00107 
00108        saved_errno = errno;
00109 #ifdef HAVE_SYSLOG_H
00110        if (zlog_fd == ZLOG_SYSLOG /* && !fpm_globals.is_child */) {
00111               len = 0;
00112               if (zlog_level == ZLOG_DEBUG) {
00113                      len += snprintf(buf, buf_size, "[%s] %s(), line %d: ", level_names[flags & ZLOG_LEVEL_MASK], function, line);
00114               } else {
00115                      len += snprintf(buf, buf_size, "[%s] ", level_names[flags & ZLOG_LEVEL_MASK]);
00116               }
00117        } else
00118 #endif
00119        {
00120               if (!fpm_globals.is_child) {
00121                      gettimeofday(&tv, 0);
00122                      len = zlog_print_time(&tv, buf, buf_size);
00123               }
00124               if (zlog_level == ZLOG_DEBUG) {
00125                      if (!fpm_globals.is_child) {
00126                             len += snprintf(buf + len, buf_size - len, "%s: pid %d, %s(), line %d: ", level_names[flags & ZLOG_LEVEL_MASK], getpid(), function, line);
00127                      } else {
00128                             len += snprintf(buf + len, buf_size - len, "%s: %s(), line %d: ", level_names[flags & ZLOG_LEVEL_MASK], function, line);
00129                      }
00130               } else {
00131                      len += snprintf(buf + len, buf_size - len, "%s: ", level_names[flags & ZLOG_LEVEL_MASK]);
00132               }
00133        }
00134 
00135        if (len > buf_size - 1) {
00136               truncated = 1;
00137        }
00138 
00139        if (!truncated) {
00140               va_start(args, fmt);
00141               len += vsnprintf(buf + len, buf_size - len, fmt, args);
00142               va_end(args);
00143               if (len >= buf_size) {
00144                      truncated = 1;
00145               }
00146        }
00147 
00148        if (!truncated) {
00149               if (flags & ZLOG_HAVE_ERRNO) {
00150                      len += snprintf(buf + len, buf_size - len, ": %s (%d)", strerror(saved_errno), saved_errno);
00151                      if (len >= buf_size) {
00152                             truncated = 1;
00153                      }
00154               }
00155        }
00156 
00157        if (truncated) {
00158               memcpy(buf + buf_size - sizeof("..."), "...", sizeof("...") - 1);
00159               len = buf_size - 1;
00160        }
00161 
00162 #ifdef HAVE_SYSLOG_H
00163        if (zlog_fd == ZLOG_SYSLOG) {
00164               buf[len] = '\0';
00165               php_syslog(syslog_priorities[zlog_level], "%s", buf);
00166               buf[len++] = '\n';
00167        } else 
00168 #endif
00169        {
00170               buf[len++] = '\n';
00171               write(zlog_fd > -1 ? zlog_fd : STDERR_FILENO, buf, len);
00172        }
00173 
00174        if (zlog_fd != STDERR_FILENO && zlog_fd != -1 && !launched && (flags & ZLOG_LEVEL_MASK) >= ZLOG_NOTICE) {
00175               write(STDERR_FILENO, buf, len);
00176        }
00177 }
00178 /* }}} */
00179