log.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  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. /** Add to <b>out</b> a copy of every currently configured log file name. Used
  510. * to enable access to these filenames with the sandbox code. */
  511. void
  512. tor_log_get_logfile_names(smartlist_t *out)
  513. {
  514. logfile_t *lf;
  515. tor_assert(out);
  516. LOCK_LOGS();
  517. for (lf = logfiles; lf; lf = lf->next) {
  518. if (lf->is_temporary || lf->is_syslog || lf->callback)
  519. continue;
  520. if (lf->filename == NULL)
  521. continue;
  522. smartlist_add(out, tor_strdup(lf->filename));
  523. }
  524. UNLOCK_LOGS();
  525. }
  526. /** Output a message to the log, prefixed with a function name <b>fn</b>. */
  527. #ifdef __GNUC__
  528. /** GCC-based implementation of the log_fn backend, used when we have
  529. * variadic macros. All arguments are as for log_fn, except for
  530. * <b>fn</b>, which is the name of the calling functions. */
  531. void
  532. log_fn_(int severity, log_domain_mask_t domain, const char *fn,
  533. const char *format, ...)
  534. {
  535. va_list ap;
  536. if (severity > log_global_min_severity_)
  537. return;
  538. va_start(ap,format);
  539. logv(severity, domain, fn, NULL, format, ap);
  540. va_end(ap);
  541. }
  542. void
  543. log_fn_ratelim_(ratelim_t *ratelim, int severity, log_domain_mask_t domain,
  544. const char *fn, const char *format, ...)
  545. {
  546. va_list ap;
  547. char *m;
  548. if (severity > log_global_min_severity_)
  549. return;
  550. m = rate_limit_log(ratelim, approx_time());
  551. if (m == NULL)
  552. return;
  553. va_start(ap, format);
  554. logv(severity, domain, fn, m, format, ap);
  555. va_end(ap);
  556. tor_free(m);
  557. }
  558. #else
  559. /** @{ */
  560. /** Variant implementation of log_fn, log_debug, log_info,... for C compilers
  561. * without variadic macros. In this case, the calling function sets
  562. * log_fn_function_name_ to the name of the function, then invokes the
  563. * appropriate log_fn_, log_debug_, etc. */
  564. const char *log_fn_function_name_=NULL;
  565. void
  566. log_fn_(int severity, log_domain_mask_t domain, const char *format, ...)
  567. {
  568. va_list ap;
  569. if (severity > log_global_min_severity_)
  570. return;
  571. va_start(ap,format);
  572. logv(severity, domain, log_fn_function_name_, NULL, format, ap);
  573. va_end(ap);
  574. log_fn_function_name_ = NULL;
  575. }
  576. void
  577. log_fn_ratelim_(ratelim_t *ratelim, int severity, log_domain_mask_t domain,
  578. const char *format, ...)
  579. {
  580. va_list ap;
  581. char *m;
  582. if (severity > log_global_min_severity_)
  583. return;
  584. m = rate_limit_log(ratelim, approx_time());
  585. if (m == NULL)
  586. return;
  587. va_start(ap, format);
  588. logv(severity, domain, log_fn_function_name_, m, format, ap);
  589. va_end(ap);
  590. tor_free(m);
  591. }
  592. void
  593. log_debug_(log_domain_mask_t domain, const char *format, ...)
  594. {
  595. va_list ap;
  596. /* For GCC we do this check in the macro. */
  597. if (PREDICT_LIKELY(LOG_DEBUG > log_global_min_severity_))
  598. return;
  599. va_start(ap,format);
  600. logv(LOG_DEBUG, domain, log_fn_function_name_, NULL, format, ap);
  601. va_end(ap);
  602. log_fn_function_name_ = NULL;
  603. }
  604. void
  605. log_info_(log_domain_mask_t domain, const char *format, ...)
  606. {
  607. va_list ap;
  608. if (LOG_INFO > log_global_min_severity_)
  609. return;
  610. va_start(ap,format);
  611. logv(LOG_INFO, domain, log_fn_function_name_, NULL, format, ap);
  612. va_end(ap);
  613. log_fn_function_name_ = NULL;
  614. }
  615. void
  616. log_notice_(log_domain_mask_t domain, const char *format, ...)
  617. {
  618. va_list ap;
  619. if (LOG_NOTICE > log_global_min_severity_)
  620. return;
  621. va_start(ap,format);
  622. logv(LOG_NOTICE, domain, log_fn_function_name_, NULL, format, ap);
  623. va_end(ap);
  624. log_fn_function_name_ = NULL;
  625. }
  626. void
  627. log_warn_(log_domain_mask_t domain, const char *format, ...)
  628. {
  629. va_list ap;
  630. if (LOG_WARN > log_global_min_severity_)
  631. return;
  632. va_start(ap,format);
  633. logv(LOG_WARN, domain, log_fn_function_name_, NULL, format, ap);
  634. va_end(ap);
  635. log_fn_function_name_ = NULL;
  636. }
  637. void
  638. log_err_(log_domain_mask_t domain, const char *format, ...)
  639. {
  640. va_list ap;
  641. if (LOG_ERR > log_global_min_severity_)
  642. return;
  643. va_start(ap,format);
  644. logv(LOG_ERR, domain, log_fn_function_name_, NULL, format, ap);
  645. va_end(ap);
  646. log_fn_function_name_ = NULL;
  647. }
  648. /** @} */
  649. #endif
  650. /** Free all storage held by <b>victim</b>. */
  651. static void
  652. log_free(logfile_t *victim)
  653. {
  654. if (!victim)
  655. return;
  656. tor_free(victim->severities);
  657. tor_free(victim->filename);
  658. tor_free(victim);
  659. }
  660. /** Close all open log files, and free other static memory. */
  661. void
  662. logs_free_all(void)
  663. {
  664. logfile_t *victim, *next;
  665. smartlist_t *messages;
  666. LOCK_LOGS();
  667. next = logfiles;
  668. logfiles = NULL;
  669. messages = pending_cb_messages;
  670. pending_cb_messages = NULL;
  671. UNLOCK_LOGS();
  672. while (next) {
  673. victim = next;
  674. next = next->next;
  675. close_log(victim);
  676. log_free(victim);
  677. }
  678. tor_free(appname);
  679. SMARTLIST_FOREACH(messages, pending_cb_message_t *, msg, {
  680. tor_free(msg->msg);
  681. tor_free(msg);
  682. });
  683. smartlist_free(messages);
  684. /* We _could_ destroy the log mutex here, but that would screw up any logs
  685. * that happened between here and the end of execution. */
  686. }
  687. /** Remove and free the log entry <b>victim</b> from the linked-list
  688. * logfiles (it is probably present, but it might not be due to thread
  689. * racing issues). After this function is called, the caller shouldn't
  690. * refer to <b>victim</b> anymore.
  691. *
  692. * Long-term, we need to do something about races in the log subsystem
  693. * in general. See bug 222 for more details.
  694. */
  695. static void
  696. delete_log(logfile_t *victim)
  697. {
  698. logfile_t *tmpl;
  699. if (victim == logfiles)
  700. logfiles = victim->next;
  701. else {
  702. for (tmpl = logfiles; tmpl && tmpl->next != victim; tmpl=tmpl->next) ;
  703. // tor_assert(tmpl);
  704. // tor_assert(tmpl->next == victim);
  705. if (!tmpl)
  706. return;
  707. tmpl->next = victim->next;
  708. }
  709. log_free(victim);
  710. }
  711. /** Helper: release system resources (but not memory) held by a single
  712. * logfile_t. */
  713. static void
  714. close_log(logfile_t *victim)
  715. {
  716. if (victim->needs_close && victim->fd >= 0) {
  717. close(victim->fd);
  718. victim->fd = -1;
  719. } else if (victim->is_syslog) {
  720. #ifdef HAVE_SYSLOG_H
  721. if (--syslog_count == 0) {
  722. /* There are no other syslogs; close the logging facility. */
  723. closelog();
  724. }
  725. #endif
  726. }
  727. }
  728. /** Adjust a log severity configuration in <b>severity_out</b> to contain
  729. * every domain between <b>loglevelMin</b> and <b>loglevelMax</b>, inclusive.
  730. */
  731. void
  732. set_log_severity_config(int loglevelMin, int loglevelMax,
  733. log_severity_list_t *severity_out)
  734. {
  735. int i;
  736. tor_assert(loglevelMin >= loglevelMax);
  737. tor_assert(loglevelMin >= LOG_ERR && loglevelMin <= LOG_DEBUG);
  738. tor_assert(loglevelMax >= LOG_ERR && loglevelMax <= LOG_DEBUG);
  739. memset(severity_out, 0, sizeof(log_severity_list_t));
  740. for (i = loglevelMin; i >= loglevelMax; --i) {
  741. severity_out->masks[SEVERITY_MASK_IDX(i)] = ~0u;
  742. }
  743. }
  744. /** Add a log handler named <b>name</b> to send all messages in <b>severity</b>
  745. * to <b>fd</b>. Copies <b>severity</b>. Helper: does no locking. */
  746. static void
  747. add_stream_log_impl(const log_severity_list_t *severity,
  748. const char *name, int fd)
  749. {
  750. logfile_t *lf;
  751. lf = tor_malloc_zero(sizeof(logfile_t));
  752. lf->fd = fd;
  753. lf->filename = tor_strdup(name);
  754. lf->severities = tor_memdup(severity, sizeof(log_severity_list_t));
  755. lf->next = logfiles;
  756. logfiles = lf;
  757. log_global_min_severity_ = get_min_log_level();
  758. }
  759. /** Add a log handler named <b>name</b> to send all messages in <b>severity</b>
  760. * to <b>fd</b>. Steals a reference to <b>severity</b>; the caller must
  761. * not use it after calling this function. */
  762. void
  763. add_stream_log(const log_severity_list_t *severity, const char *name, int fd)
  764. {
  765. LOCK_LOGS();
  766. add_stream_log_impl(severity, name, fd);
  767. UNLOCK_LOGS();
  768. }
  769. /** Initialize the global logging facility */
  770. void
  771. init_logging(void)
  772. {
  773. if (!log_mutex_initialized) {
  774. tor_mutex_init(&log_mutex);
  775. log_mutex_initialized = 1;
  776. }
  777. if (pending_cb_messages == NULL)
  778. pending_cb_messages = smartlist_new();
  779. }
  780. /** Set whether we report logging domains as a part of our log messages.
  781. */
  782. void
  783. logs_set_domain_logging(int enabled)
  784. {
  785. LOCK_LOGS();
  786. log_domains_are_logged = enabled;
  787. UNLOCK_LOGS();
  788. }
  789. /** Add a log handler to receive messages during startup (before the real
  790. * logs are initialized).
  791. */
  792. void
  793. add_temp_log(int min_severity)
  794. {
  795. log_severity_list_t *s = tor_malloc_zero(sizeof(log_severity_list_t));
  796. set_log_severity_config(min_severity, LOG_ERR, s);
  797. LOCK_LOGS();
  798. add_stream_log_impl(s, "<temp>", fileno(stdout));
  799. tor_free(s);
  800. logfiles->is_temporary = 1;
  801. UNLOCK_LOGS();
  802. }
  803. /**
  804. * Add a log handler to send messages in <b>severity</b>
  805. * to the function <b>cb</b>.
  806. */
  807. int
  808. add_callback_log(const log_severity_list_t *severity, log_callback cb)
  809. {
  810. logfile_t *lf;
  811. lf = tor_malloc_zero(sizeof(logfile_t));
  812. lf->fd = -1;
  813. lf->severities = tor_memdup(severity, sizeof(log_severity_list_t));
  814. lf->filename = tor_strdup("<callback>");
  815. lf->callback = cb;
  816. lf->next = logfiles;
  817. LOCK_LOGS();
  818. logfiles = lf;
  819. log_global_min_severity_ = get_min_log_level();
  820. UNLOCK_LOGS();
  821. return 0;
  822. }
  823. /** Adjust the configured severity of any logs whose callback function is
  824. * <b>cb</b>. */
  825. void
  826. change_callback_log_severity(int loglevelMin, int loglevelMax,
  827. log_callback cb)
  828. {
  829. logfile_t *lf;
  830. log_severity_list_t severities;
  831. set_log_severity_config(loglevelMin, loglevelMax, &severities);
  832. LOCK_LOGS();
  833. for (lf = logfiles; lf; lf = lf->next) {
  834. if (lf->callback == cb) {
  835. memcpy(lf->severities, &severities, sizeof(severities));
  836. }
  837. }
  838. log_global_min_severity_ = get_min_log_level();
  839. UNLOCK_LOGS();
  840. }
  841. /** If there are any log messages that were generated with LD_NOCB waiting to
  842. * be sent to callback-based loggers, send them now. */
  843. void
  844. flush_pending_log_callbacks(void)
  845. {
  846. logfile_t *lf;
  847. smartlist_t *messages, *messages_tmp;
  848. LOCK_LOGS();
  849. if (0 == smartlist_len(pending_cb_messages)) {
  850. UNLOCK_LOGS();
  851. return;
  852. }
  853. messages = pending_cb_messages;
  854. pending_cb_messages = smartlist_new();
  855. do {
  856. SMARTLIST_FOREACH_BEGIN(messages, pending_cb_message_t *, msg) {
  857. const int severity = msg->severity;
  858. const int domain = msg->domain;
  859. for (lf = logfiles; lf; lf = lf->next) {
  860. if (! lf->callback || lf->seems_dead ||
  861. ! (lf->severities->masks[SEVERITY_MASK_IDX(severity)] & domain)) {
  862. continue;
  863. }
  864. lf->callback(severity, domain, msg->msg);
  865. }
  866. tor_free(msg->msg);
  867. tor_free(msg);
  868. } SMARTLIST_FOREACH_END(msg);
  869. smartlist_clear(messages);
  870. messages_tmp = pending_cb_messages;
  871. pending_cb_messages = messages;
  872. messages = messages_tmp;
  873. } while (smartlist_len(messages));
  874. smartlist_free(messages);
  875. UNLOCK_LOGS();
  876. }
  877. /** Close any log handlers added by add_temp_log() or marked by
  878. * mark_logs_temp(). */
  879. void
  880. close_temp_logs(void)
  881. {
  882. logfile_t *lf, **p;
  883. LOCK_LOGS();
  884. for (p = &logfiles; *p; ) {
  885. if ((*p)->is_temporary) {
  886. lf = *p;
  887. /* we use *p here to handle the edge case of the head of the list */
  888. *p = (*p)->next;
  889. close_log(lf);
  890. log_free(lf);
  891. } else {
  892. p = &((*p)->next);
  893. }
  894. }
  895. log_global_min_severity_ = get_min_log_level();
  896. UNLOCK_LOGS();
  897. }
  898. /** Make all currently temporary logs (set to be closed by close_temp_logs)
  899. * live again, and close all non-temporary logs. */
  900. void
  901. rollback_log_changes(void)
  902. {
  903. logfile_t *lf;
  904. LOCK_LOGS();
  905. for (lf = logfiles; lf; lf = lf->next)
  906. lf->is_temporary = ! lf->is_temporary;
  907. UNLOCK_LOGS();
  908. close_temp_logs();
  909. }
  910. /** Configure all log handles to be closed by close_temp_logs(). */
  911. void
  912. mark_logs_temp(void)
  913. {
  914. logfile_t *lf;
  915. LOCK_LOGS();
  916. for (lf = logfiles; lf; lf = lf->next)
  917. lf->is_temporary = 1;
  918. UNLOCK_LOGS();
  919. }
  920. /**
  921. * Add a log handler to send messages to <b>filename</b>. If opening the
  922. * logfile fails, -1 is returned and errno is set appropriately (by open(2)).
  923. */
  924. int
  925. add_file_log(const log_severity_list_t *severity, const char *filename)
  926. {
  927. int fd;
  928. logfile_t *lf;
  929. fd = tor_open_cloexec(filename, O_WRONLY|O_CREAT|O_APPEND, 0644);
  930. if (fd<0)
  931. return -1;
  932. if (tor_fd_seekend(fd)<0) {
  933. close(fd);
  934. return -1;
  935. }
  936. LOCK_LOGS();
  937. add_stream_log_impl(severity, filename, fd);
  938. logfiles->needs_close = 1;
  939. lf = logfiles;
  940. log_global_min_severity_ = get_min_log_level();
  941. if (log_tor_version(lf, 0) < 0) {
  942. delete_log(lf);
  943. }
  944. UNLOCK_LOGS();
  945. return 0;
  946. }
  947. #ifdef HAVE_SYSLOG_H
  948. /**
  949. * Add a log handler to send messages to they system log facility.
  950. */
  951. int
  952. add_syslog_log(const log_severity_list_t *severity)
  953. {
  954. logfile_t *lf;
  955. if (syslog_count++ == 0)
  956. /* This is the first syslog. */
  957. openlog("Tor", LOG_PID | LOG_NDELAY, LOGFACILITY);
  958. lf = tor_malloc_zero(sizeof(logfile_t));
  959. lf->fd = -1;
  960. lf->severities = tor_memdup(severity, sizeof(log_severity_list_t));
  961. lf->filename = tor_strdup("<syslog>");
  962. lf->is_syslog = 1;
  963. LOCK_LOGS();
  964. lf->next = logfiles;
  965. logfiles = lf;
  966. log_global_min_severity_ = get_min_log_level();
  967. UNLOCK_LOGS();
  968. return 0;
  969. }
  970. #endif
  971. /** If <b>level</b> is a valid log severity, return the corresponding
  972. * numeric value. Otherwise, return -1. */
  973. int
  974. parse_log_level(const char *level)
  975. {
  976. if (!strcasecmp(level, "err"))
  977. return LOG_ERR;
  978. if (!strcasecmp(level, "warn"))
  979. return LOG_WARN;
  980. if (!strcasecmp(level, "notice"))
  981. return LOG_NOTICE;
  982. if (!strcasecmp(level, "info"))
  983. return LOG_INFO;
  984. if (!strcasecmp(level, "debug"))
  985. return LOG_DEBUG;
  986. return -1;
  987. }
  988. /** Return the string equivalent of a given log level. */
  989. const char *
  990. log_level_to_string(int level)
  991. {
  992. return sev_to_string(level);
  993. }
  994. /** NULL-terminated array of names for log domains such that domain_list[dom]
  995. * is a description of <b>dom</b>. */
  996. static const char *domain_list[] = {
  997. "GENERAL", "CRYPTO", "NET", "CONFIG", "FS", "PROTOCOL", "MM",
  998. "HTTP", "APP", "CONTROL", "CIRC", "REND", "BUG", "DIR", "DIRSERV",
  999. "OR", "EDGE", "ACCT", "HIST", "HANDSHAKE", "HEARTBEAT", "CHANNEL", NULL
  1000. };
  1001. /** Return a bitmask for the log domain for which <b>domain</b> is the name,
  1002. * or 0 if there is no such name. */
  1003. static log_domain_mask_t
  1004. parse_log_domain(const char *domain)
  1005. {
  1006. int i;
  1007. for (i=0; domain_list[i]; ++i) {
  1008. if (!strcasecmp(domain, domain_list[i]))
  1009. return (1u<<i);
  1010. }
  1011. return 0;
  1012. }
  1013. /** Translate a bitmask of log domains to a string. */
  1014. static char *
  1015. domain_to_string(log_domain_mask_t domain, char *buf, size_t buflen)
  1016. {
  1017. char *cp = buf;
  1018. char *eos = buf+buflen;
  1019. buf[0] = '\0';
  1020. if (! domain)
  1021. return buf;
  1022. while (1) {
  1023. const char *d;
  1024. int bit = tor_log2(domain);
  1025. size_t n;
  1026. if (bit >= N_LOGGING_DOMAINS) {
  1027. tor_snprintf(buf, buflen, "<BUG:Unknown domain %lx>", (long)domain);
  1028. return buf+strlen(buf);
  1029. }
  1030. d = domain_list[bit];
  1031. n = strlcpy(cp, d, eos-cp);
  1032. if (n >= buflen) {
  1033. tor_snprintf(buf, buflen, "<BUG:Truncating domain %lx>", (long)domain);
  1034. return buf+strlen(buf);
  1035. }
  1036. cp += n;
  1037. domain &= ~(1<<bit);
  1038. if (domain == 0 || (eos-cp) < 2)
  1039. return cp;
  1040. memcpy(cp, ",", 2); /*Nul-terminated ,"*/
  1041. cp++;
  1042. }
  1043. }
  1044. /** Parse a log severity pattern in *<b>cfg_ptr</b>. Advance cfg_ptr after
  1045. * the end of the severityPattern. Set the value of <b>severity_out</b> to
  1046. * the parsed pattern. Return 0 on success, -1 on failure.
  1047. *
  1048. * The syntax for a SeverityPattern is:
  1049. * <pre>
  1050. * SeverityPattern = *(DomainSeverity SP)* DomainSeverity
  1051. * DomainSeverity = (DomainList SP)? SeverityRange
  1052. * SeverityRange = MinSeverity ("-" MaxSeverity )?
  1053. * DomainList = "[" (SP? DomainSpec SP? ",") SP? DomainSpec "]"
  1054. * DomainSpec = "*" | Domain | "~" Domain
  1055. * </pre>
  1056. * A missing MaxSeverity defaults to ERR. Severities and domains are
  1057. * case-insensitive. "~" indicates negation for a domain; negation happens
  1058. * last inside a DomainList. Only one SeverityRange without a DomainList is
  1059. * allowed per line.
  1060. */
  1061. int
  1062. parse_log_severity_config(const char **cfg_ptr,
  1063. log_severity_list_t *severity_out)
  1064. {
  1065. const char *cfg = *cfg_ptr;
  1066. int got_anything = 0;
  1067. int got_an_unqualified_range = 0;
  1068. memset(severity_out, 0, sizeof(*severity_out));
  1069. cfg = eat_whitespace(cfg);
  1070. while (*cfg) {
  1071. const char *dash, *space;
  1072. char *sev_lo, *sev_hi;
  1073. int low, high, i;
  1074. log_domain_mask_t domains = ~0u;
  1075. if (*cfg == '[') {
  1076. int err = 0;
  1077. char *domains_str;
  1078. smartlist_t *domains_list;
  1079. log_domain_mask_t neg_domains = 0;
  1080. const char *closebracket = strchr(cfg, ']');
  1081. if (!closebracket)
  1082. return -1;
  1083. domains = 0;
  1084. domains_str = tor_strndup(cfg+1, closebracket-cfg-1);
  1085. domains_list = smartlist_new();
  1086. smartlist_split_string(domains_list, domains_str, ",", SPLIT_SKIP_SPACE,
  1087. -1);
  1088. tor_free(domains_str);
  1089. SMARTLIST_FOREACH_BEGIN(domains_list, const char *, domain) {
  1090. if (!strcmp(domain, "*")) {
  1091. domains = ~0u;
  1092. } else {
  1093. int d;
  1094. int negate=0;
  1095. if (*domain == '~') {
  1096. negate = 1;
  1097. ++domain;
  1098. }
  1099. d = parse_log_domain(domain);
  1100. if (!d) {
  1101. log_warn(LD_CONFIG, "No such logging domain as %s", domain);
  1102. err = 1;
  1103. } else {
  1104. if (negate)
  1105. neg_domains |= d;
  1106. else
  1107. domains |= d;
  1108. }
  1109. }
  1110. } SMARTLIST_FOREACH_END(domain);
  1111. SMARTLIST_FOREACH(domains_list, char *, d, tor_free(d));
  1112. smartlist_free(domains_list);
  1113. if (err)
  1114. return -1;
  1115. if (domains == 0 && neg_domains)
  1116. domains = ~neg_domains;
  1117. else
  1118. domains &= ~neg_domains;
  1119. cfg = eat_whitespace(closebracket+1);
  1120. } else {
  1121. ++got_an_unqualified_range;
  1122. }
  1123. if (!strcasecmpstart(cfg, "file") ||
  1124. !strcasecmpstart(cfg, "stderr") ||
  1125. !strcasecmpstart(cfg, "stdout") ||
  1126. !strcasecmpstart(cfg, "syslog")) {
  1127. goto done;
  1128. }
  1129. if (got_an_unqualified_range > 1)
  1130. return -1;
  1131. space = strchr(cfg, ' ');
  1132. dash = strchr(cfg, '-');
  1133. if (!space)
  1134. space = strchr(cfg, '\0');
  1135. if (dash && dash < space) {
  1136. sev_lo = tor_strndup(cfg, dash-cfg);
  1137. sev_hi = tor_strndup(dash+1, space-(dash+1));
  1138. } else {
  1139. sev_lo = tor_strndup(cfg, space-cfg);
  1140. sev_hi = tor_strdup("ERR");
  1141. }
  1142. low = parse_log_level(sev_lo);
  1143. high = parse_log_level(sev_hi);
  1144. tor_free(sev_lo);
  1145. tor_free(sev_hi);
  1146. if (low == -1)
  1147. return -1;
  1148. if (high == -1)
  1149. return -1;
  1150. got_anything = 1;
  1151. for (i=low; i >= high; --i)
  1152. severity_out->masks[SEVERITY_MASK_IDX(i)] |= domains;
  1153. cfg = eat_whitespace(space);
  1154. }
  1155. done:
  1156. *cfg_ptr = cfg;
  1157. return got_anything ? 0 : -1;
  1158. }
  1159. /** Return the least severe log level that any current log is interested in. */
  1160. int
  1161. get_min_log_level(void)
  1162. {
  1163. logfile_t *lf;
  1164. int i;
  1165. int min = LOG_ERR;
  1166. for (lf = logfiles; lf; lf = lf->next) {
  1167. for (i = LOG_DEBUG; i > min; --i)
  1168. if (lf->severities->masks[SEVERITY_MASK_IDX(i)])
  1169. min = i;
  1170. }
  1171. return min;
  1172. }
  1173. /** Switch all logs to output at most verbose level. */
  1174. void
  1175. switch_logs_debug(void)
  1176. {
  1177. logfile_t *lf;
  1178. int i;
  1179. LOCK_LOGS();
  1180. for (lf = logfiles; lf; lf=lf->next) {
  1181. for (i = LOG_DEBUG; i >= LOG_ERR; --i)
  1182. lf->severities->masks[SEVERITY_MASK_IDX(i)] = ~0u;
  1183. }
  1184. log_global_min_severity_ = get_min_log_level();
  1185. UNLOCK_LOGS();
  1186. }