sprintf + log

From: George (greerga@MIAVX1.ACS.MUOHIO.EDU)
Date: 07/13/97


Ever notice how many of the log() calls are preceded by sprintf's?

Here is a fix for that, instead of:

sprintf(buf, "This is a %s log of %d.", text, num);
log(buf);

You put:

log("This is a %s log of %d.", text, num);

And it completely compatible with all existing log() calls.  This should be
portable (based on libc5 sprintf) but I don't have access to anything
except Linux. (and VMS but not enough disk space to do anything there)

Can be used to replace log() in utils.c, in which case remove the conf. and
sysdep.h and add stdarg.h to sysdep.h or somewhere.  I put mine in a log.c.

---8<---8<---
#include <stdarg.h>
#include "conf.h"
#include "sysdep.h"

void log(const char *format, ...)
{
  va_list args;
  time_t ct = time(0);
  char *time_s = asctime(localtime(&ct));

  *(time_s + strlen(time_s) - 1) = '\0';

  fprintf(stderr, "%-19.19s :: ", time_s);

  va_start(args, format);
  vfprintf(stderr, format, args);
  va_end(args);

  fprintf(stderr, "\r\n");
}
---8<---8<---

-George

PS - Will also reduce the problem of overwriting a buffer in use with a log
        message you need to print.


      +-----------------------------------------------------------+
      | Ensure that you have read the CircleMUD Mailing List FAQ: |
      |   http://cspo.queensu.ca/~fletcher/Circle/list-faq.html   |
      +-----------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/08/00 PST