/** If lf refers to an actual file that we have just opened, and the file * contains no data, log an "opening new logfile" message at the top. * * Return -1 if the log is broken and needs to be deleted, else return 0. */ static int log_tor_version(logfile_t *lf, int reset) { char buf[256]; size_t n; int is_new; if (!lf->needs_close) /* If it doesn't get closed, it isn't really a file. */ return 0; if (lf->is_temporary) /* If it's temporary, it isn't really a file. */ return 0; is_new = lf->fd >= 0 && tor_fd_getpos(lf->fd) == 0; if (reset && !is_new) /* We are resetting, but we aren't at the start of the file; no * need to log again. */ return 0; n = _log_prefix(buf, sizeof(buf), LOG_NOTICE); if (appname) { tor_snprintf(buf+n, sizeof(buf)-n, "%s opening %slog file.\n", appname, is_new?"new ":""); } else { tor_snprintf(buf+n, sizeof(buf)-n, "Tor %s opening %slog file.\n", VERSION, is_new?"new ":""); } if (write_all(lf->fd, buf, strlen(buf), 0) < 0) /* error */ return -1; /* failed */ return 0; }
/** Helper: Format a log message into a fixed-sized buffer. (This is * factored out of <b>logv</b> so that we never format a message more * than once.) Return a pointer to the first character of the message * portion of the formatted string. */ static INLINE char * format_msg(char *buf, size_t buf_len, log_domain_mask_t domain, int severity, const char *funcname, const char *format, va_list ap, size_t *msg_len_out) { size_t n; int r; char *end_of_prefix; assert(buf_len >= 2); /* prevent integer underflow */ buf_len -= 2; /* subtract 2 characters so we have room for \n\0 */ n = _log_prefix(buf, buf_len, severity); end_of_prefix = buf+n; if (funcname && should_log_function_name(domain, severity)) { r = tor_snprintf(buf+n, buf_len-n, "%s(): ", funcname); if (r<0) n = strlen(buf); else n += r; } if (domain == LD_BUG && buf_len-n > 6) { memcpy(buf+n, "Bug: ", 6); n += 5; } r = tor_vsnprintf(buf+n,buf_len-n,format,ap); if (r < 0) { /* The message was too long; overwrite the end of the buffer with * "[...truncated]" */ if (buf_len >= TRUNCATED_STR_LEN) { size_t offset = buf_len-TRUNCATED_STR_LEN; /* We have an extra 2 characters after buf_len to hold the \n\0, * so it's safe to add 1 to the size here. */ strlcpy(buf+offset, TRUNCATED_STR, buf_len-offset+1); } /* Set 'n' to the end of the buffer, where we'll be writing \n\0. * Since we already subtracted 2 from buf_len, this is safe.*/ n = buf_len; } else { n += r; } buf[n]='\n'; buf[n+1]='\0'; *msg_len_out = n+1; return end_of_prefix; }
static int _log_iterator_next(_log_iterator *it, int level) { assert(level >= LOG_LEVEL_SILENT); assert(level <= LOG_LEVEL_FATAL); _log_end_line(it, level); _log_flush(it); while (_log_iterator_advance(it)) { if (level >= it->config->level && _log_enabled(it)) { _log_update(it); _log_prefix(it, level); return 1; } } return 0; }
static void _log_vprintf_nl(_log_iterator *it, int level, const char *fmt, va_list ap) { _log_prefix(it, level); vxprintf(it->xpf, fmt, ap); _log_end_line(it, level); }