log.c 36 KB

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