Skip to content

Commit

Permalink
LogOutput with next
Browse files Browse the repository at this point in the history
  • Loading branch information
ikhvorost committed Aug 20, 2024
1 parent a349f9b commit ee617cf
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 177 deletions.
31 changes: 1 addition & 30 deletions Sources/DLog/DLog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,41 +86,12 @@ public class DLog: Log {
/// - Parameters:
/// - output: A target output object. If it is omitted the logger uses `stdout` by default.
///
public init(_ output: LogOutput? = .stdout, config: LogConfig = LogConfig(), metadata: Metadata = Metadata()) {
public init(_ output: LogOutput? = .textEmoji, config: LogConfig = LogConfig(), metadata: Metadata = Metadata()) {
self.output = output
super.init(logger: nil, category: "DLOG", config: config, metadata: metadata)
self.logger = self
}

/// Creates the logger instance with a list of linked outputs for both swift and objective-c code.
///
/// Swift:
///
/// let logger = DLog([.textPlain, .stdout])
///
/// Objective-C:
///
/// DLog* logger = [[DLog alloc] initWithOutputs:@[LogOutput.textPlain, filter, LogOutput.stdOut]];
///
/// - Parameters:
/// - outputs: An array of outputs.
///
@objc
public convenience init(outputs: [LogOutput]) {
var output: LogOutput?

if outputs.count == 0 {
output = .stdout
}
else {
output = outputs.count == 1
? outputs.first
: outputs.reduce(.textPlain, =>)
}

self.init(output)
}

/// Creates the default logger.
@objc
public convenience init() {
Expand Down
22 changes: 1 addition & 21 deletions Sources/DLog/File.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import Foundation

/// Target output for a file.
///
public class File : LogOutput {
public class File {
private let file: FileHandle?
private let queue = DispatchQueue(label: "File")

Expand Down Expand Up @@ -63,8 +63,6 @@ public class File : LogOutput {
if append {
file?.seekToEndOfFile()
}

super.init(source: source)
}

private func write(_ text: String?) -> String? {
Expand All @@ -77,22 +75,4 @@ public class File : LogOutput {
}
return text
}

// MARK: - LogOutput

override func log(item: LogItem) -> String? {
write(super.log(item: item))
}

override func scopeEnter(scope: LogScope) -> String? {
write(super.scopeEnter(scope: scope))
}

override func scopeLeave(scope: LogScope) -> String? {
write(super.scopeLeave(scope: scope))
}

override func intervalEnd(interval: LogInterval) -> String? {
write(super.intervalEnd(interval: interval))
}
}
40 changes: 23 additions & 17 deletions Sources/DLog/Filter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import Foundation
/// Middleware output for filtering
///
public class Filter: LogOutput {

private let isItem: ((LogItem) -> Bool)?
private let isScope: ((LogScope) -> Bool)?

Expand All @@ -44,32 +45,37 @@ public class Filter: LogOutput {
public init(isItem: ((LogItem) -> Bool)?, isScope: ((LogScope) -> Bool)?) {
self.isItem = isItem
self.isScope = isScope
super.init(source: nil)
}

// MARK: - LogOutput

override func log(item: LogItem) -> String? {
let text = super.log(item: item)
let included = isItem == nil || isItem?(item) == true
return included ? text : nil
override func log(item: LogItem) {
if isItem == nil || isItem?(item) == true {
super.log(item: item)
}
}

override func enter(scope: LogScope) {
if isScope == nil || isScope?(scope) == true {
super.enter(scope: scope)
}
}

override func scopeEnter(scope: LogScope) -> String? {
let text = super.scopeEnter(scope: scope)
let included = isScope == nil || isScope?(scope) == true
return included == true ? text : nil
override func leave(scope: LogScope) {
if isScope == nil || isScope?(scope) == true {
super.leave(scope: scope)
}
}

override func scopeLeave(scope: LogScope) -> String? {
let text = super.scopeLeave(scope: scope)
let included = isScope == nil || isScope?(scope) == true
return included ? text : nil
override func begin(interval: LogInterval) {
if isItem == nil || isItem?(interval) == true {
super.begin(interval: interval)
}
}

override func intervalEnd(interval: LogInterval) -> String? {
let text = super.intervalEnd(interval: interval)
let included = isItem == nil || isItem?(interval) == true
return included ? text : nil
override func end(interval: LogInterval) {
if isItem == nil || isItem?(interval) == true {
super.end(interval: interval)
}
}
}
4 changes: 2 additions & 2 deletions Sources/DLog/LogInterval.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public class LogInterval: LogItem {
time = Date()
duration = 0

logger.output?.intervalBegin(interval: self)
logger.output?.begin(interval: self)
}

/// Finish a time interval.
Expand Down Expand Up @@ -216,6 +216,6 @@ public class LogInterval: LogItem {
let metadata = Metadata.metadata(from: items, options: config.intervalConfig.options)
self.metadata = _metadata() + [metadata]

logger.output?.intervalEnd(interval: self)
logger.output?.end(interval: self)
}
}
2 changes: 1 addition & 1 deletion Sources/DLog/LogItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class LogLocation: NSObject {
/// A log type controls the conditions under which a message should be logged.
///
@objc
public enum LogType : Int {
public enum LogType: Int {
/// The default log level to capture non critical information.
case log

Expand Down
42 changes: 12 additions & 30 deletions Sources/DLog/LogOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,44 +73,26 @@ public class LogOutput : NSObject {
#endif

/// A source output.
public var source: LogOutput!
fileprivate var next: LogOutput?

init(source: LogOutput?) {
self.source = source
func log(item: LogItem) {
next?.log(item: item)
}

@discardableResult
func log(item: LogItem) -> String? {
return source != nil
? source.log(item: item)
: nil
func enter(scope: LogScope) {
next?.enter(scope: scope)
}

@discardableResult
func scopeEnter(scope: LogScope) -> String? {
return source != nil
? source.scopeEnter(scope: scope)
: nil
func leave(scope: LogScope) {
next?.leave(scope: scope)
}

@discardableResult
func scopeLeave(scope: LogScope) -> String? {
return source != nil
? source.scopeLeave(scope: scope)
: nil
func begin(interval: LogInterval) {
next?.begin(interval: interval)
}

func intervalBegin(interval: LogInterval) {
if source != nil {
source.intervalBegin(interval: interval)
}
}

@discardableResult
func intervalEnd(interval: LogInterval) -> String? {
return source != nil
? source.intervalEnd(interval: interval)
: nil
func end(interval: LogInterval) {
next?.end(interval: interval)
}
}

Expand All @@ -131,7 +113,7 @@ extension LogOutput {
/// let logger = DLog(.textEmoji => .stdout => .file("dlog.txt"))
///
public static func => (left: LogOutput, right: LogOutput) -> LogOutput {
right.source = left
left.next = right
return right
}
}
4 changes: 2 additions & 2 deletions Sources/DLog/LogScope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public class LogScope: Log {
duration = 0

ScopeStack.shared.append(self) {
logger.output?.scopeEnter(scope: self)
logger.output?.enter(scope: self)
}
}

Expand All @@ -130,7 +130,7 @@ public class LogScope: Log {
duration = -time.timeIntervalSinceNow

ScopeStack.shared.remove(self) {
logger.output?.scopeLeave(scope: self)
logger.output?.leave(scope: self)
}
}
}
23 changes: 2 additions & 21 deletions Sources/DLog/Net.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private class LogBuffer {
///
/// `NetConsole` service can be run from a command line on your machine and then the output connects and sends your log messages to it.
///
public class Net : LogOutput {
public class Net: NSObject {
private static let type = "_dlog._tcp"
private static let domain = "local."

Expand All @@ -85,8 +85,7 @@ public class Net : LogOutput {
/// - source: A source output (defaults to `.textColored`)
public init(name: String = "DLog", source: LogOutput = .textColored) {
self.name = name

super.init(source: source)
super.init()

browser.delegate = self
browser.searchForServices(ofType: Self.type, inDomain: Self.domain)
Expand Down Expand Up @@ -119,24 +118,6 @@ public class Net : LogOutput {
guard debug else { return }
print("[NetOutput] \(text)")
}

// MARK: - LogOutput

override func log(item: LogItem) -> String? {
send(super.log(item: item))
}

override func scopeEnter(scope: LogScope) -> String? {
send(super.scopeEnter(scope: scope))
}

override func scopeLeave(scope: LogScope) -> String? {
send(super.scopeLeave(scope: scope))
}

override func intervalEnd(interval: LogInterval) -> String? {
send(super.intervalEnd(interval: interval))
}
}

extension Net : NetServiceBrowserDelegate {
Expand Down
27 changes: 14 additions & 13 deletions Sources/DLog/OSLog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ public class OSLog : LogOutput {
///
public init(subsystem: String = "com.dlog.logger", source: LogOutput = .textPlain) {
self.subsystem = subsystem

super.init(source: source)
}

private func oslog(category: String) -> os.OSLog {
Expand All @@ -85,35 +83,37 @@ public class OSLog : LogOutput {

// MARK: - LogOutput

override func log(item: LogItem) -> String? {
override func log(item: LogItem) {
super.log(item: item)

let log = oslog(category: item.category)

let location = "<\(item.location.fileName):\(item.location.line)>"

assert(Self.types[item.type] != nil)
let type = Self.types[item.type]!
os_log("%{public}@ %{public}@", dso: Dynamic.dso, log: log, type: type, location, item.message)

return super.log(item: item)
}

override func scopeEnter(scope: LogScope) -> String? {
override func enter(scope: LogScope) {
super.enter(scope: scope)

if let os_activity_current = Dynamic.OS_ACTIVITY_CURRENT {
let activity = _os_activity_create(Dynamic.dso, strdup(scope.name), os_activity_current, OS_ACTIVITY_FLAG_DEFAULT)
os_activity_scope_enter(activity, &scope.os_state)
}
return super.scopeEnter(scope: scope)
}

override func scopeLeave(scope: LogScope) -> String? {
override func leave(scope: LogScope) {
super.leave(scope: scope)

if Dynamic.OS_ACTIVITY_CURRENT != nil {
os_activity_scope_leave(&scope.os_state);
}
return super.scopeLeave(scope: scope)
}

override func intervalBegin(interval: LogInterval) {
super.intervalBegin(interval: interval)
override func begin(interval: LogInterval) {
super.begin(interval: interval)

let log = oslog(category: interval.category)
if interval.signpostID == nil {
Expand All @@ -125,12 +125,13 @@ public class OSLog : LogOutput {
}
}

override func intervalEnd(interval: LogInterval) -> String? {
override func end(interval: LogInterval) {
super.end(interval: interval)

let log = oslog(category: interval.category)

if let name = interval.staticName {
os_signpost(.end, log: log, name: name, signpostID: interval.signpostID!)
}
return super.intervalEnd(interval: interval)
}
}
Loading

0 comments on commit ee617cf

Please sign in to comment.