diff --git a/Sources/DLog/File.swift b/Sources/DLog/File.swift index 9e96125..9cf8ae7 100644 --- a/Sources/DLog/File.swift +++ b/Sources/DLog/File.swift @@ -91,12 +91,12 @@ public class File: LogOutput { write("\(item)") } - override func begin(interval: LogInterval) { + override func begin(interval: LogInterval.Item) { super.begin(interval: interval) } - override func end(interval: LogInterval) { + override func end(interval: LogInterval.Item) { super.end(interval: interval) - write(interval.text()) + write("\(interval)") } } diff --git a/Sources/DLog/Filter.swift b/Sources/DLog/Filter.swift index 9254464..17be3c2 100644 --- a/Sources/DLog/Filter.swift +++ b/Sources/DLog/Filter.swift @@ -30,6 +30,7 @@ import Foundation public class Filter: LogOutput { private let itemHandler: ((LogItem) -> Bool)? + private let intervalHandler: ((LogInterval.Item) -> Bool)? private let scopeHandler: ((LogScope.Item) -> Bool)? /// Initializes a filter output that evaluates using a specified block object. @@ -42,9 +43,10 @@ public class Filter: LogOutput { /// - Parameters: /// - block: The block is applied to the object to be evaluated. /// - public init(itemHandler: ((LogItem) -> Bool)?, scopeHandler: ((LogScope.Item) -> Bool)?) { - self.itemHandler = itemHandler - self.scopeHandler = scopeHandler + public init(item: ((LogItem) -> Bool)? = nil, interval: ((LogInterval.Item) -> Bool)? = nil, scope: ((LogScope.Item) -> Bool)? = nil) { + self.itemHandler = item + self.intervalHandler = interval + self.scopeHandler = scope } // MARK: - LogOutput @@ -70,15 +72,15 @@ public class Filter: LogOutput { super.enter(item: item) } - override func begin(interval: LogInterval) { - guard itemHandler == nil || itemHandler?(interval) == true else { + override func begin(interval: LogInterval.Item) { + guard intervalHandler == nil || intervalHandler?(interval) == true else { return } super.begin(interval: interval) } - override func end(interval: LogInterval) { - guard itemHandler == nil || itemHandler?(interval) == true else { + override func end(interval: LogInterval.Item) { + guard intervalHandler == nil || intervalHandler?(interval) == true else { return } super.end(interval: interval) diff --git a/Sources/DLog/Log.swift b/Sources/DLog/Log.swift index 7751f6d..08148d8 100644 --- a/Sources/DLog/Log.swift +++ b/Sources/DLog/Log.swift @@ -29,7 +29,7 @@ import Foundation /// @objcMembers public class Log: NSObject { - var logger: DLog! + weak var logger: DLog! let category: String let config: LogConfig @@ -43,9 +43,9 @@ public class Log: NSObject { self.metadata = LogMetadata(data: metadata) } - private func scope() -> LogScope.Item? { + private func stack() -> [Bool]? { if let scope = self as? LogScope { - return scope.item + return scope.stack } return nil } @@ -54,7 +54,7 @@ public class Log: NSObject { guard let output = logger.output else { return nil } - let item = LogItem(message: message.text, type: type, category: category, config: config, scope: scope(), metadata: metadata, location: location) + let item = LogItem(message: message.text, type: type, category: category, config: config, stack: stack(), metadata: metadata, location: location) output.log(item: item) return item } @@ -277,7 +277,7 @@ public class Log: NSObject { return scope } - private func interval(name: String, staticName: StaticString?, intervalConfig: IntervalConfig?, location: LogLocation, closure: (() -> Void)?) -> LogInterval? { + private func interval(message: String, staticName: StaticString?, intervalConfig: IntervalConfig?, location: LogLocation, closure: (() -> Void)?) -> LogInterval? { guard logger.output != nil else { return nil } @@ -287,7 +287,7 @@ public class Log: NSObject { config.intervalConfig = intervalConfig } - let interval = LogInterval(logger: logger, name: name, staticName: staticName, category: category, config: config, scope: scope(), metadata: metadata.data, location: location) + let interval = LogInterval(logger: logger, message: message, staticName: staticName, category: category, config: config, stack: stack(), metadata: metadata.data, location: location) if let block = closure { interval.begin() block() @@ -316,12 +316,110 @@ public class Log: NSObject { /// @discardableResult public func interval(_ name: StaticString = "", config: IntervalConfig? = nil, fileID: String = #fileID, file: String = #file, function: String = #function, line: UInt = #line, closure: (() -> Void)? = nil) -> LogInterval? { - interval(name: "\(name)", staticName: name, intervalConfig: config, location: LogLocation(fileID, file, function, line), closure: closure) + interval(message: "\(name)", staticName: name, intervalConfig: config, location: LogLocation(fileID, file, function, line), closure: closure) } /// Creates an interval object that logs a detailed message with accumulated statistics. @discardableResult - public func interval(name: String, fileID: String, file: String, function: String, line: UInt, closure: (() -> Void)?) -> LogInterval? { - interval(name: name, staticName: nil, intervalConfig: nil, location: LogLocation(fileID, file, function, line), closure: closure) + public func interval(message: String, fileID: String, file: String, function: String, line: UInt, closure: (() -> Void)?) -> LogInterval? { + interval(message: message, staticName: nil, intervalConfig: nil, location: LogLocation(fileID, file, function, line), closure: closure) + } +} + +extension Log { + + public class Item: CustomStringConvertible { + public let time: Date + public let category: String + public let stack: [Bool]? + public let type: LogType + public let location: LogLocation + public let metadata: Metadata + public let message: String + public let config: LogConfig + + init(time: Date, category: String, stack: [Bool]?, type: LogType, location: LogLocation, metadata: Metadata, message: String, config: LogConfig) { + self.time = time + self.category = category + self.stack = stack + self.type = type + self.location = location + self.metadata = metadata + self.message = message + self.config = config + } + + func padding() -> String { + guard let stack else { + return "" + } + return stack + .map { $0 ? "| " : " " } + .joined() + .appending("├") + } + + func typeText() -> String { + let tag = LogItem.tags[self.type]! + return switch config.style { + case .plain: + "[\(type.title)]" + + case .colored: + "[\(type.title)]".color(tag.colors) + + case .emoji: + "\(type.icon) [\(type.title)]" + } + } + + func messageText() -> String { + let tag = LogItem.tags[self.type]! + return switch config.style { + case .plain, .emoji: + message + + case .colored: + message.color(tag.textColor) + } + } + + public var description: String { + var sign = "\(config.sign)" + var time = LogItem.dateFormatter.string(from: self.time) + var level = String(format: "[%02d]", self.stack?.count ?? 0) + var category = "[\(self.category)]" + var location = "<\(self.location.fileName):\(self.location.line)>" + var metadata = self.metadata.json() + + switch config.style { + case .plain, .emoji: + break + + case .colored: + let tag = LogItem.tags[self.type]! + + sign = sign.color(.dim) + time = time.color(.dim) + level = level.color(.dim) + category = category.color(.textBlue) + location = location.color([.dim, tag.textColor]) + metadata = metadata.color(.dim) + } + + let items: [(LogOptions, String)] = [ + (.sign, sign), + (.time, time), + (.level, level), + (.category, category), + (.padding, padding()), + (.type, typeText()), + (.location, location), + (.metadata, metadata), + (.message, messageText()), + ] + + return LogItem.logPrefix(items: items, options: config.options) + } } } diff --git a/Sources/DLog/LogConfig.swift b/Sources/DLog/LogConfig.swift index fa3bb76..104b8c0 100644 --- a/Sources/DLog/LogConfig.swift +++ b/Sources/DLog/LogConfig.swift @@ -46,7 +46,7 @@ public struct LogOptions: OptionSet { self.rawValue = rawValue } - static let scope = Self(-1) + static let message = Self(-1) /// Start sign public static let sign = Self(0) diff --git a/Sources/DLog/LogItem+Text.swift b/Sources/DLog/LogItem+Text.swift index 074d3ad..0db6b13 100644 --- a/Sources/DLog/LogItem+Text.swift +++ b/Sources/DLog/LogItem+Text.swift @@ -77,7 +77,7 @@ extension String { } } -private extension LogType { +extension LogType { static let icons: [LogType : String] = [ .log : "💬", .trace : "#️⃣", @@ -88,6 +88,8 @@ private extension LogType { .assert : "🅰️", .fault : "🆘", .interval : "🕒", + .scopeEnter: "⬇️", + .scopeLeave: "⬆️", ] var icon: String { @@ -104,6 +106,8 @@ private extension LogType { .assert : "ASSERT", .fault : "FAULT", .interval : "INTERVAL", + .scopeEnter : "SCOPE", + .scopeLeave : "SCOPE", ] var title: String { @@ -113,12 +117,12 @@ private extension LogType { extension LogItem { - private struct Tag { + struct Tag { let textColor: ANSIEscapeCode let colors: [ANSIEscapeCode] } - private static let tags: [LogType : Tag] = [ + static let tags: [LogType : Tag] = [ .log : Tag(textColor: .textWhite, colors: [.backgroundWhite, .textBlack]), .info : Tag(textColor: .textGreen, colors: [.backgroundGreen, .textWhite]), .trace : Tag(textColor: .textCyan, colors: [.backgroundCyan, .textBlack]), @@ -128,6 +132,8 @@ extension LogItem { .fault : Tag(textColor: .textRed, colors: [.backgroundRed, .textWhite, .blink]), .assert : Tag(textColor: .textRed, colors: [.backgroundRed, .textWhite]), .interval : Tag(textColor: .textGreen, colors: [.backgroundGreen, .textBlack]), + .scopeEnter : Tag(textColor: .textMagenta, colors: [.backgroundMagenta, .textBlack]), + .scopeLeave : Tag(textColor: .textMagenta, colors: [.backgroundMagenta, .textBlack]), ] static let dateFormatter: DateFormatter = { @@ -136,40 +142,34 @@ extension LogItem { return dateFormatter }() - static func logPrefix(items: [(type: LogOptions, text:() -> String)], options: LogOptions) -> String { + static func logPrefix(items: [(type: LogOptions, text: String)], options: LogOptions) -> String { items.compactMap { - guard options.contains($0.type) || $0.type == .scope else { + guard options.contains($0.type) || $0.type == .message else { return nil } - let text = $0.text() - return text.trimTrailingWhitespace() + return $0.text.trimTrailingWhitespace() } .joinedCompact() } func text() -> String { - var sign = { "\(self.config.sign)" } - var time = { Self.dateFormatter.string(from: self.time) } - var level = { String(format: "[%02d]", self.scope?.level ?? 0) } - var category = { "[\(self.category)]" } - let padding = { - self.scope?.stack + var sign = "\(config.sign)" + var time = Self.dateFormatter.string(from: self.time) + var level = String(format: "[%02d]", self.stack?.count ?? "") + var category = "[\(self.category)]" + let padding = self.stack? .map { $0 ? "| " : " " } .joined() .appending("├") ?? "" - } - var type = { "[\(self.type.title)]" } - var location = { "<\(self.location.fileName):\(self.location.line)>" } - var metadata = { - self.metadata + var type = "[\(self.type.title)]" + var location = "<\(self.location.fileName):\(self.location.line)>" + var metadata = self.metadata .filter { $0.isEmpty == false } .map { let pretty = self.type == .trace && self.config.traceConfig.style == .pretty return $0.json(pretty: pretty) } .joined(separator: " ") - } - var message = message switch config.style { @@ -180,31 +180,20 @@ extension LogItem { assert(Self.tags[self.type] != nil) let tag = Self.tags[self.type]! - let s = sign - sign = { s().color(.dim) } - - let t = time - time = { t().color(.dim) } - - let l = level - level = { l().color(.dim) } - - category = { self.category.color(.textBlue) } - type = { " \(self.type.title) ".color(tag.colors) } - - let loc = location - location = { loc().color([.dim, tag.textColor]) } - - let m = metadata - metadata = { m().color(.dim) } - + sign = sign.color(.dim) + time = time.color(.dim) + level = level.color(.dim) + category = category.color(.textBlue) + type = " \(self.type.title) ".color(tag.colors) + location = location.color([.dim, tag.textColor]) + metadata = metadata.color(.dim) message = message.color(tag.textColor) case .emoji: - type = { "\(self.type.icon) [\(self.type.title)]" } + type = "\(self.type.icon) [\(self.type.title)]" } - let items: [(LogOptions, () -> String)] = [ + let items: [(LogOptions, String)] = [ (.sign, sign), (.time, time), (.level, level), diff --git a/Sources/DLog/LogItem.swift b/Sources/DLog/LogItem.swift index 8a5a897..a49060c 100644 --- a/Sources/DLog/LogItem.swift +++ b/Sources/DLog/LogItem.swift @@ -91,6 +91,9 @@ public enum LogType: Int { /// The interval log level. case interval + + case scopeEnter + case scopeLeave } /// A base log message class that the logger adds to the logs. @@ -106,7 +109,7 @@ public class LogItem: NSObject { public let category: String /// The scope of this log message. - public let scope: LogScope.Item? + public let stack: [Bool]? /// The log level of this log message. public let type: LogType @@ -123,13 +126,15 @@ public class LogItem: NSObject { /// Metadata of log message public internal(set) lazy var metadata: [Metadata] = { _metadata() }() - init(message: String, type: LogType, category: String, config: LogConfig, scope: LogScope.Item?, metadata: @escaping () -> [Metadata], location: LogLocation) { + init(message: String, type: LogType, category: String, config: LogConfig, stack: [Bool]?, metadata: @escaping () -> [Metadata], location: LogLocation) { self.message = message self.type = type self.category = category self.config = config - self.scope = scope + self.stack = stack self._metadata = metadata self.location = location } } + + diff --git a/Sources/DLog/LogOutput.swift b/Sources/DLog/LogOutput.swift index d0bb14d..74004c7 100644 --- a/Sources/DLog/LogOutput.swift +++ b/Sources/DLog/LogOutput.swift @@ -54,10 +54,10 @@ public class LogOutput: NSObject { public static func oslog(subsystem: String) -> OSLog { OSLog(subsystem: subsystem) } /// Creates `Filter` output for log items. - public static func filter(item: @escaping (LogItem) -> Bool) -> Filter { Filter(itemHandler: item, scopeHandler: nil) } + public static func filter(item: @escaping (LogItem) -> Bool) -> Filter { Filter(item: item, scope: nil) } /// Creates `Filter` output for log scopes. - public static func filter(scope: @escaping (LogScope.Item) -> Bool) -> Filter { Filter(itemHandler: nil, scopeHandler: scope) } + public static func filter(scope: @escaping (LogScope.Item) -> Bool) -> Filter { Filter(item: nil, scope: scope) } /// Creates `File` output with a file path to write. public static func file(path: String, append: Bool = false) -> File { File(path: path, append: append) } @@ -95,11 +95,11 @@ public class LogOutput: NSObject { next?.leave(item: item) } - func begin(interval: LogInterval) { + func begin(interval: LogInterval.Item) { next?.begin(interval: interval) } - func end(interval: LogInterval) { + func end(interval: LogInterval.Item) { next?.end(interval: interval) } } diff --git a/Sources/DLog/LogScope.swift b/Sources/DLog/LogScope.swift index cc9270f..5a94425 100644 --- a/Sources/DLog/LogScope.swift +++ b/Sources/DLog/LogScope.swift @@ -28,7 +28,21 @@ import os.log fileprivate class Stack { + static let shared = Stack() + var scopes = [LogScope]() + + func stack(level: Int) -> [Bool] { + var stack = [Bool](repeating: false, count: level - 1) + scopes.map { $0.level } + .map { $0 - 1} + .forEach { + if $0 < stack.count { + stack[$0] = true + } + } + return stack + } } /// An object that represents a scope triggered by the user. @@ -36,55 +50,63 @@ fileprivate class Stack { /// Scope provides a mechanism for grouping log messages. /// public class LogScope: Log { - private static var stack = Stack() + public class Activity { + public var os_state = os_activity_scope_state_s() + } + + public class Item: Log.Item { + public let activity: Activity + public var level: Int + public var duration: TimeInterval + + init(time: Date, category: String, stack: [Bool], type: LogType, location: LogLocation, metadata: Metadata, message: String, config: LogConfig, activity: Activity, level: Int, duration: TimeInterval) { + self.activity = activity + self.level = level + self.duration = duration + + super.init(time: time, category: category, stack: stack, type: type, location: location, metadata: metadata, message: message, config: config) + } + + override func padding() -> String { + let text = super.padding() + return text.replacingOccurrences(of: "├", with: duration == 0 ? "┌" : "└") + } + + override func typeText() -> String { + let text = super.typeText() + return text.replacingOccurrences(of: "[SCOPE]", with: "[\(message)]") + } + + override func messageText() -> String { + duration == 0 + ? "" + : "{duration: \(stringFromTimeInterval(duration))}" + } + } private let activity = Activity() private var location: LogLocation + var start: Date? + public let name: String public fileprivate(set) var level = 0 - public fileprivate(set) var time = Date() public fileprivate(set) var duration: TimeInterval = 0 - var item: Item? { - synchronized(Self.stack) { - guard level > 0 else { - return nil - } - let stack = Self.stack(level: level) - return item(stack: stack) + var stack: [Bool]? { + synchronized(Stack.shared) { + level > 0 ? Stack.shared.stack(level: level) : nil } } - private static func stack(level: Int) -> [Bool] { - var stack = [Bool](repeating: false, count: level - 1) - Self.stack.scopes.map { $0.level } - .map { $0 - 1} - .forEach { - if $0 < stack.count { - stack[$0] = true - } - } - return stack - } - init(name: String, logger: DLog, category: String, config: LogConfig, metadata: Metadata?, location: LogLocation) { self.name = name self.location = location super.init(logger: logger, category: category, config: config, metadata: metadata ?? logger.metadata.data) } - private func item(stack: [Bool]) -> Item { - Item(name: name, - category: category, - level: level, - stack: stack, - time: time, - duration: duration, - config: config, - activity: activity, - metadata: metadata.data, - location: location) + private func item(type: LogType, stack: [Bool]) -> Item { + Item(time: Date(), category: category, stack: stack, type: type, location: location, metadata: metadata.data, message: name, config: config, activity: activity, level: level, duration: duration) } /// Start a scope. @@ -102,20 +124,20 @@ public class LogScope: Log { /// @objc public func enter(fileID: String = #fileID, file: String = #file, function: String = #function, line: UInt = #line) { - synchronized(Self.stack) { - guard Self.stack.scopes.contains(self) == false else { + synchronized(Stack.shared) { + guard Stack.shared.scopes.contains(self) == false else { return } - let level = (Self.stack.scopes.map { $0.level }.max() ?? 0) + 1 - let stack = Self.stack(level: level) - Self.stack.scopes.append(self) + let level = (Stack.shared.scopes.map { $0.level }.max() ?? 0) + 1 + let stack = Stack.shared.stack(level: level) + Stack.shared.scopes.append(self) self.level = level - time = Date() + start = Date() duration = 0 location = LogLocation(fileID, file, function, line) - let item = item(stack: stack) + let item = item(type: .scopeEnter, stack: stack) logger.output?.enter(item: item) } } @@ -135,95 +157,19 @@ public class LogScope: Log { /// @objc public func leave(fileID: String = #fileID, file: String = #file, function: String = #function, line: UInt = #line) { - synchronized(Self.stack) { - guard let index = Self.stack.scopes.firstIndex(of: self) else { + synchronized(Stack.shared) { + guard let index = Stack.shared.scopes.firstIndex(of: self) else { return } - Self.stack.scopes.remove(at: index) - let stack = Self.stack(level: level) + Stack.shared.scopes.remove(at: index) + let stack = Stack.shared.stack(level: level) - duration = -time.timeIntervalSinceNow + level = 0 + duration = -(start?.timeIntervalSinceNow ?? 0) location = LogLocation(fileID, file, function, line) - let item = item(stack: stack) + let item = item(type: .scopeLeave, stack: stack) logger.output?.leave(item: item) } } } - -extension LogScope { - - public class Activity { - public var os_state = os_activity_scope_state_s() - } - - public struct Item: CustomStringConvertible { - public let name: String - public let category: String - public let level: Int - public let stack: [Bool] - public let time: Date - public let duration: TimeInterval - public let config: LogConfig - public let activity: Activity - public let metadata: Metadata - public let location: LogLocation - - public var description: String { - let isStart = duration == 0 - - var sign = { "\(self.config.sign)" } - var time = isStart - ? LogItem.dateFormatter.string(from: time) - : LogItem.dateFormatter.string(from: time.addingTimeInterval(duration)) - var category = { "[\(self.category)]" } - var level = { String(format: "[%02d]", self.level) } - let padding: () -> String = { - self.stack - .map { $0 ? "| " : " " } - .joined() - .appending(isStart ? "┌" : "└") - } - var type = { "" } - let ms = isStart ? nil : "(\(stringFromTimeInterval(duration)))" - var scope = { "[\(name)] \(ms ?? "")" } - var location = { "<\(self.location.fileName):\(self.location.line)>" } - var metadata = { self.metadata.json() } - - switch config.style { - case .plain: - break - - case .colored: - sign = { "\(self.config.sign)".color(.dim) } - time = time.color(.dim) - level = { String(format: "[%02d]", self.level).color(.dim) } - category = { self.category.color(.textBlue) } - - let l = location - location = { l().color(.dim) } - - let m = metadata - metadata = { m().color(.dim) } - - scope = { "[\(name.color(.textMagenta))] \((ms ?? "").color(.dim))" } - - case .emoji: - type = { "\(isStart ? "⬇️" : "⬆️")" } - } - - let items: [(LogOptions, () -> String)] = [ - (.sign, sign), - (.time, { time }), - (.level, level), - (.category, category), - (.padding, padding), - (.type, type), - (.scope, scope), - (.location, location), - (.metadata, metadata) - ] - return LogItem.logPrefix(items: items, options: config.options) - } - } -} diff --git a/Sources/DLog/Net.swift b/Sources/DLog/Net.swift index 0113e4d..e353bc0 100644 --- a/Sources/DLog/Net.swift +++ b/Sources/DLog/Net.swift @@ -136,13 +136,13 @@ public class Net: LogOutput { send("\(item)") } - override func begin(interval: LogInterval) { + override func begin(interval: LogInterval.Item) { super.begin(interval: interval) } - override func end(interval: LogInterval) { + override func end(interval: LogInterval.Item) { super.end(interval: interval) - send(interval.text()) + send("\(interval)") } } diff --git a/Sources/DLog/Standard.swift b/Sources/DLog/Standard.swift index 0a252ab..25b39bb 100644 --- a/Sources/DLog/Standard.swift +++ b/Sources/DLog/Standard.swift @@ -72,12 +72,12 @@ public class Standard: LogOutput { echo("\(item)") } - override func begin(interval: LogInterval) { + override func begin(interval: LogInterval.Item) { super.begin(interval: interval) } - override func end(interval: LogInterval) { + override func end(interval: LogInterval.Item) { super.end(interval: interval) - echo(interval.text()) + echo("\(interval)") } } diff --git a/Sources/DLogObjC/DLog.m b/Sources/DLogObjC/DLog.m index cfa506f..0a1bbb5 100644 --- a/Sources/DLogObjC/DLog.m +++ b/Sources/DLogObjC/DLog.m @@ -7,68 +7,71 @@ return [NSString stringWithFormat:@"%@/%@", bundleName, fileName]; } - -@implementation Log (PropertyWrapper) - - -- (LogBlock)log { - return ^(NSString* text, NSString* fileID, NSString* file, NSString* func, NSUInteger line){ - return [self log:^{ return [[LogMessage alloc] initWithStringLiteral:text]; } fileID:fileID file:file function:func line:line]; - }; -} - -- (LogBlock)trace { - return ^(NSString* text, NSString* fileID, NSString* file, NSString* func, NSUInteger line){ - return [self trace:^{ return [[LogMessage alloc] initWithStringLiteral:text]; } fileID:fileID file:file function:func line:line]; - }; -} - -- (LogBlock)debug { - return ^(NSString* text, NSString* fileID, NSString* file, NSString* func, NSUInteger line){ - return [self debug:^{ return [[LogMessage alloc] initWithStringLiteral:text]; } fileID:fileID file:file function:func line:line]; - }; -} - -- (LogBlock)info { - return ^(NSString* text, NSString* fileID, NSString* file, NSString* func, NSUInteger line){ - return [self info:^{ return [[LogMessage alloc] initWithStringLiteral:text]; } fileID:fileID file:file function:func line:line]; - }; -} - -- (LogBlock)warning { - return ^(NSString* text, NSString* fileID, NSString* file, NSString* func, NSUInteger line){ - return [self warning:^{ return [[LogMessage alloc] initWithStringLiteral:text]; } fileID:fileID file:file function:func line:line]; - }; -} - -- (LogBlock)error { - return ^(NSString* text, NSString* fileID, NSString* file, NSString* func, NSUInteger line){ - return [self error:^{ return [[LogMessage alloc] initWithStringLiteral:text]; } fileID:fileID file:file function:func line:line]; - }; -} - -- (AssertBlock)assertion { - return ^(BOOL condition, NSString* text, NSString* fileID, NSString* file, NSString* func, NSUInteger line){ - return [self assert:^{ return condition; } : ^{ return [[LogMessage alloc] initWithStringLiteral:text]; } fileID:fileID file:file function:func line:line]; - }; -} - -- (LogBlock)fault { - return ^(NSString* text, NSString* fileID, NSString* file, NSString* func, NSUInteger line){ - return [self fault:^{ return [[LogMessage alloc] initWithStringLiteral:text]; } fileID:fileID file:file function:func line:line]; - }; -} - -- (ScopeBlock)scope { - return ^(NSString* name, void (^block)(LogScope*)){ - return [self scope:name metadata:nil closure:block]; - }; -} - -- (IntervalBlock)interval { - return ^(NSString* name, NSString* fileID, NSString* file, NSString* func, NSUInteger line, void (^block)()){ - return [self intervalWithName:name fileID:fileID file:file function:func line:line closure:block]; - }; -} - -@end +/* + + @implementation Log (PropertyWrapper) + + + - (LogBlock)log { + return ^(NSString* text, NSString* fileID, NSString* file, NSString* func, NSUInteger line){ + return [self log:^{ return [[LogMessage alloc] initWithStringLiteral:text]; } fileID:fileID file:file function:func line:line]; + }; + } + + - (LogBlock)trace { + return ^(NSString* text, NSString* fileID, NSString* file, NSString* func, NSUInteger line){ + return [self trace:^{ return [[LogMessage alloc] initWithStringLiteral:text]; } fileID:fileID file:file function:func line:line]; + }; + } + + - (LogBlock)debug { + return ^(NSString* text, NSString* fileID, NSString* file, NSString* func, NSUInteger line){ + return [self debug:^{ return [[LogMessage alloc] initWithStringLiteral:text]; } fileID:fileID file:file function:func line:line]; + }; + } + + - (LogBlock)info { + return ^(NSString* text, NSString* fileID, NSString* file, NSString* func, NSUInteger line){ + return [self info:^{ return [[LogMessage alloc] initWithStringLiteral:text]; } fileID:fileID file:file function:func line:line]; + }; + } + + - (LogBlock)warning { + return ^(NSString* text, NSString* fileID, NSString* file, NSString* func, NSUInteger line){ + return [self warning:^{ return [[LogMessage alloc] initWithStringLiteral:text]; } fileID:fileID file:file function:func line:line]; + }; + } + + - (LogBlock)error { + return ^(NSString* text, NSString* fileID, NSString* file, NSString* func, NSUInteger line){ + return [self error:^{ return [[LogMessage alloc] initWithStringLiteral:text]; } fileID:fileID file:file function:func line:line]; + }; + } + + - (AssertBlock)assertion { + return ^(BOOL condition, NSString* text, NSString* fileID, NSString* file, NSString* func, NSUInteger line){ + return [self assert:^{ return condition; } : ^{ return [[LogMessage alloc] initWithStringLiteral:text]; } fileID:fileID file:file function:func line:line]; + }; + } + + - (LogBlock)fault { + return ^(NSString* text, NSString* fileID, NSString* file, NSString* func, NSUInteger line){ + return [self fault:^{ return [[LogMessage alloc] initWithStringLiteral:text]; } fileID:fileID file:file function:func line:line]; + }; + } + + - (ScopeBlock)scope { + return ^(NSString* name, void (^block)(LogScope*)){ + return [self scope:name metadata:nil closure:block]; + }; + } + + - (IntervalBlock)interval { + return ^(NSString* name, NSString* fileID, NSString* file, NSString* func, NSUInteger line, void (^block)()){ + return [self intervalWithName:name fileID:fileID file:file function:func line:line closure:block]; + }; + } + + @end + + */ diff --git a/Sources/DLogObjC/include/DLog.h b/Sources/DLogObjC/include/DLog.h index c058a4b..546fda5 100644 --- a/Sources/DLogObjC/include/DLog.h +++ b/Sources/DLogObjC/include/DLog.h @@ -42,7 +42,7 @@ typedef LogItem* _Nullable (^_Nonnull LogBlock)(NSString* _Nullable, NSString* _Nonnull, NSString* _Nonnull, NSString* _Nonnull, NSUInteger); typedef LogItem* _Nullable (^_Nonnull AssertBlock)(BOOL, NSString* _Nullable, NSString* _Nonnull, NSString* _Nonnull, NSString* _Nonnull, NSUInteger); typedef LogScope* _Nonnull (^_Nonnull ScopeBlock)(NSString* _Nonnull, void (^_Nullable)(LogScope* _Nonnull)); -typedef LogInterval* _Nonnull (^_Nonnull IntervalBlock)(NSString* _Nonnull, NSString* _Nonnull, NSString* _Nonnull, NSString* _Nonnull, NSUInteger, void (^_Nullable)()); +//typedef LogInterval* _Nonnull (^_Nonnull IntervalBlock)(NSString* _Nonnull, NSString* _Nonnull, NSString* _Nonnull, NSString* _Nonnull, NSUInteger, void (^_Nullable)()); extern NSString* _Nonnull fileID(NSBundle* _Nonnull bundle, NSString* _Nonnull file); @@ -57,6 +57,6 @@ extern NSString* _Nonnull fileID(NSBundle* _Nonnull bundle, NSString* _Nonnull f @property (nonatomic, readonly) AssertBlock assertion; @property (nonatomic, readonly) LogBlock fault; @property (nonatomic, readonly) ScopeBlock scope; -@property (nonatomic, readonly) IntervalBlock interval; +//@property (nonatomic, readonly) IntervalBlock interval; @end