-
Notifications
You must be signed in to change notification settings - Fork 2
/
MKLogfileReader.m
213 lines (176 loc) · 6.68 KB
/
MKLogfileReader.m
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
//---------------------------------------------------------------------------------------
// MKLogfile.m created by erik on Sat Jun 29 2002
// @(#)$Id: MKLogfileReader.m,v 1.7 2006-05-11 22:25:33 erik Exp $
//
// Copyright (c) 2002 by Mulle Kybernetik. All rights reserved.
//
// Permission to use, copy, modify and distribute this software and its documentation
// is hereby granted, provided that both the copyright notice and this permission
// notice appear in all copies of the software, derivative works or modified versions,
// and any portions thereof, and that both notices appear in supporting documentation,
// and that credit is given to Mulle Kybernetik in all documents and publicity
// pertaining to direct or indirect use of this code or its derivatives.
//
// THIS IS EXPERIMENTAL SOFTWARE AND IT IS KNOWN TO HAVE BUGS, SOME OF WHICH MAY HAVE
// SERIOUS CONSEQUENCES. THE COPYRIGHT HOLDER ALLOWS FREE USE OF THIS SOFTWARE IN ITS
// "AS IS" CONDITION. THE COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY
// DAMAGES WHATSOEVER RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE
// OR OF ANY DERIVATIVE WORK.
//---------------------------------------------------------------------------------------
#import <Foundation/Foundation.h>
#import "MKLogfileReader.h"
@interface MKLogfileReader(Private)
- (BOOL)_fillBuffer;
@end
//---------------------------------------------------------------------------------------
@implementation MKLogfileReader
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
// init/dealloc
//---------------------------------------------------------------------------------------
- (id)initWithFilename:(NSString *)aFilename
{
[super init];
filename = [aFilename copyWithZone:[self zone]];
return self;
}
- (void)dealloc
{
if(fileHandle != nil)
[self close];
[filename release];
[super dealloc];
}
//---------------------------------------------------------------------------------------
// accessor methods
//---------------------------------------------------------------------------------------
- (NSString *)filename
{
return filename;
}
- (void)setFileCreationDateFromFilesystem
{
NSDictionary *attrs;
attrs = [[NSFileManager defaultManager] fileAttributesAtPath:filename traverseLink:YES];
[fileCreationDate autorelease];
fileCreationDate = [[attrs fileCreationDate] retain];
}
//---------------------------------------------------------------------------------------
// open/close
//---------------------------------------------------------------------------------------
- (BOOL)open
{
if(fileHandle != nil)
return YES;
if((fileHandle = [NSFileHandle fileHandleForReadingAtPath:filename]) == nil)
return NO;
[fileHandle retain];
[fileHandle seekToEndOfFile];
[fileHandle seekToFileOffset:MAX(0, (int)[fileHandle offsetInFile] - 2*1024)];
[self setFileCreationDateFromFilesystem];
buffer = [[NSMutableString allocWithZone:[self zone]] init];
[self nextMessage];
return YES;
}
- (void)close
{
if(fileHandle == nil)
return;
[fileHandle release];
fileHandle = nil;
[buffer release];
buffer = nil;
[fileCreationDate release];
fileCreationDate = nil;
}
- (BOOL)reopen
{
NSCalendarDate *lastCreationDate;
unsigned int lastPosition, newLength;
if(fileHandle != nil)
{
NS_DURING
[self _fillBuffer]; // make sure we don't miss anything
lastPosition = [fileHandle offsetInFile];
[fileHandle closeFile];
NS_HANDLER
NSLog(@"## problem with file %@: failed to read rest before reopening (ignoring)", filename);
NS_ENDHANDLER
[fileHandle release];
fileHandle = nil;
}
else
{
lastPosition = 0;
}
if((fileHandle = [NSFileHandle fileHandleForReadingAtPath:filename]) == nil)
{
NSLog(@"## problem with file %@: failed to reopen.", filename);
return NO;
}
[fileHandle retain];
lastCreationDate = [[fileCreationDate retain] autorelease];
[self setFileCreationDateFromFilesystem];
if([fileCreationDate compare:lastCreationDate] == NSOrderedSame)
{
newLength = [fileHandle seekToEndOfFile];
if(newLength < lastPosition) // uh, same creation date but shorter now? start at "beginning"
[fileHandle seekToFileOffset:MAX(0, (int)newLength - 2*1024)];
else if(newLength > lastPosition) // busy, busy. go back to where we were
[fileHandle seekToFileOffset:lastPosition];
}
return YES;
}
//---------------------------------------------------------------------------------------
// get next line
//---------------------------------------------------------------------------------------
- (NSString *)nextMessage
{
static NSCharacterSet *nlSet = nil;
NSString *string;
NSRange r;
if(nlSet == nil)
nlSet = [[NSCharacterSet characterSetWithCharactersInString:@"\n"] retain];
r = [buffer rangeOfCharacterFromSet:nlSet];
if(r.length == 0)
{
[self _fillBuffer];
r = [buffer rangeOfCharacterFromSet:nlSet];
if(r.length == 0)
return nil;
}
string = [buffer substringToIndex:NSMaxRange(r)];
[buffer deleteCharactersInRange:NSMakeRange(0, NSMaxRange(r))];
return string;
}
//---------------------------------------------------------------------------------------
// helper methods
//---------------------------------------------------------------------------------------
- (BOOL)_fillBuffer
{
NSData *newData;
NSString *newString;
if(fileHandle == nil)
return NO;
NS_DURING
newData = [fileHandle readDataToEndOfFile];
NS_HANDLER
// Retry if we got a Stale NFS file handle exception
if(([[localException name] isEqualToString:NSFileHandleOperationException] == NO) ||
([[localException reason] rangeOfString:@"Stale"].length == 0))
[localException raise];
[self close];
[self open];
NSLog(@"## nfs problem, file is now %@.", (fileHandle != nil) ? @"open" : @"closed");
newData = [fileHandle readDataToEndOfFile];
NS_ENDHANDLER
if([newData length] == 0)
return NO;
newString = [[NSString allocWithZone:[self zone]] initWithData:newData encoding:NSISOLatin1StringEncoding];
[buffer appendString:newString];
[newString release];
return YES;
}
//---------------------------------------------------------------------------------------
@end
//---------------------------------------------------------------------------------------