/*
  Copyright (C) 2004 SKYRIX Software AG
  
  This file is part of OpenGroupware.org.
  
  OGo 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
  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
  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  02111-1307, USA.
*/

#include "NGLogger.h"
#include "common.h"
#include "NGLogEvent.h"
#include "NGLogAppender.h"

@implementation NGLogger

static Class StrClass = Nil;

+ (void)initialize {
  StrClass = [NSString class];
}

- (id)init {
  return [self initWithLogLevel:NGLogLevelAll];
}

- (id)initWithLogLevel:(NGLogLevel)_level {
  if ((self = [super init])) {
    NSUserDefaults *ud;
    NSString *appenderClassName;
    
    [self setLogLevel:_level];
    
    // TODO: remove this as soon as we have a config
    ud = [NSUserDefaults standardUserDefaults];
    appenderClassName = [ud stringForKey:@"NGLogDefaultAppenderClass"];
    if (appenderClassName == nil)
      appenderClassName = @"NGLogConsoleAppender";
    self->_appender = [[NSClassFromString(appenderClassName) alloc] init];
  }
  return self;
}

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

/* accessors */

- (void)setLogLevel:(NGLogLevel)_level {
  self->logLevel = _level;
}

- (NGLogLevel)logLevel {
  return self->logLevel;
}

/* logging */

- (void)logLevel:(NGLogLevel)_level withFormat:(NSString *)_fmt, ... {
  NSString *msg;
  va_list va;
  
  if(self->logLevel > _level)
    return;
  
  va_start(va, _fmt);
  msg = [[StrClass alloc] initWithFormat:_fmt arguments:va];
  va_end(va);
  [self logLevel:_level message:msg];
  [msg release];
}

- (void)logLevel:(NGLogLevel)_level message:(NSString *)_msg {
  NGLogEvent *event;

  event = [[NGLogEvent alloc] initWithLevel:_level message:_msg];
  
  // iterate appenders
  // TODO: as soon as we have more appenders, we need to iterate on them
  [self->_appender appendLogEvent:event];
  [event release];
}

/* log conditions */

- (BOOL)isLogDebugEnabled {
  return self->logLevel >= NGLogLevelDebug;
}

- (BOOL)isLogInfoEnabled {
  return self->logLevel >= NGLogLevelInfo;
}

- (BOOL)isLogWarnEnabled {
  return self->logLevel >= NGLogLevelWarn;
}

- (BOOL)isLogErrorEnabled {
  return self->logLevel >= NGLogLevelError;
}

- (BOOL)isLogFatalEnabled {
  return self->logLevel >= NGLogLevelFatal;
}

@end /* NGLogger */
