]> err.no Git - sope/blob - sope-core/NGExtensions/FdExt.subproj/NSObject+Logs.m
65087c561711b2ef5145f1941cd259dfd5640d25
[sope] / sope-core / NGExtensions / FdExt.subproj / NSObject+Logs.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE 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   SOPE 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 SOPE; 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 "NSObject+Logs.h"
23 #include "NGLoggerManager.h"
24 #include "NGLogger.h"
25 #include "common.h"
26
27 @implementation NSObject(NGLogs)
28
29 static Class StringClass = Nil;
30
31 static inline Class NSStringClass(void) {
32   if (StringClass == Nil) StringClass = [NSString class];
33   return StringClass;
34 }
35
36 - (BOOL)isDebuggingEnabled {
37 #if DEBUG
38   return YES;
39 #else
40   return NO;
41 #endif
42 }
43
44 - (id)logger {
45   static id logger = nil;
46   
47   if (logger == nil) {
48     NGLoggerManager *lm;
49     
50     lm = [NGLoggerManager defaultLoggerManager];
51
52 #if 0 // broken: if [self class] is passed in, 'logger' can't be a static
53     logger = [lm loggerForClass:[self class]];
54 #else
55     logger = [lm loggerForClass:[NSObject class]];
56 #if 0
57     NSLog(@"%@: created logger: %@", self, logger);
58 #endif
59 #endif
60   }
61   return logger;
62 }
63
64 - (id)debugLogger {
65   return [self logger];
66 }
67
68 - (NSString *)loggingPrefix {
69   /* improve perf ... */
70   return [NSStringClass() stringWithFormat:@"<0x%08X[%@]>",
71                        self, NSStringFromClass([self class])];
72 }
73
74
75 - (void)debugWithFormat:(NSString *)_fmt arguments:(va_list)_va {
76 #if DEBUG
77   NSString *msg, *msg2;
78   
79   if (![self isDebuggingEnabled]) return;
80   
81   msg  = [[NSStringClass() alloc] initWithFormat:_fmt arguments:_va];
82   msg2 = [[NSStringClass() alloc] initWithFormat:
83                                     @"<%@>D %@", [self loggingPrefix], msg];
84   [[self debugLogger] logLevel:NGLogLevelDebug message:msg2];
85   [msg2 release];
86   [msg  release];
87 #else
88 #  warning debug is disabled, debugWithFormat wont print anything ..
89 #endif
90 }
91
92 - (void)logWithFormat:(NSString *)_fmt arguments:(va_list)_va {
93   NGLogger *logger;
94   NSString *msg;
95   
96   logger = [self logger];
97   if (![logger isLogInfoEnabled]) return;
98   
99   msg = [[NSStringClass() alloc] initWithFormat:_fmt arguments:_va];
100   [logger logWithFormat:@"%@ %@", [self loggingPrefix], msg];
101   [msg release];
102 }
103
104 - (void)warnWithFormat:(NSString *)_fmt arguments:(va_list)_va {
105   NGLogger *logger;
106   NSString *msg;
107
108   logger = [self logger];
109   if (![logger isLogWarnEnabled]) return;
110
111   msg = [[NSStringClass() alloc] initWithFormat:_fmt arguments:_va];
112   [logger warnWithFormat:@"%@ %@", [self loggingPrefix], msg];
113   [msg release];
114 }
115
116 - (void)errorWithFormat:(NSString *)_fmt arguments:(va_list)_va {
117   NGLogger *logger;
118   NSString *msg;
119   
120   logger = [self logger];
121   if (![logger isLogErrorEnabled]) return;
122
123   msg = [[NSStringClass() alloc] initWithFormat:_fmt arguments:_va];
124   [logger errorWithFormat:@"%@ %@", [self loggingPrefix], msg];
125   [msg release];
126 }
127
128 - (void)fatalWithFormat:(NSString *)_fmt arguments:(va_list)_va {
129   NGLogger *logger;
130   NSString *msg;
131   
132   logger = [self logger];
133   if (![logger isLogFatalEnabled]) return;
134
135   msg = [[NSStringClass() alloc] initWithFormat:_fmt arguments:_va];
136   [logger fatalWithFormat:@"%@ %@", [self loggingPrefix], msg];
137   [msg release];
138 }
139
140 - (void)debugWithFormat:(NSString *)_fmt, ... {
141   va_list ap;
142   
143   va_start(ap, _fmt);
144   [self debugWithFormat:_fmt arguments:ap];
145   va_end(ap);
146 }
147 - (void)logWithFormat:(NSString *)_fmt, ... {
148   va_list ap;
149   
150   va_start(ap, _fmt);
151   [self logWithFormat:_fmt arguments:ap];
152   va_end(ap);
153 }
154 - (void)warnWithFormat:(NSString *)_fmt, ... {
155   va_list ap;
156   
157   va_start(ap, _fmt);
158   [self warnWithFormat:_fmt arguments:ap];
159   va_end(ap);
160 }
161 - (void)errorWithFormat:(NSString *)_fmt, ... {
162   va_list ap;
163   
164   va_start(ap, _fmt);
165   [self errorWithFormat:_fmt arguments:ap];
166   va_end(ap);
167 }
168 - (void)fatalWithFormat:(NSString *)_fmt, ... {
169   va_list ap;
170   
171   va_start(ap, _fmt);
172   [self fatalWithFormat:_fmt arguments:ap];
173   va_end(ap);
174 }
175
176 @end /* NSObject(NGLogs) */