home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / contrib / lib / log.c < prev    next >
C/C++ Source or Header  |  1992-06-07  |  3KB  |  158 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/types.h>
  4. #include <sys/times.h>
  5. #include <sys/param.h>
  6. #include "config.h"
  7. #include "libcnews.h"
  8. #include "debug.h"
  9. #include "log.h"
  10.  
  11. #include <fcntl.h>    /* sys/file.h on some systems? */
  12.  
  13. /* imports */
  14. time_t time();
  15.  
  16. char *logstr;                /* remote host name for logs */
  17.  
  18. static time_t lastlogtime = 0;
  19. static struct tms last_tms;
  20.  
  21. void
  22. log3int(fmt, i, j, k)
  23. char *fmt;
  24. int i, j, k;
  25. {
  26.     /*
  27.      * magic length buffer , we check it.  Note that on some
  28.      * machines, syslog croaks on strings above a certain length.
  29.      * This length is implementation dependent.
  30.      */
  31.     char buf[256];
  32.     int fmtlen;
  33.     int len;
  34.  
  35.     if (logstr == NULL)
  36.         return;
  37.     len = strlen(logstr);
  38.     fmtlen = strlen(fmt) + 1 + 3 * 20 + 1; /* space + 3 ints + NUL */
  39.     if (fmtlen + len > sizeof buf) {
  40.         len = sizeof buf - fmtlen;
  41.         (void) strncpy(buf, logstr, len);
  42.     } else
  43.         (void) strcpy(buf, logstr);
  44.     buf[len++] = ' ';
  45.     buf[len] = '\0';
  46.     (void) sprintf(&buf[len], fmt, i, j, k);
  47.     log(buf);
  48. }
  49.  
  50. void
  51. logtimes(s)
  52. char *s;
  53. {
  54.     struct tms tmsbuf;
  55.     double dtu, dts;
  56. #define FMT "%.32s %.32s user %.2g system %.2g elapsed %ld"
  57.     char buf[64 + 2 * 32 + 20 + sizeof FMT];    /* 2 doubles, 1 int */
  58.     time_t now;
  59.  
  60.     if (s == NULL && lastlogtime != 0)
  61.         return;
  62.     if (times(&tmsbuf) < 0)
  63.         error("times() failed", "");
  64.     now = time(&now);
  65.     if (now < 0)
  66.         error("time() failed", "");
  67.     if (s != NULL && lastlogtime != 0) {
  68.         dtu = (tmsbuf.tms_utime - last_tms.tms_utime) / HZ;
  69.         dts = (tmsbuf.tms_stime - last_tms.tms_stime) / HZ;
  70.         sprintf(buf, FMT, logstr, s, dtu, dts,
  71.             (long) (now - lastlogtime));
  72.         log(buf);
  73.     }
  74.     ddprintf(dfp, "lastlogtime = %ld, now = %ld, last_tms = {%ld, %ld, %ld, %ld}, tmsbuf = {%ld, %ld, %ld, %ld}\n",
  75.          (long) lastlogtime,(long) now,
  76.          (long) last_tms.tms_utime, (long) last_tms.tms_stime,
  77.          (long) last_tms.tms_cutime, (long) last_tms.tms_cstime,
  78.          (long) tmsbuf.tms_utime, (long) tmsbuf.tms_stime,
  79.          (long) tmsbuf.tms_cutime, (long) tmsbuf.tms_cstime);
  80.     lastlogtime = now;
  81.     last_tms = tmsbuf;
  82. }
  83.  
  84. #ifdef LOGFILE    /* Use a file to log statistics */
  85. #include <sys/timeb.h>
  86.  
  87. FILE *logfp;
  88. char *logcookie;
  89.  
  90. /* ARGSUSED */
  91. void
  92. loginit(s)
  93. char *s;
  94. {
  95.     char pidstr[32];    /* large enough for an int in decimal */
  96.     char *logfile = LOGFILE;
  97.     int fd;
  98.  
  99.     if (logfile[0] != '/')
  100.         logfile = ctlfile(logfile);
  101. #ifdef O_APPEND
  102.     if ((fd = open(logfile, O_WRONLY|O_APPEND|O_CREAT, 0666)) < 0)
  103.         error("open(%s, O_WRONLY|O_APPEND|O_CREAT) failed", logfile);
  104.     if ((logfp = fdopen(fd, "a+")) == NULL)
  105.         error("fdopen(open(%s), \"a+\") failed", logfile);
  106. #else
  107.     logfp = efopen(logfile, "a+");
  108.     fd = 0;                    /* avoid lint noise */
  109. #endif
  110.     sprintf(pidstr, "[%d]: ", getpid());
  111.     /*
  112.      * In normal syslog, the "." would be the hostname.  That's
  113.      * redundant here, so we ignore it.  The . keeps the column
  114.      * count right for scripts that scan the logs.  We also don't
  115.      * bother to glue the string s before the pid, since we'll
  116.      * usually have one file per daemon type.
  117.      */
  118.     logcookie = str3save(" . ", "" /* s */, pidstr);
  119.     logtimes((char *) 0);
  120. }
  121.  
  122. void
  123. log(s)
  124. char *s;
  125. {
  126.     timestamp(logfp, (time_t *) NULL);
  127.     (void) fputs(logcookie, logfp);
  128.     (void) fputs(s, logfp);
  129.     (void) putc('\n', logfp);
  130. }
  131. #endif /* LOGFILE */
  132.  
  133.  
  134.  
  135. #ifdef LOGLEVEL    /* Use BSD syslog */
  136.  
  137. #include <syslog.h>
  138.  
  139. void
  140. loginit(s)
  141. char *s;
  142. {
  143. #ifdef LOG_DAEMON
  144.     openlog(s, LOG_PID, LOGLEVEL);
  145. #else
  146.     openlog(s, LOG_PID);
  147. #endif
  148.     logtimes((char *) 0);
  149. }
  150.  
  151. void
  152. log(s)
  153. char *s;
  154. {
  155.     syslog(LOG_INFO, "%s", s);
  156. }
  157. #endif /* LOGLEVEL */
  158.