/*
  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"

@interface NGLogger (PrivateAPI)
- (void)logLevel:(NGLogLevel)_level message:(NSString *)_msg;
- (void)logLevel:(NGLogLevel)_level withFormat:(NSString *)_fmt, ...;
@end

@implementation NGLogger

static Class NSStringClass = Nil;

+ (void)initialize {
  static BOOL didInit = NO;
  if (didInit)
    return;
  didInit       = YES;
  NSStringClass = [NSString class];
}

+ (id)defaultLogger {
  static NGLogger *logger = nil; // THREAD
  
  if (logger == nil)
    logger = [[self alloc] init];
  return logger;
}

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

- (id)initWithLogLevel:(NGLogLevel)_level appender:(id)_appenderParam {
  if ((self = [super init]) != nil) {
    [self setLogLevel:_level];
    self->_appender = [_appenderParam retain]; // TODO: fix ivar name
  }
  return self;
}

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

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

/* accessors */

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

/* logging */

- (void)debugWithFormat:(NSString *)_fmt arguments:(va_list)_va {
  NSString *msg;

  if (self->logLevel < NGLogLevelDebug) return;

  msg = [[NSStringClass alloc] initWithFormat:_fmt arguments:_va];
  [self logLevel:NGLogLevelDebug message:msg];
  [msg release];
}

- (void)logWithFormat:(NSString *)_fmt arguments:(va_list)_va {
  NSString *msg;

  if (self->logLevel < NGLogLevelInfo) return;

  msg = [[NSStringClass alloc] initWithFormat:_fmt arguments:_va];
  [self logLevel:NGLogLevelInfo message:msg];
  [msg release];
}

- (void)warnWithFormat:(NSString *)_fmt arguments:(va_list)_va {
  NSString *msg;

  if (self->logLevel < NGLogLevelWarn) return;

  msg = [[NSStringClass alloc] initWithFormat:_fmt arguments:_va];
  [self logLevel:NGLogLevelWarn message:msg];
  [msg release];
}

- (void)errorWithFormat:(NSString *)_fmt arguments:(va_list)_va {
  NSString *msg;

  if (self->logLevel < NGLogLevelError) return;

  msg = [[NSStringClass alloc] initWithFormat:_fmt arguments:_va];
  [self logLevel:NGLogLevelError message:msg];
  [msg release];
}

- (void)fatalWithFormat:(NSString *)_fmt arguments:(va_list)_va {
  NSString *msg;

  if (self->logLevel < NGLogLevelFatal) return;

  msg = [[NSStringClass alloc] initWithFormat:_fmt arguments:_va];
  [self logLevel:NGLogLevelFatal 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)isDebuggingEnabled {
  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 */
