Skip to content

Commit 2b2fb73

Browse files
committed
log freestanding 추가
1 parent d185d07 commit 2b2fb73

File tree

7 files changed

+99
-6
lines changed

7 files changed

+99
-6
lines changed

Sources/MacroKit/ExpressionMacros.swift

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@_exported import os.log
2+
13
/// A macro that produces both a value and a string containing the
24
/// source code that generated the value. For example,
35
///
@@ -6,3 +8,6 @@
68
/// produces a tuple `(x + y, "x + y")`.
79
@freestanding(expression)
810
public macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "Macros", type: "StringifyMacro")
11+
12+
@freestanding(expression)
13+
public macro log(level: OSLogType, _ message: String) = #externalMacro(module: "Macros", type: "LoggingExpressionMacros")

Sources/MacroKit/MemberMacros.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public macro Logging() = #externalMacro(module: "Macros", type: "LoggingMacro")
1818
// MARK: - Helper
1919

2020
public enum LoggingMacroHelper {
21-
public static func generateLogger(_ fileID: String = #fileID, category: String) -> Logger {
21+
public static func generateOSLog(_ fileID: String = #fileID, category: String) -> OSLog {
2222
let subsystem = fileID.components(separatedBy: "/").first.map { "kr.minsone.\($0)" }
23-
return subsystem.map { Logger(subsystem: $0, category: category) }
24-
?? Logger()
23+
return subsystem.flatMap { OSLog(subsystem: $0, category: category) }
24+
?? .default
2525
}
2626
}

Sources/MacroKitClient/main.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ print("cat is \(cat.isCat)")
3232
@Logging
3333
class LogModel {
3434
func log() {
35-
logger.debug("Msg")
35+
#log(level: .debug, "log messsage")
3636
}
3737
}
3838

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import SwiftDiagnostics
2+
import SwiftSyntax
3+
import SwiftSyntaxMacros
4+
5+
/// OS 버전에 따라 Logger의 호출이 달라지므로, 조건에 따라 변경이 필요함
6+
public struct LoggingExpressionMacros: ExpressionMacro {
7+
public static func expansion(
8+
of node: some FreestandingMacroExpansionSyntax,
9+
in context: some MacroExpansionContext
10+
) -> ExprSyntax {
11+
let argumentList = node.argumentList
12+
guard
13+
argumentList.count == 2,
14+
let firstElement = argumentList.first,
15+
let secondElement = argumentList.last
16+
else {
17+
fatalError("compiler bug: the macro does not have any arguments")
18+
}
19+
20+
let oslogType = firstElement.expression
21+
.as(MemberAccessExprSyntax.self)?
22+
.declName.baseName.text
23+
let msg = secondElement.expression
24+
.as(StringLiteralExprSyntax.self)?
25+
.segments.first?
26+
.as(StringSegmentSyntax.self)?
27+
.content.text
28+
29+
guard
30+
let oslogType, let msg
31+
else {
32+
fatalError("compiler bug: the macro does not have any arguments")
33+
}
34+
35+
return "\(raw: generateExpr(oslogType: oslogType, msg: msg))"
36+
}
37+
38+
private static func generateExpr(oslogType: String, msg: String) -> String {
39+
"""
40+
{
41+
#if os(iOS)
42+
if #available(iOS 14.0, *) {
43+
Logger(logger).log(level: .\(oslogType), "\(msg)")
44+
} else {
45+
\(debug(oslogType: oslogType, msg: msg))
46+
}
47+
#else
48+
\(debug(oslogType: oslogType, msg: msg))
49+
#endif
50+
}()
51+
"""
52+
}
53+
54+
private static func debug(oslogType: String, msg: String) -> String {
55+
"""
56+
os_log(.\(oslogType), log: logger, "\(msg)")
57+
"""
58+
}
59+
}

Sources/Macros/MacrosPlugin.swift

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ struct MacrosPlugin: CompilerPlugin {
88
static var expressionMacros: [Macro.Type] {
99
[
1010
StringifyMacro.self,
11+
LoggingExpressionMacros.self,
1112
]
1213
}
1314

Sources/Macros/Member/Logging/LoggingMacro.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public struct LoggingMacro: MemberMacro {
3434
return [
3535
DeclSyntax(
3636
"""
37-
lazy var logger: Logger = {
38-
LoggingMacroHelper.generateLogger(category: String(describing: Self.self))
37+
lazy var logger: OSLog = {
38+
LoggingMacroHelper.generateOSLog(category: String(describing: Self.self))
3939
}()
4040
"""),
4141
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import Macros
2+
import SwiftSyntaxMacros
3+
import SwiftSyntaxMacrosTestSupport
4+
import XCTest
5+
6+
final class LoggingExpressionMacrosTests: XCTestCase {
7+
func testMacro() throws {
8+
assertMacroExpansion(
9+
#"""
10+
#log(level: .debug, "message")
11+
"""#,
12+
expandedSource: #"""
13+
{
14+
#if os(iOS)
15+
if #available (iOS 14.0, *) {
16+
Logger(logger).log(level: .debug, "message")
17+
} else {
18+
os_log(.debug, log: logger, "message")
19+
}
20+
#else
21+
os_log(.debug, log: logger, "message")
22+
#endif
23+
}()
24+
"""#,
25+
macros: ["log": LoggingExpressionMacros.self]
26+
)
27+
}
28+
}

0 commit comments

Comments
 (0)