/*
  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 "NSObject+ExtendedLogging.h"
#include "NGLogger.h"
#include "NGLoggerManager.h"
#include "common.h"

@implementation NSObject(NGExtendedLogging)

- (id)logger {
  static id   sharedLogger = nil;
  static BOOL shouldLog    = YES;
  
  if (sharedLogger == nil && shouldLog) {
    NGLoggerManager *lm;
    
    lm           = [NGLoggerManager defaultLoggerManager];
    sharedLogger = [lm loggerForDefaultKey:@"NGDefaultLogLevel"];
    if (!sharedLogger)
      shouldLog = NO;
  }
  return sharedLogger;
}

/* log methods */

- (void)logDebugWithFormat:(NSString *)_fmt, ... {
  NSString *msg;
  va_list va;
  
  va_start(va, _fmt);
  msg = [[NSString alloc] initWithFormat:_fmt arguments:va];
  va_end(va);
  [self logLevel:NGLogLevelDebug message:msg];
  [msg release];
}

- (void)logInfoWithFormat:(NSString *)_fmt, ... {
  NSString *msg;
  va_list va;
  
  va_start(va, _fmt);
  msg = [[NSString alloc] initWithFormat:_fmt arguments:va];
  va_end(va);
  [self logLevel:NGLogLevelInfo message:msg];
  [msg release];
}

- (void)logWarnWithFormat:(NSString *)_fmt, ... {
  NSString *msg;
  va_list va;
  
  va_start(va, _fmt);
  msg = [[NSString alloc] initWithFormat:_fmt arguments:va];
  va_end(va);
  [self logLevel:NGLogLevelWarn message:msg];
  [msg release];
}

- (void)logErrorWithFormat:(NSString *)_fmt, ... {
  NSString *msg;
  va_list va;
  
  va_start(va, _fmt);
  msg = [[NSString alloc] initWithFormat:_fmt arguments:va];
  va_end(va);
  [self logLevel:NGLogLevelError message:msg];
  [msg release];
}

- (void)logFatalWithFormat:(NSString *)_fmt, ... {
  NSString *msg;
  va_list va;
  
  va_start(va, _fmt);
  msg = [[NSString alloc] initWithFormat:_fmt arguments:va];
  va_end(va);
  [self logLevel:NGLogLevelFatal message:msg];
  [msg release];
}

- (void)logLevel:(NGLogLevel)_level message:(NSString *)_msg {
  [[self logger] logLevel:_level message:_msg];
}

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

/* log conditions (do not use, slow ...) */

- (BOOL)isLogDebugEnabled {
  return [[self logger] isLogDebugEnabled];
}

- (BOOL)isLogInfoEnabled {
  return [[self logger] isLogInfoEnabled];
}

- (BOOL)isLogWarnEnabled {
  return [[self logger] isLogWarnEnabled];
}

- (BOOL)isLogErrorEnabled {
  return [[self logger] isLogErrorEnabled];
}

- (BOOL)isLogFatalEnabled {
  return [[self logger] isLogFatalEnabled];
}

@end /* NSObject(NGExtendedLogging) */
