tor
master
|
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 "compat.h"
#include "util.h"
#include "torlog.h"
#include "container.h"
#include "micro-revision.i"
Data Structures | |
struct | logfile_t |
struct | pending_log_message_t |
Macros | |
#define | LOG_PRIVATE |
#define | SEVERITY_MASK_IDX(sev) ((sev) - LOG_ERR) |
#define | raw_assert(x) assert(x) |
#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 | MAX_SIGSAFE_FDS 8 |
#define | TRUNCATED_STR "[...truncated]" |
#define | TRUNCATED_STR_LEN 14 |
Typedefs | |
typedef struct logfile_t | logfile_t |
typedef struct pending_log_message_t | pending_log_message_t |
Functions | |
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) |
MOCK_IMPL (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,...) |
void | tor_log_err_sigsafe (const char *m,...) |
int | tor_log_get_sigsafe_err_fds (const int **out) |
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 | set_log_severity_config (int loglevelMin, int loglevelMax, log_severity_list_t *severity_out) |
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_temp_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, const int truncate_log) |
int | parse_log_level (const char *level) |
const char * | log_level_to_string (int level) |
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 |
int | log_global_min_severity_ = LOG_NOTICE |
Functions to send messages to log files or the console.
#define LOCK_LOGS | ( | ) |
Lock the log_mutex to prevent others from changing the logfile_t list
#define MAX_SIGSAFE_FDS 8 |
Maximum number of fds that will get notifications if we crash
#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.
#define SEVERITY_MASK_IDX | ( | sev | ) | ((sev) - LOG_ERR) |
Given a severity, yields an index into log_severity_list_t.masks to use for that severity.
#define TRUNCATED_STR "[...truncated]" |
The string we stick at the end of a log message when it is too long, and its length.
#define UNLOCK_LOGS | ( | ) |
Unlock the log_mutex
typedef struct pending_log_message_t pending_log_message_t |
Represents a log message that we are going to send to callback-driven loggers once we can do so in a non-reentrant way.
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.
int add_file_log | ( | const log_severity_list_t * | severity, |
const char * | filename, | ||
const int | truncate_log | ||
) |
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)).
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.
void add_temp_log | ( | int | min_severity | ) |
Add a log handler to receive messages during startup (before the real logs are initialized).
void change_callback_log_severity | ( | int | loglevelMin, |
int | loglevelMax, | ||
log_callback | cb | ||
) |
Adjust the configured severity of any logs whose callback function is cb.
void close_temp_logs | ( | void | ) |
Close any log handlers added by add_temp_log() or marked by mark_logs_temp().
void flush_log_messages_from_startup | ( | void | ) |
Flush all the messages we stored from startup while waiting for log initialization.
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.
int get_min_log_level | ( | void | ) |
Return the least severe log level that any current log is interested in.
void init_logging | ( | int | disable_startup_queue | ) |
Initialize the global logging facility
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 functions.
const char* log_level_to_string | ( | int | level | ) |
Return the string equivalent of a given log 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.
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.
void logs_free_all | ( | void | ) |
Close all open log files, and free other static memory.
void logs_set_domain_logging | ( | int | enabled | ) |
Set whether we report logging domains as a part of our log messages.
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".
void mark_logs_temp | ( | void | ) |
Configure all log handles to be closed by close_temp_logs().
MOCK_IMPL | ( | 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).
int parse_log_level | ( | const char * | level | ) |
If level is a valid log severity, return the corresponding numeric value. Otherwise, return -1.
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.
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.
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.
void set_log_time_granularity | ( | int | granularity_msec | ) |
Define log time granularity for all logs to be granularity_msec milliseconds.
void switch_logs_debug | ( | void | ) |
Switch all logs to output at most verbose level.
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.
void tor_log_err_sigsafe | ( | const char * | m, |
... | |||
) |
Given a list of string arguments ending with a NULL, writes them to our logs and to stderr (if possible). This function is safe to call from within a signal handler.
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.
int tor_log_get_sigsafe_err_fds | ( | const int ** | out | ) |
Set *out to a pointer to an array of the fds to log errors to from inside a signal handler. Return the number of elements in the array.
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.
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.
void truncate_logs | ( | void | ) |
Truncate all the log files.
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.
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)