/*!
  @header ECLoggingConfiguration
  @abstract Module of Encore
 
  @availability OS X, GNUstep
  @copyright 2004, 2005, 2006 Oliver Langer

  Author: 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

  03.02.06 ola     initial version
  22.08.06 ola     license changed
  -------------------------------------------------------------------------
  </pre>
*/

#include <Encore/ECExceptions.h>
#include <Encore/ECLogging.h>
#include <Encore/ECLoggingConfiguration.h>
#include <Encore/ECDefaultLoggingFormatter.h>
#include <Encore/ECNSLogLoggingWriter.h>
#include <Encore/ECLogger.h>

@implementation ECLoggingConfiguration

- init {
  id tmp;
  
  self = [super init];
  
  self->loggingFormatter = nil;
  tmp = [[ECDefaultLoggingFormatter alloc] init];
  [self setLoggingFormatter: tmp];
  [tmp release];
  
  self->loggingWriter = nil;
  tmp = [[ECNSLogLoggingWriter alloc] init];
  [self setLoggingWriter: tmp];
  [tmp release];
  
  self->loggingLevel = LOGLEVEL_DEBUG;
  return self;
}


- (void) dealloc {
  [self->loggingFormatter release];
  [self->loggingWriter release];
  [self->logger release];
  
  [super dealloc];
}



- (ECLogger *) logger {
  if( nil == self->logger ) {
    self->logger = [[ECLogger alloc] 
      initWithLoggingLevel: self->loggingLevel
      withLoggingFormatter: self->loggingFormatter
      withLoggingWriter: self->loggingWriter];
  }
  
  return self->logger;
}


- (id <ECLoggingFormatter>) loggingFormatter {
  return self->loggingFormatter;
}


- (id <ECLoggingWriter>) loggingWriter {
  return self->loggingWriter;
}


- setLoggingFormatter: (id <ECLoggingFormatter>) aLoggingFormatter {
  id tmp;
  
  tmp = self->loggingFormatter;
  
  self->loggingFormatter = [aLoggingFormatter retain];
  
  [tmp release];
  
  return self;
}


- setLoggingLevel: (NSString *) level {
  self->loggingLevel = LOGLEVEL_UNKNOWN;
  
  if( NSOrderedSame == [level caseInsensitiveCompare: @"DEBUG"] ) {
    self->loggingLevel = LOGLEVEL_DEBUG; 
  } else
  if( NSOrderedSame == [level caseInsensitiveCompare: @"INFO"] ) {
    self->loggingLevel = LOGLEVEL_INFO; 
  } else
  if( NSOrderedSame == [level caseInsensitiveCompare: @"TRACE"] ) {
    self->loggingLevel = LOGLEVEL_TRACE; 
  } else
  if( NSOrderedSame == [level caseInsensitiveCompare: @"WARN"] ) {
    self->loggingLevel = LOGLEVEL_WARN; 
  } else
  if( NSOrderedSame == [level caseInsensitiveCompare: @"ERROR"] ) {
    self->loggingLevel = LOGLEVEL_ERROR; 
  } else
  if( NSOrderedSame == [level caseInsensitiveCompare: @"CRITICAL"] ) {
    self->loggingLevel = LOGLEVEL_CRITICAL; 
  } else
  if( NSOrderedSame == [level caseInsensitiveCompare: @"FATAL"] ) {
    self->loggingLevel = LOGLEVEL_FATAL; 
  }
  
  if( LOGLEVEL_UNKNOWN == self->loggingLevel ) {
    [[[ECIllegalArgumentException alloc]
      initWithArgumentInfo: 
        [NSString stringWithFormat:
          @"ECLoggingConfiguration::setLoggingLevel: Unknown logging level %@",
          level]]
      raise];
  }
  
  return self;
}


- setLoggingWriter: (id <ECLoggingWriter>) aLoggingWriter {
  id tmp;
  
  tmp = self->loggingWriter;  
  self->loggingWriter = [aLoggingWriter retain];
  [tmp release];
  
  return self;
}

@end
