log.c++ 3.39 KB
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <getopt.h>
#ifdef __sgi__
#include <bstring.h>
#endif
#include <stdarg.h>
#include <netinet/in.h>
#include "log.h"
#include <PR/ultraerror.h>

//----------------------------------------------------------------------
// Log implementation
//----------------------------------------------------------------------
void Log::Open(char *name)
{
    int rv;
    
    logfile = fopen(name, "r");
    if (!logfile) {
        printf("couldn't open file %s\n", name);
        exit(1);
    }

    // read the header
    rv = fread(&log, sizeof(OSLogFileHdr), 1, logfile);
    if (rv != 1) {
        printf("couldn't read log header\n");
        exit(1);
    }

    if (ntohl(log.magic) != OS_LOG_MAGIC) {
        fprintf(stderr, "File %s is not a log file\n", name);
        exit(1);
    }

    return;
}

void Log::PrintLog()
{
    OSLogItem hdr;
    int i;
    char *fmt;
    
    long *buf = (long *)malloc(OS_LOG_MAX_ARGS*sizeof(long));

    int done = 0;
    while (!done) {
        int rv = fread(&hdr, sizeof(OSLogItem), 1, logfile);
        if (rv != 1) {
            exit(0);
        }
        
        printf("0x%8.8x (%4.4d): ", ntohl(hdr.timeStamp), ntohs(hdr.eventID));

        int found = 0;
        if ((ntohl(hdr.magic) != OS_LOG_MAGIC &&
	     ntohl(hdr.magic) != OS_ERROR_MAGIC) ||
            (ntohs(hdr.argCount) > OS_LOG_MAX_ARGS)) {
            printf("got bogus event, searching for next...\n");

            while(!found) {
                int offset = ftell(logfile);
                int a = getw(logfile);
                if (a == OS_LOG_MAGIC) {
                    fseek (logfile, offset, SEEK_SET);
                    found = 1;
                } else if (feof(logfile) || ferror(logfile)) {
                    printf("EOF or error, bye.\n");
                    exit(1);
                }
            }
        }

        if (found)
            continue;

        if (hdr.argCount) {
            
            fread(buf, sizeof(long), ntohs(hdr.argCount), logfile);
            for(int i = 0; i < ntohs(hdr.argCount); i++) {
		buf[i] = ntohl(buf[i]);
	    }
        }
        
        if ((fmt = fmtEntry[ntohs(hdr.eventID)]) != 0) {
            if (ntohs(hdr.argCount))
                vprintf(fmt, (va_list)buf);
            else
                printf(fmt);
        } else {
            printf("id %4d: ", ntohs(hdr.eventID));
            for (i = 0; i < ntohs(hdr.argCount); i++) {
                printf("0x%.8x ", buf[i]);
            }
        }
        printf("\n");
    }
}

void Log::ParseFormatFile(char *name)
{
    FILE *file;
    char buf[1024];
    int id;
    
    file = fopen(name, "r");
    if (!file) {
        fprintf(stderr, "couldn't open file %s\n", name);
        exit(1);
    }

    
#if 0
    fprintf(stderr, "Format Strings\n");
#endif

    int lineno = 0;
    while (fgets (buf, 1024, file) != NULL) {
        lineno++;
        sscanf(buf, "%d\n", &id);
        if (id) {
            fmtEntry[id] = getFmtStr(buf);
#if 0
            fprintf(stderr, "\tid %4d %s\n", id, fmtEntry[id]);
#endif
        }        
    }
}

char *Log::getFmtStr(char *buf)
{
    char *start = strchr(buf, '"');     // find first quote
    char *end   = strrchr(buf, '"');    // find last quote

    start++;
    
    char *dest = (char *)malloc(end-start+1);
    strncpy(dest, start, end-start);
    dest[end-start] = 0;

    return dest;
}