Permalink
Browse files

This patch changes substantially the way in which errors and warnings

are logged. From now on, warnings, regular errors, and critical issues
are logged in the error log. That was supposed to be the default
behavior, although most of the code used the PRINT_ERROR macro which
was not working with the logging subsystem. In this change-set a few
new macros are introduced so regular functions can log errors without
having access to the connection or the virtual server objects. Most of
the PRINT_ERROR references ought to be replaced with either LOG_ERROR,
LOG_WARNING or LOG_CRITICAL. So far only the cherokee/server.c
references have been replaced for testing purposes. More related
commits to come.


git-svn-id: svn://cherokee-project.com/cherokee/trunk@3401 5dc97367-97f1-0310-9951-d761b3857238
  • Loading branch information...
1 parent 14a4723 commit 4d387f4f4e4758f6619f02093d9bf335d803ae6c @alobbs alobbs committed Jun 29, 2009
View
@@ -97,41 +97,49 @@
#endif
#ifdef HAVE_PTHREAD
-# define CHEROKEE_MUTEX_T(n) pthread_mutex_t n
-# define CHEROKEE_RWLOCK_T(n) pthread_rwlock_t n
-# define CHEROKEE_THREAD_JOIN(t) pthread_join(t,NULL)
-# define CHEROKEE_THREAD_SELF pthread_self()
-
-# define CHEROKEE_MUTEX_LOCK(m) pthread_mutex_lock(m)
-# define CHEROKEE_MUTEX_UNLOCK(m) pthread_mutex_unlock(m)
-# define CHEROKEE_MUTEX_INIT(m,n) pthread_mutex_init(m,n)
-# define CHEROKEE_MUTEX_DESTROY(m) pthread_mutex_destroy(m)
-# define CHEROKEE_MUTEX_TRY_LOCK(m) pthread_mutex_trylock(m)
-
-# define CHEROKEE_RWLOCK_INIT(m,n) pthread_rwlock_init(m,n)
-# define CHEROKEE_RWLOCK_READER(m) pthread_rwlock_rdlock(m)
-# define CHEROKEE_RWLOCK_WRITER(m) pthread_rwlock_wrlock(m)
-# define CHEROKEE_RWLOCK_TRYREADER(m) pthread_rwlock_tryrdlock(m)
-# define CHEROKEE_RWLOCK_TRYWRITER(m) pthread_rwlock_trywrlock(m)
-# define CHEROKEE_RWLOCK_UNLOCK(m) pthread_rwlock_unlock(m)
-# define CHEROKEE_RWLOCK_DESTROY(m) pthread_rwlock_destroy(m)
+# define CHEROKEE_MUTEX_T(n) pthread_mutex_t n
+# define CHEROKEE_RWLOCK_T(n) pthread_rwlock_t n
+# define CHEROKEE_THREAD_JOIN(t) pthread_join(t,NULL)
+# define CHEROKEE_THREAD_SELF pthread_self()
+
+# define CHEROKEE_THREAD_PROP_GET(p) pthread_getspecific(p)
+# define CHEROKEE_THREAD_PROP_SET(p,v) pthread_setspecific(p,v)
+# define CHEROKEE_THREAD_PROP_NEW(p,f) pthread_key_create2(p,f)
+
+# define CHEROKEE_MUTEX_LOCK(m) pthread_mutex_lock(m)
+# define CHEROKEE_MUTEX_UNLOCK(m) pthread_mutex_unlock(m)
+# define CHEROKEE_MUTEX_INIT(m,n) pthread_mutex_init(m,n)
+# define CHEROKEE_MUTEX_DESTROY(m) pthread_mutex_destroy(m)
+# define CHEROKEE_MUTEX_TRY_LOCK(m) pthread_mutex_trylock(m)
+
+# define CHEROKEE_RWLOCK_INIT(m,n) pthread_rwlock_init(m,n)
+# define CHEROKEE_RWLOCK_READER(m) pthread_rwlock_rdlock(m)
+# define CHEROKEE_RWLOCK_WRITER(m) pthread_rwlock_wrlock(m)
+# define CHEROKEE_RWLOCK_TRYREADER(m) pthread_rwlock_tryrdlock(m)
+# define CHEROKEE_RWLOCK_TRYWRITER(m) pthread_rwlock_trywrlock(m)
+# define CHEROKEE_RWLOCK_UNLOCK(m) pthread_rwlock_unlock(m)
+# define CHEROKEE_RWLOCK_DESTROY(m) pthread_rwlock_destroy(m)
#else
# define CHEROKEE_MUTEX_T(n)
# define CHEROKEE_RWLOCK_T(n)
# define CHEROKEE_THREAD_JOIN(t)
-# define CHEROKEE_THREAD_SELF 0
+# define CHEROKEE_THREAD_SELF 0
+
+# define CHEROKEE_THREAD_PROP_GET(p) NULL
+# define CHEROKEE_THREAD_PROP_SET(p,v) NULL
+# define CHEROKEE_THREAD_PROP_NEW(p,f) 0
# define CHEROKEE_MUTEX_LOCK(m)
# define CHEROKEE_MUTEX_UNLOCK(m)
# define CHEROKEE_MUTEX_INIT(m,n)
# define CHEROKEE_MUTEX_DESTROY(m)
-# define CHEROKEE_MUTEX_TRY_LOCK(m) 0
+# define CHEROKEE_MUTEX_TRY_LOCK(m) 0
# define CHEROKEE_RWLOCK_INIT(m,n)
# define CHEROKEE_RWLOCK_READER(m)
# define CHEROKEE_RWLOCK_WRITER(m)
-# define CHEROKEE_RWLOCK_TRYREADER(m) 0
-# define CHEROKEE_RWLOCK_TRYWRITER(m) 0
+# define CHEROKEE_RWLOCK_TRYREADER(m) 0
+# define CHEROKEE_RWLOCK_TRYWRITER(m) 0
# define CHEROKEE_RWLOCK_UNLOCK(m)
# define CHEROKEE_RWLOCK_DESTROY(m)
#endif
View
@@ -0,0 +1,85 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+/* Cherokee
+ *
+ * Authors:
+ * Alvaro Lopez Ortega <alvaro@alobbs.com>
+ *
+ * Copyright (C) 2001-2009 Alvaro Lopez Ortega
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include "common-internal.h"
+#include "error_log.h"
+#include "util.h"
+
+static cherokee_logger_t *default_error_logger = NULL;
+
+ret_t
+cherokee_error_log_set_log (cherokee_logger_t *logger)
+{
+ default_error_logger = logger;
+ return ret_ok;
+}
+
+ret_t
+cherokee_error_log (cherokee_error_type_t type, const char *format, ...)
+{
+ va_list ap;
+ cherokee_logger_t *logger;
+ cherokee_buffer_t tmp = CHEROKEE_BUF_INIT;
+
+ /* Error message formatting */
+ cherokee_buf_add_bogonow (&tmp, false);
+
+ switch (type) {
+ case cherokee_err_warning:
+ cherokee_buffer_add_str (&tmp, " (warning) ");
+ break;
+ case cherokee_err_error:
+ cherokee_buffer_add_str (&tmp, " (error) ");
+ break;
+ case cherokee_err_critical:
+ cherokee_buffer_add_str (&tmp, " (critical) ");
+ break;
+ }
+
+ va_start (ap, format);
+ cherokee_buffer_add_va_list (&tmp, format, ap);
+ va_end (ap);
+
+ /* Logging: 1st option - connection's logger */
+ logger = LOGGER (CHEROKEE_THREAD_PROP_GET (thread_logger_error_ptr));
+
+ /* Logging: 2nd option - default logger */
+ if (logger == NULL) {
+ logger = default_error_logger;
+ }
+
+ /* Do logging */
+ if (logger) {
+ cherokee_logger_write_error (logger, &tmp);
+ }
+
+ if ((logger == NULL) ||
+ (type == cherokee_err_critical))
+ {
+ fprintf (stderr, "%s", tmp.buf);
+ }
+
+ cherokee_buffer_mrproper (&tmp);
+ return ret_ok;
+}
View
@@ -0,0 +1,59 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+/* Cherokee
+ *
+ * Authors:
+ * Alvaro Lopez Ortega <alvaro@alobbs.com>
+ *
+ * Copyright (C) 2001-2009 Alvaro Lopez Ortega
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#if !defined (CHEROKEE_INSIDE_CHEROKEE_H) && !defined (CHEROKEE_COMPILATION)
+# error "Only <cherokee/cherokee.h> can be included directly, this file may disappear or change contents."
+#endif
+
+#ifndef CHEROKEE_ERROR_LOG_H
+#define CHEROKEE_ERROR_LOG_H
+
+#include <cherokee/common.h>
+#include <cherokee/logger.h>
+
+typedef enum {
+ cherokee_err_warning,
+ cherokee_err_error,
+ cherokee_err_critical
+} cherokee_error_type_t;
+
+#ifdef __GNUC__
+# define LOG_WARNING(fmt,arg...) cherokee_error_log(cherokee_err_warning, "%s:%d - "fmt, __FILE__, __LINE__, ##arg)
+# define LOG_ERROR(fmt,arg...) cherokee_error_log(cherokee_err_error, "%s:%d - "fmt, __FILE__, __LINE__, ##arg)
+# define LOG_CRITICAL(fmt,arg...) cherokee_error_log(cherokee_err_critical, "%s:%d - "fmt, __FILE__, __LINE__, ##arg)
+#else
+# define LOG_WARNING(t,fmt,...) cherokee_error_log(cherokee_err_warning, "%s:%d - "fmt, __FILE__, __LINE__, __VA_ARGS__)
+# define LOG_ERROR(t,fmt,...) cherokee_error_log(cherokee_err_error, "%s:%d - "fmt, __FILE__, __LINE__, __VA_ARGS__)
+# define LOG_CRITIAL(t,fmt,...) cherokee_error_log(cherokee_err_critical, "%s:%d - "fmt, __FILE__, __LINE__, __VA_ARGS__)
+#endif
+
+#define LOG_WARNING_S(str) LOG_WARNING("%s", str)
+#define LOG_ERROR_S(str) LOG_ERROR("%s", str)
+#define LOG_CRITICAL_S(str) LOG_CRITICAL("%s", str)
+
+
+ret_t cherokee_error_log (cherokee_error_type_t type, const char *format, ...);
+ret_t cherokee_error_log_set_log (cherokee_logger_t *logger);
+
+#endif /* CHEROKEE_ERROR_LOG_H */
View
@@ -29,6 +29,7 @@
#include "connection-protected.h"
#include "handler_fastcgi.h"
#include "source_interpreter.h"
+#include "error_log.h"
#include <unistd.h>
@@ -287,11 +288,7 @@ process_package (cherokee_fcgi_manager_t *mgr, cherokee_buffer_t *inbuf)
switch (type) {
case FCGI_STDERR:
if (CONN_VSRV(conn)->logger != NULL) {
- cherokee_buffer_t tmp = CHEROKEE_BUF_INIT;
-
- cherokee_buffer_add (&tmp, data, len);
- cherokee_logger_write_string (CONN_VSRV(conn)->logger, "%s\n", tmp.buf);
- cherokee_buffer_mrproper (&tmp);
+ LOG_ERROR ("%s\n", data);
}
exit(1);
break;
View
@@ -54,7 +54,7 @@
#include "header.h"
#include "header-protected.h"
#include "post.h"
-
+#include "error_log.h"
#define ENTRIES "handler,cgi"
@@ -609,9 +609,7 @@ manage_child_cgi_process (cherokee_handler_cgi_t *cgi, int pipe_cgi[2], int pipe
if (re >= 0) {
re = setuid (info.st_uid);
if (re != 0) {
- cherokee_logger_write_string (CONN_VSRV(conn)->logger,
- "%s: couldn't set UID %d\n",
- script, info.st_uid);
+ LOG_ERROR("%s: couldn't set UID %d\n", script, info.st_uid);
}
}
}
View
@@ -33,6 +33,7 @@
#include "thread.h"
#include "source_interpreter.h"
#include "bogotime.h"
+#include "error_log.h"
#include "fastcgi.h"
@@ -116,11 +117,7 @@ process_package (cherokee_handler_fcgi_t *hdl, cherokee_buffer_t *inbuf, cheroke
/* printf ("READ:STDERR (%d): %s", len, data?data:""); */
if (CONN_VSRV(conn)->logger != NULL) {
- cherokee_buffer_t tmp = CHEROKEE_BUF_INIT;
-
- cherokee_buffer_add (&tmp, data, len);
- cherokee_logger_write_string (CONN_VSRV(conn)->logger, "%s\n", tmp.buf);
- cherokee_buffer_mrproper (&tmp);
+ LOG_ERROR("%s\n", data);
}
else if (SOURCE_INT(hdl->src_ref)->debug) {
PRINT_MSG ("%.*s\n", len, data);
View
@@ -43,6 +43,7 @@
#include "iocache.h"
#include "util.h"
#include "handler_dirlist.h"
+#include "error_log.h"
#define ENTRIES "handler,file"
@@ -170,10 +171,8 @@ check_cached (cherokee_handler_file_t *fhdl)
req_time = cherokee_dtm_str2time (header);
if (unlikely (req_time == DTM_TIME_EVAL)) {
- cherokee_logger_write_string (
- CONN_VSRV(conn)->logger,
- "Warning: Unparseable time '%s'\n",
- header);
+ LOG_WARNING("Unparseable time '%s'\n", header);
+
/* restore end of line */
*end = tmp;
return ret_ok;
@@ -251,10 +250,8 @@ check_cached (cherokee_handler_file_t *fhdl)
req_time = cherokee_dtm_str2time (header);
if (unlikely (req_time == DTM_TIME_EVAL)) {
- cherokee_logger_write_string (
- CONN_VSRV(conn)->logger,
- "Warning: Unparseable time '%s'\n",
- header);
+ LOG_WARNING ("Unparseable time '%s'\n", header);
+
*end = tmp;
return ret_ok;
}
View
@@ -58,6 +58,7 @@ cherokee_init (void)
/* Init the bogotime mechanism
*/
cherokee_bogotime_init();
+ cherokee_bogotime_update();
/* Init threading stuff
*/
View
@@ -259,21 +259,6 @@ cherokee_logger_write_access (cherokee_logger_t *logger, void *conn)
ret_t
-cherokee_logger_write_error (cherokee_logger_t *logger, void *conn)
-{
- ret_t ret = ret_error;
-
- if (logger->write_error) {
- CHEROKEE_MUTEX_LOCK (&PRIV(logger)->mutex);
- ret = logger->write_error (logger, conn);
- CHEROKEE_MUTEX_UNLOCK (&PRIV(logger)->mutex);
- }
-
- return ret;
-}
-
-
-ret_t
cherokee_logger_get_error_writer (cherokee_logger_t *logger,
cherokee_logger_writer_t **writer)
{
@@ -289,28 +274,20 @@ cherokee_logger_get_error_writer (cherokee_logger_t *logger,
}
-
ret_t
-cherokee_logger_write_string (cherokee_logger_t *logger, const char *format, ...)
+cherokee_logger_write_error (cherokee_logger_t *logger,
+ cherokee_buffer_t *error)
{
- va_list ap;
+ ret_t ret;
if (logger == NULL)
return ret_ok;
- if (logger->write_string) {
- ret_t ret;
- cherokee_buffer_t tmp = CHEROKEE_BUF_INIT;
-
+ if (logger->write_error) {
CHEROKEE_MUTEX_LOCK(&PRIV(logger)->mutex);
- va_start (ap, format);
- cherokee_buffer_add_va_list (&tmp, (char *)format, ap);
- va_end (ap);
+ ret = logger->write_error (logger, error);
CHEROKEE_MUTEX_UNLOCK(&PRIV(logger)->mutex);
- ret = logger->write_string (logger, tmp.buf);
-
- cherokee_buffer_mrproper (&tmp);
return ret;
}
Oops, something went wrong.

0 comments on commit 4d387f4

Please sign in to comment.