From 499129093599f2395331c53fc2b911ed66b97a22 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Tue, 5 Apr 2005 22:56:17 +0000 Subject: [PATCH] Revise control spec and implementation to allow all log messages to be sent to controller with their severities intact. svn:r4010 --- doc/control-spec.txt | 11 ++++++ src/common/log.c | 12 +++++++ src/common/log.h | 2 ++ src/or/config.c | 3 +- src/or/control.c | 85 ++++++++++++++++++++++++++++++++++++++------ src/or/or.h | 1 + 6 files changed, 103 insertions(+), 11 deletions(-) diff --git a/doc/control-spec.txt b/doc/control-spec.txt index decff5280e..8e00f30464 100644 --- a/doc/control-spec.txt +++ b/doc/control-spec.txt @@ -215,11 +215,22 @@ the message. Message [NUL-terminated] + + 0x0006 -- New descriptors available OR List [NUL-terminated, comma-delimited list of OR identity] + 0x0007 -- Debug message occurred + 0x0008 -- Info message occurred + 0x0009 -- Notice message occurred + 0x000A -- Warning message occurred + 0x000B -- Error message occurred + + Message [NUL-terminated] + + 3.8. AUTHENTICATE (Type 0x0007) Sent from the client to the server. Contains a 'magic cookie' to prove diff --git a/src/common/log.c b/src/common/log.c index 863f18b69f..ce33810a63 100644 --- a/src/common/log.c +++ b/src/common/log.c @@ -373,6 +373,18 @@ int add_callback_log(int loglevelMin, int loglevelMax, log_callback cb) return 0; } +void change_callback_log_severity(int loglevelMin, int loglevelMax, + log_callback cb) +{ + logfile_t *lf; + for (lf = logfiles; lf; lf = lf->next) { + if (lf->callback == cb) { + lf->loglevel = loglevelMin; + lf->max_loglevel = loglevelMax; + } + } +} + /** Close any log handlers added by add_temp_log or marked by mark_logs_temp */ void close_temp_logs(void) { diff --git a/src/common/log.h b/src/common/log.h index aa4d7ca91f..bfcb557e6d 100644 --- a/src/common/log.h +++ b/src/common/log.h @@ -69,6 +69,8 @@ void close_temp_logs(void); void mark_logs_temp(void); void configure_libevent_logging(void); void suppress_libevent_log_msg(const char *msg); +void change_callback_log_severity(int loglevelMin, int loglevelMax, + log_callback cb); /* Outputs a message to stdout */ void _log(int severity, const char *format, ...) CHECK_PRINTF(2,3); diff --git a/src/or/config.c b/src/or/config.c index d143d85064..97039fa96a 100644 --- a/src/or/config.c +++ b/src/or/config.c @@ -324,7 +324,8 @@ options_act(void) { /* Close the temporary log we used while starting up, if it isn't already * gone. */ close_temp_logs(); - add_callback_log(LOG_NOTICE, LOG_ERR, control_event_logmsg); + add_callback_log(LOG_ERR, LOG_ERR, control_event_logmsg); + adjust_event_log_severity(); options->_ConnLimit = set_max_file_descriptors((unsigned)options->ConnLimit, MAXCONNECTIONS); diff --git a/src/or/control.c b/src/or/control.c index c494993932..514ff0c6bd 100644 --- a/src/or/control.c +++ b/src/or/control.c @@ -74,9 +74,14 @@ const char control_c_id[] = "$Id$"; #define EVENT_STREAM_STATUS 0x0002 #define EVENT_OR_CONN_STATUS 0x0003 #define EVENT_BANDWIDTH_USED 0x0004 -#define EVENT_WARNING 0x0005 +#define EVENT_LOG_OBSOLETE 0x0005 #define EVENT_NEW_DESC 0x0006 -#define _EVENT_MAX 0x0006 +#define EVENT_DEBUG_MSG 0x0007 +#define EVENT_INFO_MSG 0x0008 +#define EVENT_NOTICE_MSG 0x0009 +#define EVENT_WARN_MSG 0x000A +#define EVENT_ERR_MSG 0x000B +#define _EVENT_MAX 0x000B /** Array mapping from message type codes to human-readable message * type names. */ @@ -167,13 +172,38 @@ control_cmd_to_string(uint16_t cmd) return (cmd<=_CONTROL_CMD_MAX_RECOGNIZED) ? CONTROL_COMMANDS[cmd] : "Unknown"; } +static INLINE int +event_to_log_severity(int event) +{ + switch (event) { + case EVENT_DEBUG_MSG: return LOG_DEBUG; + case EVENT_INFO_MSG: return LOG_INFO; + case EVENT_NOTICE_MSG: return LOG_NOTICE; + case EVENT_WARN_MSG: return LOG_WARN; + case EVENT_ERR_MSG: return LOG_ERR; + default: return -1; + } +} + +static INLINE int +log_severity_to_event(int severity) +{ + switch (severity) { + case LOG_DEBUG: return EVENT_DEBUG_MSG; + case LOG_INFO: return EVENT_INFO_MSG; + case LOG_NOTICE: return EVENT_NOTICE_MSG; + case LOG_WARN: return EVENT_WARN_MSG; + case LOG_ERR: return EVENT_ERR_MSG; + default: return -1; + } +} + /** Set global_event_mask to the bitwise OR of each live control * connection's event_mask field. */ static void update_global_event_mask(void) { connection_t **conns; int n_conns, i; - global_event_mask = 0; get_connection_array(&conns, &n_conns); for (i = 0; i < n_conns; ++i) { @@ -182,6 +212,35 @@ static void update_global_event_mask(void) global_event_mask |= conns[i]->event_mask; } } + + adjust_event_log_severity(); +} + +void adjust_event_log_severity(void) { + int i; + int min_log_event=EVENT_ERR_MSG, max_log_event=EVENT_DEBUG_MSG; + + for (i = EVENT_DEBUG_MSG; i <= EVENT_ERR_MSG; ++i) { + if (EVENT_IS_INTERESTING(i)) { + min_log_event = i; + break; + } + } + for (i = EVENT_ERR_MSG; i >= EVENT_DEBUG_MSG; --i) { + if (EVENT_IS_INTERESTING(i)) { + max_log_event = i; + break; + } + } + if (EVENT_IS_INTERESTING(EVENT_LOG_OBSOLETE)) { + if (min_log_event > EVENT_NOTICE_MSG) + min_log_event = EVENT_NOTICE_MSG; + if (max_log_event < EVENT_ERR_MSG) + max_log_event = EVENT_ERR_MSG; + } + change_callback_log_severity(event_to_log_severity(min_log_event), + event_to_log_severity(max_log_event), + control_event_logmsg); } /** Send a message of type type containing len bytes @@ -1098,14 +1157,20 @@ control_event_bandwidth_used(uint32_t n_read, uint32_t n_written) void control_event_logmsg(int severity, const char *msg) { - size_t len; - if (severity > LOG_NOTICE) /* Less important than notice? ignore for now. */ - return; - if (!EVENT_IS_INTERESTING(EVENT_WARNING)) - return; + int oldlog = EVENT_IS_INTERESTING(EVENT_LOG_OBSOLETE) && + (severity == LOG_NOTICE || severity == LOG_WARN || severity == LOG_ERR); + int event = log_severity_to_event(severity); - len = strlen(msg); - send_control_event(EVENT_WARNING, (uint32_t)(len+1), msg); + if (event<0 || !EVENT_IS_INTERESTING(event)) + event = 0; + + if (oldlog || event) { + size_t len = strlen(msg); + if (event) + send_control_event(event, (uint32_t)(len+1), msg); + if (oldlog) + send_control_event(EVENT_LOG_OBSOLETE, (uint32_t)(len+1), msg); + } } /** Called whenever we receive new router descriptors: tell any diff --git a/src/or/or.h b/src/or/or.h index 25c912f4fc..b555ffd37c 100644 --- a/src/or/or.h +++ b/src/or/or.h @@ -1407,6 +1407,7 @@ typedef enum or_conn_status_event_t { OR_CONN_EVENT_CLOSED = 3, } or_conn_status_event_t; +void adjust_event_log_severity(void); int connection_control_finished_flushing(connection_t *conn); int connection_control_reached_eof(connection_t *conn); int connection_control_process_inbuf(connection_t *conn);