示例#1
0
static void
printMessage (FILE * logfile, int level, const char * name, const char * message, const char * file, int line)
{
    if (logfile != NULL)
    {
        char timestr[64];
        tr_logGetTimeStr (timestr, sizeof (timestr));
        if (name)
            fprintf (logfile, "[%s] %s %s (%s:%d)\n", timestr, name, message, file, line);
        else
            fprintf (logfile, "[%s] %s (%s:%d)\n", timestr, message, file, line);
    }
#ifdef HAVE_SYSLOG
    else /* daemon... write to syslog */
    {
        int priority;

        /* figure out the syslog priority */
        switch (level) {
            case TR_LOG_ERROR: priority = LOG_ERR; break;
            case TR_LOG_DEBUG: priority = LOG_DEBUG; break;
            default:           priority = LOG_INFO; break;
        }

        if (name)
            syslog (priority, "%s %s (%s:%d)", name, message, file, line);
        else
            syslog (priority, "%s (%s:%d)", message, file, line);
    }
#endif
}
示例#2
0
static void
printMessage (tr_sys_file_t logfile, int level, const char * name, const char * message, const char * file, int line)
{
    if (logfile != TR_BAD_SYS_FILE)
    {
        char timestr[64];
        tr_logGetTimeStr (timestr, sizeof (timestr));
        if (name)
            tr_sys_file_write_fmt (logfile, "[%s] %s %s (%s:%d)" TR_NATIVE_EOL_STR,
                                   NULL, timestr, name, message, file, line);
        else
            tr_sys_file_write_fmt (logfile, "[%s] %s (%s:%d)" TR_NATIVE_EOL_STR,
                                   NULL, timestr, message, file, line);
    }
#ifdef HAVE_SYSLOG
    else /* daemon... write to syslog */
    {
        int priority;

        /* figure out the syslog priority */
        switch (level) {
            case TR_LOG_ERROR: priority = LOG_ERR; break;
            case TR_LOG_DEBUG: priority = LOG_DEBUG; break;
            default:           priority = LOG_INFO; break;
        }

        if (name)
            syslog (priority, "%s %s (%s:%d)", name, message, file, line);
        else
            syslog (priority, "%s (%s:%d)", message, file, line);
    }
#else
    (void) level;
#endif
}
示例#3
0
void
tr_logAddDeep (const char  * file,
               int           line,
               const char  * name,
               const char  * fmt,
               ...)
{
  const tr_sys_file_t fp = tr_logGetFile ();
  if (fp != TR_BAD_SYS_FILE || IsDebuggerPresent ())
    {
      va_list args;
      char timestr[64];
      char * message;
      size_t message_len;
      struct evbuffer * buf = evbuffer_new ();
      char * base = tr_sys_path_basename (file, NULL);

      evbuffer_add_printf (buf, "[%s] ",
                           tr_logGetTimeStr (timestr, sizeof (timestr)));
      if (name)
        evbuffer_add_printf (buf, "%s ", name);
      va_start (args, fmt);
      evbuffer_add_vprintf (buf, fmt, args);
      va_end (args);
      evbuffer_add_printf (buf, " (%s:%d)" TR_NATIVE_EOL_STR, base, line);
      /* FIXME (libevent2) ifdef this out for nonwindows platforms */
      message = evbuffer_free_to_str (buf, &message_len);
      OutputDebugStringA (message);
      if (fp != TR_BAD_SYS_FILE)
        tr_sys_file_write (fp, message, message_len, NULL, NULL);

      tr_free (message);
      tr_free (base);
    }
}
示例#4
0
void
tr_logAddDeep (const char  * file,
               int           line,
               const char  * name,
               const char  * fmt,
               ...)
{
  FILE * fp = tr_logGetFile ();
  if (fp || IsDebuggerPresent ())
    {
      va_list args;
      char timestr[64];
      char * message;
      struct evbuffer * buf = evbuffer_new ();
      char * base = tr_basename (file);

      evbuffer_add_printf (buf, "[%s] ",
                           tr_logGetTimeStr (timestr, sizeof (timestr)));
      if (name)
        evbuffer_add_printf (buf, "%s ", name);
      va_start (args, fmt);
      evbuffer_add_vprintf (buf, fmt, args);
      va_end (args);
      evbuffer_add_printf (buf, " (%s:%d)\n", base, line);
      /* FIXME (libevent2) ifdef this out for nonwindows platforms */
      message = evbuffer_free_to_str (buf);
      OutputDebugStringA (message);
      if (fp)
        fputs (message, fp);

      tr_free (message);
      tr_free (base);
    }
}
示例#5
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;
}
示例#6
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];
  va_list ap;
  tr_lockLock (getMessageLock ());

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

  OutputDebugStringA (buf);

  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
        {
          FILE * fp;
          char timestr[64];

          fp = tr_logGetFile ();
          if (fp == NULL)
            fp = stderr;

          tr_logGetTimeStr (timestr, sizeof (timestr));

          if (name)
            fprintf (fp, "[%s] %s: %s\n", timestr, name, buf);
          else
            fprintf (fp, "[%s] %s\n", timestr, buf);
          fflush (fp);
        }
    }

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