<commit_before>//
//  Logger.swift
//  ELLog
//
//  Created by <NAME> on 3/15/15.
//  Copyright (c) 2015 WalmartLabs. All rights reserved.
//

import Foundation

/**
Logging Level option flags.
*/
public struct LogLevel: OptionSetType, BooleanType, CustomStringConvertible {
    /// Logging disabled.
    public let rawValue: UInt
    
    public static let None = LogLevel(rawValue: 0)
    
    /// Error logging enabled.
    public static let Error = LogLevel(rawValue: 1 << 0)
    
    /// Debug logging enabled.
    public static let Debug = LogLevel(rawValue: 1 << 1)
    
    /// Info logging enabled.
    public static let Info = LogLevel(rawValue: 1 << 2)
    
    /// Verbose logging enabled.
    public static let Verbose = LogLevel(rawValue: 1 << 3)
    
    /// All logging enabled.
    public static let All:LogLevel = [.Error, .Debug, .Info, .Verbose]

    /// Returns a string representation of the current logging level(s).
    public var description: String {
        var options: Array<String> = []

        if self == .None {
            return "NONE"
        }
        
        if contains(LogLevel.All) {
            return "ALL"
        }
        
        if contains(.Error) {
            options.append("ERROR")
        }

        if contains(.Debug) {
            options.append("DEBUG")
        }

        if contains(.Info) {
            options.append("INFO")
        }

        if contains(.Verbose) {
            options.append("VERBOSE")
        }

        return options.joinWithSeparator(", ")
    }

    public init(rawValue: UInt) { self.rawValue = rawValue }
    
    // BooleanType
    public var boolValue: Bool { return rawValue  != 0 }
}

/**
Logger class.

Logs messages to the added destinations based on LogLevel flags.
*/
@objc(ELLogger)
public class Logger: NSObject {

    /**
    The default logger instance.  This is typically a LogConsoleDestination with a log level of .Debug.
    */
    public static let defaultInstance = loggerDefault()
    
    /**
    Allows this logger to be enabled/disabled.
    */
    public var enabled: Bool {
        get {
            objc_sync_enter(self)
            let value = _enabled
            objc_sync_exit(self)
            return value
        }
        set(value) {
            objc_sync_enter(self)
            _enabled = value
            objc_sync_exit(self)
        }
    }
    private var _enabled = false
    
    public override init() {
        super.init()
        let console = LogConsoleDestination(level: [.Debug, .Error])
        addDestination(console)
    }

    /**
    Dispatches the provided log information to the logging destination.
    */
    public func log(level: LogLevel, message: String, function: String = __FUNCTION__, filename: String = __FILE__, line: UInt = __LINE__) {
        let detail = LogDetail()
        detail.date = NSDate()
        detail.message = message
        detail.level = level.rawValue
        detail.function = function
        detail.filename = filename
        detail.line = line

        log(detail)
    }

    /**
    Add a new destination to this Logger instance.  This appends another destination
    to the list.  Only messages with a log level matching what this destination consumes
    will be sent here.
    <commit_msg>Somehow enabled was defaulted to false.<commit_after>//
//  Logger.swift
//  ELLog
//
//  Created by <NAME> on 3/15/15.
//  Copyright (c) 2015 WalmartLabs. All rights reserved.
//

import Foundation

/**
Logging Level option flags.
*/
public struct LogLevel: OptionSetType, BooleanType, CustomStringConvertible {
    /// Logging disabled.
    public let rawValue: UInt
    
    public static let None = LogLevel(rawValue: 0)
    
    /// Error logging enabled.
    public static let Error = LogLevel(rawValue: 1 << 0)
    
    /// Debug logging enabled.
    public static let Debug = LogLevel(rawValue: 1 << 1)
    
    /// Info logging enabled.
    public static let Info = LogLevel(rawValue: 1 << 2)
    
    /// Verbose logging enabled.
    public static let Verbose = LogLevel(rawValue: 1 << 3)
    
    /// All logging enabled.
    public static let All:LogLevel = [.Error, .Debug, .Info, .Verbose]

    /// Returns a string representation of the current logging level(s).
    public var description: String {
        var options: Array<String> = []

        if self == .None {
            return "NONE"
        }
        
        if contains(LogLevel.All) {
            return "ALL"
        }
        
        if contains(.Error) {
            options.append("ERROR")
        }

        if contains(.Debug) {
            options.append("DEBUG")
        }

        if contains(.Info) {
            options.append("INFO")
        }

        if contains(.Verbose) {
            options.append("VERBOSE")
        }

        return options.joinWithSeparator(", ")
    }

    public init(rawValue: UInt) { self.rawValue = rawValue }
    
    // BooleanType
    public var boolValue: Bool { return rawValue  != 0 }
}

/**
Logger class.

Logs messages to the added destinations based on LogLevel flags.
*/
@objc(ELLogger)
public class Logger: NSObject {

    /**
    The default logger instance.  This is typically a LogConsoleDestination with a log level of .Debug.
    */
    public static let defaultInstance = loggerDefault()
    
    /**
    Allows this logger to be enabled/disabled.
    */
    public var enabled: Bool {
        get {
            objc_sync_enter(self)
            let value = _enabled
            objc_sync_exit(self)
            return value
        }
        set(value) {
            objc_sync_enter(self)
            _enabled = value
            objc_sync_exit(self)
        }
    }
    private var _enabled = true
    
    public override init() {
        super.init()
        let console = LogConsoleDestination(level: [.Debug, .Error])
        addDestination(console)
    }

    /**
    Dispatches the provided log information to the logging destination.
    */
    public func log(level: LogLevel, message: String, function: String = __FUNCTION__, filename: String = __FILE__, line: UInt = __LINE__) {
        let detail = LogDetail()
        detail.date = NSDate()
        detail.message = message
        detail.level = level.rawValue
        detail.function = function
        detail.filename = filename
        detail.line = line

        log(detail)
    }

    /**
    Add a new destination to this Logger instance.  This appends another destination
    to the list.  Only messages with a log level matching what this destination consumes
    will be sent here.
    
{'fraction_non_alphanumeric': 0.0935361216730038, 'fraction_numerical': 0.005779467680608365, 'mean_word_length': 1.9528513695554557, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6392208', 'n_tokens_mistral': 1856, 'n_tokens_neox': 1799, 'n_words': 577}