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 36 static FILE *logfile; 37 #endif 38 static char logbuff[2000]; 39 static char *logptr; 40 static struct mbuf *logtop; 41 static struct mbuf *lognext; 42 static int logcnt; 43 static int mypid; 44 45 int loglevel = (1 << LOG_LCP)| (1 << LOG_PHASE); 46 47 void 48 ListLog() 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 58 int 59 LogOpen() 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 75 void 76 LogFlush() 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 103 void 104 DupLog() 105 { 106 mypid = 0; 107 #ifdef USELOGFILE 108 dup2(fileno(logfile), 2); 109 #endif 110 } 111 112 void 113 LogClose() 114 { 115 LogFlush(); 116 #ifdef USELOGFILE 117 fclose(logfile); 118 #endif 119 } 120 121 void 122 logprintf(format, arg1, arg2, arg3, arg4, arg5, arg6) 123 char *format; 124 void *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 131 void 132 LogDumpBp(level, header, bp) 133 int level; 134 char *header; 135 struct 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 167 void 168 LogDumpBuff(level, header, ptr, cnt) 169 int level; 170 char *header; 171 u_char *ptr; 172 int 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 197 void 198 LogTimeStamp() 199 { 200 struct tm *ptm; 201 time_t ltime; 202 203 if (mypid == 0) 204 mypid = getpid(); 205 ltime = time(0); 206 ptm = localtime(<ime); 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 213 void 214 LogPrintf(level, format, arg1, arg2, arg3, arg4, arg5, arg6) 215 int level; 216 char *format; 217 void *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