summaryrefslogtreecommitdiffstats
path: root/usr.sbin/ppp/log.c
blob: 23d0f0559e1ca5778f9466f2b8f712fbb142222f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 *	            PPP logging facility
 *
 *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
 *
 *   Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation,
 * advertising materials, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * by the Internet Initiative Japan, Inc.  The name of the
 * IIJ may not be used to endorse or promote products derived
 * from this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * $Id: log.c,v 1.4 1996/05/11 20:48:30 phk Exp $
 *
 */
#include "defs.h"
#include <time.h>
#include <netdb.h>
#ifdef __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif
#ifdef NO_VSPRINTF
#include <stdio.h>
#endif

#include "hdlc.h"

#define	MAXLOG	70

#define USELOGFILE

#ifdef USELOGFILE
static FILE *logfile;
#endif
static char logbuff[2000];
static char *logptr;
static struct mbuf *logtop;
static struct mbuf *lognext;
static int  logcnt;
static int  mypid;

int loglevel = LOG_LINK_BIT | LOG_CARRIER_BIT | LOG_PHASE_BIT;

void
ListLog()
{
  struct mbuf *bp;

  for (bp = logtop; bp; bp = bp->next) {
    write(1, MBUF_CTOP(bp), bp->cnt);
    usleep(10);
  }
}

int
LogOpen()
{
#ifdef USELOGFILE
  logfile = fopen(LOGFILE, "a");
  if (logfile == NULL) {
    fprintf(stderr, "can't open %s.\r\n", LOGFILE);
    return(1);
  }
#endif
  fprintf(stderr, "Log level is %02x\r\n", loglevel);
  logptr = logbuff;
  logcnt = 0;
  logtop = lognext = NULL;
  return(0);
}

void
LogFlush()
{
  struct mbuf *bp;
  int cnt;

#ifdef USELOGFILE
  *logptr = 0;
  fprintf(logfile, "%s", logbuff);
  fflush(logfile);
#endif
  cnt = logptr - logbuff + 1;
  bp = mballoc(cnt, MB_LOG);
  bcopy(logbuff, MBUF_CTOP(bp), cnt);
  bp->cnt = cnt;
  if (lognext) {
    lognext->next = bp;
    lognext = bp;
    if (++logcnt > MAXLOG) {
      logcnt--;
      logtop = mbfree(logtop);
    }
  } else {
    lognext = logtop = bp;
  }
  logptr = logbuff;
}

void
DupLog()
{
  mypid = 0;
#ifdef USELOGFILE
  dup2(fileno(logfile), 2);
#endif
}

void
LogClose()
{
  LogFlush();
#ifdef USELOGFILE
  fclose(logfile);
#endif
}

#ifdef NO_VSPRINTF
void
vsprintf(buf, fmt, av)
char *buf;
char *fmt;
va_list av;
{
  FILE foo;

  foo._cnt = BUFSIZ;
  foo._base = foo._ptr = buf; /* may have to cast(unsigned char *) */
  foo._flag = _IOWRT+_IOSTRG;
  (void) _doprnt(fmt, (va_list)av, &foo);
  *foo._ptr = '\0'; /* plant terminating null character */
}
#endif

static void
vlogprintf(format, ap)
char *format;
va_list ap;
{
  vsnprintf(logptr, sizeof(logbuff)-(logptr-logbuff), format, ap);
  logptr += strlen(logptr);
  LogFlush();
}

void
#ifdef __STDC__
logprintf(char *format, ...)
#else
logprintf(va_alist)
va_dcl
#endif
{
  va_list ap;
#ifdef __STDC__
  va_start(ap, format);
#else
  char *format;

  va_start(ap);
  format = va_arg(ap, char *);
#endif
  vlogprintf(format, ap);
  va_end(ap);
}

void
LogDumpBp(level, header, bp)
int level;
char *header;
struct mbuf *bp;
{
  u_char *cp;
  int cnt, loc;

  if (!(loglevel & (1 << level)))
    return;
  LogTimeStamp();
  snprintf(logptr, sizeof(logbuff)-(logptr-logbuff), "%s\n", header);
  logptr += strlen(logptr);
  loc = 0;
  LogTimeStamp();
  while (bp) {
    cp = MBUF_CTOP(bp);
    cnt = bp->cnt;
    while (cnt-- > 0) {
      snprintf(logptr, sizeof(logbuff)-(logptr-logbuff), " %02x", *cp++);
      logptr += strlen(logptr);
      if (++loc == 16) {
	loc = 0;
	*logptr++ = '\n';
	if (logptr - logbuff > 1500)
	  LogFlush();
  	if (cnt) LogTimeStamp();
      }
    }
    bp = bp->next;
  }
  if (loc) *logptr++ = '\n';
  LogFlush();
}

void
LogDumpBuff(level, header, ptr, cnt)
int level;
char *header;
u_char *ptr;
int cnt;
{
  int loc;

  if (cnt < 1) return;
  if (!(loglevel & (1 << level)))
    return;
  LogTimeStamp();
  snprintf(logptr, sizeof(logbuff)-(logptr-logbuff), "%s\n", header);
  logptr += strlen(logptr);
  LogTimeStamp();
  loc = 0;
  while (cnt-- > 0) {
    snprintf(logptr, sizeof(logbuff)-(logptr-logbuff), " %02x", *ptr++);
    logptr += strlen(logptr);
    if (++loc == 16) {
      loc = 0;
      *logptr++ = '\n';
      if (cnt) LogTimeStamp();
    }
  }
  if (loc) *logptr++ = '\n';
  LogFlush();
}

void
LogTimeStamp()
{
  struct tm *ptm;
  time_t ltime;

  if (mypid == 0)
    mypid = getpid();
  ltime = time(0);
  ptm = localtime(&ltime);
  snprintf(logptr, sizeof(logbuff)-(logptr-logbuff),
    "%02d-%02d %02d:%02d:%02d [%d] ",
    ptm->tm_mon + 1, ptm->tm_mday,
	ptm->tm_hour, ptm->tm_min, ptm->tm_sec, mypid);
  logptr += strlen(logptr);
}

void
#ifdef __STDC__
LogPrintf(int level, char *format, ...)
#else
LogPrintf(va_alist)
va_dcl
#endif
{
  va_list ap;
#ifdef __STDC__
  va_start(ap, format);
#else
  int level;
  char *format;

  va_start(ap);
  int = va_arg(ap, int);
  format = va_arg(ap, char *);
#endif
  if (!(loglevel & level))
    return;
  LogTimeStamp();
  vlogprintf(format, ap);
  va_end(ap);
}
OpenPOWER on IntegriCloud