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
29static Class StringClass = Nil;
30
31static 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 NSMapTable      *loggerForClassMap = NULL;
46  static NGLoggerManager *lm                = nil;
47  NGLogger *logger;
48
49  if (!loggerForClassMap) {
50    loggerForClassMap = NSCreateMapTable(NSNonOwnedPointerMapKeyCallBacks,
51                                         NSNonRetainedObjectMapValueCallBacks,
52                                         200);
53    lm = [NGLoggerManager defaultLoggerManager];
54  }
55  logger = NSMapGet(loggerForClassMap, object_getClass(self));
56  if (!logger) {
57    logger = [lm loggerForClass: object_getClass(self)];
58    NSMapInsert(loggerForClassMap, object_getClass(self), logger);
59  }
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%p[%@]>",
71                       self, NSStringFromClass([self class])];
72}
73
74
75- (void)debugWithFormat:(NSString *)_fmt arguments:(va_list)_va {
76#if DEBUG
77  NGLogger *logger;
78  NSString *msg, *msg2;
79
80  /* This does some fancy formatting and calls [NGLogger logLevel:message:]
81   * thereby bypassing the logLevel check normally done in the logger.
82   * So we must do it here
83   */
84
85  logger = [self debugLogger];
86  if ((![self isDebuggingEnabled]) || ([logger logLevel] < NGLogLevelDebug))
87    return;
88
89  msg  = [[NSStringClass() alloc] initWithFormat:_fmt arguments:_va];
90  msg2 = [[NSStringClass() alloc] initWithFormat:
91				    @"<%@>D %@", [self loggingPrefix], msg];
92  [logger logLevel:NGLogLevelDebug message:msg2];
93  [msg2 release];
94  [msg  release];
95#else
96#  warning debug is disabled, debugWithFormat wont print anything ..
97#endif
98}
99
100- (void)logWithFormat:(NSString *)_fmt arguments:(va_list)_va {
101  NGLogger *logger;
102  NSString *msg;
103
104  logger = [self logger];
105  if (![logger isLogInfoEnabled]) return;
106
107  msg = [[NSStringClass() alloc] initWithFormat:_fmt arguments:_va];
108  [logger logWithFormat:@"%@ %@", [self loggingPrefix], msg];
109  [msg release];
110}
111
112- (void)warnWithFormat:(NSString *)_fmt arguments:(va_list)_va {
113  NGLogger *logger;
114  NSString *msg;
115
116  logger = [self logger];
117  if (![logger isLogWarnEnabled]) return;
118
119  msg = [[NSStringClass() alloc] initWithFormat:_fmt arguments:_va];
120  [logger warnWithFormat:@"%@ %@", [self loggingPrefix], msg];
121  [msg release];
122}
123
124- (void)errorWithFormat:(NSString *)_fmt arguments:(va_list)_va {
125  NGLogger *logger;
126  NSString *msg;
127
128  logger = [self logger];
129  if (![logger isLogErrorEnabled]) return;
130
131  msg = [[NSStringClass() alloc] initWithFormat:_fmt arguments:_va];
132  [logger errorWithFormat:@"%@ %@", [self loggingPrefix], msg];
133  [msg release];
134}
135
136- (void)fatalWithFormat:(NSString *)_fmt arguments:(va_list)_va {
137  NGLogger *logger;
138  NSString *msg;
139
140  logger = [self logger];
141  if (![logger isLogFatalEnabled]) return;
142
143  msg = [[NSStringClass() alloc] initWithFormat:_fmt arguments:_va];
144  [logger fatalWithFormat:@"%@ %@", [self loggingPrefix], msg];
145  [msg release];
146}
147
148- (void)debugWithFormat:(NSString *)_fmt, ... {
149  va_list ap;
150
151  va_start(ap, _fmt);
152  [self debugWithFormat:_fmt arguments:ap];
153  va_end(ap);
154}
155- (void)logWithFormat:(NSString *)_fmt, ... {
156  va_list ap;
157
158  va_start(ap, _fmt);
159  [self logWithFormat:_fmt arguments:ap];
160  va_end(ap);
161}
162- (void)warnWithFormat:(NSString *)_fmt, ... {
163  va_list ap;
164
165  va_start(ap, _fmt);
166  [self warnWithFormat:_fmt arguments:ap];
167  va_end(ap);
168}
169- (void)errorWithFormat:(NSString *)_fmt, ... {
170  va_list ap;
171
172  va_start(ap, _fmt);
173  [self errorWithFormat:_fmt arguments:ap];
174  va_end(ap);
175}
176- (void)fatalWithFormat:(NSString *)_fmt, ... {
177  va_list ap;
178
179  va_start(ap, _fmt);
180  [self fatalWithFormat:_fmt arguments:ap];
181  va_end(ap);
182}
183
184@end /* NSObject(NGLogs) */
185