log.c revision 6059
1/*
2 *	            PPP logging facility
3 *
4 *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
5 *
6 *   Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the Internet Initiative Japan, Inc.  The name of the
14 * IIJ may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * $Id:$
21 *
22 *      TODO:
23 *
24 */
25#include "defs.h"
26#include <time.h>
27#include <netdb.h>
28
29#include "hdlc.h"
30
31#define	MAXLOG	70
32
33#define USELOGFILE
34
35#ifdef USELOGFILE
36static FILE *logfile;
37#endif
38static char logbuff[2000];
39static char *logptr;
40static struct mbuf *logtop;
41static struct mbuf *lognext;
42static int  logcnt;
43static int  mypid;
44
45int loglevel = (1 << LOG_LCP)| (1 << LOG_PHASE);
46
47void
48ListLog()
49{
50  struct mbuf *bp;
51
52  for (bp = logtop; bp; bp = bp->next) {
53    write(1, MBUF_CTOP(bp), bp->cnt);
54    usleep(10);
55  }
56}
57
58int
59LogOpen()
60{
61#ifdef USELOGFILE
62  logfile = fopen(LOGFILE, "a");
63  if (logfile == NULL) {
64    fprintf(stderr, "can't open %s.\r\n", LOGFILE);
65    return(1);
66  }
67#endif
68  fprintf(stderr, "Log level is %02x\r\n", loglevel);
69  logptr = logbuff;
70  logcnt = 0;
71  logtop = lognext = NULL;
72  return(0);
73}
74
75void
76LogFlush()
77{
78  struct mbuf *bp;
79  int cnt;
80
81#ifdef USELOGFILE
82  *logptr = 0;
83  fprintf(logfile, "%s", logbuff);
84  fflush(logfile);
85#endif
86  cnt = logptr - logbuff + 1;
87  bp = mballoc(cnt, MB_LOG);
88  bcopy(logbuff, MBUF_CTOP(bp), cnt);
89  bp->cnt = cnt;
90  if (lognext) {
91    lognext->next = bp;
92    lognext = bp;
93    if (++logcnt > MAXLOG) {
94      logcnt--;
95      logtop = mbfree(logtop);
96    }
97  } else {
98    lognext = logtop = bp;
99  }
100  logptr = logbuff;
101}
102
103void
104DupLog()
105{
106  mypid = 0;
107#ifdef USELOGFILE
108  dup2(fileno(logfile), 2);
109#endif
110}
111
112void
113LogClose()
114{
115  LogFlush();
116#ifdef USELOGFILE
117  fclose(logfile);
118#endif
119}
120
121void
122logprintf(format, arg1, arg2, arg3, arg4, arg5, arg6)
123char *format;
124void *arg1, *arg2, *arg3, *arg4, *arg5, *arg6;
125{
126  sprintf(logptr, format, arg1, arg2, arg3, arg4, arg5, arg6);
127  logptr += strlen(logptr);
128  LogFlush();
129}
130
131void
132LogDumpBp(level, header, bp)
133int level;
134char *header;
135struct mbuf *bp;
136{
137  u_char *cp;
138  int cnt, loc;
139
140  if (!(loglevel & (1 << level)))
141    return;
142  LogTimeStamp();
143  sprintf(logptr, "%s\n", header);
144  logptr += strlen(logptr);
145  loc = 0;
146  LogTimeStamp();
147  while (bp) {
148    cp = MBUF_CTOP(bp);
149    cnt = bp->cnt;
150    while (cnt-- > 0) {
151      sprintf(logptr, " %02x", *cp++);
152      logptr += strlen(logptr);
153      if (++loc == 16) {
154	loc = 0;
155	*logptr++ = '\n';
156	if (logptr - logbuff > 1500)
157	  LogFlush();
158  	if (cnt) LogTimeStamp();
159      }
160    }
161    bp = bp->next;
162  }
163  if (loc) *logptr++ = '\n';
164  LogFlush();
165}
166
167void
168LogDumpBuff(level, header, ptr, cnt)
169int level;
170char *header;
171u_char *ptr;
172int cnt;
173{
174  int loc;
175
176  if (cnt < 1) return;
177  if (!(loglevel & (1 << level)))
178    return;
179  LogTimeStamp();
180  sprintf(logptr, "%s\n", header);
181  logptr += strlen(logptr);
182  LogTimeStamp();
183  loc = 0;
184  while (cnt-- > 0) {
185    sprintf(logptr, " %02x", *ptr++);
186    logptr += strlen(logptr);
187    if (++loc == 16) {
188      loc = 0;
189      *logptr++ = '\n';
190      if (cnt) LogTimeStamp();
191    }
192  }
193  if (loc) *logptr++ = '\n';
194  LogFlush();
195}
196
197void
198LogTimeStamp()
199{
200  struct tm *ptm;
201  time_t ltime;
202
203  if (mypid == 0)
204    mypid = getpid();
205  ltime = time(0);
206  ptm = localtime(&ltime);
207  sprintf(logptr, "%02d-%02d %02d:%02d:%02d [%d] ",
208    ptm->tm_mon + 1, ptm->tm_mday,
209	ptm->tm_hour, ptm->tm_min, ptm->tm_sec, mypid);
210  logptr += strlen(logptr);
211}
212
213void
214LogPrintf(level, format, arg1, arg2, arg3, arg4, arg5, arg6)
215int level;
216char *format;
217void *arg1, *arg2, *arg3, *arg4, *arg5, *arg6;
218{
219  if (!(loglevel & (1 << level)))
220    return;
221  LogTimeStamp();
222  logprintf(format, arg1, arg2, arg3, arg4, arg5, arg6);
223}
224