/*!
  @header ECLogger
  @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

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

#include <Encore/ECLogger.h>

@implementation ECLogger
- initWithLoggingLevel: (ECLoggingLevel) aLoggingLevel
  withLoggingFormatter: (id <ECLoggingFormatter>) aLoggingFormatter
  withLoggingWriter: (id <ECLoggingWriter>) aLoggingWriter {
  self = [super init];
  
  self->loggingLevel = aLoggingLevel;
  self->loggingFormatter = [aLoggingFormatter retain];
  self->loggingWriter = [aLoggingWriter retain];
  
  return self;
}


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


- critical: (NSString *) msgFormat, ... {
  va_list args;
  va_start( args, msgFormat );
  
  if( [self isCriticalEnabled] ) {
    [self logUsingLevel: LOGLEVEL_CRITICAL
      withMessage: msgFormat
      withArguments: args];
  }
  
  va_end( args );
  
  return self;
}


- debug: (NSString *) msgFormat, ... {
  va_list args;
  va_start( args, msgFormat );
  
  if( [self isDebugEnabled] ) {
    [self logUsingLevel: LOGLEVEL_DEBUG
      withMessage: msgFormat
      withArguments: args];
  }
  
  va_end( args );
  
  return self;
}


- error: (NSString *) msgFormat, ... {
  va_list args;
  va_start( args, msgFormat );
  
  if( [self isErrorEnabled] ) {
    [self logUsingLevel: LOGLEVEL_ERROR
      withMessage: msgFormat
      withArguments: args];
  }
  
  va_end( args );
  
  return self;
}


- fatal: (NSString *) msgFormat, ... {
  va_list args;
  va_start( args, msgFormat );
  
  [self logUsingLevel: LOGLEVEL_FATAL
    withMessage: msgFormat
    withArguments: args];
  
  va_end( args );
  
  return self;
}


- info: (NSString *) msgFormat, ... {
  va_list args;
  va_start( args, msgFormat );
  
  if( [self isInfoEnabled] ) {
    [self logUsingLevel: LOGLEVEL_INFO
      withMessage: msgFormat
      withArguments: args];
  }
  
  va_end( args );
  
  return self;
}


- (BOOL) isDebugEnabled {
  return self->loggingLevel <= LOGLEVEL_DEBUG;
}


- (BOOL) isTraceEnabled {
  return self->loggingLevel <= LOGLEVEL_TRACE;
}


- (BOOL) isInfoEnabled {
  return self->loggingLevel <= LOGLEVEL_INFO;
}


- (BOOL) isWarnEnabled {
  return self->loggingLevel <= LOGLEVEL_WARN;
}


- (BOOL) isErrorEnabled {
  return self->loggingLevel <= LOGLEVEL_ERROR;
}


- (BOOL) isCriticalEnabled {
  return self->loggingLevel <= LOGLEVEL_CRITICAL;
}


- logUsingLevel: (ECLoggingLevel) aLevel
  withMessage: (NSString *) aMsg
  withArguments: (va_list) args {
  
  NSString *formattedOutput;
  
  formattedOutput = [self->loggingFormatter 
    formatLoggingOutputForLevel: aLevel
    format: aMsg
    arguments: args];
    
  if( nil != formattedOutput ) {
    [self->loggingWriter writeLog: formattedOutput];
  }
  
  return self;
}


- warn: (NSString *) msgFormat, ... {
  va_list args;
  va_start( args, msgFormat );
  
  if( [self isWarnEnabled] ) {
    [self logUsingLevel: LOGLEVEL_WARN
      withMessage: msgFormat
      withArguments: args];
  }
  
  va_end( args );
  
  return self;
}


- trace: (NSString *) msgFormat, ... {
  va_list args;
  va_start( args, msgFormat );
  
  if( [self isTraceEnabled] ) {
    [self logUsingLevel: LOGLEVEL_TRACE
      withMessage: msgFormat
      withArguments: args];
  }
  
  va_end( args );
  
  return self;
}
@end
