forked from apollographql/apollo-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodegenLogger.swift
More file actions
52 lines (46 loc) · 1.78 KB
/
CodegenLogger.swift
File metadata and controls
52 lines (46 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import Foundation
/// Helper to get logs printing to stdout so they can be read from the command line.
public struct CodegenLogger {
public enum LogLevel: Int {
case error
case warning
case debug
var name: String {
switch self {
case .error:
return "ERROR"
case .warning:
return "WARNING"
case .debug:
return "DEBUG"
}
}
}
/// The `LogLevel` at which to print logs. Higher raw values than this will
/// be ignored. Defaults to `debug`.
public static var level = LogLevel.debug
/// Logs the given string if its `logLevel` is at or above `CodegenLogger.level`, otherwise ignores it.
///
/// - Parameter logString: The string to log out, as an autoclosure
/// - Parameter logLevel: The log level at which to print this specific log. Defaults to `debug`.
/// - Parameter file: The file where this function was called. Defaults to the direct caller.
/// - Parameter line: The line where this function was called. Defaults to the direct caller.
public static func log(_ logString: @autoclosure () -> String,
logLevel: LogLevel = .debug,
file: StaticString = #file,
line: UInt = #line) {
guard logLevel.rawValue <= CodegenLogger.level.rawValue else {
// We're not logging anything at this level.
return
}
var standardOutput = FileHandle.standardOutput
print("[\(logLevel.name) - ApolloCodegenLib:\(file.apollo.lastPathComponent):\(line)] - \(logString())", to: &standardOutput)
}
}
// Extension which allows `print` to ouput to a FileHandle
extension FileHandle: TextOutputStream {
public func write(_ string: String) {
guard let data = string.data(using: .utf8) else { return }
self.write(data)
}
}