]> err.no Git - sope/blob - sope-core/NGExtensions/FdExt.subproj/NSObject+Logs.m
f6c77231a38d58eb543b2252c00102c099aa6e71
[sope] / sope-core / NGExtensions / FdExt.subproj / NSObject+Logs.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 "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     logger = [lm loggerForClass:[self class]];
52   }
53   return logger;
54 }
55
56 - (id)debugLogger {
57   return [self logger];
58 }
59
60 - (NSString *)loggingPrefix {
61   /* improve perf ... */
62   return [NSStringClass() stringWithFormat:@"<0x%08X[%@]>",
63                        self, NSStringFromClass([self class])];
64 }
65
66
67 - (void)debugWithFormat:(NSString *)_fmt arguments:(va_list)_va {
68 #if DEBUG
69   NSString *msg;
70   
71   if (![self isDebuggingEnabled]) return;
72   
73   msg = [[NSStringClass() alloc] initWithFormat:_fmt arguments:_va];
74   [[self debugLogger] debugWithFormat:@"<%@>D %@", [self loggingPrefix], msg];
75   [msg release];
76 #else
77 #  warning debug is disabled, debugWithFormat wont print anything ..
78 #endif
79 }
80
81 - (void)logWithFormat:(NSString *)_fmt arguments:(va_list)_va {
82   NGLogger *logger;
83   NSString *msg;
84   
85   logger = [self logger];
86   if (![logger isLogInfoEnabled]) return;
87
88   msg = [[NSStringClass() alloc] initWithFormat:_fmt arguments:_va];
89   [logger logWithFormat:@"%@ %@", [self loggingPrefix], msg];
90   [msg release];
91 }
92
93 - (void)warnWithFormat:(NSString *)_fmt arguments:(va_list)_va {
94   NGLogger *logger;
95   NSString *msg;
96
97   logger = [self logger];
98   if (![logger isLogWarnEnabled]) return;
99
100   msg = [[NSStringClass() alloc] initWithFormat:_fmt arguments:_va];
101   [logger warnWithFormat:@"%@ %@", [self loggingPrefix], msg];
102   [msg release];
103 }
104
105 - (void)errorWithFormat:(NSString *)_fmt arguments:(va_list)_va {
106   NGLogger *logger;
107   NSString *msg;
108   
109   logger = [self logger];
110   if (![logger isLogErrorEnabled]) return;
111
112   msg = [[NSStringClass() alloc] initWithFormat:_fmt arguments:_va];
113   [logger errorWithFormat:@"%@ %@", [self loggingPrefix], msg];
114   [msg release];
115 }
116
117 - (void)fatalWithFormat:(NSString *)_fmt arguments:(va_list)_va {
118   NGLogger *logger;
119   NSString *msg;
120   
121   logger = [self logger];
122   if (![logger isLogFatalEnabled]) return;
123
124   msg = [[NSStringClass() alloc] initWithFormat:_fmt arguments:_va];
125   [logger fatalWithFormat:@"%@ %@", [self loggingPrefix], msg];
126   [msg release];
127 }
128
129 - (void)debugWithFormat:(NSString *)_fmt, ... {
130   va_list ap;
131   
132   va_start(ap, _fmt);
133   [self debugWithFormat:_fmt arguments:ap];
134   va_end(ap);
135 }
136 - (void)logWithFormat:(NSString *)_fmt, ... {
137   va_list ap;
138   
139   va_start(ap, _fmt);
140   [self logWithFormat:_fmt arguments:ap];
141   va_end(ap);
142 }
143 - (void)warnWithFormat:(NSString *)_fmt, ... {
144   va_list ap;
145   
146   va_start(ap, _fmt);
147   [self warnWithFormat:_fmt arguments:ap];
148   va_end(ap);
149 }
150 - (void)errorWithFormat:(NSString *)_fmt, ... {
151   va_list ap;
152   
153   va_start(ap, _fmt);
154   [self errorWithFormat:_fmt arguments:ap];
155   va_end(ap);
156 }
157 - (void)fatalWithFormat:(NSString *)_fmt, ... {
158   va_list ap;
159   
160   va_start(ap, _fmt);
161   [self fatalWithFormat:_fmt arguments:ap];
162   va_end(ap);
163 }
164
165 @end /* NSObject(NGLogs) */