r18804@catbus: nickm | 2008-03-13 18:18:31 -0400

Refactor log domain mask code so that nobody outside of log.c has to use SEVERITY_MASK_IDX.  It is error-prone.


svn:r14016
This commit is contained in:
Nick Mathewson 2008-03-13 22:18:38 +00:00
parent d928e5685f
commit 46155aca17
5 changed files with 29 additions and 26 deletions

View File

@ -24,6 +24,7 @@ const char log_c_id[] = "$Id$";
#include <time.h>
#endif
#include "util.h"
#define LOG_PRIVATE
#include "log.h"
#include "container.h"
@ -474,16 +475,21 @@ close_log(logfile_t *victim)
}
}
/** DOCDOC XXXX021 remove me. */
static log_severity_list_t *
new_severity_list(int loglevelMin, int loglevelMax)
/** Adjust a log severity configuration in <b>severity_out</b> to contain
* every domain between <b>loglevelMin</b> and <b>loglevelMax</b>, inclusive.
*/
void
set_log_severity_config(int loglevelMin, int loglevelMax,
log_severity_list_t *severity_out)
{
log_severity_list_t *out = tor_malloc_zero(sizeof(log_severity_list_t));
int i;
tor_assert(loglevelMin >= loglevelMax);
tor_assert(loglevelMin >= LOG_ERR && loglevelMin <= LOG_DEBUG);
tor_assert(loglevelMax >= LOG_ERR && loglevelMax <= LOG_DEBUG);
memset(severity_out, 0, sizeof(log_severity_list_t));
for (i = loglevelMin; i >= loglevelMax; --i) {
out->masks[SEVERITY_MASK_IDX(i)] = ~0u;
severity_out->masks[SEVERITY_MASK_IDX(i)] = ~0u;
}
return out;
}
/** Add a log handler named <b>name</b> to send all messages in <b>severity</b>
@ -529,9 +535,10 @@ init_logging(void)
void
add_temp_log(void)
{
log_severity_list_t *s = tor_malloc_zero(sizeof(log_severity_list_t));
set_log_severity_config(LOG_NOTICE, LOG_ERR, s);
LOCK_LOGS();
add_stream_log_impl(new_severity_list(LOG_NOTICE, LOG_ERR),
"<temp>", stdout);
add_stream_log_impl(s, "<temp>", stdout);
logfiles->is_temporary = 1;
UNLOCK_LOGS();
}
@ -567,8 +574,7 @@ change_callback_log_severity(int loglevelMin, int loglevelMax,
LOCK_LOGS();
for (lf = logfiles; lf; lf = lf->next) {
if (lf->callback == cb) {
tor_free(lf->severities);
lf->severities = new_severity_list(loglevelMin, loglevelMax);
set_log_severity_config(loglevelMin, loglevelMax, lf->severities);
}
}
_log_global_min_severity = get_min_log_level();

View File

@ -104,9 +104,11 @@ typedef struct log_severity_list_t {
log_domain_mask_t masks[LOG_DEBUG-LOG_ERR+1];
} log_severity_list_t;
#ifdef LOG_PRIVATE
/** Given a severity, yields an index into log_severity_list_t.masks to use
* for that severity. */
#define SEVERITY_MASK_IDX(sev) ((sev) - LOG_ERR)
#endif
/** Callback type used for add_callback_log. */
typedef void (*log_callback)(int severity, uint32_t domain, const char *msg);
@ -116,6 +118,8 @@ int parse_log_level(const char *level);
const char *log_level_to_string(int level);
int parse_log_severity_config(const char **cfg,
log_severity_list_t *severity_out);
void set_log_severity_config(int minSeverity, int maxSeverity,
log_severity_list_t *severity_out);
void add_stream_log(log_severity_list_t *severity, const char *name,
FILE *stream);
int add_file_log(log_severity_list_t *severity, const char *filename);

View File

@ -3636,8 +3636,7 @@ main(int c, char**v)
{
log_severity_list_t *s = tor_malloc_zero(sizeof(log_severity_list_t));
for (i = loglevel; i >= LOG_ERR; --i)
s->masks[SEVERITY_MASK_IDX(i)] = ~0u;
set_log_severity_config(loglevel, LOG_ERR, s);
add_stream_log(s, "", stdout);
}

View File

@ -190,13 +190,10 @@ parse_commandline(int argc, char **argv)
}
s = tor_malloc_zero(sizeof(log_severity_list_t));
s->masks[SEVERITY_MASK_IDX(LOG_ERR)] = ~0u;
s->masks[SEVERITY_MASK_IDX(LOG_WARN)] = ~0u;
if (verbose) {
s->masks[SEVERITY_MASK_IDX(LOG_NOTICE)] = ~0u;
s->masks[SEVERITY_MASK_IDX(LOG_INFO)] = ~0u;
s->masks[SEVERITY_MASK_IDX(LOG_DEBUG)] = ~0u;
}
if (verbose)
set_log_severity_config(LOG_DEBUG, LOG_ERR, s);
else
set_log_severity_config(LOG_WARN, LOG_ERR, s);
add_stream_log(s, "<stderr>", stderr);
if (!identity_key_file) {

View File

@ -318,13 +318,10 @@ main(int argc, char **argv)
usage();
}
s->masks[SEVERITY_MASK_IDX(LOG_ERR)] = ~0u;
s->masks[SEVERITY_MASK_IDX(LOG_WARN)] = ~0u;
if (isVerbose) {
s->masks[SEVERITY_MASK_IDX(LOG_NOTICE)] = ~0u;
s->masks[SEVERITY_MASK_IDX(LOG_INFO)] = ~0u;
s->masks[SEVERITY_MASK_IDX(LOG_DEBUG)] = ~0u;
}
if (isVerbose)
set_log_severity_config(LOG_DEBUG, LOG_ERR, s);
else
set_log_severity_config(LOG_WARN, LOG_ERR, s);
add_stream_log(s, "<stderr>", stderr);
if (n_args == 1) {