Tor  0.4.7.0-alpha-dev
Data Structures | Macros | Functions | Variables
log.c File Reference

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

#include "orconfig.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lib/log/log.h"
#include "lib/log/log_sys.h"
#include "lib/version/git_revision.h"
#include "lib/log/ratelim.h"
#include "lib/lock/compat_mutex.h"
#include "lib/smartlist_core/smartlist_core.h"
#include "lib/smartlist_core/smartlist_foreach.h"
#include "lib/smartlist_core/smartlist_split.h"
#include "lib/err/torerr.h"
#include "lib/intmath/bits.h"
#include "lib/string/compat_string.h"
#include "lib/string/printf.h"
#include "lib/malloc/malloc.h"
#include "lib/string/util_string.h"
#include "lib/wallclock/tor_gettimeofday.h"
#include "lib/wallclock/approx_time.h"
#include "lib/wallclock/time_to_tm.h"
#include "lib/fdio/fdio.h"
#include "lib/cc/ctassert.h"

Go to the source code of this file.

Data Structures

struct  logfile_t
 
struct  pending_log_message_t
 

Macros

#define LOG_PRIVATE
 
#define log_free(lg)    FREE_AND_NULL(logfile_t, log_free_, (lg))
 
#define MAX_STARTUP_MSG_LEN   (1<<16)
 
#define LOCK_LOGS()
 
#define UNLOCK_LOGS()
 
#define pending_log_message_free(msg)    FREE_AND_NULL(pending_log_message_t, pending_log_message_free_, (msg))
 
#define TRUNCATED_STR   "[...truncated]"
 
#define TRUNCATED_STR_LEN   14
 

Functions

static void log_free_ (logfile_t *victim)
 
static const char * sev_to_string (int severity)
 
static 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)
 
static void close_log_sigsafe (logfile_t *victim)
 
static char * domain_to_string (log_domain_mask_t domain, char *buf, size_t buflen)
 
static char * format_msg (char *buf, size_t buf_len, log_domain_mask_t domain, int severity, const char *funcname, const char *suffix, const char *format, va_list ap, size_t *msg_len_out)
 
void log_set_application_name (const char *name)
 
int log_message_is_interesting (int severity, log_domain_mask_t domain)
 
void tor_log_string (int severity, log_domain_mask_t domain, const char *function, const char *string)
 
void set_log_time_granularity (int granularity_msec)
 
static size_t log_prefix_ (char *buf, size_t buf_len, int severity)
 
static int log_tor_version (logfile_t *lf, int reset)
 
static pending_log_message_tpending_log_message_new (int severity, log_domain_mask_t domain, const char *fullmsg, const char *shortmsg)
 
static void pending_log_message_free_ (pending_log_message_t *msg)
 
static int logfile_is_external (const logfile_t *lf)
 
static int logfile_wants_message (const logfile_t *lf, int severity, log_domain_mask_t domain)
 
static void logfile_deliver (logfile_t *lf, const char *buf, size_t msg_len, const char *msg_after_prefix, log_domain_mask_t domain, int severity, int *callbacks_deferred)
 
STATIC void logv (int severity, log_domain_mask_t domain, const char *funcname, const char *suffix, const char *format, va_list ap)
 
void tor_log (int severity, log_domain_mask_t domain, const char *format,...)
 
static int int_array_contains (const int *array, int n, int item)
 
void tor_log_update_sigsafe_err_fds (void)
 
void tor_log_get_logfile_names (smartlist_t *out)
 
void log_fn_ (int severity, log_domain_mask_t domain, const char *fn, const char *format,...)
 
void log_fn_ratelim_ (ratelim_t *ratelim, int severity, log_domain_mask_t domain, const char *fn, const char *format,...)
 
void logs_free_all (void)
 
void logs_flush_sigsafe (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 (int disable_startup_queue)
 
void logs_set_domain_logging (int enabled)
 
void add_default_log (int min_severity)
 
void logs_set_pending_callback_callback (pending_callback_callback cb)
 
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 flush_pending_log_callbacks (void)
 
void flush_log_messages_from_startup (void)
 
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 fd)
 
int parse_log_level (const char *level)
 
const char * log_level_to_string (int level)
 
 CTASSERT (ARRAY_LENGTH(domain_list)==N_LOGGING_DOMAINS+1)
 
 CTASSERT (HIGHEST_RESERVED_LD_DOMAIN_< LD_ALL_DOMAINS)
 
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)
 
void truncate_logs (void)
 

Variables

const int LOG_WARN_ = LOG_WARN
 
const int LOG_NOTICE_ = LOG_NOTICE
 
const log_domain_mask_t LD_GENERAL_ = LD_GENERAL
 
const log_domain_mask_t LD_NET_ = LD_NET
 
static tor_mutex_t log_mutex
 
static int log_mutex_initialized = 0
 
static logfile_tlogfiles = NULL
 
static int log_domains_are_logged = 0
 
static smartlist_tpending_cb_messages = NULL
 
static pending_callback_callback pending_cb_cb = NULL
 
static smartlist_tpending_startup_messages = NULL
 
static size_t pending_startup_messages_len
 
static int queue_startup_messages = 1
 
static int pretty_fn_has_parens = 0
 
int log_global_min_severity_ = LOG_NOTICE
 
static char * appname = NULL
 
static int log_time_granularity = 1
 
static const char * domain_list []
 

Detailed Description

Functions to send messages to log files or the console.

Definition in file log.c.

Macro Definition Documentation

◆ LOCK_LOGS

#define LOCK_LOGS ( )
Value:
STMT_BEGIN \
raw_assert(log_mutex_initialized); \
tor_mutex_acquire(&log_mutex); \
STMT_END
static tor_mutex_t log_mutex
Definition: log.c:124
static int log_mutex_initialized
Definition: log.c:126

Lock the log_mutex to prevent others from changing the logfile_t list

Definition at line 175 of file log.c.

◆ MAX_STARTUP_MSG_LEN

#define MAX_STARTUP_MSG_LEN   (1<<16)

Don't store more than this many bytes of messages while waiting for the logs to get configured.

Definition at line 172 of file log.c.

◆ TRUNCATED_STR

#define TRUNCATED_STR   "[...truncated]"

The string we stick at the end of a log message when it is too long, and its length.

Definition at line 57 of file log.c.

◆ UNLOCK_LOGS

#define UNLOCK_LOGS ( )
Value:
STMT_BEGIN \
raw_assert(log_mutex_initialized); \
tor_mutex_release(&log_mutex); \
STMT_END

Unlock the log_mutex

Definition at line 180 of file log.c.

Function Documentation

◆ add_callback_log()

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.

Definition at line 977 of file log.c.

◆ add_default_log()

void add_default_log ( int  min_severity)

Add a log handler to accept messages when no other log is configured.

Definition at line 944 of file log.c.

Referenced by add_default_log_for_quiet_level().

◆ add_file_log()

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

Add a log handler to send messages to filename via fd. If opening the logfile failed, -1 is returned and errno is set appropriately (by open(2)). Takes ownership of fd.

Definition at line 1148 of file log.c.

Referenced by open_and_add_file_log().

◆ add_stream_log()

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.

Definition at line 902 of file log.c.

◆ add_stream_log_impl()

STATIC void add_stream_log_impl ( 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. Copies severity. Helper: does no locking.

Definition at line 885 of file log.c.

Referenced by add_file_log(), and add_stream_log().

◆ change_callback_log_severity()

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

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

Definition at line 997 of file log.c.

Referenced by control_adjust_event_log_severity().

◆ close_log()

static void close_log ( logfile_t victim)
static

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

Definition at line 850 of file log.c.

Referenced by close_temp_logs(), and logs_free_all().

◆ close_log_sigsafe()

static void close_log_sigsafe ( logfile_t victim)
static

Helper: release system resources (but not memory) held by a single signal-safe logfile_t. If the log's resources can not be released in a signal handler, does nothing.

Definition at line 837 of file log.c.

Referenced by close_log().

◆ close_temp_logs()

void close_temp_logs ( void  )

Close any log handlers marked by mark_logs_temp().

Definition at line 1095 of file log.c.

Referenced by rollback_log_changes().

◆ delete_log()

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.

Definition at line 817 of file log.c.

Referenced by add_file_log().

◆ domain_to_string()

static char * domain_to_string ( log_domain_mask_t  domain,
char *  buf,
size_t  buflen 
)
static

Translate a bitmask of log domains to a string.

Definition at line 1270 of file log.c.

◆ flush_log_messages_from_startup()

void flush_log_messages_from_startup ( void  )

Flush all the messages we stored from startup while waiting for log initialization.

Definition at line 1058 of file log.c.

◆ flush_pending_log_callbacks()

void flush_pending_log_callbacks ( void  )

If there are any log messages that were generated with LD_NOCB waiting to be sent to callback-based loggers, send them now.

Definition at line 1016 of file log.c.

Referenced by queued_events_flush_all().

◆ format_msg()

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

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.

Definition at line 330 of file log.c.

◆ get_min_log_level()

int get_min_log_level ( void  )

Return the least severe log level that any current log is interested in.

Definition at line 1426 of file log.c.

Referenced by add_file_log(), change_callback_log_severity(), and switch_logs_debug().

◆ init_logging()

void init_logging ( int  disable_startup_queue)

Initialize the global logging facility

Definition at line 911 of file log.c.

◆ int_array_contains()

static int int_array_contains ( const int *  array,
int  n,
int  item 
)
static

Helper function; return true iff the n-element array array contains item.

Definition at line 612 of file log.c.

Referenced by tor_log_update_sigsafe_err_fds().

◆ log_fn_()

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

Implementation of the log_fn backend, used when we have variadic macros. All arguments are as for log_fn, except for fn, which is the name of the calling function.

Definition at line 705 of file log.c.

Referenced by tor_log_string().

◆ log_free_()

static void log_free_ ( logfile_t victim)
static

Free all storage held by victim.

Definition at line 734 of file log.c.

◆ log_level_to_string()

const char* log_level_to_string ( int  level)

Return the string equivalent of a given log level.

Definition at line 1231 of file log.c.

◆ log_message_is_interesting()

int log_message_is_interesting ( int  severity,
log_domain_mask_t  domain 
)

Return true if some of the running logs might be interested in a log message of the given severity in the given domains. If this function returns true, the log message might be ignored anyway, but if it returns false, it is definitely_ safe not to log the message.

Definition at line 222 of file log.c.

◆ log_prefix_()

static size_t log_prefix_ ( char *  buf,
size_t  buf_len,
int  severity 
)
inlinestatic

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

Definition at line 257 of file log.c.

Referenced by log_tor_version().

◆ log_set_application_name()

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.

Definition at line 211 of file log.c.

Referenced by tor_init().

◆ log_tor_version()

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.

Definition at line 292 of file log.c.

Referenced by add_file_log().

◆ logfile_deliver()

static void logfile_deliver ( logfile_t lf,
const char *  buf,
size_t  msg_len,
const char *  msg_after_prefix,
log_domain_mask_t  domain,
int  severity,
int *  callbacks_deferred 
)
inlinestatic

Send a message to lf. The full message, with time prefix and severity, is in buf. The message itself is in msg_after_prefix. If callbacks_deferred points to true, then we already deferred this message for pending callbacks and don't need to do it again. Otherwise, if we need to do it, do it, and set callbacks_deferred to 1.

Definition at line 481 of file log.c.

Referenced by flush_log_messages_from_startup().

◆ logfile_is_external()

static int logfile_is_external ( const logfile_t lf)
inlinestatic

Helper function: returns true iff the log file, given in lf, is handled externally via the system log API, or is an external callback function.

Definition at line 448 of file log.c.

Referenced by logfile_wants_message(), and tor_log_update_sigsafe_err_fds().

◆ logfile_wants_message()

static int logfile_wants_message ( const logfile_t lf,
int  severity,
log_domain_mask_t  domain 
)
inlinestatic

Return true iff lf would like to receive a message with the specified severity in the specified domain.

Definition at line 458 of file log.c.

Referenced by flush_log_messages_from_startup().

◆ logs_flush_sigsafe()

void logs_flush_sigsafe ( void  )

Flush the signal-safe log files.

This function is safe to call from a signal handler. It is currently called by the BUG() macros, when terminating the process on an abnormal condition.

Definition at line 791 of file log.c.

Referenced by tor_abort_().

◆ logs_free_all()

void logs_free_all ( void  )

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

Definition at line 745 of file log.c.

◆ logs_set_domain_logging()

void logs_set_domain_logging ( int  enabled)

Set whether we report logging domains as a part of our log messages.

Definition at line 934 of file log.c.

◆ logs_set_pending_callback_callback()

void logs_set_pending_callback_callback ( pending_callback_callback  cb)

Register "cb" as the callback to call when there are new pending log callbacks to be flushed with flush_pending_log_callbacks().

Note that this callback, if present, can be invoked from any thread.

This callback must not log.

It is intentional that this function contains the name "callback" twice: it sets a "callback" to be called on the condition that there is a "pending callback".

Definition at line 967 of file log.c.

◆ logv()

STATIC void logv ( int  severity,
log_domain_mask_t  domain,
const char *  funcname,
const char *  suffix,
const char *  format,
va_list  ap 
)

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).

Definition at line 535 of file log.c.

Referenced by log_fn_().

◆ mark_logs_temp()

void mark_logs_temp ( void  )

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

Definition at line 1131 of file log.c.

◆ parse_log_domain()

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.

Definition at line 1258 of file log.c.

◆ parse_log_level()

int parse_log_level ( const char *  level)

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

Definition at line 1214 of file log.c.

◆ 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.

Definition at line 1322 of file log.c.

◆ pending_log_message_free_()

static void pending_log_message_free_ ( pending_log_message_t msg)
static

Release all storage held by msg.

Definition at line 435 of file log.c.

◆ rollback_log_changes()

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.

Definition at line 1119 of file log.c.

Referenced by options_rollback_log_transaction().

◆ set_log_severity_config()

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.

Definition at line 868 of file log.c.

Referenced by change_callback_log_severity().

◆ set_log_time_granularity()

void set_log_time_granularity ( int  granularity_msec)

Define log time granularity for all logs to be granularity_msec milliseconds.

Definition at line 247 of file log.c.

◆ sev_to_string()

static const char* sev_to_string ( int  severity)
inlinestatic

Helper: map a log severity to descriptive string.

Definition at line 88 of file log.c.

Referenced by log_level_to_string(), and log_prefix_().

◆ should_log_function_name()

static int should_log_function_name ( log_domain_mask_t  domain,
int  severity 
)
inlinestatic

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

Definition at line 104 of file log.c.

◆ switch_logs_debug()

void switch_logs_debug ( void  )

Switch all logs to output at most verbose level.

Definition at line 1441 of file log.c.

◆ tor_log()

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

◆ tor_log_get_logfile_names()

void tor_log_get_logfile_names ( smartlist_t out)

Add to out a copy of every currently configured log file name. Used to enable access to these filenames with the sandbox code.

Definition at line 683 of file log.c.

◆ tor_log_string()

void tor_log_string ( int  severity,
log_domain_mask_t  domain,
const char *  function,
const char *  string 
)

As tor_log, but takes an optional function name, and does not treat its string as a printf format.

For use by Rust integration.

Definition at line 235 of file log.c.

◆ tor_log_update_sigsafe_err_fds()

void tor_log_update_sigsafe_err_fds ( void  )

Function to call whenever the list of logs changes to get ready to log from signal handlers.

Definition at line 625 of file log.c.

◆ truncate_logs()

void truncate_logs ( void  )

Truncate all the log files.

Definition at line 1456 of file log.c.

Variable Documentation

◆ appname

char* appname = NULL
static

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

Definition at line 203 of file log.c.

Referenced by log_set_application_name(), and log_tor_version().

◆ domain_list

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", "HEARTBEAT", "CHANNEL",
"SCHED", "GUARD", "CONSDIFF", "DOS", "PROCESS", "PT", "BTRACK", "MESG",
NULL
}

NULL-terminated array of names for log domains such that domain_list[dom] is a description of dom.

Remember to update doc/man/tor.1.txt if you modify this list.

Definition at line 1241 of file log.c.

Referenced by domain_to_string(), and parse_log_domain().

◆ log_domains_are_logged

int log_domains_are_logged = 0
static

Boolean: do we report logging domains?

Definition at line 131 of file log.c.

Referenced by logs_set_domain_logging().

◆ log_global_min_severity_

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.

Definition at line 187 of file log.c.

Referenced by add_file_log(), change_callback_log_severity(), log_fn_(), log_message_is_interesting(), and switch_logs_debug().

◆ log_mutex

tor_mutex_t log_mutex
static

A mutex to guard changes to logfiles and logging.

Definition at line 124 of file log.c.

Referenced by init_logging().

◆ log_mutex_initialized

int log_mutex_initialized = 0
static

True iff we have initialized log_mutex

Definition at line 126 of file log.c.

Referenced by init_logging().

◆ log_time_granularity

int log_time_granularity = 1
static

Log time granularity in milliseconds.

Definition at line 242 of file log.c.

Referenced by log_prefix_(), and set_log_time_granularity().

◆ LOG_WARN_

const int LOG_WARN_ = LOG_WARN

Defining compile-time constants for Tor log levels (used by the Rust log wrapper at src/rust/tor_log)

Definition at line 63 of file log.c.

◆ logfiles

logfile_t* logfiles = NULL
static

◆ pending_cb_cb

pending_callback_callback pending_cb_cb = NULL
static

Callback to invoke when pending_cb_messages becomes nonempty.

Definition at line 152 of file log.c.

Referenced by logs_free_all().

◆ pending_cb_messages

smartlist_t* pending_cb_messages = NULL
static

Log messages waiting to be replayed onto callback-based logs

Definition at line 149 of file log.c.

Referenced by flush_pending_log_callbacks(), init_logging(), and logs_free_all().

◆ pending_startup_messages

smartlist_t* pending_startup_messages = NULL
static

Log messages waiting to be replayed once the logging system is initialized.

Definition at line 156 of file log.c.

Referenced by flush_log_messages_from_startup(), init_logging(), and logs_free_all().

◆ pending_startup_messages_len

size_t pending_startup_messages_len
static

Number of bytes of messages queued in pending_startup_messages. (This is the length of the messages, not the number of bytes used to store them.)

Definition at line 161 of file log.c.

Referenced by flush_log_messages_from_startup().

◆ pretty_fn_has_parens

int pretty_fn_has_parens = 0
static

True iff PRETTY_FUNCTION includes parenthesized arguments.

Definition at line 168 of file log.c.

Referenced by init_logging().

◆ queue_startup_messages

int queue_startup_messages = 1
static

True iff we should store messages while waiting for the logs to get configured.

Definition at line 165 of file log.c.

Referenced by flush_log_messages_from_startup(), and init_logging().