0001    // Log.swift
0002    //
0003    // The MIT License (MIT)
0004    //
0005    // Copyright (c) 2015 Zewo
0006    //
0007    // Permission is hereby granted, free of charge, to any person obtaining a copy
0008    // of this software and associated documentation files (the "Software"), to deal
0009    // in the Software without restriction, including without limitation the rights
0010    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0011    // copies of the Software, and to permit persons to whom the Software is
0012    // furnished to do so, subject to the following conditions:
0013    //
0014    // The above copyright notice and this permission notice shall be included in all
0015    // copies or substantial portions of the Software.
0016    //
0017    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0018    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0019    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0020    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0021    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0022    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0023    // SOFTWARE.
0024    
0025    @_exported import File
0026    
0027    public final class Log {
0028        public struct Level
Log.swift:35
        public static let Trace   = Level(rawValue: 1 << 0)
Log.swift:36
        public static let Debug   = Level(rawValue: 1 << 1)
Log.swift:37
        public static let Info    = Level(rawValue: 1 << 2)
Log.swift:38
        public static let Warning = Level(rawValue: 1 << 3)
Log.swift:39
        public static let Error   = Level(rawValue: 1 << 4)
Log.swift:40
        public static let Fatal   = Level(rawValue: 1 << 5)
Log.swift:41
        public static let All     = Level(rawValue: ~0)
Log.swift:45
    public var levels: Level
Log.swift:49
    public init(stream: File = standardErrorStream, levels: Level = .All) {
Log.swift:58
    public func log(level: Level, item: Any, terminator: String = "\n", flush: Bool = true) {
: OptionSetType { 0029 public let rawValue
Log.swift:32
            self.rawValue = rawValue
: Int32 0030 0031 public init
Log.swift:35
        public static let Trace   = Level(rawValue: 1 << 0)
Log.swift:36
        public static let Debug   = Level(rawValue: 1 << 1)
Log.swift:37
        public static let Info    = Level(rawValue: 1 << 2)
Log.swift:38
        public static let Warning = Level(rawValue: 1 << 3)
Log.swift:39
        public static let Error   = Level(rawValue: 1 << 4)
Log.swift:40
        public static let Fatal   = Level(rawValue: 1 << 5)
Log.swift:41
        public static let All     = Level(rawValue: ~0)
(rawValue: Int32) { 0032 self.rawValue = rawValue 0033 } 0034 0035 public static let Trace
Log.swift:75
        log(.Trace, item: item, terminator: terminator, flush: flush)
= Level(rawValue: 1 << 0) 0036 public static let Debug
Log.swift:79
        log(.Debug, item: item, terminator: terminator, flush: flush)
= Level(rawValue: 1 << 1) 0037 public static let Info
Log.swift:83
        log(.Info, item: item, terminator: terminator, flush: flush)
= Level(rawValue: 1 << 2) 0038 public static let Warning
Log.swift:87
        log(.Warning, item: item, terminator: terminator, flush: flush)
= Level(rawValue: 1 << 3) 0039 public static let Error
Log.swift:91
        log(.Error, item: item, terminator: terminator, flush: flush)
= Level(rawValue: 1 << 4) 0040 public static let Fatal
Log.swift:95
        log(.Fatal, item: item, terminator: terminator, flush: flush)
= Level(rawValue: 1 << 5) 0041 public static let All = Level(rawValue: ~0) 0042 } 0043 0044 public var stream: File 0045 public var levels
Log.swift:59
        if levels.contains(level) {
: Level 0046 private let messageChannel = Channel<String>() 0047 private let once = Once() 0048 0049 public init(stream: File = standardErrorStream, levels: Level = .All) { 0050 self.stream = stream 0051 self.levels = levels 0052 } 0053 0054 deinit { 0055 messageChannel.close() 0056 } 0057 0058 public func log
Log.swift:75
        log(.Trace, item: item, terminator: terminator, flush: flush)
Log.swift:79
        log(.Debug, item: item, terminator: terminator, flush: flush)
Log.swift:83
        log(.Info, item: item, terminator: terminator, flush: flush)
Log.swift:87
        log(.Warning, item: item, terminator: terminator, flush: flush)
Log.swift:91
        log(.Error, item: item, terminator: terminator, flush: flush)
Log.swift:95
        log(.Fatal, item: item, terminator: terminator, flush: flush)
(level: Level, item: Any, terminator: String = "\n", flush: Bool = true) { 0059 if levels.contains(level) { 0060 once.runInBackground { 0061 for message in self.messageChannel { 0062 do { 0063 try self.stream.write(message) 0064 } catch { 0065 print("Log error: \(error)") 0066 print("Log message: \(message)") 0067 } 0068 } 0069 } 0070 messageChannel.send(String(item) + terminator) 0071 } 0072 } 0073 0074 public func trace(item: Any, terminator: String = "\n", flush: Bool = true) { 0075 log(.Trace, item: item, terminator: terminator, flush: flush) 0076 } 0077 0078 public func debug(item: Any, terminator: String = "\n", flush: Bool = true) { 0079 log(.Debug, item: item, terminator: terminator, flush: flush) 0080 } 0081 0082 public func info(item: Any, terminator: String = "\n", flush: Bool = true) { 0083 log(.Info, item: item, terminator: terminator, flush: flush) 0084 } 0085 0086 public func warning(item: Any, terminator: String = "\n", flush: Bool = true) { 0087 log(.Warning, item: item, terminator: terminator, flush: flush) 0088 } 0089 0090 public func error(item: Any, terminator: String = "\n", flush: Bool = true) { 0091 log(.Error, item: item, terminator: terminator, flush: flush) 0092 } 0093 0094 public func fatal(item: Any, terminator: String = "\n", flush: Bool = true) { 0095 log(.Fatal, item: item, terminator: terminator, flush: flush) 0096 } 0097 }