log.c++
3.39 KB
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
#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;
}