]> err.no Git - sope/blob - sope-core/NGExtensions/NGLogging.subproj/NGLogger.m
New subproject NGLogging added
[sope] / sope-core / NGExtensions / NGLogging.subproj / NGLogger.m
1 /*
2  Copyright (C) 2000-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 - (id)init {
30   self = [self initWithLogLevel:NGLogLevelAll];
31   return self;
32 }
33
34 - (id)initWithLogLevel:(NGLogLevel)_level {
35   if((self = [super init])) {
36     NSUserDefaults *ud;
37     NSString *appenderClassName;
38     
39     [self setLogLevel:_level];
40     
41 #warning ** remove this as soon as we have a config
42     ud = [NSUserDefaults standardUserDefaults];
43     appenderClassName = [ud stringForKey:@"NGLogDefaultAppenderClass"];
44     if(appenderClassName == nil)
45       appenderClassName = @"NGLogConsoleAppender";
46     self->_appender = [[NSClassFromString(appenderClassName) alloc] init];
47   }
48   return self;
49 }
50
51 - (void)dealloc {
52   [self->_appender release];
53   [super dealloc];
54 }
55
56
57 - (void)setLogLevel:(NGLogLevel)_level {
58   self->logLevel = _level;
59 }
60
61 - (NGLogLevel)logLevel {
62   return self->logLevel;
63 }
64
65 - (void)logLevel:(NGLogLevel)_level withFormat:(NSString *)_fmt, ... {
66   NSString *msg;
67   va_list va;
68   
69   if(self->logLevel > _level)
70     return;
71   
72   va_start(va, _fmt);
73   msg = [[NSString alloc] initWithFormat:_fmt arguments:va];
74   va_end(va);
75   [self logLevel:_level message:msg];
76   [msg release];
77 }
78
79 - (void)logLevel:(NGLogLevel)_level message:(NSString *)_msg {
80   NGLogEvent *event;
81
82   event = [[NGLogEvent alloc] initWithLevel:_level message:_msg];
83
84   // iterate appenders
85   // TODO: as soon as we have more appenders, we need to iterate on them
86   [self->_appender appendLogEvent:event];
87   [event release];
88 }
89
90 - (BOOL)isLogDebugEnabled {
91   return self->logLevel >= NGLogLevelDebug;
92 }
93
94 - (BOOL)isLogInfoEnabled {
95   return self->logLevel >= NGLogLevelInfo;
96 }
97
98 - (BOOL)isLogWarnEnabled {
99   return self->logLevel >= NGLogLevelWarn;
100 }
101
102 - (BOOL)isLogErrorEnabled {
103   return self->logLevel >= NGLogLevelError;
104 }
105
106 - (BOOL)isLogFatalEnabled {
107   return self->logLevel >= NGLogLevelFatal;
108 }
109
110 @end