log.c File Reference

Functions to send messages to log files or the console. More...

#include "orconfig.h"
#include <stdarg.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include "compat.h"
#include "util.h"
#include "log.h"
#include "container.h"

Data Structures

struct  logfile_t

Defines

#define LOG_PRIVATE
#define TRUNCATED_STR   "[...truncated]"
#define TRUNCATED_STR_LEN   14
#define LOCK_LOGS()
#define UNLOCK_LOGS()   STMT_BEGIN tor_mutex_release(&log_mutex); STMT_END

Functions

static void log_free (logfile_t *victim)
static INLINE const char * sev_to_string (int severity)
static INLINE int should_log_function_name (log_domain_mask_t domain, int severity)
static void delete_log (logfile_t *victim)
static void close_log (logfile_t *victim)
void log_set_application_name (const char *name)
static INLINE size_t _log_prefix (char *buf, size_t buf_len, int severity)
static int log_tor_version (logfile_t *lf, int reset)
static INLINE char * format_msg (char *buf, size_t buf_len, log_domain_mask_t domain, int severity, const char *funcname, const char *format, va_list ap, size_t *msg_len_out)
static void logv (int severity, log_domain_mask_t domain, const char *funcname, const char *format, va_list ap)
void tor_log (int severity, log_domain_mask_t domain, const char *format,...)
void _log_fn (int severity, log_domain_mask_t domain, const char *format,...)
void _log_debug (log_domain_mask_t domain, const char *format,...)
void _log_info (log_domain_mask_t domain, const char *format,...)
void _log_notice (log_domain_mask_t domain, const char *format,...)
void _log_warn (log_domain_mask_t domain, const char *format,...)
void _log_err (log_domain_mask_t domain, const char *format,...)
void logs_free_all (void)
void set_log_severity_config (int loglevelMin, int loglevelMax, log_severity_list_t *severity_out)
static void add_stream_log_impl (const log_severity_list_t *severity, const char *name, int fd)
void add_stream_log (const log_severity_list_t *severity, const char *name, int fd)
void init_logging (void)
void add_temp_log (int min_severity)
int add_callback_log (const log_severity_list_t *severity, log_callback cb)
void change_callback_log_severity (int loglevelMin, int loglevelMax, log_callback cb)
void close_temp_logs (void)
void rollback_log_changes (void)
void mark_logs_temp (void)
int add_file_log (const log_severity_list_t *severity, const char *filename)
int add_syslog_log (const log_severity_list_t *severity)
int parse_log_level (const char *level)
const char * log_level_to_string (int level)
static log_domain_mask_t parse_log_domain (const char *domain)
int parse_log_severity_config (const char **cfg_ptr, log_severity_list_t *severity_out)
int get_min_log_level (void)
void switch_logs_debug (void)

Variables

static tor_mutex_t log_mutex
static int log_mutex_initialized = 0
static logfile_tlogfiles = NULL
static int syslog_count = 0
int _log_global_min_severity = LOG_NOTICE
static char * appname = NULL
const char * _log_fn_function_name = NULL
static const char * domain_list []


Detailed Description

Functions to send messages to log files or the console.


Define Documentation

 
#define LOCK_LOGS (  ) 

Value:

STMT_BEGIN                                          \
  tor_mutex_acquire(&log_mutex);                                        \
  STMT_END


Function Documentation

static INLINE size_t _log_prefix ( char *  buf,
size_t  buf_len,
int  severity 
) [static]

Helper: Write the standard prefix for log lines to a buf_len character buffer in buf.

References sev_to_string(), tor_gettimeofday(), and tor_snprintf().

Referenced by format_msg(), and log_tor_version().

int add_callback_log ( const log_severity_list_t severity,
log_callback  cb 
)

Add a log handler to send messages in severity to the function cb.

References _log_global_min_severity, logfile_t::callback, logfile_t::fd, logfile_t::filename, get_min_log_level(), logfile_t::next, and logfile_t::severities.

Referenced by options_act_reversible().

int add_file_log ( const log_severity_list_t severity,
const char *  filename 
)

Add a log handler to send messages to filename. If opening the logfile fails, -1 is returned and errno is set appropriately (by open(2)).

References _log_global_min_severity, add_stream_log_impl(), delete_log(), get_min_log_level(), log_tor_version(), logfile_t::needs_close, and tor_fd_seekend().

Referenced by options_init_logs().

void add_stream_log ( const log_severity_list_t severity,
const char *  name,
int  fd 
)

Add a log handler named name to send all messages in severity to fd. Steals a reference to severity; the caller must not use it after calling this function.

References add_stream_log_impl().

Referenced by options_init_logs().

static void add_stream_log_impl ( const log_severity_list_t severity,
const char *  name,
int  fd 
) [static]

Add a log handler named name to send all messages in severity to fd. Copies severity. Helper: does no locking.

References _log_global_min_severity, logfile_t::fd, logfile_t::filename, get_min_log_level(), logfile_t::next, and logfile_t::severities.

Referenced by add_file_log(), add_stream_log(), and add_temp_log().

int add_syslog_log ( const log_severity_list_t severity  ) 

Add a log handler to send messages to they system log facility.

References _log_global_min_severity, logfile_t::fd, logfile_t::filename, get_min_log_level(), logfile_t::is_syslog, logfile_t::next, logfile_t::severities, and syslog_count.

Referenced by options_init_logs().

void add_temp_log ( int  min_severity  ) 

Add a log handler to receive messages during startup (before the real logs are initialized).

References add_stream_log_impl(), logfile_t::is_temporary, set_log_severity_config(), and tor_free.

Referenced by tor_init().

void change_callback_log_severity ( int  loglevelMin,
int  loglevelMax,
log_callback  cb 
)

Adjust the configured severity of any logs whose callback function is cb.

References _log_global_min_severity, logfile_t::callback, get_min_log_level(), logfile_t::next, set_log_severity_config(), and logfile_t::severities.

Referenced by control_adjust_event_log_severity().

static void close_log ( logfile_t victim  )  [static]

Helper: release system resources (but not memory) held by a single logfile_t.

References logfile_t::fd, logfile_t::is_syslog, logfile_t::needs_close, and syslog_count.

Referenced by close_temp_logs(), and logs_free_all().

void close_temp_logs ( void   ) 

static void delete_log ( logfile_t victim  )  [static]

Remove and free the log entry victim from the linked-list logfiles (it is probably present, but it might not be due to thread racing issues). After this function is called, the caller shouldn't refer to victim anymore.

Long-term, we need to do something about races in the log subsystem in general. See bug 222 for more details.

References log_free(), and logfile_t::next.

Referenced by add_file_log().

static INLINE char* format_msg ( char *  buf,
size_t  buf_len,
log_domain_mask_t  domain,
int  severity,
const char *  funcname,
const char *  format,
va_list  ap,
size_t *  msg_len_out 
) [static]

Helper: Format a log message into a fixed-sized buffer. (This is factored out of logv so that we never format a message more than once.) Return a pointer to the first character of the message portion of the formatted string.

References _log_prefix(), LD_BUG, should_log_function_name(), tor_snprintf(), and tor_vsnprintf().

Referenced by logv().

int get_min_log_level ( void   ) 

void init_logging ( void   ) 

Initialize the global logging facility

References tor_mutex_init().

Referenced by tor_main().

static void log_free ( logfile_t victim  )  [static]

Free all storage held by victim.

References logfile_t::filename, logfile_t::severities, and tor_free.

Referenced by close_temp_logs(), delete_log(), and logs_free_all().

const char* log_level_to_string ( int  level  ) 

Return the string equivalent of a given log level.

References sev_to_string().

void log_set_application_name ( const char *  name  ) 

Set the "application name" for the logs to name: we'll use this name in the message we write when starting up, and at the start of each new log.

Tor uses this string to write the version number to the log file.

References appname, and tor_free.

Referenced by tor_init().

static int log_tor_version ( logfile_t lf,
int  reset 
) [static]

If lf refers to an actual file that we have just opened, and the file contains no data, log an "opening new logfile" message at the top.

Return -1 if the log is broken and needs to be deleted, else return 0.

References _log_prefix(), appname, logfile_t::fd, logfile_t::is_temporary, logfile_t::needs_close, tor_fd_getpos(), tor_snprintf(), and write_all().

Referenced by add_file_log().

void logs_free_all ( void   ) 

Close all open log files, and free other static memory.

References appname, close_log(), log_free(), logfile_t::next, and tor_free.

Referenced by tor_free_all().

static void logv ( int  severity,
log_domain_mask_t  domain,
const char *  funcname,
const char *  format,
va_list  ap 
) [static]

Helper: sends a message to the appropriate logfiles, at loglevel severity. If provided, funcname is prepended to the message. The actual message is derived as from tor_snprintf(format,ap).

References logfile_t::callback, logfile_t::fd, format_msg(), logfile_t::is_syslog, log_severity_list_t::masks, logfile_t::next, logfile_t::seems_dead, logfile_t::severities, tor_free, and write_all().

Referenced by tor_log().

void mark_logs_temp ( void   ) 

Configure all log handles to be closed by close_temp_logs().

References logfile_t::is_temporary, and logfile_t::next.

Referenced by options_act_reversible().

static log_domain_mask_t parse_log_domain ( const char *  domain  )  [static]

Return a bitmask for the log domain for which domain is the name, or 0 if there is no such name.

Referenced by parse_log_severity_config().

int parse_log_level ( const char *  level  ) 

If level is a valid log severity, return the corresponding numeric value. Otherwise, return -1.

Referenced by parse_log_severity_config().

int parse_log_severity_config ( const char **  cfg_ptr,
log_severity_list_t severity_out 
)

Parse a log severity pattern in *cfg_ptr. Advance cfg_ptr after the end of the severityPattern. Set the value of severity_out to the parsed pattern. Return 0 on success, -1 on failure.

The syntax for a SeverityPattern is:

   SeverityPattern = *(DomainSeverity SP)* DomainSeverity
   DomainSeverity = (DomainList SP)? SeverityRange
   SeverityRange = MinSeverity ("-" MaxSeverity )?
   DomainList = "[" (SP? DomainSpec SP? ",") SP? DomainSpec "]"
   DomainSpec = "*" | Domain | "~" Domain
 
A missing MaxSeverity defaults to ERR. Severities and domains are case-insensitive. "~" indicates negation for a domain; negation happens last inside a DomainList. Only one SeverityRange without a DomainList is allowed per line.

References eat_whitespace(), LD_CONFIG, log_severity_list_t::masks, parse_log_domain(), parse_log_level(), smartlist_create(), smartlist_free(), smartlist_split_string(), strcasecmpstart(), and tor_free.

Referenced by options_init_logs().

void rollback_log_changes ( void   ) 

Make all currently temporary logs (set to be closed by close_temp_logs) live again, and close all non-temporary logs.

References close_temp_logs(), logfile_t::is_temporary, and logfile_t::next.

Referenced by options_act_reversible().

void set_log_severity_config ( int  loglevelMin,
int  loglevelMax,
log_severity_list_t severity_out 
)

Adjust a log severity configuration in severity_out to contain every domain between loglevelMin and loglevelMax, inclusive.

References log_severity_list_t::masks, and tor_assert.

Referenced by add_temp_log(), and change_callback_log_severity().

static INLINE const char* sev_to_string ( int  severity  )  [static]

Helper: map a log severity to descriptive string.

Referenced by _log_prefix(), and log_level_to_string().

static INLINE int should_log_function_name ( log_domain_mask_t  domain,
int  severity 
) [static]

Helper: decide whether to include the function name in the log message.

References LD_BUG.

Referenced by format_msg().

void switch_logs_debug ( void   ) 

Switch all logs to output at most verbose level.

References _log_global_min_severity, get_min_log_level(), log_severity_list_t::masks, logfile_t::next, and logfile_t::severities.

Referenced by signal_callback().

void tor_log ( int  severity,
log_domain_mask_t  domain,
const char *  format,
  ... 
)

Output a message to the log.

References _log_global_min_severity, and logv().

Referenced by circuit_log_path(), circuit_note_clock_jumped(), and tor_log_mallinfo().


Variable Documentation

const char* _log_fn_function_name = NULL

Output a message to the log, prefixed with a function name fn.

int _log_global_min_severity = LOG_NOTICE

What's the lowest log level anybody cares about? Checking this lets us bail out early from log_debug if we aren't debugging.

Referenced by add_callback_log(), add_file_log(), add_stream_log_impl(), add_syslog_log(), change_callback_log_severity(), close_temp_logs(), switch_logs_debug(), and tor_log().

char* appname = NULL [static]

Name of the application: used to generate the message we write at the start of each new log.

Referenced by log_set_application_name(), log_tor_version(), and logs_free_all().

const char* domain_list[] [static]

Initial value:

 {
  "GENERAL", "CRYPTO", "NET", "CONFIG", "FS", "PROTOCOL", "MM",
  "HTTP", "APP", "CONTROL", "CIRC", "REND", "BUG", "DIR", "DIRSERV",
  "OR", "EDGE", "ACCT", "HIST", "HANDSHAKE", NULL
}
NULL-terminated array of names for log domains such that domain_list[dom] is a description of dom.

A mutex to guard changes to logfiles and logging.

logfile_t* logfiles = NULL [static]

Linked list of logfile_t.

int syslog_count = 0 [static]

The number of open syslog log handlers that we have. When this reaches 0, we can close our connection to the syslog facility.

Referenced by add_syslog_log(), and close_log().


Generated on Tue May 25 00:30:44 2010 for tor by  doxygen 1.5.6