-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f6ec99
commit b0dffad
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import ProjectDescription | ||
import ProjectDescriptionHelpers | ||
|
||
@_spi(PomoNyang) | ||
import DependencyPlugin | ||
|
||
let project: Project = .project( | ||
module: PomoNyang.Shared.Logger, | ||
scripts: [], | ||
targets: [ | ||
.sources | ||
], | ||
dependencies: [] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// Logger.swift | ||
// Logger | ||
// | ||
// Created by devMinseok on 7/19/24. | ||
// Copyright © 2024 PomoNyang. All rights reserved. | ||
// | ||
|
||
import OSLog | ||
|
||
public enum LogCategory: String { | ||
case network = "Network" | ||
case ui = "UI" | ||
case database = "Database" | ||
case analytics = "Analytics" | ||
case performance = "Performance" | ||
case etc = "ETC" | ||
case web = "WEB" | ||
} | ||
|
||
public final class Logger { | ||
public static let shared = Logger() | ||
|
||
public private(set) var logs: [String] = [] | ||
|
||
private init() {} | ||
|
||
#if DEBUG | ||
private let appIdentifier = Bundle.main.bundleIdentifier ?? "???" | ||
private let logsAccessQueue = DispatchQueue(label: "com.kimcaddie.logger.logsAccessQueue", attributes: .concurrent) | ||
|
||
private func logger(category: LogCategory) -> os.Logger { | ||
return os.Logger(subsystem: appIdentifier, category: category.rawValue) | ||
} | ||
|
||
public func log(level: OSLogType = .default, category: LogCategory = .etc, _ string: @autoclosure () -> String) { | ||
let logMessage = string() | ||
if ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" { | ||
print("\(logMessage)") | ||
} else { | ||
self.logger(category: category).log(level: level, "\(logMessage)") | ||
} | ||
appendLog(logMessage) | ||
} | ||
|
||
public func clear() { | ||
logsAccessQueue.async(flags: .barrier) { | ||
self.logs = [] | ||
} | ||
} | ||
|
||
private func appendLog(_ logMessage: String) { | ||
logsAccessQueue.async(flags: .barrier) { | ||
self.logs.append(logMessage) | ||
} | ||
} | ||
#else | ||
@inlinable @inline(__always) | ||
public func log(level: OSLogType = .default, category: LogCategory = .etc, _ string: @autoclosure () -> String) {} | ||
|
||
@inlinable @inline(__always) | ||
public func clear() {} | ||
#endif | ||
} |