#ifndef TOR_TORLOG_H
#define TOR_TORLOG_H
#include <stdarg.h>
#include "lib/cc/torint.h"
#include "lib/cc/compat_compiler.h"
#include "lib/defs/logging_types.h"
#include "lib/testsupport/testsupport.h"
#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#define LOG_WARN LOG_WARNING
#if LOG_DEBUG < LOG_ERR
#ifndef COCCI
#error "Your syslog.h thinks high numbers are more important. " \
"We aren't prepared to deal with that."
#endif
#endif
#else
#define LOG_DEBUG 7
#define LOG_INFO 6
#define LOG_NOTICE 5
#define LOG_WARN 4
#define LOG_ERR 3
#endif
#define LD_GENERAL (UINT64_C(1)<<0)
#define LD_CRYPTO (UINT64_C(1)<<1)
#define LD_NET (UINT64_C(1)<<2)
#define LD_CONFIG (UINT64_C(1)<<3)
#define LD_FS (UINT64_C(1)<<4)
#define LD_PROTOCOL (UINT64_C(1)<<5)
#define LD_MM (UINT64_C(1)<<6)
#define LD_HTTP (UINT64_C(1)<<7)
#define LD_APP (UINT64_C(1)<<8)
#define LD_CONTROL (UINT64_C(1)<<9)
#define LD_CIRC (UINT64_C(1)<<10)
#define LD_REND (UINT64_C(1)<<11)
#define LD_BUG (UINT64_C(1)<<12)
#define LD_DIR (UINT64_C(1)<<13)
#define LD_DIRSERV (UINT64_C(1)<<14)
#define LD_OR (UINT64_C(1)<<15)
#define LD_EDGE (UINT64_C(1)<<16)
#define LD_EXIT LD_EDGE
#define LD_ACCT (UINT64_C(1)<<17)
#define LD_HIST (UINT64_C(1)<<18)
#define LD_HANDSHAKE (UINT64_C(1)<<19)
#define LD_HEARTBEAT (UINT64_C(1)<<20)
#define LD_CHANNEL (UINT64_C(1)<<21)
#define LD_SCHED (UINT64_C(1)<<22)
#define LD_GUARD (UINT64_C(1)<<23)
#define LD_CONSDIFF (UINT64_C(1)<<24)
#define LD_DOS (UINT64_C(1)<<25)
#define LD_PROCESS (UINT64_C(1)<<26)
#define LD_PT (UINT64_C(1)<<27)
#define LD_BTRACK (UINT64_C(1)<<28)
#define LD_MESG (UINT64_C(1)<<29)
#define N_LOGGING_DOMAINS 30
#define HIGHEST_RESERVED_LD_DOMAIN_ (UINT64_C(1)<<(N_LOGGING_DOMAINS - 1))
#define LD_ALL_DOMAINS ((~(UINT64_C(0)))>>(64 - N_LOGGING_DOMAINS))
#define N_LOGGING_FLAGS 3
#define LOWEST_RESERVED_LD_FLAG_ (UINT64_C(1)<<(64 - N_LOGGING_FLAGS))
#define LD_ALL_FLAGS ((~(UINT64_C(0)))<<(64 - N_LOGGING_FLAGS))
#ifdef TOR_UNIT_TESTS
#define LD_NO_MOCK (UINT64_C(1)<<61)
#endif
#define LD_NOCB (UINT64_C(1)<<62)
#define LD_NOFUNCNAME (UINT64_C(1)<<63)
typedef struct log_severity_list_t {
log_domain_mask_t masks[LOG_DEBUG-LOG_ERR+1];
} log_severity_list_t;
typedef void (*log_callback)(int severity, log_domain_mask_t domain,
const char *msg);
void init_logging(int disable_startup_queue);
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(const log_severity_list_t *severity,
const char *name, int fd);
MOCK_DECL(int, add_file_log,(const log_severity_list_t *severity,
const char *filename,
int fd));
#ifdef HAVE_SYSLOG_H
int add_syslog_log(const log_severity_list_t *severity,
const char* syslog_identity_tag);
#endif #ifdef HAVE_ANDROID_LOG_H
int add_android_log(const log_severity_list_t *severity,
const char *android_identity_tag);
#endif int add_callback_log(const log_severity_list_t *severity, log_callback cb);
typedef void (*pending_callback_callback)(void);
void logs_set_pending_callback_callback(pending_callback_callback cb);
void logs_set_domain_logging(int enabled);
int get_min_log_level(void);
void switch_logs_debug(void);
void logs_free_all(void);
void logs_flush_sigsafe(void);
void add_default_log(int min_severity);
void close_temp_logs(void);
void rollback_log_changes(void);
void mark_logs_temp(void);
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 log_set_application_name(const char *name);
MOCK_DECL(void, set_log_time_granularity,(int granularity_msec));
void truncate_logs(void);
void tor_log(int severity, log_domain_mask_t domain, const char *format, ...)
CHECK_PRINTF(3,4);
void tor_log_update_sigsafe_err_fds(void);
struct smartlist_t;
void tor_log_get_logfile_names(struct smartlist_t *out);
extern int log_global_min_severity_;
#ifdef TOR_COVERAGE
#define debug_logging_enabled() (1)
#else
static inline bool debug_logging_enabled(void);
static inline bool debug_logging_enabled(void)
{
return PREDICT_UNLIKELY(log_global_min_severity_ == LOG_DEBUG);
}
#endif
void log_fn_(int severity, log_domain_mask_t domain,
const char *funcname, const char *format, ...)
CHECK_PRINTF(4,5);
struct ratelim_t;
void log_fn_ratelim_(struct ratelim_t *ratelim, int severity,
log_domain_mask_t domain, const char *funcname,
const char *format, ...)
CHECK_PRINTF(5,6);
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);
#if defined(__GNUC__) && __GNUC__ <= 3
#define log_fn(severity, domain, args...) \
log_fn_(severity, domain, __FUNCTION__, args)
#define log_fn_ratelim(ratelim, severity, domain, args...) \
log_fn_ratelim_(ratelim, severity, domain, __FUNCTION__, args)
#define log_debug(domain, args...) \
STMT_BEGIN \
if (debug_logging_enabled()) \
log_fn_(LOG_DEBUG, domain, __FUNCTION__, args); \
STMT_END
#define log_info(domain, args...) \
log_fn_(LOG_INFO, domain, __FUNCTION__, args)
#define log_notice(domain, args...) \
log_fn_(LOG_NOTICE, domain, __FUNCTION__, args)
#define log_warn(domain, args...) \
log_fn_(LOG_WARN, domain, __FUNCTION__, args)
#define log_err(domain, args...) \
log_fn_(LOG_ERR, domain, __FUNCTION__, args)
#else
#define log_debug(domain, args, ...) \
STMT_BEGIN \
if (debug_logging_enabled()) \
log_fn_(LOG_DEBUG, domain, __FUNCTION__, args, ##__VA_ARGS__); \
STMT_END
#define log_info(domain, args,...) \
log_fn_(LOG_INFO, domain, __FUNCTION__, args, ##__VA_ARGS__)
#define log_notice(domain, args,...) \
log_fn_(LOG_NOTICE, domain, __FUNCTION__, args, ##__VA_ARGS__)
#define log_warn(domain, args,...) \
log_fn_(LOG_WARN, domain, __FUNCTION__, args, ##__VA_ARGS__)
#define log_err(domain, args,...) \
log_fn_(LOG_ERR, domain, __FUNCTION__, args, ##__VA_ARGS__)
#define log_fn(severity, domain, args,...) \
log_fn_(severity, domain, __FUNCTION__, args, ##__VA_ARGS__)
#define log_fn_ratelim(ratelim, severity, domain, args,...) \
log_fn_ratelim_(ratelim, severity, domain, __FUNCTION__, \
args, ##__VA_ARGS__)
#endif
extern const int LOG_WARN_;
extern const int LOG_NOTICE_;
extern const log_domain_mask_t LD_NET_;
extern const log_domain_mask_t LD_GENERAL_;
#ifdef LOG_PRIVATE
MOCK_DECL(STATIC void, logv, (int severity, log_domain_mask_t domain,
const char *funcname, const char *suffix, const char *format,
va_list ap) CHECK_PRINTF(5,0));
MOCK_DECL(STATIC void, add_stream_log_impl,(
const log_severity_list_t *severity, const char *name, int fd));
#endif
#if defined(LOG_PRIVATE) || defined(TOR_UNIT_TESTS)
#define SEVERITY_MASK_IDX(sev) ((sev) - LOG_ERR)
#endif
#endif