Back to index

php5  5.3.10
Defines | Functions | Variables
zlog.c File Reference
#include "fpm_config.h"
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <stdarg.h>
#include <sys/time.h>
#include <errno.h>
#include "php_syslog.h"
#include "zlog.h"
#include "fpm.h"

Go to the source code of this file.

Defines

#define MAX_LINE_LENGTH   1024

Functions

const char * zlog_get_level_name (int log_level)
void zlog_set_launched (void)
size_t zlog_print_time (struct timeval *tv, char *timebuf, size_t timebuf_len)
int zlog_set_fd (int new_fd)
int zlog_set_level (int new_value)
void zlog_ex (const char *function, int line, int flags, const char *fmt,...)

Variables

static int zlog_fd = -1
static int zlog_level = ZLOG_NOTICE
static int launched = 0
static const char * level_names []

Define Documentation

#define MAX_LINE_LENGTH   1024

Definition at line 20 of file zlog.c.


Function Documentation

void zlog_ex ( const char *  function,
int  line,
int  flags,
const char *  fmt,
  ... 
)

Definition at line 94 of file zlog.c.

{
       struct timeval tv;
       char buf[MAX_LINE_LENGTH];
       const size_t buf_size = MAX_LINE_LENGTH;
       va_list args;
       size_t len = 0;
       int truncated = 0;
       int saved_errno;

       if ((flags & ZLOG_LEVEL_MASK) < zlog_level) {
              return;
       }

       saved_errno = errno;
#ifdef HAVE_SYSLOG_H
       if (zlog_fd == ZLOG_SYSLOG /* && !fpm_globals.is_child */) {
              len = 0;
              if (zlog_level == ZLOG_DEBUG) {
                     len += snprintf(buf, buf_size, "[%s] %s(), line %d: ", level_names[flags & ZLOG_LEVEL_MASK], function, line);
              } else {
                     len += snprintf(buf, buf_size, "[%s] ", level_names[flags & ZLOG_LEVEL_MASK]);
              }
       } else
#endif
       {
              if (!fpm_globals.is_child) {
                     gettimeofday(&tv, 0);
                     len = zlog_print_time(&tv, buf, buf_size);
              }
              if (zlog_level == ZLOG_DEBUG) {
                     if (!fpm_globals.is_child) {
                            len += snprintf(buf + len, buf_size - len, "%s: pid %d, %s(), line %d: ", level_names[flags & ZLOG_LEVEL_MASK], getpid(), function, line);
                     } else {
                            len += snprintf(buf + len, buf_size - len, "%s: %s(), line %d: ", level_names[flags & ZLOG_LEVEL_MASK], function, line);
                     }
              } else {
                     len += snprintf(buf + len, buf_size - len, "%s: ", level_names[flags & ZLOG_LEVEL_MASK]);
              }
       }

       if (len > buf_size - 1) {
              truncated = 1;
       }

       if (!truncated) {
              va_start(args, fmt);
              len += vsnprintf(buf + len, buf_size - len, fmt, args);
              va_end(args);
              if (len >= buf_size) {
                     truncated = 1;
              }
       }

       if (!truncated) {
              if (flags & ZLOG_HAVE_ERRNO) {
                     len += snprintf(buf + len, buf_size - len, ": %s (%d)", strerror(saved_errno), saved_errno);
                     if (len >= buf_size) {
                            truncated = 1;
                     }
              }
       }

       if (truncated) {
              memcpy(buf + buf_size - sizeof("..."), "...", sizeof("...") - 1);
              len = buf_size - 1;
       }

#ifdef HAVE_SYSLOG_H
       if (zlog_fd == ZLOG_SYSLOG) {
              buf[len] = '\0';
              php_syslog(syslog_priorities[zlog_level], "%s", buf);
              buf[len++] = '\n';
       } else 
#endif
       {
              buf[len++] = '\n';
              write(zlog_fd > -1 ? zlog_fd : STDERR_FILENO, buf, len);
       }

       if (zlog_fd != STDERR_FILENO && zlog_fd != -1 && !launched && (flags & ZLOG_LEVEL_MASK) >= ZLOG_NOTICE) {
              write(STDERR_FILENO, buf, len);
       }
}

Here is the call graph for this function:

const char* zlog_get_level_name ( int  log_level)

Definition at line 44 of file zlog.c.

{
       if (log_level < 0) {
              log_level = zlog_level;
       } else if (log_level < ZLOG_DEBUG || log_level > ZLOG_ALERT) {
              return "unknown value";
       }

       return level_names[log_level];
}

Here is the caller graph for this function:

size_t zlog_print_time ( struct timeval *  tv,
char *  timebuf,
size_t  timebuf_len 
)

Definition at line 60 of file zlog.c.

{
       struct tm t;
       size_t len;

       len = strftime(timebuf, timebuf_len, "[%d-%b-%Y %H:%M:%S", localtime_r((const time_t *) &tv->tv_sec, &t));
       if (zlog_level == ZLOG_DEBUG) {
              len += snprintf(timebuf + len, timebuf_len - len, ".%06d", (int) tv->tv_usec);
       }
       len += snprintf(timebuf + len, timebuf_len - len, "] ");
       return len;
}

Here is the caller graph for this function:

int zlog_set_fd ( int  new_fd)

Definition at line 74 of file zlog.c.

{
       int old_fd = zlog_fd;

       zlog_fd = new_fd;
       return old_fd;
}

Here is the caller graph for this function:

void zlog_set_launched ( void  )

Definition at line 56 of file zlog.c.

                             {
       launched = 1;
}

Here is the caller graph for this function:

int zlog_set_level ( int  new_value)

Definition at line 83 of file zlog.c.

{
       int old_value = zlog_level;

       if (new_value < ZLOG_DEBUG || new_value > ZLOG_ALERT) return old_value;

       zlog_level = new_value;
       return old_value;
}

Here is the caller graph for this function:


Variable Documentation

int launched = 0 [static]

Definition at line 24 of file zlog.c.

const char* level_names[] [static]
Initial value:
 {
       [ZLOG_DEBUG]   = "DEBUG",
       [ZLOG_NOTICE]  = "NOTICE",
       [ZLOG_WARNING] = "WARNING",
       [ZLOG_ERROR]   = "ERROR",
       [ZLOG_ALERT]   = "ALERT",
}

Definition at line 26 of file zlog.c.

int zlog_fd = -1 [static]

Definition at line 22 of file zlog.c.

Definition at line 23 of file zlog.c.