Skip to content

Commit

Permalink
feature: add isPaginateLogEnabled to SabyAppleLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
0xWOF committed Jan 23, 2024
1 parent bead451 commit 12eb237
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 22 deletions.
27 changes: 27 additions & 0 deletions Source/AppleLogger/Constant/LoggerConstant.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// LoggerConstant.swift
// SabyAppleLogger
//
// Created by WOF on 1/23/24.
//

import Foundation

struct LoggerConstant {
private static let paginateSize = 10

static let paginatedLog = { (message: String) in
stride(from: 0, to: message.count, by: paginateSize).map { point in
let start = message.index(
message.startIndex,
offsetBy: point
)
let end = message.index(
message.startIndex,
offsetBy: point + paginateSize,
limitedBy: message.endIndex
) ?? message.endIndex
return String(message[start..<end])
}
}
}
10 changes: 5 additions & 5 deletions Source/AppleLogger/Data/LogLevel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public enum LogLevel: Comparable, CaseIterable {
public var name: String {
switch self {
case .debug:
return "DEBUG"
return "Debug"
case .info:
return "INFO"
return "Info"
case .warning:
return "WARNING"
return "Warning"
case .error:
return "ERROR"
return "Error"
case .fault:
return "FAULT"
return "Fault"
}
}

Expand Down
12 changes: 10 additions & 2 deletions Source/AppleLogger/Data/LoggerSetting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@ public struct LoggerSetting {
/// A variable used for displaying `category` value in console
public let category: String

/// A variable used for paginate log because of `os_log`'s 1024 length limit
public let isPaginateLogEnabled: Bool

/// An internal variable used to execute `os_log`
var osLog: OSLog
let osLog: OSLog

public init(subsystem: String, category: String) {
public init(
subsystem: String,
category: String,
isPaginateLogEnabled: Bool = false
) {
self.subsystem = subsystem
self.category = category
self.isPaginateLogEnabled = isPaginateLogEnabled

self.osLog = OSLog(subsystem: subsystem, category: category)
}
Expand Down
45 changes: 34 additions & 11 deletions Source/AppleLogger/Implement/LogService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,50 @@
import OSLog

public protocol LogService {
var setting: LoggerSetting { get }
func log(level: LogLevel, _ message: String)
}

extension LogService {
fileprivate func log(
level: LogLevel,
_ message: String,
_ printBlock: (String) -> Void
) {
if setting.isPaginateLogEnabled {
let logs = LoggerConstant.paginatedLog(message)
let id = arc4random() % 1000000000

logs.enumerated().forEach { (index, log) in
printBlock(
"\(log)"
+ "\nlog={page=\(index + 1)/\(logs.count), id=\(id)}"
)
}
return
}

printBlock(message)
}
}

public struct OSLogService: LogService {
let osLog: OSLog
public let setting: LoggerSetting

public func log(level: LogLevel, _ message: String) {
self.sendLog("%s", log: self.osLog, type: level.osLogType, message)
}

private func sendLog(_ message: StaticString, log: OSLog, type: OSLogType, _ args: CVarArg) {
os_log(message, log: log, type: type, args)
}

init(_ osLog: OSLog) {
self.osLog = osLog
log(level: level, message) {
os_log("%{public}s", log: setting.osLog, type: level.osLogType, $0)
}
}
}

public struct PrintLogService: LogService {
public let setting: LoggerSetting

public func log(level: LogLevel, _ message: String) {
print("[AirBridge : \(level.name)] \(message)")
let message = "[\(setting.subsystem)/\(setting.category)/\(level.name)] \(message)"
log(level: level, message) {
print($0)
}
}
}
4 changes: 2 additions & 2 deletions Source/AppleLogger/Implement/OSLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public final class OSLogger: LoggerType {
public init(_ subsystem: String, category: String) {
let setting = LoggerSetting(subsystem: subsystem, category: category)
self.loggerSetting = setting
self.logService = OSLogService(setting.osLog)
self.logService = OSLogService(setting: setting)
}

public init(setting: LoggerSetting) {
self.loggerSetting = setting
self.logService = OSLogService(setting.osLog)
self.logService = OSLogService(setting: setting)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Source/AppleLogger/Implement/PrintLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public final class PrintLogger: LoggerType {

public init(_ subsystem: String, category: String) {
self.loggerSetting = LoggerSetting(subsystem: subsystem, category: category)
self.logService = PrintLogService()
self.logService = PrintLogService(setting: loggerSetting)
}

public init(setting: LoggerSetting) {
self.loggerSetting = setting
self.logService = PrintLogService()
self.logService = PrintLogService(setting: loggerSetting)
}
}

Expand Down
2 changes: 2 additions & 0 deletions Test/AppleLogger/LoggerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ fileprivate var defaultSetting: LoggerSetting {

// MARK: - Mocks for test
fileprivate class MockLogService: LogService {
let setting = defaultSetting

let expectation: XCTestExpectation?

func log(level: SabyAppleLogger.LogLevel, _ message: String) {
Expand Down

0 comments on commit 12eb237

Please sign in to comment.