Beispiel #1
0
void logString(int level, struct __sourceloc whence, const char *str)
{
  if (level != LOG_LEVEL_SILENT) {
    _log_iterator it;
    _log_iterator_start(&it);
    _rotate_log_file(&it);
    const char *s = str;
    const char *p;
    for (p = str; *p; ++p) {
      if (*p == '\n') {
	_log_iterator_rewind(&it);
	while (_log_iterator_next(&it, level)) {
	  _log_prefix_whence(&it, whence);
	  xprintf(it.xpf, "%.*s", (int)(p - s), s);
	}
	s = p + 1;
      }
    }
    if (p > s) {
      _log_iterator_rewind(&it);
      while (_log_iterator_next(&it, level)) {
	_log_prefix_whence(&it, whence);
	xprintf(it.xpf, "%.*s", (int)(p - s), s);
      }
    }
  }
}
Beispiel #2
0
void vlogMessage(int level, struct __sourceloc whence, const char *fmt, va_list ap)
{
  if (level != LOG_LEVEL_SILENT) {
    _log_iterator it;
    _log_iterator_start(&it);
    _rotate_log_file(&it);
    while (_log_iterator_next(&it, level)) {
      _log_prefix_whence(&it, whence);
      va_list ap1;
      va_copy(ap1, ap);
      vxprintf(it.xpf, fmt, ap1);
      va_end(ap1);
    }
  }
}
Beispiel #3
0
void
log_msg(log_level_t level, const char * const area, const char * const msg)
{
    if (level >= level_filter) {
        long result;
        dt = g_date_time_new_now(tz);

        gchar *date_fmt = g_date_time_format(dt, "%d/%m/%Y %H:%M:%S");
        fprintf(logp, "%s: %s: %s\n", date_fmt, area, msg);
        g_date_time_unref(dt);

        fflush(logp);
        g_free(date_fmt);

        result = ftell(logp);
        if (result != -1 && result >= prefs_get_max_log_size()) {
            _rotate_log_file();
        }
    }
}
Beispiel #4
0
void logArgv(int level, struct __sourceloc whence, const char *label, int argc, const char *const *argv)
{
  if (level != LOG_LEVEL_SILENT) {
    struct strbuf b;
    strbuf_init(&b, NULL, 0);
    strbuf_append_argv(&b, argc, argv);
    size_t len = strbuf_count(&b);
    strbuf_init(&b, alloca(len + 1), len + 1);
    strbuf_append_argv(&b, argc, argv);
    _log_iterator it;
    _log_iterator_start(&it);
    _rotate_log_file(&it);
    while (_log_iterator_next(&it, level)) {
      _log_prefix_whence(&it, whence);
      if (label) {
	xputs(label, it.xpf);
	xputc(' ', it.xpf);
      }
      xputs(strbuf_str(&b), it.xpf);
    }
  }
}
Beispiel #5
0
void
log_msg(log_level_t level, const char * const area, const char * const msg)
{
    if (level >= level_filter && logp != NULL) {
        dt = g_date_time_new_now(tz);

        char *level_str = _log_string_from_level(level);

        gchar *date_fmt = g_date_time_format(dt, "%d/%m/%Y %H:%M:%S");

        fprintf(logp, "%s: %s: %s: %s\n", date_fmt, area, level_str, msg);
        g_date_time_unref(dt);

        fflush(logp);
        g_free(date_fmt);

        if (prefs_get_boolean(PREF_LOG_ROTATE)) {
            long result = ftell(logp);
            if (result != -1 && result >= prefs_get_max_log_size()) {
                _rotate_log_file();
            }
        }
    }
}
Beispiel #6
0
int log_backtrace(int level, struct __sourceloc whence)
{
#ifndef NO_BACKTRACE
  _log_iterator it;
  _log_iterator_start(&it);
  _rotate_log_file(&it);
  char execpath[MAXPATHLEN];
  if (get_self_executable_path(execpath, sizeof execpath) == -1)
    return WHY("cannot log backtrace: own executable path unknown");
  char tempfile[MAXPATHLEN];
  if (!FORM_SERVAL_INSTANCE_PATH(tempfile, "servalgdb.XXXXXX"))
    return -1;
  int tmpfd = mkstemp(tempfile);
  if (tmpfd == -1)
    return WHYF_perror("mkstemp(%s)", alloca_str_toprint(tempfile));
  if (write_str(tmpfd, "backtrace\n") == -1) {
    close(tmpfd);
    unlink(tempfile);
    return -1;
  }
  if (close(tmpfd) == -1) {
    WHY_perror("close");
    unlink(tempfile);
    return -1;
  }
  char pidstr[12];
  snprintf(pidstr, sizeof pidstr, "%jd", (intmax_t)getpid());
  int stdout_fds[2];
  if (pipe(stdout_fds) == -1)
    return WHY_perror("pipe");
  pid_t child_pid;
  switch (child_pid = fork()) {
  case -1: // error
    WHY_perror("fork");
    close(stdout_fds[0]);
    close(stdout_fds[1]);
    return WHY("cannot log backtrace: fork failed");
  case 0: // child
    if (dup2(stdout_fds[1], 1) == -1 || dup2(stdout_fds[1], 2) == -1) {
      perror("dup2");
      _exit(-1);
    }
    close(0);
    if (open("/dev/null", O_RDONLY) != 0) {
      perror("open(\"/dev/null\")");
      _exit(-2);
    }
    close(stdout_fds[0]);
    /* XXX: Need the cast on Solaris because it defins NULL as 0L and gcc doesn't
     * see it as a sentinal */
    execlp("gdb", "gdb", "-n", "-batch", "-x", tempfile, execpath, pidstr, (void*)NULL);
    perror("execlp(\"gdb\")");
    do { _exit(-3); } while (1);
    break;
  }
  // parent
  close(stdout_fds[1]);
  _log_iterator_printf_nl(&it, level, whence, "GDB BACKTRACE");
  char buf[1024];
  char *const bufe = buf + sizeof buf;
  char *linep = buf;
  char *readp = buf;
  ssize_t nr;
  while ((nr = read(stdout_fds[0], readp, bufe - readp)) > 0) {
    char *p = readp;
    readp = readp + nr;
    for (; p < readp; ++p)
      if (*p == '\n' || *p == '\0') {
	*p = '\0';
	_log_iterator_printf_nl(&it, level, __NOWHERE__, "%s", linep);
	linep = p + 1;
      }
    if (readp >= bufe && linep == buf) {
      // Line does not fit into buffer.
      char t = bufe[-1];
      bufe[-1] = '\0';
      _log_iterator_printf_nl(&it, level, __NOWHERE__, "%s", buf);
      buf[0] = t;
      readp = buf + 1;
    } else if (readp + 120 >= bufe && linep != buf) {
      // Buffer low on space.
      if (linep < readp)
	memmove(buf, linep, readp - linep);
      readp -= linep - buf;
      linep = buf;
    }
    // Invariant: readp < bufe
  }
  if (nr == -1)
    WHY_perror("read");
  if (readp > linep) {
    *readp = '\0';
    _log_iterator_printf_nl(&it, level, __NOWHERE__, "%s", linep);
  }
  close(stdout_fds[0]);
  int status = 0;
  if (waitpid(child_pid, &status, 0) == -1)
    WHY_perror("waitpid");
  strbuf b = strbuf_local(buf, sizeof buf);
  strbuf_append_exit_status(b, status);
  _log_iterator_printf_nl(&it, level, __NOWHERE__, "gdb %s", buf);
  unlink(tempfile);
#endif
  return 0;
}