Beispiel #1
0
static void
pumpLogMessages (tr_sys_file_t logfile)
{
    const tr_log_message * l;
    tr_log_message * list = tr_logGetQueue ();

    for (l=list; l!=NULL; l=l->next)
        printMessage (logfile, l->level, l->name, l->message, l->file, l->line);

    if (logfile != TR_BAD_SYS_FILE)
        tr_sys_file_flush (logfile, NULL);

    tr_logFreeQueue (list);
}
Beispiel #2
0
void
tr_logAddMessage (const char * file,
                  int line,
                  tr_log_level level,
                  const char * name,
                  const char * fmt,
                  ...)
{
  const int err = errno; /* message logging shouldn't affect errno */
  char buf[1024];
  int buf_len;
  va_list ap;
  tr_lockLock (getMessageLock ());

  /* build the text message */
  *buf = '\0';
  va_start (ap, fmt);
  buf_len = evutil_vsnprintf (buf, sizeof (buf), fmt, ap);
  va_end (ap);

  if (buf_len < 0)
    return;

#ifdef _WIN32
  if ((size_t) buf_len < sizeof (buf) - 3)
    {
      buf[buf_len + 0] = '\r';
      buf[buf_len + 1] = '\n';
      buf[buf_len + 2] = '\0';
      OutputDebugStringA (buf);
      buf[buf_len + 0] = '\0';
    }
  else
    {
      OutputDebugStringA (buf);
    }
#endif

  if (*buf)
    {
      if (tr_logGetQueueEnabled ())
        {
          tr_log_message * newmsg;
          newmsg = tr_new0 (tr_log_message, 1);
          newmsg->level = level;
          newmsg->when = tr_time ();
          newmsg->message = tr_strdup (buf);
          newmsg->file = file;
          newmsg->line = line;
          newmsg->name = tr_strdup (name);

          *myQueueTail = newmsg;
          myQueueTail = &newmsg->next;
          ++myQueueLength;

          if (myQueueLength > TR_LOG_MAX_QUEUE_LENGTH)
            {
              tr_log_message * old = myQueue;
              myQueue = old->next;
              old->next = NULL;
              tr_logFreeQueue (old);
              --myQueueLength;
              assert (myQueueLength == TR_LOG_MAX_QUEUE_LENGTH);
            }
        }
      else
        {
          tr_sys_file_t fp;
          char timestr[64];

          fp = tr_logGetFile ();
          if (fp == TR_BAD_SYS_FILE)
            fp = tr_sys_file_get_std (TR_STD_SYS_FILE_ERR, NULL);

          tr_logGetTimeStr (timestr, sizeof (timestr));

          if (name)
            tr_sys_file_write_fmt (fp, "[%s] %s: %s" TR_NATIVE_EOL_STR, NULL, timestr, name, buf);
          else
            tr_sys_file_write_fmt (fp, "[%s] %s" TR_NATIVE_EOL_STR, NULL, timestr, buf);
          tr_sys_file_flush (fp, NULL);
        }
    }

  tr_lockUnlock (getMessageLock ());
  errno = err;
}