Skip to content

Commit 707ac4c

Browse files
authored
Add a path argument to log messages (#21)
- When specified the log messages match those typically output by compilers - This means that Xcode will properly parse them and you'll see them in the issues pane on the left, and clicking them will take you to the right file, etc.
1 parent d8156b9 commit 707ac4c

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

Sources/SourceParsingFramework/Utilities/Logger.swift

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ public enum LoggingLevel: Int {
3838
}
3939
}
4040

41+
/// A text description the log level.
42+
public var text: String {
43+
switch self {
44+
case .debug: return "debug"
45+
case .info: return "info"
46+
case .warning: return "warning"
47+
case .error: return "error"
48+
}
49+
}
50+
4151
/// Retrieve the logging level based on the given String value.
4252
///
4353
/// - parameter value: The `String` value to parse from.
@@ -67,49 +77,61 @@ public func set(minLoggingOutputLevel level: LoggingLevel) {
6777
/// Log the given message at the `debug` level.
6878
///
6979
/// - parameter message: The message to log.
80+
/// - parameter path: The file path this error applies to
81+
/// (if specified, the log output changes to one that matches compiler error and can be parsed easily by build systems)
7082
/// - note: The mesasge is only logged if the current `minLoggingOutputLevel`
7183
/// is set at or below the `debug` level.
72-
public func debug(_ message: String) {
73-
log(message, atLevel: .debug)
84+
public func debug(_ message: String, path: String? = nil) {
85+
log(message, atLevel: .debug, path: path)
7486
}
7587

7688
/// Log the given message at the `info` level.
7789
///
7890
/// - parameter message: The message to log.
91+
/// - parameter path: The file path this error applies to
92+
/// (if specified, the log output changes to one that matches compiler error and can be parsed easily by build systems)
7993
/// - note: The mesasge is only logged if the current `minLoggingOutputLevel`
8094
/// is set at or below the `info` level.
81-
public func info(_ message: String) {
82-
log(message, atLevel: .info)
95+
public func info(_ message: String, path: String? = nil) {
96+
log(message, atLevel: .info, path: path)
8397
}
8498

8599
/// Log the given message at the `warning` level.
86100
///
87101
/// - parameter message: The message to log.
102+
/// - parameter path: The file path this error applies to
103+
/// (if specified, the log output changes to one that matches compiler error and can be parsed easily by build systems)
88104
/// - note: The mesasge is only logged if the current `minLoggingOutputLevel`
89105
/// is set at or below the `warning` level.
90-
public func warning(_ message: String) {
91-
log(message, atLevel: .warning)
106+
public func warning(_ message: String, path: String? = nil) {
107+
log(message, atLevel: .warning, path: path)
92108
}
93109

94110
/// Log the given message at the `error` level and terminate.
95111
///
96112
/// - parameter message: The message to log.
113+
/// - parameter path: The file path this error applies to
114+
/// (if specified, the log output changes to one that matches compiler error and can be parsed easily by build systems)
97115
/// - returns: `Never`.
98116
/// - note: The mesasge is only logged if the current `minLoggingOutputLevel`
99117
/// is set at or below the `error` level.
100118
/// - note: This method terminates the program. It returns `Never`.
101-
public func error(_ message: String) -> Never {
102-
log(message, atLevel: .error)
119+
public func error(_ message: String, path: String? = nil) -> Never {
120+
log(message, atLevel: .error, path: path)
103121
exit(1)
104122
}
105123

106-
private func log(_ message: String, atLevel level: LoggingLevel) {
124+
private func log(_ message: String, atLevel level: LoggingLevel, path: String?) {
107125
#if DEBUG
108126
UnitTestLogger.instance.log(message, at: level)
109127
#endif
110128

111129
if level.rawValue >= minLoggingOutputLevel.value {
112-
print("\(level.emoticon) \(message)")
130+
if let path = path {
131+
print("\(path):1:1: \(level.text): \(message)")
132+
} else {
133+
print("\(level.text): \(level.emoticon) \(message)")
134+
}
113135
}
114136
}
115137

0 commit comments

Comments
 (0)