forked from bastiaanv/EversenseKit
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOSLog.swift
More file actions
140 lines (117 loc) · 4.51 KB
/
OSLog.swift
File metadata and controls
140 lines (117 loc) · 4.51 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import Combine
import OSLog
class EversenseLogger {
private let logger: Logger
private let fileManager = FileManager.default
init(category: String) {
logger = Logger(subsystem: "org.nightscout.EversenseKit", category: category)
}
public func debug(_ msg: String, file: String = #file, _ function: String = #function, _ line: Int = #line) {
let message = "\(file.file) - \(function)#\(line): \(msg)"
logger.debug("\(message, privacy: .public)")
writeToFile(message, .debug)
}
public func info(_ msg: String, file: String = #file, _ function: String = #function, _ line: Int = #line) {
let message = "\(file.file) - \(function)#\(line): \(msg)"
logger.info("\(message, privacy: .public)")
writeToFile(message, .info)
}
public func warning(_ msg: String, file: String = #file, _ function: String = #function, _ line: Int = #line) {
let message = "\(file.file) - \(function)#\(line): \(msg)"
logger.warning("\(message, privacy: .public)")
writeToFile(message, .notice)
}
public func error(_ msg: String, file: String = #file, _ function: String = #function, _ line: Int = #line) {
let message = "\(file.file) - \(function)#\(line): \(msg)"
logger.error("\(message, privacy: .public)")
writeToFile(message, .error)
}
private func writeToFile(_ msg: String, _ type: OSLogEntryLog.Level) {
if !fileManager.fileExists(atPath: logDir) {
try? fileManager.createDirectory(
atPath: logDir,
withIntermediateDirectories: false,
attributes: nil
)
}
if !fileManager.fileExists(atPath: logFile) {
createFile(at: startOfDay)
} else if let attributes = try? fileManager.attributesOfItem(atPath: logFile),
let creationDate = attributes[.creationDate] as? Date, creationDate < startOfDay
{
try? fileManager.removeItem(atPath: logFilePrev)
try? fileManager.moveItem(atPath: logFile, toPath: logFilePrev)
createFile(at: startOfDay)
}
let logEntry = "[\(dateFormatter.string(from: Date.now)) \(getLevel(type))] \(msg)\n"
let data = logEntry.data(using: .utf8)!
try? data.append(fileURL: URL(fileURLWithPath: logFile))
}
private var startOfDay: Date {
Calendar.current.startOfDay(for: Date.now)
}
private var logFile: String {
getDocumentsDirectory().appendingPathComponent("eversense/eversense_log.txt").path
}
private var logDir: String {
getDocumentsDirectory().appendingPathComponent("eversense").path
}
private var logFilePrev: String {
getDocumentsDirectory().appendingPathComponent("eversense/eversense_log_prev.txt").path
}
private func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = paths[0]
return documentsDirectory
}
private var dateFormatter: DateFormatter {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
return dateFormatter
}
private func createFile(at date: Date) {
fileManager.createFile(atPath: logFile, contents: nil, attributes: [.creationDate: date])
}
func getDebugLogs() -> [URL] {
var items: [URL] = []
if fileManager.fileExists(atPath: logFile) {
items.append(URL(fileURLWithPath: logFile))
}
if fileManager.fileExists(atPath: logFilePrev) {
items.append(URL(fileURLWithPath: logFilePrev))
}
return items
}
private func getLevel(_ type: OSLogEntryLog.Level) -> String {
switch type {
case .info:
return "INFO"
case .notice:
return "WARNING"
case .error:
return "ERROR"
case .fault:
return "FAULT"
case .debug:
return "DEBUG"
default:
return "UNKNOWN"
}
}
}
private extension Data {
func append(fileURL: URL) throws {
if let fileHandle = FileHandle(forWritingAtPath: fileURL.path) {
defer {
fileHandle.closeFile()
}
fileHandle.seekToEndOfFile()
fileHandle.write(self)
} else {
try write(to: fileURL, options: .atomic)
}
}
}
private extension String {
var file: String { components(separatedBy: "/").last ?? "" }
}