]> err.no Git - sope/blobdiff - sope-appserver/NGObjWeb/WOCoreApplication.m
minor code cleanups
[sope] / sope-appserver / NGObjWeb / WOCoreApplication.m
index 145fe2f39896ccd4a91c6232ce6691e4dfada1c0..632d34d12dfa720ebf7b6718d4c2b89041820631 100644 (file)
@@ -1,20 +1,20 @@
 /*
-  Copyright (C) 2000-2004 SKYRIX Software AG
+  Copyright (C) 2000-2005 SKYRIX Software AG
 
-  This file is part of OpenGroupware.org.
+  This file is part of SOPE.
 
-  OGo is free software; you can redistribute it and/or modify it under
+  SOPE is free software; you can redistribute it and/or modify it under
   the terms of the GNU Lesser General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version.
 
-  OGo is distributed in the hope that it will be useful, but WITHOUT ANY
+  SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
   License for more details.
 
   You should have received a copy of the GNU Lesser General Public
-  License along with OGo; see the file COPYING.  If not, write to the
+  License along with SOPE; see the file COPYING.  If not, write to the
   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
   02111-1307, USA.
 */
@@ -49,6 +49,7 @@ NGObjWeb_DECLARE NSString *WOApplicationDidTerminateNotification =
 
 @interface WOCoreApplication(PrivateMethods)
 + (void)_initDefaults;
++ (id)logger;
 - (NSDictionary *)memoryStatistics;
 - (WOResponse *)handleException:(NSException *)_exc;
 @end
@@ -62,9 +63,10 @@ NGObjWeb_DECLARE NSString *WOApplicationDidTerminateNotification =
 
 @implementation WOCoreApplication
 
-static BOOL  perflog          = NO;
-static BOOL  outputValidateOn = NO;
-static Class NSDateClass      = Nil;
+static BOOL     outputValidateOn = NO;
+static Class    NSDateClass      = Nil;
+static NGLogger *logger          = nil;
+static NGLogger *perfLogger      = nil;
 
 + (int)version {
   return 1;
@@ -75,8 +77,8 @@ static NSMutableArray *activeApps = nil; // THREAD
 
 + (id)application {
   if (WOApp == nil) {
-    NSLog(@"WARNING(%s): some code called +application without "
-          @"an active app !", __PRETTY_FUNCTION__);
+    [self warnWithFormat:@"%s: some code called +application without an "
+            @"active app !", __PRETTY_FUNCTION__];
 #if DEBUG && 0
 #  warning REMOVE THAT ABORT IN PRODUCTION CODE !!!
     abort();
@@ -96,7 +98,7 @@ static NSMutableArray *activeApps = nil; // THREAD
   unsigned idx;
   
   if (WOApp != self) {
-    [self logWithFormat:
+    [self warnWithFormat:
             @"tried to deactivate inactive application !\n"
             @"  self:   %@\n"
             @"  active: %@",
@@ -119,13 +121,17 @@ static NSMutableArray *activeApps = nil; // THREAD
     properly if this is called first.
   */
   static BOOL didInit = NO;
-  NSUserDefaults *ud;
+  NSUserDefaults  *ud;
+  NGLoggerManager *lm;
   if (didInit) return;
   didInit = YES;
   [self _initDefaults];
-  
-  ud = [NSUserDefaults standardUserDefaults];
-  perflog          = [ud boolForKey:@"WOProfileApplication"];
+
+  lm         = [NGLoggerManager defaultLoggerManager];
+  logger     = [lm loggerForClass:self];
+  perfLogger = [lm loggerForDefaultKey:@"WOProfileApplication"];
+
+  ud               = [NSUserDefaults standardUserDefaults];
   outputValidateOn = [ud boolForKey:@"WOOutputValidationEnabled"];
   NSDateClass      = [NSDate class];
 }
@@ -185,6 +191,16 @@ static NSMutableArray *activeApps = nil; // THREAD
   [super dealloc];
 }
 
+/* NGLogging */
+
++ (id)logger {
+  return logger;
+}
+
+- (id)logger {
+  return logger;
+}
+
 /* signals */
 
 - (void)processHupSignal:(int)_signal {
@@ -239,7 +255,7 @@ static NSMutableArray *activeApps = nil; // THREAD
 
   adaptorClass = NSClassFromString(_name);
   if (adaptorClass == Nil) {
-    [self logWithFormat:@"ERROR: did not find adaptor class %@", _name];
+    [self errorWithFormat:@"did not find adaptor class %@", _name];
     return nil;
   }
 
@@ -286,21 +302,23 @@ static NSMutableArray *activeApps = nil; // THREAD
   WOResponse *r  = nil;
   
   if ([self respondsToSelector:@selector(handleException:)]) {
-    NSLog(@"WARNING: calling deprecated -handleException method !");
+    [self warnWithFormat:@"calling deprecated -handleException method !"];
     return [self handleException:_exc];
   }
   
 #if 0 && DEBUG
-  [self logWithFormat:@"%@: caught (without context):\n  %@.", self, _exc];
+  [self errorWithFormat:@"%@: caught (without context):\n  %@.", self, _exc];
   abort();
 #endif
   
   if (_ctx == nil) {
-    [self logWithFormat:@"%@: caught (without context):\n  %@.", self, _exc];
+    [self errorWithFormat:@"%@: caught (without context):\n  %@.",
+      self, _exc];
     [self terminate];
   }
   else if (rq == nil) {
-    [self logWithFormat:@"%@: caught (without request):\n  %@.", self, _exc];
+    [self errorWithFormat:@"%@: caught (without request):\n  %@.",
+      self, _exc];
     [self terminate];
   }
   else {
@@ -310,7 +328,7 @@ static NSMutableArray *activeApps = nil; // THREAD
   }
   
   if ((r = [WOResponse responseWithRequest:rq]) == nil)
-    [self logWithFormat:@"could not create response !"];
+    [self warnWithFormat:@"could not create response !"];
     
   [r setStatus:500];
   return r;
@@ -422,7 +440,7 @@ static NSMutableArray *activeApps = nil; // THREAD
         adaptor = [self adaptorWithName:[moreAdaptors objectAtIndex:i]
                         arguments:nil];
         if (adaptor == nil) {
-          [self logWithFormat:@"could not find WOAdaptor '%@' !",
+          [self warnWithFormat:@"could not find WOAdaptor '%@' !",
                 [moreAdaptors objectAtIndex:i]];
           continue;
         }
@@ -566,8 +584,8 @@ static NSMutableArray *activeApps = nil; // THREAD
   static BOOL didWarn = NO;
 
   if (!didWarn) {
-    [self logWithFormat:
-            @"WARNING: output validation is enabled, this will "
+    [self warnWithFormat:
+            @"output validation is enabled, this will "
             @"slow down request processing!"];
     didWarn = YES;
   }
@@ -603,7 +621,7 @@ static NSMutableArray *activeApps = nil; // THREAD
       if ([self hideValidationIssue:issue])
         continue;
       [self logWithFormat:@"validate-output[%@]: %@",
-             [(NSException *)issue name], [issue reason]];
+              [(NSException *)issue name], [issue reason]];
       continue;
     }
     
@@ -623,24 +641,24 @@ static NSMutableArray *activeApps = nil; // THREAD
   WOResponse     *response = nil;
   NSTimeInterval startDispatch = 0.0;
   
-  if (perflog)
+  if (perfLogger)
     startDispatch = [[NSDateClass date] timeIntervalSince1970];
   
   /* let request handler process the request */
   {
     NSTimeInterval startDispatch = 0.0;
     
-    if (perflog)
+    if (perfLogger)
       startDispatch = [[NSDateClass date] timeIntervalSince1970];
     
     /* the call ;-) */
     response = [handler handleRequest:_request];
     
-    if (perflog) {
+    if (perfLogger) {
       NSTimeInterval rt;
       rt = [[NSDateClass date] timeIntervalSince1970] - startDispatch;
-      NSLog(@"  [woapp-rq]: request handler took %4.3fs.",
-            rt < 0.0 ? -1.0 : rt);
+      [perfLogger logWithFormat:@"[woapp-rq]: request handler took %4.3fs.",
+                                    rt < 0.0 ? -1.0 : rt];
     }
   }
   
@@ -649,11 +667,11 @@ static NSMutableArray *activeApps = nil; // THREAD
   if (outputValidateOn)
     [self validateOutputOfResponse:response];
   
-  if (perflog) {
+  if (perfLogger) {
     NSTimeInterval rt;
     rt = [[NSDateClass date] timeIntervalSince1970] - startDispatch;
-    NSLog(@"[woapp]: dispatchRequest took %4.3fs.",
-          rt < 0.0 ? -1.0 : rt);
+    [perfLogger logWithFormat:@"[woapp]: dispatchRequest took %4.3fs.",
+                  rt < 0.0 ? -1.0 : rt];
   }
   
   return response;
@@ -662,14 +680,14 @@ static NSMutableArray *activeApps = nil; // THREAD
   WORequestHandler *handler;
   
   if ([self respondsToSelector:@selector(handleRequest:)]) {
-    [self logWithFormat:
-            @"WARNING: calling deprecated -handleRequest: method .."];
+    [self warnWithFormat:
+            @"calling deprecated -handleRequest: method .."];
     return [self handleRequest:_request];
   }
   
   /* find request handler for request */
   if ((handler = [self handlerForRequest:_request]) == nil) {
-    [self logWithFormat:@"ERROR: got no request handler for request: %@ !",
+    [self errorWithFormat:@"got no request handler for request: %@ !",
            _request];
     return nil;
   }
@@ -731,18 +749,22 @@ static NSMutableArray *activeApps = nil; // THREAD
   
   apath = [self findNGObjWebResource:@"Defaults" ofType:@"plist"];
   if (apath == nil)
-    NSLog(@"ERROR: cannot find Defaults.plist resource of NGObjWeb library!");
+    [self errorWithFormat:@"cannot find Defaults.plist resource of NGObjWeb library!"];
+#if HEAVY_DEBUG
+  else
+    [self debugWithFormat:@"Note: loading default defaults: %@", apath];
+#endif
   
   owDefaults = [NSDictionary dictionaryWithContentsOfFile:apath];
   if (owDefaults) {
     [[NSUserDefaults standardUserDefaults] registerDefaults:owDefaults];
 #if HEAVY_DEBUG
-    [self logWithFormat:@"did register NGObjWeb defaults: %@\n%@", 
-           apath, owDefaults];
+    [self debugWithFormat:@"did register NGObjWeb defaults: %@\n%@", 
+            apath, owDefaults];
 #endif
   }
   else {
-    [self logWithFormat:@"ERROR: could not load NGObjWeb defaults: '%@'",
+    [self errorWithFormat:@"could not load NGObjWeb defaults: '%@'",
            apath];
   }
 }
@@ -779,13 +801,16 @@ static NSMutableArray *activeApps = nil; // THREAD
   woport = [[self userDefaults] objectForKey:@"WOPort"];
   if ([woport isKindOfClass:[NSNumber class]])
     return woport;
-  
   woport = [woport stringValue];
+  if ([woport length] > 0 && isdigit([woport characterAtIndex:0]))
+    return [NSNumber numberWithInt:[woport intValue]];
+  
   addr   = NGSocketAddressFromString(woport);
   
   if ([addr isKindOfClass:[NGInternetSocketAddress class]])
     return [NSNumber numberWithInt:[(NGInternetSocketAddress *)addr port]];
-  
+
+  [self fatalWithFormat:@"GOT NO PORT FOR ADDR: %@ (%@)", addr, woport];
   return nil;
 }