Annotation of embedaddon/mpd/src/log.c, revision 1.1
1.1 ! misho 1:
! 2: /*
! 3: * log.c
! 4: *
! 5: * Written by Toshiharu OHNO <tony-o@iij.ad.jp>
! 6: * Copyright (c) 1993, Internet Initiative Japan, Inc. All rights reserved.
! 7: * See ``COPYRIGHT.iij''
! 8: *
! 9: * Rewritten by Archie Cobbs <archie@freebsd.org>
! 10: * Copyright (c) 1995-1999 Whistle Communications, Inc. All rights reserved.
! 11: * See ``COPYRIGHT.whistle''
! 12: */
! 13:
! 14: #include "ppp.h"
! 15: #ifdef SYSLOG_FACILITY
! 16: #include <syslog.h>
! 17: #endif
! 18:
! 19: /*
! 20: * DEFINITIONS
! 21: */
! 22:
! 23: #define DUMP_BYTES_PER_LINE 16
! 24: #define ROUNDUP(x,r) (((x)%(r))?((x)+((r)-((x)%(r)))):(x))
! 25: #define MAX_LOG_LINE 500
! 26:
! 27: /* Log option descriptor */
! 28:
! 29: struct logopt
! 30: {
! 31: int mask;
! 32: const char *name;
! 33: const char *desc;
! 34: };
! 35:
! 36: /*
! 37: * GLOBAL VARIABLES
! 38: */
! 39:
! 40: int gLogOptions = LG_DEFAULT_OPT | LG_ALWAYS;
! 41: #ifdef SYSLOG_FACILITY
! 42: char gSysLogIdent[32];
! 43: #endif
! 44:
! 45: /*
! 46: * INTERNAL VARIABLES
! 47: */
! 48:
! 49: #define ADD_OPT(x,d) { LG_ ##x, #x, d },
! 50:
! 51: static struct logopt LogOptionList[] =
! 52: {
! 53: #ifdef LG_BUND
! 54: ADD_OPT(BUND, "Bundle events")
! 55: #endif
! 56: #ifdef LG_BUND2
! 57: ADD_OPT(BUND2, "Detailed bundle events")
! 58: #endif
! 59: #ifdef LG_LINK
! 60: ADD_OPT(LINK, "Link events")
! 61: #endif
! 62: #ifdef LG_REP
! 63: ADD_OPT(REP, "Repeater events")
! 64: #endif
! 65: #ifdef LG_LCP
! 66: ADD_OPT(LCP, "LCP negotiation")
! 67: #endif
! 68: #ifdef LG_LCP2
! 69: ADD_OPT(LCP2, "LCP events and debugging")
! 70: #endif
! 71: #ifdef LG_AUTH
! 72: ADD_OPT(AUTH, "Link authentication events")
! 73: #endif
! 74: #ifdef LG_AUTH2
! 75: ADD_OPT(AUTH2, "Link authentication details")
! 76: #endif
! 77: #ifdef LG_IPCP
! 78: ADD_OPT(IPCP, "IPCP negotiation")
! 79: #endif
! 80: #ifdef LG_IPCP2
! 81: ADD_OPT(IPCP2, "IPCP events and debugging")
! 82: #endif
! 83: #ifdef LG_IPV6CP
! 84: ADD_OPT(IPV6CP, "IPV6CP negotiation")
! 85: #endif
! 86: #ifdef LG_IPV6CP2
! 87: ADD_OPT(IPV6CP2, "IPV6CP events and debugging")
! 88: #endif
! 89: #ifdef LG_CCP
! 90: ADD_OPT(CCP, "CCP negotiation")
! 91: #endif
! 92: #ifdef LG_CCP2
! 93: ADD_OPT(CCP2, "CCP events and debugging")
! 94: #endif
! 95: #ifdef LG_ECP
! 96: ADD_OPT(ECP, "ECP negotiation")
! 97: #endif
! 98: #ifdef LG_ECP2
! 99: ADD_OPT(ECP2, "ECP events and debugging")
! 100: #endif
! 101: #ifdef LG_FSM
! 102: ADD_OPT(FSM, "All FSM events (except echo & reset)")
! 103: #endif
! 104: #ifdef LG_ECHO
! 105: ADD_OPT(ECHO, "Echo/reply events for all automata")
! 106: #endif
! 107: #ifdef LG_PHYS
! 108: ADD_OPT(PHYS, "Physical layer events")
! 109: #endif
! 110: #ifdef LG_PHYS2
! 111: ADD_OPT(PHYS2, "Physical layer debug")
! 112: #endif
! 113: #ifdef LG_PHYS3
! 114: ADD_OPT(PHYS3, "Physical layer control packet dump")
! 115: #endif
! 116: #ifdef LG_CHAT
! 117: ADD_OPT(CHAT, "Modem chat script")
! 118: #endif
! 119: #ifdef LG_CHAT2
! 120: ADD_OPT(CHAT2, "Chat script extra debugging output")
! 121: #endif
! 122: #ifdef LG_IFACE
! 123: ADD_OPT(IFACE, "IP interface and route management")
! 124: #endif
! 125: #ifdef LG_IFACE2
! 126: ADD_OPT(IFACE2, "IP interface and route management debug")
! 127: #endif
! 128: #ifdef LG_FRAME
! 129: ADD_OPT(FRAME, "Dump all incoming & outgoing frames")
! 130: #endif
! 131: #ifdef LG_RADIUS
! 132: ADD_OPT(RADIUS, "Radius authentication events")
! 133: #endif
! 134: #ifdef LG_RADIUS2
! 135: ADD_OPT(RADIUS2, "Radius authentication debug")
! 136: #endif
! 137: #ifdef LG_CONSOLE
! 138: ADD_OPT(CONSOLE, "Console events")
! 139: #endif
! 140: #ifdef LG_EVENTS
! 141: ADD_OPT(EVENTS, "Daemon events debug")
! 142: #endif
! 143: };
! 144:
! 145: #define NUM_LOG_LEVELS (sizeof(LogOptionList) / sizeof(*LogOptionList))
! 146:
! 147: /*
! 148: * LogOpen()
! 149: */
! 150:
! 151: int
! 152: LogOpen(void)
! 153: {
! 154: #ifdef SYSLOG_FACILITY
! 155: if (!*gSysLogIdent)
! 156: strcpy(gSysLogIdent, "mpd");
! 157: openlog(gSysLogIdent, 0, LOG_DAEMON);
! 158: #endif
! 159: return(0);
! 160: }
! 161:
! 162: /*
! 163: * LogClose()
! 164: */
! 165:
! 166: void
! 167: LogClose(void)
! 168: {
! 169: #ifdef SYSLOG_FACILITY
! 170: closelog();
! 171: #endif
! 172: }
! 173:
! 174: /*
! 175: * LogCommand()
! 176: */
! 177:
! 178: int
! 179: LogCommand(Context ctx, int ac, char *av[], void *arg)
! 180: {
! 181: u_int k;
! 182: int bits, add;
! 183:
! 184: if (ac == 0) {
! 185: #define LG_FMT " %-12s %-10s %s\r\n"
! 186:
! 187: Printf(LG_FMT, "Log Option", "Enabled", "Description");
! 188: Printf(LG_FMT, "----------", "-------", "-----------");
! 189: for (k = 0; k < NUM_LOG_LEVELS; k++) {
! 190: Printf(" " LG_FMT, LogOptionList[k].name,
! 191: (gLogOptions & LogOptionList[k].mask) ? "Yes" : "No",
! 192: LogOptionList[k].desc);
! 193: }
! 194: return(0);
! 195: }
! 196:
! 197: while (ac--) {
! 198: switch (**av) {
! 199: case '+':
! 200: (*av)++;
! 201: default:
! 202: add = TRUE;
! 203: break;
! 204: case '-':
! 205: add = FALSE;
! 206: (*av)++;
! 207: break;
! 208: }
! 209: for (k = 0;
! 210: k < NUM_LOG_LEVELS && strcasecmp(*av, LogOptionList[k].name);
! 211: k++);
! 212: if (k < NUM_LOG_LEVELS)
! 213: bits = LogOptionList[k].mask;
! 214: else {
! 215: if (!strcasecmp(*av, "all")) {
! 216: for (bits = k = 0; k < NUM_LOG_LEVELS; k++)
! 217: bits |= LogOptionList[k].mask;
! 218: } else {
! 219: Printf("\"%s\" is unknown. Enter \"log\" for list.\r\n", *av);
! 220: bits = 0;
! 221: }
! 222: }
! 223: if (add)
! 224: gLogOptions |= bits;
! 225: else
! 226: gLogOptions &= ~bits;
! 227: av++;
! 228: }
! 229: return(0);
! 230: }
! 231:
! 232: /*
! 233: * LogPrintf()
! 234: *
! 235: * The way to print something to the log
! 236: */
! 237:
! 238: void
! 239: LogPrintf(const char *fmt, ...)
! 240: {
! 241: va_list args;
! 242:
! 243: va_start(args, fmt);
! 244: vLogPrintf(fmt, args);
! 245: va_end(args);
! 246: }
! 247:
! 248: void
! 249: vLogPrintf(const char *fmt, va_list args)
! 250: {
! 251: if (!SLIST_EMPTY(&gConsole.sessions)) {
! 252: char buf[256];
! 253: ConsoleSession s;
! 254:
! 255: vsnprintf(buf, sizeof(buf), fmt, args);
! 256: #ifdef SYSLOG_FACILITY
! 257: syslog(LOG_INFO, "%s", buf);
! 258: #endif
! 259: RWLOCK_RDLOCK(gConsole.lock);
! 260: SLIST_FOREACH(s, &gConsole.sessions, next) {
! 261: if (Enabled(&s->options, CONSOLE_LOGGING))
! 262: s->write(s, "%s\r\n", buf);
! 263: }
! 264: RWLOCK_UNLOCK(gConsole.lock);
! 265: #ifdef SYSLOG_FACILITY
! 266: } else {
! 267: vsyslog(LOG_INFO, fmt, args);
! 268: #endif
! 269: }
! 270: }
! 271:
! 272: /*
! 273: * LogPrintf2()
! 274: *
! 275: * The way to print something to the log
! 276: */
! 277:
! 278: void
! 279: LogPrintf2(const char *fmt, ...)
! 280: {
! 281: va_list args;
! 282:
! 283: va_start(args, fmt);
! 284: #ifdef SYSLOG_FACILITY
! 285: vsyslog(LOG_INFO, fmt, args);
! 286: #endif
! 287: va_end(args);
! 288: }
! 289:
! 290: /*
! 291: * LogDumpBp2()
! 292: *
! 293: * Dump the contents of an Mbuf to the log
! 294: */
! 295:
! 296: void
! 297: LogDumpBp2(Mbuf bp, const char *fmt, ...)
! 298: {
! 299: int k, total;
! 300: u_char bytes[DUMP_BYTES_PER_LINE];
! 301: char line[128];
! 302: int linelen;
! 303: va_list ap;
! 304:
! 305: /* Do header */
! 306: va_start(ap, fmt);
! 307: vLogPrintf(fmt, ap);
! 308: va_end(ap);
! 309:
! 310: /* Do data */
! 311: line[0]=' ';
! 312: line[1]=' ';
! 313: line[2]=' ';
! 314: line[3]=0;
! 315: linelen=3;
! 316:
! 317: total = 0;
! 318: if (bp) {
! 319: int start, stop, last = 0;
! 320:
! 321: stop = ROUNDUP(total + MBLEN(bp), DUMP_BYTES_PER_LINE);
! 322: for (start = total; total < stop; ) {
! 323: u_int const byte = (MBDATAU(bp))[total - start];
! 324:
! 325: if (total < start + MBLEN(bp)) {
! 326: sprintf(line+linelen, " %02x", byte);
! 327: last = total % DUMP_BYTES_PER_LINE;
! 328: } else
! 329: sprintf(line+linelen, " ");
! 330: linelen+=3;
! 331:
! 332: bytes[total % DUMP_BYTES_PER_LINE] = byte;
! 333: total++;
! 334:
! 335: if (total % DUMP_BYTES_PER_LINE == 0) {
! 336: snprintf(line+linelen, sizeof(line), " ");
! 337: linelen+=2;
! 338: for (k = 0; k <= last; k++) {
! 339: line[linelen++] = isgraph(bytes[k]) ? bytes[k] : '.';
! 340: line[linelen] = 0;
! 341: }
! 342: LogPrintf("%s",line);
! 343: line[0]=' ';
! 344: line[1]=' ';
! 345: line[2]=' ';
! 346: line[3]=0;
! 347: linelen=3;
! 348: }
! 349: }
! 350: }
! 351: }
! 352:
! 353: /*
! 354: * LogDumpBuf2()
! 355: *
! 356: * Dump the contents of a buffer to the log
! 357: */
! 358:
! 359: void
! 360: LogDumpBuf2(const u_char *buf, int count, const char *fmt, ...)
! 361: {
! 362: int k, stop, total;
! 363: char line[128];
! 364: int linelen;
! 365: va_list ap;
! 366:
! 367: /* Do header */
! 368: va_start(ap, fmt);
! 369: vLogPrintf(fmt, ap);
! 370: va_end(ap);
! 371:
! 372: /* Do data */
! 373: line[0]=' ';
! 374: line[1]=' ';
! 375: line[2]=' ';
! 376: line[3]=0;
! 377: linelen=3;
! 378:
! 379: stop = ROUNDUP(count, DUMP_BYTES_PER_LINE);
! 380: for (total = 0; total < stop; ) {
! 381: if (total < count)
! 382: sprintf(line+linelen, " %02x", buf[total]);
! 383: else
! 384: sprintf(line+linelen, " ");
! 385: linelen+=3;
! 386: total++;
! 387: if (total % DUMP_BYTES_PER_LINE == 0) {
! 388: snprintf(line+linelen, sizeof(line), " ");
! 389: linelen+=2;
! 390: for (k = total - DUMP_BYTES_PER_LINE; k < total && k < count; k++) {
! 391: line[linelen++] = isgraph(buf[k]) ? buf[k] : '.';
! 392: line[linelen] = 0;
! 393: }
! 394: LogPrintf("%s",line);
! 395: line[0]=' ';
! 396: line[1]=' ';
! 397: line[2]=' ';
! 398: line[3]=0;
! 399: linelen=3;
! 400: }
! 401: }
! 402: }
! 403:
! 404: /*
! 405: * Perror()
! 406: */
! 407:
! 408: void
! 409: Perror(const char *fmt, ...)
! 410: {
! 411: va_list args;
! 412: char buf[200];
! 413:
! 414: va_start(args, fmt);
! 415: vsnprintf(buf, sizeof(buf), fmt, args);
! 416: va_end(args);
! 417: snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
! 418: ": %s", strerror(errno));
! 419: Log(LG_ERR, ("%s", buf));
! 420: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>