Tor
0.4.7.0-alpha-dev
|
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_t * | pending_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_t * | logfiles = NULL |
static int | log_domains_are_logged = 0 |
static smartlist_t * | pending_cb_messages = NULL |
static pending_callback_callback | pending_cb_cb = NULL |
static smartlist_t * | pending_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 [] |
Functions to send messages to log files or the console.
Definition in file log.c.
#define LOCK_LOGS | ( | ) |
Lock the log_mutex to prevent others from changing the logfile_t list
#define MAX_STARTUP_MSG_LEN (1<<16) |
#define TRUNCATED_STR "[...truncated]" |
#define UNLOCK_LOGS | ( | ) |
Unlock the log_mutex
int add_callback_log | ( | const log_severity_list_t * | severity, |
log_callback | cb | ||
) |
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().
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().
void add_stream_log | ( | const log_severity_list_t * | severity, |
const char * | name, | ||
int | fd | ||
) |
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().
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().
|
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().
|
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().
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().
|
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().
|
static |
void flush_log_messages_from_startup | ( | void | ) |
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().
|
inlinestatic |
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().
void init_logging | ( | int | disable_startup_queue | ) |
|
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().
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().
|
static |
const char* log_level_to_string | ( | int | level | ) |
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.
|
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().
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().
|
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().
|
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().
|
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().
|
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().
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_().
void logs_free_all | ( | void | ) |
void logs_set_domain_logging | ( | int | enabled | ) |
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".
STATIC void logv | ( | int | severity, |
log_domain_mask_t | domain, | ||
const char * | funcname, | ||
const char * | suffix, | ||
const char * | format, | ||
va_list | ap | ||
) |
void mark_logs_temp | ( | void | ) |
Configure all log handles to be closed by close_temp_logs().
|
static |
int parse_log_level | ( | const char * | level | ) |
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.
|
static |
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().
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().
void set_log_time_granularity | ( | int | granularity_msec | ) |
|
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_().
|
inlinestatic |
void switch_logs_debug | ( | void | ) |
void tor_log | ( | int | severity, |
log_domain_mask_t | domain, | ||
const char * | format, | ||
... | |||
) |
Output a message to the log. It gets logged to all logfiles that care about messages with severity in domain. The content is formatted printf-style based on format and extra arguments.
Definition at line 590 of file log.c.
Referenced by channel_dump_statistics(), channel_listener_dump_statistics(), circuit_log_path(), circuit_note_clock_jumped(), directory_info_has_arrived(), dump_routerlist_mem_usage(), dumpmemusage(), dumpstats(), handle_missing_protocol_warning_impl(), handle_response_fetch_consensus(), hs_service_dump_stats(), init_key_from_file(), keypin_load_journal_impl(), log_cert_lifetime(), log_new_relay_greeting(), pubsub_cfg_dump(), rep_hist_dump_stats(), and string_is_key_value().
void tor_log_get_logfile_names | ( | smartlist_t * | out | ) |
void tor_log_string | ( | int | severity, |
log_domain_mask_t | domain, | ||
const char * | function, | ||
const char * | string | ||
) |
void tor_log_update_sigsafe_err_fds | ( | void | ) |
|
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().
|
static |
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().
|
static |
Boolean: do we report logging domains?
Definition at line 131 of file log.c.
Referenced by logs_set_domain_logging().
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().
|
static |
A mutex to guard changes to logfiles and logging.
Definition at line 124 of file log.c.
Referenced by init_logging().
|
static |
True iff we have initialized log_mutex
Definition at line 126 of file log.c.
Referenced by init_logging().
|
static |
Log time granularity in milliseconds.
Definition at line 242 of file log.c.
Referenced by log_prefix_(), and set_log_time_granularity().
const int LOG_WARN_ = LOG_WARN |
|
static |
Linked list of logfile_t.
Definition at line 129 of file log.c.
Referenced by add_file_log(), change_callback_log_severity(), close_temp_logs(), delete_log(), flush_log_messages_from_startup(), get_min_log_level(), logs_flush_sigsafe(), logs_free_all(), mark_logs_temp(), rollback_log_changes(), switch_logs_debug(), tor_log_update_sigsafe_err_fds(), and truncate_logs().
|
static |
Callback to invoke when pending_cb_messages becomes nonempty.
Definition at line 152 of file log.c.
Referenced by logs_free_all().
|
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().
|
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().
|
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().
|
static |
True iff PRETTY_FUNCTION includes parenthesized arguments.
Definition at line 168 of file log.c.
Referenced by init_logging().
|
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().