log.c 36 KB

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