log.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. /* Copyright (c) 2001, Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2013, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file log.c
  8. * \brief Functions to send messages to log files or the console.
  9. **/
  10. #include "orconfig.h"
  11. #include <stdarg.h>
  12. #include <assert.h>
  13. // #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #ifdef HAVE_SYS_TIME_H
  17. #include <sys/time.h>
  18. #endif
  19. #ifdef HAVE_TIME_H
  20. #include <time.h>
  21. #endif
  22. #ifdef HAVE_UNISTD_H
  23. #include <unistd.h>
  24. #endif
  25. #ifdef HAVE_SYS_TYPES_H
  26. #include <sys/types.h>
  27. #endif
  28. #ifdef HAVE_FCNTL_H
  29. #include <fcntl.h>
  30. #endif
  31. #include "compat.h"
  32. #include "util.h"
  33. #define LOG_PRIVATE
  34. #include "torlog.h"
  35. #include "container.h"
  36. /** Given a severity, yields an index into log_severity_list_t.masks to use
  37. * for that severity. */
  38. #define SEVERITY_MASK_IDX(sev) ((sev) - LOG_ERR)
  39. /** @{ */
  40. /** The string we stick at the end of a log message when it is too long,
  41. * and its length. */
  42. #define TRUNCATED_STR "[...truncated]"
  43. #define TRUNCATED_STR_LEN 14
  44. /** @} */
  45. /** Information for a single logfile; only used in log.c */
  46. typedef struct logfile_t {
  47. struct logfile_t *next; /**< Next logfile_t in the linked list. */
  48. char *filename; /**< Filename to open. */
  49. int fd; /**< fd to receive log messages, or -1 for none. */
  50. int seems_dead; /**< Boolean: true if the stream seems to be kaput. */
  51. int needs_close; /**< Boolean: true if the stream gets closed on shutdown. */
  52. int is_temporary; /**< Boolean: close after initializing logging subsystem.*/
  53. int is_syslog; /**< Boolean: send messages to syslog. */
  54. log_callback callback; /**< If not NULL, send messages to this function. */
  55. log_severity_list_t *severities; /**< Which severity of messages should we
  56. * log for each log domain? */
  57. } logfile_t;
  58. static void log_free(logfile_t *victim);
  59. /** Helper: map a log severity to descriptive string. */
  60. static INLINE const char *
  61. sev_to_string(int severity)
  62. {
  63. switch (severity) {
  64. case LOG_DEBUG: return "debug";
  65. case LOG_INFO: return "info";
  66. case LOG_NOTICE: return "notice";
  67. case LOG_WARN: return "warn";
  68. case LOG_ERR: return "err";
  69. default: /* Call assert, not tor_assert, since tor_assert
  70. * calls log on failure. */
  71. assert(0); return "UNKNOWN";
  72. }
  73. }
  74. /** Helper: decide whether to include the function name in the log message. */
  75. static INLINE int
  76. should_log_function_name(log_domain_mask_t domain, int severity)
  77. {
  78. switch (severity) {
  79. case LOG_DEBUG:
  80. case LOG_INFO:
  81. /* All debugging messages occur in interesting places. */
  82. return 1;
  83. case LOG_NOTICE:
  84. case LOG_WARN:
  85. case LOG_ERR:
  86. /* We care about places where bugs occur. */
  87. return (domain == LD_BUG);
  88. default:
  89. /* Call assert, not tor_assert, since tor_assert calls log on failure. */
  90. assert(0); return 0;
  91. }
  92. }
  93. /** A mutex to guard changes to logfiles and logging. */
  94. static tor_mutex_t log_mutex;
  95. /** True iff we have initialized log_mutex */
  96. static int log_mutex_initialized = 0;
  97. /** Linked list of logfile_t. */
  98. static logfile_t *logfiles = NULL;
  99. /** Boolean: do we report logging domains? */
  100. static int log_domains_are_logged = 0;
  101. #ifdef HAVE_SYSLOG_H
  102. /** The number of open syslog log handlers that we have. When this reaches 0,
  103. * we can close our connection to the syslog facility. */
  104. static int syslog_count = 0;
  105. #endif
  106. /** Represents a log message that we are going to send to callback-driven
  107. * loggers once we can do so in a non-reentrant way. */
  108. typedef struct pending_cb_message_t {
  109. int severity; /**< The severity of the message */
  110. log_domain_mask_t domain; /**< The domain of the message */
  111. char *msg; /**< The content of the message */
  112. } pending_cb_message_t;
  113. /** Log messages waiting to be replayed onto callback-based logs */
  114. static smartlist_t *pending_cb_messages = NULL;
  115. /** Lock the log_mutex to prevent others from changing the logfile_t list */
  116. #define LOCK_LOGS() STMT_BEGIN \
  117. tor_mutex_acquire(&log_mutex); \
  118. STMT_END
  119. /** Unlock the log_mutex */
  120. #define UNLOCK_LOGS() STMT_BEGIN tor_mutex_release(&log_mutex); STMT_END
  121. /** What's the lowest log level anybody cares about? Checking this lets us
  122. * bail out early from log_debug if we aren't debugging. */
  123. int log_global_min_severity_ = LOG_NOTICE;
  124. static void delete_log(logfile_t *victim);
  125. static void close_log(logfile_t *victim);
  126. static char *domain_to_string(log_domain_mask_t domain,
  127. char *buf, size_t buflen);
  128. static INLINE char *format_msg(char *buf, size_t buf_len,
  129. log_domain_mask_t domain, int severity, const char *funcname,
  130. const char *suffix,
  131. const char *format, va_list ap, size_t *msg_len_out)
  132. CHECK_PRINTF(7,0);
  133. static void logv(int severity, log_domain_mask_t domain, const char *funcname,
  134. const char *suffix, const char *format, va_list ap)
  135. CHECK_PRINTF(5,0);
  136. /** Name of the application: used to generate the message we write at the
  137. * start of each new log. */
  138. static char *appname = NULL;
  139. /** Set the "application name" for the logs to <b>name</b>: we'll use this
  140. * name in the message we write when starting up, and at the start of each new
  141. * log.
  142. *
  143. * Tor uses this string to write the version number to the log file. */
  144. void
  145. log_set_application_name(const char *name)
  146. {
  147. tor_free(appname);
  148. appname = name ? tor_strdup(name) : NULL;
  149. }
  150. /** Log time granularity in milliseconds. */
  151. static int log_time_granularity = 1;
  152. /** Define log time granularity for all logs to be <b>granularity_msec</b>
  153. * milliseconds. */
  154. void
  155. set_log_time_granularity(int granularity_msec)
  156. {
  157. log_time_granularity = granularity_msec;
  158. }
  159. /** Helper: Write the standard prefix for log lines to a
  160. * <b>buf_len</b> character buffer in <b>buf</b>.
  161. */
  162. static INLINE size_t
  163. log_prefix_(char *buf, size_t buf_len, int severity)
  164. {
  165. time_t t;
  166. struct timeval now;
  167. struct tm tm;
  168. size_t n;
  169. int r, ms;
  170. tor_gettimeofday(&now);
  171. t = (time_t)now.tv_sec;
  172. ms = (int)now.tv_usec / 1000;
  173. if (log_time_granularity >= 1000) {
  174. t -= t % (log_time_granularity / 1000);
  175. ms = 0;
  176. } else {
  177. ms -= ((int)now.tv_usec / 1000) % log_time_granularity;
  178. }
  179. n = strftime(buf, buf_len, "%b %d %H:%M:%S", tor_localtime_r(&t, &tm));
  180. r = tor_snprintf(buf+n, buf_len-n, ".%.3i [%s] ", ms,
  181. sev_to_string(severity));
  182. if (r<0)
  183. return buf_len-1;
  184. else
  185. return n+r;
  186. }
  187. /** If lf refers to an actual file that we have just opened, and the file
  188. * contains no data, log an "opening new logfile" message at the top.
  189. *
  190. * Return -1 if the log is broken and needs to be deleted, else return 0.
  191. */
  192. static int
  193. log_tor_version(logfile_t *lf, int reset)
  194. {
  195. char buf[256];
  196. size_t n;
  197. int is_new;
  198. if (!lf->needs_close)
  199. /* If it doesn't get closed, it isn't really a file. */
  200. return 0;
  201. if (lf->is_temporary)
  202. /* If it's temporary, it isn't really a file. */
  203. return 0;
  204. is_new = lf->fd >= 0 && tor_fd_getpos(lf->fd) == 0;
  205. if (reset && !is_new)
  206. /* We are resetting, but we aren't at the start of the file; no
  207. * need to log again. */
  208. return 0;
  209. n = log_prefix_(buf, sizeof(buf), LOG_NOTICE);
  210. if (appname) {
  211. tor_snprintf(buf+n, sizeof(buf)-n,
  212. "%s opening %slog file.\n", appname, is_new?"new ":"");
  213. } else {
  214. tor_snprintf(buf+n, sizeof(buf)-n,
  215. "Tor %s opening %slog file.\n", VERSION, is_new?"new ":"");
  216. }
  217. if (write_all(lf->fd, buf, strlen(buf), 0) < 0) /* error */
  218. return -1; /* failed */
  219. return 0;
  220. }
  221. /** Helper: Format a log message into a fixed-sized buffer. (This is
  222. * factored out of <b>logv</b> so that we never format a message more
  223. * than once.) Return a pointer to the first character of the message
  224. * portion of the formatted string.
  225. */
  226. static INLINE char *
  227. format_msg(char *buf, size_t buf_len,
  228. log_domain_mask_t domain, int severity, const char *funcname,
  229. const char *suffix,
  230. const char *format, va_list ap, size_t *msg_len_out)
  231. {
  232. size_t n;
  233. int r;
  234. char *end_of_prefix;
  235. char *buf_end;
  236. assert(buf_len >= 16); /* prevent integer underflow and general stupidity */
  237. buf_len -= 2; /* subtract 2 characters so we have room for \n\0 */
  238. buf_end = buf+buf_len; /* point *after* the last char we can write to */
  239. n = log_prefix_(buf, buf_len, severity);
  240. end_of_prefix = buf+n;
  241. if (log_domains_are_logged) {
  242. char *cp = buf+n;
  243. if (cp == buf_end) goto format_msg_no_room_for_domains;
  244. *cp++ = '{';
  245. if (cp == buf_end) goto format_msg_no_room_for_domains;
  246. cp = domain_to_string(domain, cp, (buf+buf_len-cp));
  247. if (cp == buf_end) goto format_msg_no_room_for_domains;
  248. *cp++ = '}';
  249. if (cp == buf_end) goto format_msg_no_room_for_domains;
  250. *cp++ = ' ';
  251. if (cp == buf_end) goto format_msg_no_room_for_domains;
  252. end_of_prefix = cp;
  253. n = cp-buf;
  254. format_msg_no_room_for_domains:
  255. /* This will leave end_of_prefix and n unchanged, and thus cause
  256. * whatever log domain string we had written to be clobbered. */
  257. ;
  258. }
  259. if (funcname && should_log_function_name(domain, severity)) {
  260. r = tor_snprintf(buf+n, buf_len-n, "%s(): ", funcname);
  261. if (r<0)
  262. n = strlen(buf);
  263. else
  264. n += r;
  265. }
  266. if (domain == LD_BUG && buf_len-n > 6) {
  267. memcpy(buf+n, "Bug: ", 6);
  268. n += 5;
  269. }
  270. r = tor_vsnprintf(buf+n,buf_len-n,format,ap);
  271. if (r < 0) {
  272. /* The message was too long; overwrite the end of the buffer with
  273. * "[...truncated]" */
  274. if (buf_len >= TRUNCATED_STR_LEN) {
  275. size_t offset = buf_len-TRUNCATED_STR_LEN;
  276. /* We have an extra 2 characters after buf_len to hold the \n\0,
  277. * so it's safe to add 1 to the size here. */
  278. strlcpy(buf+offset, TRUNCATED_STR, buf_len-offset+1);
  279. }
  280. /* Set 'n' to the end of the buffer, where we'll be writing \n\0.
  281. * Since we already subtracted 2 from buf_len, this is safe.*/
  282. n = buf_len;
  283. } else {
  284. n += r;
  285. if (suffix) {
  286. size_t suffix_len = strlen(suffix);
  287. if (buf_len-n >= suffix_len) {
  288. memcpy(buf+n, suffix, suffix_len);
  289. n += suffix_len;
  290. }
  291. }
  292. }
  293. buf[n]='\n';
  294. buf[n+1]='\0';
  295. *msg_len_out = n+1;
  296. return end_of_prefix;
  297. }
  298. /** Helper: sends a message to the appropriate logfiles, at loglevel
  299. * <b>severity</b>. If provided, <b>funcname</b> is prepended to the
  300. * message. The actual message is derived as from tor_snprintf(format,ap).
  301. */
  302. static void
  303. logv(int severity, log_domain_mask_t domain, const char *funcname,
  304. const char *suffix, const char *format, va_list ap)
  305. {
  306. char buf[10024];
  307. size_t msg_len = 0;
  308. int formatted = 0;
  309. logfile_t *lf;
  310. char *end_of_prefix=NULL;
  311. int callbacks_deferred = 0;
  312. /* Call assert, not tor_assert, since tor_assert calls log on failure. */
  313. assert(format);
  314. /* check that severity is sane. Overrunning the masks array leads to
  315. * interesting and hard to diagnose effects */
  316. assert(severity >= LOG_ERR && severity <= LOG_DEBUG);
  317. LOCK_LOGS();
  318. if ((! (domain & LD_NOCB)) && smartlist_len(pending_cb_messages))
  319. flush_pending_log_callbacks();
  320. lf = logfiles;
  321. while (lf) {
  322. if (! (lf->severities->masks[SEVERITY_MASK_IDX(severity)] & domain)) {
  323. lf = lf->next;
  324. continue;
  325. }
  326. if (! (lf->fd >= 0 || lf->is_syslog || lf->callback)) {
  327. lf = lf->next;
  328. continue;
  329. }
  330. if (lf->seems_dead) {
  331. lf = lf->next;
  332. continue;
  333. }
  334. if (!formatted) {
  335. end_of_prefix =
  336. format_msg(buf, sizeof(buf), domain, severity, funcname, suffix,
  337. format, ap, &msg_len);
  338. formatted = 1;
  339. }
  340. if (lf->is_syslog) {
  341. #ifdef HAVE_SYSLOG_H
  342. char *m = end_of_prefix;
  343. #ifdef MAXLINE
  344. /* Some syslog implementations have limits on the length of what you can
  345. * pass them, and some very old ones do not detect overflow so well.
  346. * Regrettably, they call their maximum line length MAXLINE. */
  347. #if MAXLINE < 64
  348. #warn "MAXLINE is a very low number; it might not be from syslog.h after all"
  349. #endif
  350. if (msg_len >= MAXLINE)
  351. m = tor_strndup(end_of_prefix, MAXLINE-1);
  352. #endif
  353. syslog(severity, "%s", m);
  354. #ifdef MAXLINE
  355. if (m != end_of_prefix) {
  356. tor_free(m);
  357. }
  358. #endif
  359. #endif
  360. lf = lf->next;
  361. continue;
  362. } else if (lf->callback) {
  363. if (domain & LD_NOCB) {
  364. if (!callbacks_deferred && pending_cb_messages) {
  365. pending_cb_message_t *msg = tor_malloc(sizeof(pending_cb_message_t));
  366. msg->severity = severity;
  367. msg->domain = domain;
  368. msg->msg = tor_strdup(end_of_prefix);
  369. smartlist_add(pending_cb_messages, msg);
  370. callbacks_deferred = 1;
  371. }
  372. } else {
  373. lf->callback(severity, domain, end_of_prefix);
  374. }
  375. lf = lf->next;
  376. continue;
  377. }
  378. if (write_all(lf->fd, buf, msg_len, 0) < 0) { /* error */
  379. /* don't log the error! mark this log entry to be blown away, and
  380. * continue. */
  381. lf->seems_dead = 1;
  382. }
  383. lf = lf->next;
  384. }
  385. UNLOCK_LOGS();
  386. }
  387. /** Output a message to the log. It gets logged to all logfiles that
  388. * care about messages with <b>severity</b> in <b>domain</b>. The content
  389. * is formatted printf-style based on <b>format</b> and extra arguments.
  390. * */
  391. void
  392. tor_log(int severity, log_domain_mask_t domain, const char *format, ...)
  393. {
  394. va_list ap;
  395. if (severity > log_global_min_severity_)
  396. return;
  397. va_start(ap,format);
  398. logv(severity, domain, NULL, NULL, format, ap);
  399. va_end(ap);
  400. }
  401. /** Maximum number of fds that will get notifications if we crash */
  402. #define MAX_SIGSAFE_FDS 8
  403. /** Array of fds to log crash-style warnings to. */
  404. static int sigsafe_log_fds[MAX_SIGSAFE_FDS] = { STDERR_FILENO };
  405. /** The number of elements used in sigsafe_log_fds */
  406. static int n_sigsafe_log_fds = 1;
  407. /** Write <b>s</b> to each element of sigsafe_log_fds. Return 0 on success, -1
  408. * on failure. */
  409. static int
  410. tor_log_err_sigsafe_write(const char *s)
  411. {
  412. int i;
  413. ssize_t r;
  414. size_t len = strlen(s);
  415. int err = 0;
  416. for (i=0; i < n_sigsafe_log_fds; ++i) {
  417. r = write(sigsafe_log_fds[i], s, len);
  418. err += (r != (ssize_t)len);
  419. }
  420. return err ? -1 : 0;
  421. }
  422. /** Given a list of string arguments ending with a NULL, writes them
  423. * to our logs and to stderr (if possible). This function is safe to call
  424. * from within a signal handler. */
  425. void
  426. tor_log_err_sigsafe(const char *m, ...)
  427. {
  428. va_list ap;
  429. const char *x;
  430. char timebuf[32];
  431. time_t now = time(NULL);
  432. if (!m)
  433. return;
  434. if (log_time_granularity >= 2000) {
  435. int g = log_time_granularity / 1000;
  436. now -= now % g;
  437. }
  438. timebuf[0] = '\0';
  439. format_dec_number_sigsafe(now, timebuf, sizeof(timebuf));
  440. tor_log_err_sigsafe_write("\n=========================================="
  441. "================== T=");
  442. tor_log_err_sigsafe_write(timebuf);
  443. tor_log_err_sigsafe_write("\n");
  444. tor_log_err_sigsafe_write(m);
  445. va_start(ap, m);
  446. while ((x = va_arg(ap, const char*))) {
  447. tor_log_err_sigsafe_write(x);
  448. }
  449. va_end(ap);
  450. }
  451. /** Set *<b>out</b> to a pointer to an array of the fds to log errors to from
  452. * inside a signal handler. Return the number of elements in the array. */
  453. int
  454. tor_log_get_sigsafe_err_fds(const int **out)
  455. {
  456. *out = sigsafe_log_fds;
  457. return n_sigsafe_log_fds;
  458. }
  459. /** Helper function; return true iff the <b>n</b>-element array <b>array</b>
  460. * contains <b>item</b>. */
  461. static int
  462. int_array_contains(const int *array, int n, int item)
  463. {
  464. int j;
  465. for (j = 0; j < n; ++j) {
  466. if (array[j] == item)
  467. return 1;
  468. }
  469. return 0;
  470. }
  471. /** Function to call whenever the list of logs changes to get ready to log
  472. * from signal handlers. */
  473. void
  474. tor_log_update_sigsafe_err_fds(void)
  475. {
  476. const logfile_t *lf;
  477. LOCK_LOGS();
  478. /* Always try for stderr. This is safe because when we daemonize, we dup2
  479. * /dev/null to stderr, */
  480. sigsafe_log_fds[0] = STDERR_FILENO;
  481. n_sigsafe_log_fds = 1;
  482. for (lf = logfiles; lf; lf = lf->next) {
  483. /* Don't try callback to the control port, or syslogs: We can't
  484. * do them from a signal handler. Don't try stdout: we always do stderr.
  485. */
  486. if (lf->is_temporary || lf->is_syslog ||
  487. lf->callback || lf->seems_dead || lf->fd < 0)
  488. continue;
  489. if (lf->severities->masks[SEVERITY_MASK_IDX(LOG_ERR)] &
  490. (LD_BUG|LD_GENERAL)) {
  491. /* Avoid duplicates */
  492. if (int_array_contains(sigsafe_log_fds, n_sigsafe_log_fds, lf->fd))
  493. continue;
  494. sigsafe_log_fds[n_sigsafe_log_fds++] = lf->fd;
  495. if (n_sigsafe_log_fds == MAX_SIGSAFE_FDS)
  496. break;
  497. }
  498. }
  499. UNLOCK_LOGS();
  500. }
  501. /** Output a message to the log, prefixed with a function name <b>fn</b>. */
  502. #ifdef __GNUC__
  503. /** GCC-based implementation of the log_fn backend, used when we have
  504. * variadic macros. All arguments are as for log_fn, except for
  505. * <b>fn</b>, which is the name of the calling functions. */
  506. void
  507. log_fn_(int severity, log_domain_mask_t domain, const char *fn,
  508. const char *format, ...)
  509. {
  510. va_list ap;
  511. if (severity > log_global_min_severity_)
  512. return;
  513. va_start(ap,format);
  514. logv(severity, domain, fn, NULL, format, ap);
  515. va_end(ap);
  516. }
  517. void
  518. log_fn_ratelim_(ratelim_t *ratelim, int severity, log_domain_mask_t domain,
  519. const char *fn, const char *format, ...)
  520. {
  521. va_list ap;
  522. char *m;
  523. if (severity > log_global_min_severity_)
  524. return;
  525. m = rate_limit_log(ratelim, approx_time());
  526. if (m == NULL)
  527. return;
  528. va_start(ap, format);
  529. logv(severity, domain, fn, m, format, ap);
  530. va_end(ap);
  531. tor_free(m);
  532. }
  533. #else
  534. /** @{ */
  535. /** Variant implementation of log_fn, log_debug, log_info,... for C compilers
  536. * without variadic macros. In this case, the calling function sets
  537. * log_fn_function_name_ to the name of the function, then invokes the
  538. * appropriate log_fn_, log_debug_, etc. */
  539. const char *log_fn_function_name_=NULL;
  540. void
  541. log_fn_(int severity, log_domain_mask_t domain, const char *format, ...)
  542. {
  543. va_list ap;
  544. if (severity > log_global_min_severity_)
  545. return;
  546. va_start(ap,format);
  547. logv(severity, domain, log_fn_function_name_, NULL, format, ap);
  548. va_end(ap);
  549. log_fn_function_name_ = NULL;
  550. }
  551. void
  552. log_fn_ratelim_(ratelim_t *ratelim, int severity, log_domain_mask_t domain,
  553. const char *format, ...)
  554. {
  555. va_list ap;
  556. char *m;
  557. if (severity > log_global_min_severity_)
  558. return;
  559. m = rate_limit_log(ratelim, approx_time());
  560. if (m == NULL)
  561. return;
  562. va_start(ap, format);
  563. logv(severity, domain, log_fn_function_name_, m, format, ap);
  564. va_end(ap);
  565. tor_free(m);
  566. }
  567. void
  568. log_debug_(log_domain_mask_t domain, const char *format, ...)
  569. {
  570. va_list ap;
  571. /* For GCC we do this check in the macro. */
  572. if (PREDICT_LIKELY(LOG_DEBUG > log_global_min_severity_))
  573. return;
  574. va_start(ap,format);
  575. logv(LOG_DEBUG, domain, log_fn_function_name_, NULL, format, ap);
  576. va_end(ap);
  577. log_fn_function_name_ = NULL;
  578. }
  579. void
  580. log_info_(log_domain_mask_t domain, const char *format, ...)
  581. {
  582. va_list ap;
  583. if (LOG_INFO > log_global_min_severity_)
  584. return;
  585. va_start(ap,format);
  586. logv(LOG_INFO, domain, log_fn_function_name_, NULL, format, ap);
  587. va_end(ap);
  588. log_fn_function_name_ = NULL;
  589. }
  590. void
  591. log_notice_(log_domain_mask_t domain, const char *format, ...)
  592. {
  593. va_list ap;
  594. if (LOG_NOTICE > log_global_min_severity_)
  595. return;
  596. va_start(ap,format);
  597. logv(LOG_NOTICE, domain, log_fn_function_name_, NULL, format, ap);
  598. va_end(ap);
  599. log_fn_function_name_ = NULL;
  600. }
  601. void
  602. log_warn_(log_domain_mask_t domain, const char *format, ...)
  603. {
  604. va_list ap;
  605. if (LOG_WARN > log_global_min_severity_)
  606. return;
  607. va_start(ap,format);
  608. logv(LOG_WARN, domain, log_fn_function_name_, NULL, format, ap);
  609. va_end(ap);
  610. log_fn_function_name_ = NULL;
  611. }
  612. void
  613. log_err_(log_domain_mask_t domain, const char *format, ...)
  614. {
  615. va_list ap;
  616. if (LOG_ERR > log_global_min_severity_)
  617. return;
  618. va_start(ap,format);
  619. logv(LOG_ERR, domain, log_fn_function_name_, NULL, format, ap);
  620. va_end(ap);
  621. log_fn_function_name_ = NULL;
  622. }
  623. /** @} */
  624. #endif
  625. /** Free all storage held by <b>victim</b>. */
  626. static void
  627. log_free(logfile_t *victim)
  628. {
  629. if (!victim)
  630. return;
  631. tor_free(victim->severities);
  632. tor_free(victim->filename);
  633. tor_free(victim);
  634. }
  635. /** Close all open log files, and free other static memory. */
  636. void
  637. logs_free_all(void)
  638. {
  639. logfile_t *victim, *next;
  640. smartlist_t *messages;
  641. LOCK_LOGS();
  642. next = logfiles;
  643. logfiles = NULL;
  644. messages = pending_cb_messages;
  645. pending_cb_messages = NULL;
  646. UNLOCK_LOGS();
  647. while (next) {
  648. victim = next;
  649. next = next->next;
  650. close_log(victim);
  651. log_free(victim);
  652. }
  653. tor_free(appname);
  654. SMARTLIST_FOREACH(messages, pending_cb_message_t *, msg, {
  655. tor_free(msg->msg);
  656. tor_free(msg);
  657. });
  658. smartlist_free(messages);
  659. /* We _could_ destroy the log mutex here, but that would screw up any logs
  660. * that happened between here and the end of execution. */
  661. }
  662. /** Remove and free the log entry <b>victim</b> from the linked-list
  663. * logfiles (it is probably present, but it might not be due to thread
  664. * racing issues). After this function is called, the caller shouldn't
  665. * refer to <b>victim</b> anymore.
  666. *
  667. * Long-term, we need to do something about races in the log subsystem
  668. * in general. See bug 222 for more details.
  669. */
  670. static void
  671. delete_log(logfile_t *victim)
  672. {
  673. logfile_t *tmpl;
  674. if (victim == logfiles)
  675. logfiles = victim->next;
  676. else {
  677. for (tmpl = logfiles; tmpl && tmpl->next != victim; tmpl=tmpl->next) ;
  678. // tor_assert(tmpl);
  679. // tor_assert(tmpl->next == victim);
  680. if (!tmpl)
  681. return;
  682. tmpl->next = victim->next;
  683. }
  684. log_free(victim);
  685. }
  686. /** Helper: release system resources (but not memory) held by a single
  687. * logfile_t. */
  688. static void
  689. close_log(logfile_t *victim)
  690. {
  691. if (victim->needs_close && victim->fd >= 0) {
  692. close(victim->fd);
  693. victim->fd = -1;
  694. } else if (victim->is_syslog) {
  695. #ifdef HAVE_SYSLOG_H
  696. if (--syslog_count == 0) {
  697. /* There are no other syslogs; close the logging facility. */
  698. closelog();
  699. }
  700. #endif
  701. }
  702. }
  703. /** Adjust a log severity configuration in <b>severity_out</b> to contain
  704. * every domain between <b>loglevelMin</b> and <b>loglevelMax</b>, inclusive.
  705. */
  706. void
  707. set_log_severity_config(int loglevelMin, int loglevelMax,
  708. log_severity_list_t *severity_out)
  709. {
  710. int i;
  711. tor_assert(loglevelMin >= loglevelMax);
  712. tor_assert(loglevelMin >= LOG_ERR && loglevelMin <= LOG_DEBUG);
  713. tor_assert(loglevelMax >= LOG_ERR && loglevelMax <= LOG_DEBUG);
  714. memset(severity_out, 0, sizeof(log_severity_list_t));
  715. for (i = loglevelMin; i >= loglevelMax; --i) {
  716. severity_out->masks[SEVERITY_MASK_IDX(i)] = ~0u;
  717. }
  718. }
  719. /** Add a log handler named <b>name</b> to send all messages in <b>severity</b>
  720. * to <b>fd</b>. Copies <b>severity</b>. Helper: does no locking. */
  721. static void
  722. add_stream_log_impl(const log_severity_list_t *severity,
  723. const char *name, int fd)
  724. {
  725. logfile_t *lf;
  726. lf = tor_malloc_zero(sizeof(logfile_t));
  727. lf->fd = fd;
  728. lf->filename = tor_strdup(name);
  729. lf->severities = tor_memdup(severity, sizeof(log_severity_list_t));
  730. lf->next = logfiles;
  731. logfiles = lf;
  732. log_global_min_severity_ = get_min_log_level();
  733. }
  734. /** Add a log handler named <b>name</b> to send all messages in <b>severity</b>
  735. * to <b>fd</b>. Steals a reference to <b>severity</b>; the caller must
  736. * not use it after calling this function. */
  737. void
  738. add_stream_log(const log_severity_list_t *severity, const char *name, int fd)
  739. {
  740. LOCK_LOGS();
  741. add_stream_log_impl(severity, name, fd);
  742. UNLOCK_LOGS();
  743. }
  744. /** Initialize the global logging facility */
  745. void
  746. init_logging(void)
  747. {
  748. if (!log_mutex_initialized) {
  749. tor_mutex_init(&log_mutex);
  750. log_mutex_initialized = 1;
  751. }
  752. if (pending_cb_messages == NULL)
  753. pending_cb_messages = smartlist_new();
  754. }
  755. /** Set whether we report logging domains as a part of our log messages.
  756. */
  757. void
  758. logs_set_domain_logging(int enabled)
  759. {
  760. LOCK_LOGS();
  761. log_domains_are_logged = enabled;
  762. UNLOCK_LOGS();
  763. }
  764. /** Add a log handler to receive messages during startup (before the real
  765. * logs are initialized).
  766. */
  767. void
  768. add_temp_log(int min_severity)
  769. {
  770. log_severity_list_t *s = tor_malloc_zero(sizeof(log_severity_list_t));
  771. set_log_severity_config(min_severity, LOG_ERR, s);
  772. LOCK_LOGS();
  773. add_stream_log_impl(s, "<temp>", fileno(stdout));
  774. tor_free(s);
  775. logfiles->is_temporary = 1;
  776. UNLOCK_LOGS();
  777. }
  778. /**
  779. * Add a log handler to send messages in <b>severity</b>
  780. * to the function <b>cb</b>.
  781. */
  782. int
  783. add_callback_log(const log_severity_list_t *severity, log_callback cb)
  784. {
  785. logfile_t *lf;
  786. lf = tor_malloc_zero(sizeof(logfile_t));
  787. lf->fd = -1;
  788. lf->severities = tor_memdup(severity, sizeof(log_severity_list_t));
  789. lf->filename = tor_strdup("<callback>");
  790. lf->callback = cb;
  791. lf->next = logfiles;
  792. LOCK_LOGS();
  793. logfiles = lf;
  794. log_global_min_severity_ = get_min_log_level();
  795. UNLOCK_LOGS();
  796. return 0;
  797. }
  798. /** Adjust the configured severity of any logs whose callback function is
  799. * <b>cb</b>. */
  800. void
  801. change_callback_log_severity(int loglevelMin, int loglevelMax,
  802. log_callback cb)
  803. {
  804. logfile_t *lf;
  805. log_severity_list_t severities;
  806. set_log_severity_config(loglevelMin, loglevelMax, &severities);
  807. LOCK_LOGS();
  808. for (lf = logfiles; lf; lf = lf->next) {
  809. if (lf->callback == cb) {
  810. memcpy(lf->severities, &severities, sizeof(severities));
  811. }
  812. }
  813. log_global_min_severity_ = get_min_log_level();
  814. UNLOCK_LOGS();
  815. }
  816. /** If there are any log messages that were generated with LD_NOCB waiting to
  817. * be sent to callback-based loggers, send them now. */
  818. void
  819. flush_pending_log_callbacks(void)
  820. {
  821. logfile_t *lf;
  822. smartlist_t *messages, *messages_tmp;
  823. LOCK_LOGS();
  824. if (0 == smartlist_len(pending_cb_messages)) {
  825. UNLOCK_LOGS();
  826. return;
  827. }
  828. messages = pending_cb_messages;
  829. pending_cb_messages = smartlist_new();
  830. do {
  831. SMARTLIST_FOREACH_BEGIN(messages, pending_cb_message_t *, msg) {
  832. const int severity = msg->severity;
  833. const int domain = msg->domain;
  834. for (lf = logfiles; lf; lf = lf->next) {
  835. if (! lf->callback || lf->seems_dead ||
  836. ! (lf->severities->masks[SEVERITY_MASK_IDX(severity)] & domain)) {
  837. continue;
  838. }
  839. lf->callback(severity, domain, msg->msg);
  840. }
  841. tor_free(msg->msg);
  842. tor_free(msg);
  843. } SMARTLIST_FOREACH_END(msg);
  844. smartlist_clear(messages);
  845. messages_tmp = pending_cb_messages;
  846. pending_cb_messages = messages;
  847. messages = messages_tmp;
  848. } while (smartlist_len(messages));
  849. smartlist_free(messages);
  850. UNLOCK_LOGS();
  851. }
  852. /** Close any log handlers added by add_temp_log() or marked by
  853. * mark_logs_temp(). */
  854. void
  855. close_temp_logs(void)
  856. {
  857. logfile_t *lf, **p;
  858. LOCK_LOGS();
  859. for (p = &logfiles; *p; ) {
  860. if ((*p)->is_temporary) {
  861. lf = *p;
  862. /* we use *p here to handle the edge case of the head of the list */
  863. *p = (*p)->next;
  864. close_log(lf);
  865. log_free(lf);
  866. } else {
  867. p = &((*p)->next);
  868. }
  869. }
  870. log_global_min_severity_ = get_min_log_level();
  871. UNLOCK_LOGS();
  872. }
  873. /** Make all currently temporary logs (set to be closed by close_temp_logs)
  874. * live again, and close all non-temporary logs. */
  875. void
  876. rollback_log_changes(void)
  877. {
  878. logfile_t *lf;
  879. LOCK_LOGS();
  880. for (lf = logfiles; lf; lf = lf->next)
  881. lf->is_temporary = ! lf->is_temporary;
  882. UNLOCK_LOGS();
  883. close_temp_logs();
  884. }
  885. /** Configure all log handles to be closed by close_temp_logs(). */
  886. void
  887. mark_logs_temp(void)
  888. {
  889. logfile_t *lf;
  890. LOCK_LOGS();
  891. for (lf = logfiles; lf; lf = lf->next)
  892. lf->is_temporary = 1;
  893. UNLOCK_LOGS();
  894. }
  895. /**
  896. * Add a log handler to send messages to <b>filename</b>. If opening the
  897. * logfile fails, -1 is returned and errno is set appropriately (by open(2)).
  898. */
  899. int
  900. add_file_log(const log_severity_list_t *severity, const char *filename)
  901. {
  902. int fd;
  903. logfile_t *lf;
  904. fd = tor_open_cloexec(filename, O_WRONLY|O_CREAT|O_APPEND, 0644);
  905. if (fd<0)
  906. return -1;
  907. if (tor_fd_seekend(fd)<0) {
  908. close(fd);
  909. return -1;
  910. }
  911. LOCK_LOGS();
  912. add_stream_log_impl(severity, filename, fd);
  913. logfiles->needs_close = 1;
  914. lf = logfiles;
  915. log_global_min_severity_ = get_min_log_level();
  916. if (log_tor_version(lf, 0) < 0) {
  917. delete_log(lf);
  918. }
  919. UNLOCK_LOGS();
  920. return 0;
  921. }
  922. #ifdef HAVE_SYSLOG_H
  923. /**
  924. * Add a log handler to send messages to they system log facility.
  925. */
  926. int
  927. add_syslog_log(const log_severity_list_t *severity)
  928. {
  929. logfile_t *lf;
  930. if (syslog_count++ == 0)
  931. /* This is the first syslog. */
  932. openlog("Tor", LOG_PID | LOG_NDELAY, LOGFACILITY);
  933. lf = tor_malloc_zero(sizeof(logfile_t));
  934. lf->fd = -1;
  935. lf->severities = tor_memdup(severity, sizeof(log_severity_list_t));
  936. lf->filename = tor_strdup("<syslog>");
  937. lf->is_syslog = 1;
  938. LOCK_LOGS();
  939. lf->next = logfiles;
  940. logfiles = lf;
  941. log_global_min_severity_ = get_min_log_level();
  942. UNLOCK_LOGS();
  943. return 0;
  944. }
  945. #endif
  946. /** If <b>level</b> is a valid log severity, return the corresponding
  947. * numeric value. Otherwise, return -1. */
  948. int
  949. parse_log_level(const char *level)
  950. {
  951. if (!strcasecmp(level, "err"))
  952. return LOG_ERR;
  953. if (!strcasecmp(level, "warn"))
  954. return LOG_WARN;
  955. if (!strcasecmp(level, "notice"))
  956. return LOG_NOTICE;
  957. if (!strcasecmp(level, "info"))
  958. return LOG_INFO;
  959. if (!strcasecmp(level, "debug"))
  960. return LOG_DEBUG;
  961. return -1;
  962. }
  963. /** Return the string equivalent of a given log level. */
  964. const char *
  965. log_level_to_string(int level)
  966. {
  967. return sev_to_string(level);
  968. }
  969. /** NULL-terminated array of names for log domains such that domain_list[dom]
  970. * is a description of <b>dom</b>. */
  971. static const char *domain_list[] = {
  972. "GENERAL", "CRYPTO", "NET", "CONFIG", "FS", "PROTOCOL", "MM",
  973. "HTTP", "APP", "CONTROL", "CIRC", "REND", "BUG", "DIR", "DIRSERV",
  974. "OR", "EDGE", "ACCT", "HIST", "HANDSHAKE", "HEARTBEAT", "CHANNEL", NULL
  975. };
  976. /** Return a bitmask for the log domain for which <b>domain</b> is the name,
  977. * or 0 if there is no such name. */
  978. static log_domain_mask_t
  979. parse_log_domain(const char *domain)
  980. {
  981. int i;
  982. for (i=0; domain_list[i]; ++i) {
  983. if (!strcasecmp(domain, domain_list[i]))
  984. return (1u<<i);
  985. }
  986. return 0;
  987. }
  988. /** Translate a bitmask of log domains to a string. */
  989. static char *
  990. domain_to_string(log_domain_mask_t domain, char *buf, size_t buflen)
  991. {
  992. char *cp = buf;
  993. char *eos = buf+buflen;
  994. buf[0] = '\0';
  995. if (! domain)
  996. return buf;
  997. while (1) {
  998. const char *d;
  999. int bit = tor_log2(domain);
  1000. size_t n;
  1001. if (bit >= N_LOGGING_DOMAINS) {
  1002. tor_snprintf(buf, buflen, "<BUG:Unknown domain %lx>", (long)domain);
  1003. return buf+strlen(buf);
  1004. }
  1005. d = domain_list[bit];
  1006. n = strlcpy(cp, d, eos-cp);
  1007. if (n >= buflen) {
  1008. tor_snprintf(buf, buflen, "<BUG:Truncating domain %lx>", (long)domain);
  1009. return buf+strlen(buf);
  1010. }
  1011. cp += n;
  1012. domain &= ~(1<<bit);
  1013. if (domain == 0 || (eos-cp) < 2)
  1014. return cp;
  1015. memcpy(cp, ",", 2); /*Nul-terminated ,"*/
  1016. cp++;
  1017. }
  1018. }
  1019. /** Parse a log severity pattern in *<b>cfg_ptr</b>. Advance cfg_ptr after
  1020. * the end of the severityPattern. Set the value of <b>severity_out</b> to
  1021. * the parsed pattern. Return 0 on success, -1 on failure.
  1022. *
  1023. * The syntax for a SeverityPattern is:
  1024. * <pre>
  1025. * SeverityPattern = *(DomainSeverity SP)* DomainSeverity
  1026. * DomainSeverity = (DomainList SP)? SeverityRange
  1027. * SeverityRange = MinSeverity ("-" MaxSeverity )?
  1028. * DomainList = "[" (SP? DomainSpec SP? ",") SP? DomainSpec "]"
  1029. * DomainSpec = "*" | Domain | "~" Domain
  1030. * </pre>
  1031. * A missing MaxSeverity defaults to ERR. Severities and domains are
  1032. * case-insensitive. "~" indicates negation for a domain; negation happens
  1033. * last inside a DomainList. Only one SeverityRange without a DomainList is
  1034. * allowed per line.
  1035. */
  1036. int
  1037. parse_log_severity_config(const char **cfg_ptr,
  1038. log_severity_list_t *severity_out)
  1039. {
  1040. const char *cfg = *cfg_ptr;
  1041. int got_anything = 0;
  1042. int got_an_unqualified_range = 0;
  1043. memset(severity_out, 0, sizeof(*severity_out));
  1044. cfg = eat_whitespace(cfg);
  1045. while (*cfg) {
  1046. const char *dash, *space;
  1047. char *sev_lo, *sev_hi;
  1048. int low, high, i;
  1049. log_domain_mask_t domains = ~0u;
  1050. if (*cfg == '[') {
  1051. int err = 0;
  1052. char *domains_str;
  1053. smartlist_t *domains_list;
  1054. log_domain_mask_t neg_domains = 0;
  1055. const char *closebracket = strchr(cfg, ']');
  1056. if (!closebracket)
  1057. return -1;
  1058. domains = 0;
  1059. domains_str = tor_strndup(cfg+1, closebracket-cfg-1);
  1060. domains_list = smartlist_new();
  1061. smartlist_split_string(domains_list, domains_str, ",", SPLIT_SKIP_SPACE,
  1062. -1);
  1063. tor_free(domains_str);
  1064. SMARTLIST_FOREACH_BEGIN(domains_list, const char *, domain) {
  1065. if (!strcmp(domain, "*")) {
  1066. domains = ~0u;
  1067. } else {
  1068. int d;
  1069. int negate=0;
  1070. if (*domain == '~') {
  1071. negate = 1;
  1072. ++domain;
  1073. }
  1074. d = parse_log_domain(domain);
  1075. if (!d) {
  1076. log_warn(LD_CONFIG, "No such logging domain as %s", domain);
  1077. err = 1;
  1078. } else {
  1079. if (negate)
  1080. neg_domains |= d;
  1081. else
  1082. domains |= d;
  1083. }
  1084. }
  1085. } SMARTLIST_FOREACH_END(domain);
  1086. SMARTLIST_FOREACH(domains_list, char *, d, tor_free(d));
  1087. smartlist_free(domains_list);
  1088. if (err)
  1089. return -1;
  1090. if (domains == 0 && neg_domains)
  1091. domains = ~neg_domains;
  1092. else
  1093. domains &= ~neg_domains;
  1094. cfg = eat_whitespace(closebracket+1);
  1095. } else {
  1096. ++got_an_unqualified_range;
  1097. }
  1098. if (!strcasecmpstart(cfg, "file") ||
  1099. !strcasecmpstart(cfg, "stderr") ||
  1100. !strcasecmpstart(cfg, "stdout") ||
  1101. !strcasecmpstart(cfg, "syslog")) {
  1102. goto done;
  1103. }
  1104. if (got_an_unqualified_range > 1)
  1105. return -1;
  1106. space = strchr(cfg, ' ');
  1107. dash = strchr(cfg, '-');
  1108. if (!space)
  1109. space = strchr(cfg, '\0');
  1110. if (dash && dash < space) {
  1111. sev_lo = tor_strndup(cfg, dash-cfg);
  1112. sev_hi = tor_strndup(dash+1, space-(dash+1));
  1113. } else {
  1114. sev_lo = tor_strndup(cfg, space-cfg);
  1115. sev_hi = tor_strdup("ERR");
  1116. }
  1117. low = parse_log_level(sev_lo);
  1118. high = parse_log_level(sev_hi);
  1119. tor_free(sev_lo);
  1120. tor_free(sev_hi);
  1121. if (low == -1)
  1122. return -1;
  1123. if (high == -1)
  1124. return -1;
  1125. got_anything = 1;
  1126. for (i=low; i >= high; --i)
  1127. severity_out->masks[SEVERITY_MASK_IDX(i)] |= domains;
  1128. cfg = eat_whitespace(space);
  1129. }
  1130. done:
  1131. *cfg_ptr = cfg;
  1132. return got_anything ? 0 : -1;
  1133. }
  1134. /** Return the least severe log level that any current log is interested in. */
  1135. int
  1136. get_min_log_level(void)
  1137. {
  1138. logfile_t *lf;
  1139. int i;
  1140. int min = LOG_ERR;
  1141. for (lf = logfiles; lf; lf = lf->next) {
  1142. for (i = LOG_DEBUG; i > min; --i)
  1143. if (lf->severities->masks[SEVERITY_MASK_IDX(i)])
  1144. min = i;
  1145. }
  1146. return min;
  1147. }
  1148. /** Switch all logs to output at most verbose level. */
  1149. void
  1150. switch_logs_debug(void)
  1151. {
  1152. logfile_t *lf;
  1153. int i;
  1154. LOCK_LOGS();
  1155. for (lf = logfiles; lf; lf=lf->next) {
  1156. for (i = LOG_DEBUG; i >= LOG_ERR; --i)
  1157. lf->severities->masks[SEVERITY_MASK_IDX(i)] = ~0u;
  1158. }
  1159. log_global_min_severity_ = get_min_log_level();
  1160. UNLOCK_LOGS();
  1161. }
  1162. #if 0
  1163. static void
  1164. dump_log_info(logfile_t *lf)
  1165. {
  1166. const char *tp;
  1167. if (lf->filename) {
  1168. printf("=== log into \"%s\" (%s-%s) (%stemporary)\n", lf->filename,
  1169. sev_to_string(lf->min_loglevel),
  1170. sev_to_string(lf->max_loglevel),
  1171. lf->is_temporary?"":"not ");
  1172. } else if (lf->is_syslog) {
  1173. printf("=== syslog (%s-%s) (%stemporary)\n",
  1174. sev_to_string(lf->min_loglevel),
  1175. sev_to_string(lf->max_loglevel),
  1176. lf->is_temporary?"":"not ");
  1177. } else {
  1178. printf("=== log (%s-%s) (%stemporary)\n",
  1179. sev_to_string(lf->min_loglevel),
  1180. sev_to_string(lf->max_loglevel),
  1181. lf->is_temporary?"":"not ");
  1182. }
  1183. }
  1184. void
  1185. describe_logs(void)
  1186. {
  1187. logfile_t *lf;
  1188. printf("==== BEGIN LOGS ====\n");
  1189. for (lf = logfiles; lf; lf = lf->next)
  1190. dump_log_info(lf);
  1191. printf("==== END LOGS ====\n");
  1192. }
  1193. #endif