Oolite
Loading...
Searching...
No Matches
OOAsyncLogger Class Reference
Inheritance diagram for OOAsyncLogger:
Collaboration diagram for OOAsyncLogger:

Instance Methods

(void) - asyncLogMessage:
(void) - endLogging
(void) - changeFile
(BOOL) - startLogging
(void) - loggerThread
(void) - flushLog
(id) - init [implementation]
(void) - dealloc [implementation]

Private Attributes

OOAsyncQueue * messageQueue
NSConditionLock * threadStateMonitor
NSFileHandle * logFile
NSTimer * flushTimer

Detailed Description

Definition at line 84 of file OOLogOutputHandler.m.

Method Documentation

◆ asyncLogMessage:

- (void) asyncLogMessage: (NSString*) message

Definition at line 353 of file OOLogOutputHandler.m.

353 :(NSString*)message
354{
355 // Don't log of saturated flag is set.
356 if (sSaturated)
357 return;
358
359 if (message != nil) {
360 message = [message stringByAppendingString:@"\n"];
361
362#if OOLITE_WINDOWS
363 // Convert Unix line endings to Windows ones.
364 NSArray* messageComponents = [message componentsSeparatedByString:@"\n"];
365 message = [messageComponents componentsJoinedByString:@"\r\n"];
366#endif
367
368 [messageQueue enqueue:[message dataUsingEncoding:NSUTF8StringEncoding]];
369
370 if (flushTimer == nil) {
371 // No pending flush
372 flushTimer = [NSTimer scheduledTimerWithTimeInterval:kFlushInterval target:self selector:@selector(flushLog) userInfo:nil repeats:NO];
373 }
374 }
375}
static BOOL sSaturated
return nil

References OOAsyncQueue::enqueue:, flushTimer, nil, and sSaturated.

Referenced by endLogging, and OOLogOutputHandlerPrint().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ changeFile

- (void) changeFile

Definition at line 346 of file OOLogOutputHandler.m.

347{
348 [self endLogging];
349 if (![self startLogging])
350 sWriteToStderr = YES;
351}
static BOOL sWriteToStderr

References changeFile, endLogging, startLogging, and sWriteToStderr.

Referenced by changeFile, and OOLogOutputHandlerChangeLogFile().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dealloc

- (void) dealloc
implementation

Definition at line 260 of file OOLogOutputHandler.m.

261{
265 // We don't own a reference to flushTimer.
266
267 [super dealloc];
268}
#define DESTROY(x)
OOAsyncQueue * messageQueue
NSFileHandle * logFile
NSConditionLock * threadStateMonitor

References dealloc, DESTROY, logFile, messageQueue, and threadStateMonitor.

Referenced by dealloc.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ endLogging

- (void) endLogging

Definition at line 330 of file OOLogOutputHandler.m.

331{
332 NSString* postamble = nil;
333
334 if (messageQueue != nil && threadStateMonitor != nil) {
335 // We're fully inited; write postamble, wait for worker thread to terminate cleanly, and close file.
336 postamble = [NSString stringWithFormat:@"\nClosing log at %@.", [NSDate date]];
337 [self asyncLogMessage:postamble];
338 [messageQueue enqueue:@"die"]; // Kill message
339 [threadStateMonitor lockWhenCondition:kConditionReadyToDealloc];
340 [threadStateMonitor unlock];
341
342 [logFile closeFile];
343 }
344}

References asyncLogMessage:, endLogging, OOAsyncQueue::enqueue:, messageQueue, nil, and threadStateMonitor.

Referenced by changeFile, endLogging, and OOLogOutputHandlerClose().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ flushLog

- (void) flushLog

Definition at line 377 of file OOLogOutputHandler.m.

378{
379 flushTimer = nil;
380 [messageQueue enqueue:@"flush"];
381}

References OOAsyncQueue::enqueue:, flushLog, flushTimer, and nil.

Referenced by flushLog.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init

- (id) init
implementation

Definition at line 223 of file OOLogOutputHandler.m.

224{
225 BOOL OK = YES;
226 NSString* logPath = nil;
227 NSString* oldPath = nil;
228 NSFileManager* fmgr = nil;
229
230 self = [super init];
231 if (self == nil)
232 OK = NO;
233
234 if (OK) {
235 fmgr = [NSFileManager defaultManager];
236 logPath = OOLogHandlerGetLogPath();
237
238 // If there is an existing file, move it to Previous.log.
239 if ([fmgr fileExistsAtPath:logPath]) {
240 oldPath = [OOLogHandlerGetLogBasePath() stringByAppendingPathComponent:@"Previous.log"];
241 [fmgr oo_removeItemAtPath:oldPath];
242 if (![fmgr oo_moveItemAtPath:logPath toPath:oldPath]) {
243 if (![fmgr oo_removeItemAtPath:logPath]) {
244 NSLog(@"Log setup: could not move or delete existing log at %@, will log to stdout instead.", logPath);
245 OK = NO;
246 }
247 }
248 }
249 }
250
251 if (OK)
252 OK = [self startLogging];
253
254 if (!OK)
255 DESTROY(self);
256
257 return self;
258}
NSString * OOLogHandlerGetLogPath(void)
#define NSLog(format,...)
Definition OOLogging.h:148

References DESTROY, init, nil, NSLog, OOLogHandlerGetLogPath(), and startLogging.

Referenced by init.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ loggerThread

- (void) loggerThread

Definition at line 383 of file OOLogOutputHandler.m.

384{
385 id message = nil;
386 NSAutoreleasePool *rootPool = nil, *pool = nil;
387 NSUInteger size = 0;
388
389 rootPool = [[NSAutoreleasePool alloc] init];
390 [NSThread ooSetCurrentThreadName:@"loggerThread"];
391
392 // Signal readiness
393 [messageQueue retain];
394 [threadStateMonitor lock];
395 [threadStateMonitor unlockWithCondition:kConditionWorking];
396
397 @try {
398 for (;;) {
399 pool = [[NSAutoreleasePool alloc] init];
400
401 message = [messageQueue dequeue];
402
403 if (!sSaturated && [message isKindOfClass:[NSData class]]) {
404 size += [message length];
405 if (size > 1 << 30) // 1 GiB
406 {
407 sSaturated = YES;
408#if OOLITE_WINDOWS
409 message = @"\r\n\r\n\r\n***** LOG TRUNCATED DUE TO EXCESSIVE LENGTH *****\r\n";
410#else
411 message = @"\n\n\n***** LOG TRUNCATED DUE TO EXCESSIVE LENGTH *****\n";
412#endif
413 message = [message dataUsingEncoding:NSUTF8StringEncoding];
414 }
415
416 [logFile writeData:message];
417 } else if ([message isEqual:@"flush"]) {
418 [logFile synchronizeFile];
419 } else if ([message isEqual:@"die"]) {
420 break;
421 }
422
423 [pool release];
424 }
425 }
426 @catch (NSException* exception) {
427 }
428 [pool release];
429
430 // Clean up; after this, ivars are out of bounds.
431 [messageQueue release];
432 [threadStateMonitor lock];
433 [threadStateMonitor unlockWithCondition:kConditionReadyToDealloc];
434
435 [rootPool release];
436}
voidpf void uLong size
Definition ioapi.h:126

References OOAsyncQueue::dequeue, loggerThread, nil, and sSaturated.

Referenced by loggerThread.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ startLogging

- (BOOL) startLogging

Definition at line 270 of file OOLogOutputHandler.m.

271{
272 BOOL OK = YES;
273 NSString* logPath = nil;
274 NSFileManager* fmgr = nil;
275
276 fmgr = [NSFileManager defaultManager];
277
278 if (OK) {
279 messageQueue = [[OOAsyncQueue alloc] init];
280 if (messageQueue == nil)
281 OK = NO;
282 }
283
284 if (OK) {
285 // set up threadStateMonitor -- used as a binary semaphore of sorts to check when the worker thread starts and stops.
286 threadStateMonitor = [[NSConditionLock alloc] initWithCondition:kConditionReadyToDealloc];
287 if (threadStateMonitor == nil)
288 OK = NO;
289 [threadStateMonitor setName:@"OOLogOutputHandler.stateMonitor"];
290 }
291
292 if (OK) {
293 // Create work thread to actually handle messages.
294 // This needs to be done early to avoid messy state if something goes wrong.
295 [NSThread detachNewThreadSelector:@selector(loggerThread) toTarget:self withObject:nil];
296 // Wait for it to start.
297 if (![threadStateMonitor lockWhenCondition:kConditionWorking beforeDate:[NSDate dateWithTimeIntervalSinceNow:5.0]]) {
298 // If it doesn't signal a start within five seconds, assume something's wrong.
299 // Send kill signal, just in case it comes to life...
300 [messageQueue enqueue:@"die"];
301 // ...and stop -dealloc from waiting for thread death
302 [threadStateMonitor release];
304 OK = NO;
305 }
306 [threadStateMonitor unlockWithCondition:kConditionWorking];
307 }
308
309 if (OK) {
310 logPath = OOLogHandlerGetLogPath();
311 OK = (logPath != nil);
312 }
313
314 if (OK) {
315 // Create shiny new log file
316 OK = [fmgr createFileAtPath:logPath contents:nil attributes:nil];
317 if (OK) {
318 logFile = [[NSFileHandle fileHandleForWritingAtPath:logPath] retain];
319 OK = (logFile != nil);
320 }
321 if (!OK) {
322 NSLog(@"Log setup: could not open log at %@, will log to stdout instead.", logPath);
323 OK = NO;
324 }
325 }
326
327 return OK;
328}
@ kConditionWorking

References OOAsyncQueue::enqueue:, kConditionWorking, logFile, messageQueue, nil, NSLog, OOLogHandlerGetLogPath(), startLogging, and threadStateMonitor.

Referenced by changeFile, init, and startLogging.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ flushTimer

- (NSTimer*) flushTimer
private

Definition at line 89 of file OOLogOutputHandler.m.

Referenced by asyncLogMessage:, and flushLog.

◆ logFile

- (NSFileHandle*) logFile
private

Definition at line 88 of file OOLogOutputHandler.m.

Referenced by dealloc, and startLogging.

◆ messageQueue

- (OOAsyncQueue*) messageQueue
private

Definition at line 86 of file OOLogOutputHandler.m.

Referenced by dealloc, endLogging, and startLogging.

◆ threadStateMonitor

- (NSConditionLock*) threadStateMonitor
private

Definition at line 87 of file OOLogOutputHandler.m.

Referenced by dealloc, endLogging, and startLogging.


The documentation for this class was generated from the following file: