log.c 35 KB

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