>> On 28 Jul 1999 11:22:56 -0400, 
>> [EMAIL PROTECTED] (John R. Levine) said:

J> What do you do about daily or weekly log summaries?  I still haven't
J> come up with a good way to do that with cyclog.

   I munged some of the cyclog code around to make it write to a file based
   on the current date.  We use this for a loghost that holds syslog output
   from several other Unix systems.  The listing is small, so it's enclosed
   below; it replaces cyclog.c.

-- 
Karl Vogel
ASC/YCOA, Wright-Patterson AFB, OH 45433, USA
[EMAIL PROTECTED]  or  [EMAIL PROTECTED]

-----------------------------------------------------------------------------
#include <sys/types.h>
#include <sys/time.h>
#include "direntry.h"
#include "substdio.h"
#include "subfd.h"
#include "exit.h"
#include "sgetopt.h"
#include "strerr.h"
#include "scan.h"
#include "fmt.h"
#include "now.h"

#define FATAL "daylog: fatal: "
#define WARNING "daylog: warning: "
void die_usage()
{
  strerr_die1x(100,"daylog: usage: daylog dir");
}

unsigned long size = 10240;
char fn[20 + FMT_ULONG];

int safewrite(fd,buf,len)
int fd;
char *buf;
int len;
{
  int w;
  for (;;) {
    w = write(fd,buf,len);
    if (w > 0) return w;
    strerr_warn4(WARNING,"unable to write to ",fn,", pausing: ",&strerr_sys);
    sleep(60);
  }
}

void trace(x1)  /* KEV */
char *x1;
{
  strerr_sysinit();
  if (x1) substdio_puts(subfderr,x1);
  substdio_puts(subfderr,"\n");
  substdio_flush(subfderr);
}

char outbuf[1024];
substdio ssout;

int flushread(fd,buf,len) int fd; char *buf; int len;
{
  substdio_flush(&ssout);
  return read(fd,buf,len);
}

char inbuf[1024];
substdio ssin = SUBSTDIO_FDBUF(flushread,0,inbuf,sizeof inbuf);

void main(argc,argv)
int argc;
char **argv;
{
  char *dir;
  char *fns;
  char ch;
  int fd;
  int flageof;
  int len;
  int opt;
  struct tm *t;
  unsigned long bytes;
  unsigned long lastnow;

  umask(022);

  while ((opt = getopt(argc,argv,"")) != opteof)
    switch(opt) {
      default:
        die_usage();
    }

  argv += optind;
  dir = *argv;
  if (!dir)
    die_usage();

  if (chdir(dir) == -1)
    strerr_die4sys(111,FATAL,"unable to chdir to ",dir,": ");

  flageof = 0;

  while (!flageof) {
    for (;;) {
      lastnow = now();
      t = localtime(&lastnow);
      fns = fn;
      len = fmt_ulong(fns,(unsigned long) (1900 + t->tm_year));
      fns += len;
      *fns++ = '-';
      len = fmt_uint0(fns,(unsigned int) (1 + t->tm_mon),2);
      fns += len;
      *fns++ = '-';
      len = fmt_uint0(fns,(unsigned int) t->tm_mday,2);
      fns += len;
      *fns = '\0';

      fd = open_append(fn);
      if (fd != -1) break;
      strerr_warn4(WARNING,"unable to create ",fn,", pausing: ",&strerr_sys);
      sleep(60);
    }

    substdio_fdbuf(&ssout,safewrite,fd,outbuf,sizeof outbuf);
    for (bytes = size;bytes > 0;--bytes) {
      if (substdio_get(&ssin,&ch,1) < 1) { flageof = 1; break; }
      substdio_BPUTC(&ssout,ch);
      if (ch == '\n') break;
    }
    substdio_flush(&ssout);

    while (fsync(fd) == -1) {
      strerr_warn4(WARNING,"unable to sync to ",fn,", pausing: ",&strerr_sys);
      sleep(60);
    }
    fchmod(fd,0644); /* if it fails, too bad */
    close(fd);
  }

  _exit(0);
}

Reply via email to