/*!
  @header ECLoggingTEST
  @abstract Module of Encore
 
  @availability OS X, GNUstep
  @copyright 2004, 2005, 2006 Oliver Langer
  
  This library 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.1 of the License, or (at your option) any later version.

  This library 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 this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA

  <pre>
  -------------------------------------------------------------------------
  Modification history

  05.01.06 ola     initial version
  23.08.06 ola     license changed
  -------------------------------------------------------------------------
  </pre>
*/

#include <ECLoggingTEST.h>

@implementation ECLoggingTEST

- installForContext: (NSString *) aContext usingLevel: (NSString *) aLevel 
  usingFileoutputAtPath: (NSString *) filename {
  ECDefaultLoggingFormatter *loggingFormatter;
  id  loggingWriter;
  ECLoggingConfiguration *loggingConfiguration;

  loggingFormatter = [[[ECDefaultLoggingFormatter alloc] init] autorelease];
  if( nil == filename ) {
    loggingWriter = [[[ECNSLogLoggingWriter alloc] init] autorelease];
  } else {
    loggingWriter = [[[ECFileLoggingWriter alloc] init] autorelease];
    [loggingWriter setBaseFilename: filename];
    [loggingWriter setMaxFilesize: 128]; /* force one logging roation*/
  }
  loggingConfiguration = [[[ECLoggingConfiguration alloc] init] autorelease];

  [loggingConfiguration setLoggingLevel: aLevel ];
  [loggingConfiguration setLoggingWriter: loggingWriter];
  [loggingConfiguration setLoggingFormatter: loggingFormatter];

  if( nil != aContext ) {
    [[ECLogging instance] addLoggingConfiguration: loggingConfiguration
      forContext: aContext];
  } else {
    [[ECLogging instance] addRootLoggingConfiguration: loggingConfiguration];
  }
  return self;
}


- manualInitialization {
  [self installForContext: @"sample" usingLevel: @"INFO"
    usingFileoutputAtPath: nil];
  [self installForContext: @"sample.specific" usingLevel: @"TRACE"
    usingFileoutputAtPath: nil];
  [self installForContext: @"sample.moreSpecificA" usingLevel: @"DEBUG"
    usingFileoutputAtPath: nil];
  [self installForContext: @"sample.moreSpecificB" usingLevel: @"ERROR"
    usingFileoutputAtPath: nil];
  [self installForContext: @"sample.file" usingLevel: @"DEBUG"
    usingFileoutputAtPath: @"logging.log"];

  [self installForContext: nil usingLevel: @"DEBUG"
    usingFileoutputAtPath: nil ];
  return self;
}


- logWithLogger: (ECLogger *) logger {
  if( [logger isDebugEnabled] ) {
    [logger debug: @"This is a logging message of level DEBUG!"];
  }

  if( [logger isTraceEnabled] ) {
    [logger trace: @"Trace is given hereby..."];
  }
  
  if( [logger isInfoEnabled] ) {
    [logger info: @"This is a logging message of level INFO!"];
  }
  
  [logger error: @"Logging with error code=%u and msg=%@", 112,
    @"Error Message" ];
 
  return self;
}


- testLogging {
  NSLog( @"ECLoggingTEST::testLogging: BEGIN...." );

  EC_AUTORELEASEPOOL_BEGIN

  [self manualInitialization];
  
  NSLog( @"Log as INFO----------->" );
  [self logWithLogger: [ECLogging loggerForContext: @"sample"]];
  NSLog( @"Log as TRACE----------->" );
  [self logWithLogger: [ECLogging loggerForContext: @"sample.specific"]];
  NSLog( @"Log as DEBUG----------->" );
  [self logWithLogger: [ECLogging loggerForContext: @"sample.moreSpecificA"]];
  NSLog( @"Log as ERROR----------->" );
  [self logWithLogger: [ECLogging loggerForContext: @"sample.moreSpecificB"]];
  NSLog( @"No Logger at all (--> root logger)----------->" );
  [self logWithLogger: [ECLogging loggerForContext: @"NotSpecifiedContext"]];

  NSLog( @"Log into file..." );
  [self logWithLogger: [ECLogging loggerForContext: @"sample.file"]];

  EC_AUTORELEASEPOOL_END
  NSLog( @"ECLoggingTEST::testLogging: FINISHED" );
  
  return self;
}


- testLoggingConfiguredViaXML {

  EC_AUTORELEASEPOOL_BEGIN
  NSLog( @"ECLoggingTEST::testLoggingConfiguredViaXML: BEGIN..." );

  [ECLoggingConfigurationFactory configureUsingXMLFile: @"./logging.xml"];
  
  NSLog( @"Run some logging tests..." );
  NSLog( @"Log as DEBUG----------->" );
  [self logWithLogger: [ECLogging loggerForContext: @"sample.A"]];
  NSLog( @"Log as INFO----------->" );
  [self logWithLogger: [ECLogging loggerForContext: @"sample.B"]];
  NSLog( @"Log using ROOT loger----------->" );
  [self logWithLogger: [ECLogging loggerForContext: @"NotSpecifiedContext"]];

  EC_AUTORELEASEPOOL_END

  return self;
}
@end
