]> err.no Git - sope/blob - sope-core/NGExtensions/NGLogging.subproj/NGLogFileHandleAppender.m
stabilized NGLogging - adapted enhancements in parts of NGObjWeb
[sope] / sope-core / NGExtensions / NGLogging.subproj / NGLogFileHandleAppender.m
1 /*
2   Copyright (C) 2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include "NGLogFileHandleAppender.h"
23 #include "NGLogEvent.h"
24 #include "common.h"
25
26 @implementation NGLogFileHandleAppender
27
28 static NSData *nextLineData = nil;
29
30 + (void)initialize {
31   static BOOL didInit = NO;
32   
33   if (didInit) return;
34
35   didInit = YES;
36   nextLineData = [[@"\n" dataUsingEncoding:NSASCIIStringEncoding] retain];
37 }
38
39 - (id)initWithConfig:(NSDictionary *)_config {
40   self = [super initWithConfig:_config];
41   if (self) {
42     self->flushImmediately = NO;
43     self->encoding         = [NSString defaultCStringEncoding];
44     [self openFileHandleWithConfig:_config];
45   }
46   return self;
47 }
48
49 - (void)dealloc {
50   if ([self isFileHandleOpen])
51     [self closeFileHandle];
52   [self->fh release];
53   [super dealloc];
54 }
55
56 - (BOOL)isFileHandleOpen {
57   return self->fh ? YES : NO;
58 }
59
60 - (void)openFileHandleWithConfig:(NSDictionary *)_config {
61 }
62
63 - (void)closeFileHandle {
64   [self->fh closeFile];
65 }
66
67 - (void)appendLogEvent:(NGLogEvent *)_event {
68   NSString *formatted;
69   NSData   *bin;
70
71   formatted = [self formattedEvent:_event];
72   bin       = [formatted dataUsingEncoding:self->encoding];
73   [self->fh writeData:bin];
74   [self->fh writeData:nextLineData];
75   if (self->flushImmediately)
76     [self->fh synchronizeFile];
77 }
78
79 @end /* NGLogFileHandleAppender */