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

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

#include <Encore/ECDefaultLoggingFormatter.h>

@implementation ECDefaultLoggingFormatter

+ (NSString *) defaultLoggingFormatterFormat {
  return ECDefaultLoggingFormatterFormat;
}


- init {
  self = [super init];

  self->parameterEvaluater = [[ECDefaultParameterEvaluaterImpl alloc] init];
  self->parameterString = [[ECParameterString alloc]
    initWithFormat: ECDefaultLoggingFormatterFormat
    parameterEvaluater: self->parameterEvaluater];

  return self;
}


- (void) dealloc {
  [self->parameterEvaluater release];
  [self->parameterString release];

  [super dealloc];
}


- setOutputFormat: (NSString *) anOutputFormat {
  id tmp;
  
  tmp = self->parameterString;
  
  self->parameterString = [[ECParameterString alloc]
    initWithFormat: anOutputFormat
    parameterEvaluater: self->parameterEvaluater];

  [tmp release];
  
  return self;
}


- (NSString *) formatLoggingOutputForLevel: (ECLoggingLevel) loggingLevel
  format: (NSString *) formatString arguments: (va_list) args {
  NSString *paramStr;
  NSString *msg;
  
  switch( loggingLevel ) {
    case LOGLEVEL_TRACE:
      [self->parameterEvaluater setValue: @"TRACE" forVariable: @"__LEVEL__"];
      break;
    case LOGLEVEL_DEBUG:
      [self->parameterEvaluater setValue: @"DEBUG" forVariable: @"__LEVEL__"];
      break;
    case LOGLEVEL_INFO:
      [self->parameterEvaluater setValue: @"INFO" forVariable: @"__LEVEL__"];
      break;
    case LOGLEVEL_WARN:
      [self->parameterEvaluater setValue: @"WARN" forVariable: @"__LEVEL__"];
      break;
    case LOGLEVEL_ERROR:
      [self->parameterEvaluater setValue: @"ERROR" forVariable: @"__LEVEL__"];
      break;
    case LOGLEVEL_FATAL:
      [self->parameterEvaluater setValue: @"FATAL" forVariable: @"__LEVEL__"];
    default:
      [self->parameterEvaluater setValue: @"UNKNOWN LEVEL" 
        forVariable: @"__LEVEL__"];
  }

  [self->parameterEvaluater setValue: [[NSDate date] description]
    forVariable: @"__DATE__"];

  msg = [[NSString alloc] initWithFormat: formatString
    arguments: args];
    
  [self->parameterEvaluater setValue: msg
    forVariable: @"__MESSAGE__"];

  [msg release];
  
  paramStr = [self->parameterString substituteVariables];

  return [[[NSMutableString alloc] initWithFormat: paramStr
    arguments: args] autorelease];
}
@end
