log.c 36 KB

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