]> err.no Git - sope/blob - sope-core/NGExtensions/NGLogging.subproj/NGLogger.m
applied bundle manager patch by Matthew
[sope] / sope-core / NGExtensions / NGLogging.subproj / NGLogger.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 "NGLogger.h"
23 #include "common.h"
24 #include "NGLogEvent.h"
25 #include "NGLogAppender.h"
26
27 @implementation NGLogger
28
29 static Class StrClass = Nil;
30
31 + (void)initialize {
32   StrClass = [NSString class];
33 }
34
35 - (id)init {
36   return [self initWithLogLevel:NGLogLevelAll];
37 }
38
39 - (id)initWithLogLevel:(NGLogLevel)_level {
40   if ((self = [super init])) {
41     NSUserDefaults *ud;
42     NSString *appenderClassName;
43     
44     [self setLogLevel:_level];
45     
46     // TODO: remove this as soon as we have a config
47     ud = [NSUserDefaults standardUserDefaults];
48     appenderClassName = [ud stringForKey:@"NGLogDefaultAppenderClass"];
49     if (appenderClassName == nil)
50       appenderClassName = @"NGLogConsoleAppender";
51     self->_appender = [[NSClassFromString(appenderClassName) alloc] init];
52   }
53   return self;
54 }
55
56 - (void)dealloc {
57   [self->_appender release];
58   [super dealloc];
59 }
60
61 /* accessors */
62
63 - (void)setLogLevel:(NGLogLevel)_level {
64   self->logLevel = _level;
65 }
66
67 - (NGLogLevel)logLevel {
68   return self->logLevel;
69 }
70
71 /* logging */
72
73 - (void)logLevel:(NGLogLevel)_level withFormat:(NSString *)_fmt, ... {
74   NSString *msg;
75   va_list va;
76   
77   if(self->logLevel > _level)
78     return;
79   
80   va_start(va, _fmt);
81   msg = [[StrClass alloc] initWithFormat:_fmt arguments:va];
82   va_end(va);
83   [self logLevel:_level message:msg];
84   [msg release];
85 }
86
87 - (void)logLevel:(NGLogLevel)_level message:(NSString *)_msg {
88   NGLogEvent *event;
89
90   event = [[NGLogEvent alloc] initWithLevel:_level message:_msg];
91   
92   // iterate appenders
93   // TODO: as soon as we have more appenders, we need to iterate on them
94   [self->_appender appendLogEvent:event];
95   [event release];
96 }
97
98 /* log conditions */
99
100 - (BOOL)isLogDebugEnabled {
101   return self->logLevel >= NGLogLevelDebug;
102 }
103
104 - (BOOL)isLogInfoEnabled {
105   return self->logLevel >= NGLogLevelInfo;
106 }
107
108 - (BOOL)isLogWarnEnabled {
109   return self->logLevel >= NGLogLevelWarn;
110 }
111
112 - (BOOL)isLogErrorEnabled {
113   return self->logLevel >= NGLogLevelError;
114 }
115
116 - (BOOL)isLogFatalEnabled {
117   return self->logLevel >= NGLogLevelFatal;
118 }
119
120 @end /* NGLogger */