2018-08-14 09:18:03 +02:00
|
|
|
//
|
|
|
|
// MIT License
|
|
|
|
//
|
|
|
|
// Copyright (c) 2018 Wojciech Nagrodzki
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
// SOFTWARE.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2018-08-28 19:14:55 +02:00
|
|
|
protocol SizeLimitedFileFactory {
|
|
|
|
|
|
|
|
/// Returns newly initialized SizeLimitedFile instance.
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - fileURL: URL of the file.
|
|
|
|
/// - fileSizeLimit: Maximum size the file can reach in bytes.
|
|
|
|
/// - Throws: An error that may occur while the file is being opened for writing.
|
|
|
|
func makeInstance(fileURL: URL, fileSizeLimit: UInt64) throws -> SizeLimitedFile
|
|
|
|
}
|
|
|
|
|
2018-08-26 13:30:53 +02:00
|
|
|
/// Allows log files rotation.
|
|
|
|
protocol Logrotate {
|
|
|
|
|
|
|
|
/// Rotates log files `rotations` number of times.
|
|
|
|
///
|
|
|
|
/// First deletes file at `<fileURL>.<rotations>`.
|
|
|
|
/// Next moves files located at:
|
|
|
|
///
|
|
|
|
/// `<fileURL>, <fileURL>.1, <fileURL>.2 ... <fileURL>.<rotations - 1>`
|
|
|
|
///
|
|
|
|
/// to `<fileURL>.1, <fileURL>.2 ... <fileURL>.<rotations>`
|
|
|
|
func rotate() throws
|
|
|
|
}
|
|
|
|
|
|
|
|
protocol LogrotateFactory {
|
|
|
|
|
|
|
|
/// Returns newly initialized Logrotate instance.
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - fileURL: URL of the log file.
|
|
|
|
/// - rotations: Number of times log files are rotated before being removed.
|
|
|
|
func makeInstance(fileURL: URL, rotations: Int) -> Logrotate
|
|
|
|
}
|
|
|
|
|
2018-08-15 13:02:41 +02:00
|
|
|
/// Logger that writes messages into the file at specified URL with log rotation support.
|
2018-08-14 09:18:03 +02:00
|
|
|
public final class DiskLogger: Logger {
|
2018-08-15 13:02:41 +02:00
|
|
|
|
2018-08-14 09:18:03 +02:00
|
|
|
private let fileURL: URL
|
|
|
|
private let fileSizeLimit: UInt64
|
|
|
|
private let rotations: Int
|
2018-08-26 12:08:07 +02:00
|
|
|
private let fileSystem: FileSystem
|
2018-08-28 19:14:55 +02:00
|
|
|
private let sizeLimitedFileFactory: SizeLimitedFileFactory
|
2018-08-26 13:30:53 +02:00
|
|
|
private let logrotateFactory: LogrotateFactory
|
2018-08-21 19:37:36 +02:00
|
|
|
private let formatter: DateFormatter
|
2018-08-14 09:18:03 +02:00
|
|
|
private let queue = DispatchQueue(label: "com.wnagrodzki.DiskLogger", qos: .background, attributes: [], autoreleaseFrequency: .workItem, target: nil)
|
|
|
|
private var buffer = Data()
|
2018-08-28 19:14:55 +02:00
|
|
|
private var sizeLimitedFile: SizeLimitedFile!
|
2018-08-14 09:18:03 +02:00
|
|
|
|
2018-08-15 13:02:41 +02:00
|
|
|
/// Initializes new DiskLogger instance.
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - fileURL: URL of the log file.
|
|
|
|
/// - fileSizeLimit: Maximum size log file can reach in bytes. Attempt to exceeding that limit triggers log files rotation.
|
|
|
|
/// - rotations: Number of times log files are rotated before being removed.
|
2018-08-26 13:30:53 +02:00
|
|
|
public convenience init(fileURL: URL, fileSizeLimit: UInt64, rotations: Int) {
|
2018-08-28 19:14:55 +02:00
|
|
|
self.init(fileURL: fileURL, fileSizeLimit: fileSizeLimit, rotations: rotations, fileSystem: FileManager.default, sizeLimitedFileFactory: FileWriterFactory(), logrotateFactory: FileRotateFactory())
|
2018-08-26 13:30:53 +02:00
|
|
|
}
|
|
|
|
|
2018-08-28 19:14:55 +02:00
|
|
|
init(fileURL: URL, fileSizeLimit: UInt64, rotations: Int, fileSystem: FileSystem, sizeLimitedFileFactory: SizeLimitedFileFactory, logrotateFactory: LogrotateFactory) {
|
2018-08-14 09:18:03 +02:00
|
|
|
self.fileURL = fileURL
|
|
|
|
self.fileSizeLimit = fileSizeLimit
|
|
|
|
self.rotations = rotations
|
2018-08-26 12:08:07 +02:00
|
|
|
self.fileSystem = fileSystem
|
2018-08-28 19:14:55 +02:00
|
|
|
self.sizeLimitedFileFactory = sizeLimitedFileFactory
|
2018-08-26 13:30:53 +02:00
|
|
|
self.logrotateFactory = logrotateFactory
|
2018-08-21 19:37:36 +02:00
|
|
|
formatter = DateFormatter()
|
|
|
|
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
|
|
|
|
formatter.timeZone = TimeZone(secondsFromGMT: 0)
|
2018-08-14 09:18:03 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 19:58:34 +02:00
|
|
|
public func log(time: Date, level: LogLevel, location: String, message: @autoclosure () -> String) {
|
|
|
|
let message = formatter.string(from: time) + " <" + level.logDescription + "> " + location + " " + message() + "\n"
|
2018-08-14 09:18:03 +02:00
|
|
|
log(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func log(_ message: String) {
|
2018-08-25 11:41:19 +02:00
|
|
|
let data = Data(message.utf8)
|
2018-08-14 09:18:03 +02:00
|
|
|
queue.async {
|
|
|
|
self.buffer.append(data)
|
|
|
|
|
|
|
|
do {
|
2018-08-28 19:14:55 +02:00
|
|
|
try self.openSizeLimitedFile()
|
2018-08-24 21:58:08 +02:00
|
|
|
do {
|
|
|
|
try self.writeBuffer()
|
|
|
|
}
|
2018-08-28 19:14:55 +02:00
|
|
|
catch is SizeLimitedFileQuotaReached {
|
|
|
|
self.closeSizeLimitedFile()
|
2018-08-24 21:58:08 +02:00
|
|
|
try self.rotateLogFiles()
|
2018-09-01 08:59:25 +02:00
|
|
|
try self.openSizeLimitedFile()
|
|
|
|
try self.writeBuffer()
|
2018-08-24 21:58:08 +02:00
|
|
|
}
|
2018-08-14 09:18:03 +02:00
|
|
|
}
|
|
|
|
catch {
|
2018-09-01 08:59:25 +02:00
|
|
|
let message = self.formatter.string(from: Date()) + " <" + LogLevel.warning.logDescription + "> " + String(describing: error) + "\n"
|
2018-08-25 12:27:10 +02:00
|
|
|
let data = Data(message.utf8)
|
|
|
|
self.buffer.append(data)
|
2018-08-14 09:18:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 19:14:55 +02:00
|
|
|
private func openSizeLimitedFile() throws {
|
|
|
|
guard sizeLimitedFile == nil else { return }
|
2018-08-26 12:08:07 +02:00
|
|
|
if fileSystem.itemExists(at: fileURL) == false {
|
|
|
|
_ = fileSystem.createFile(at: fileURL)
|
2018-08-14 09:18:03 +02:00
|
|
|
}
|
2018-08-28 19:14:55 +02:00
|
|
|
sizeLimitedFile = try sizeLimitedFileFactory.makeInstance(fileURL: fileURL, fileSizeLimit: fileSizeLimit)
|
2018-08-14 09:18:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private func writeBuffer() throws {
|
2018-08-28 19:14:55 +02:00
|
|
|
try sizeLimitedFile.write(buffer)
|
2018-08-14 09:18:03 +02:00
|
|
|
buffer.removeAll()
|
|
|
|
}
|
|
|
|
|
2018-08-28 19:14:55 +02:00
|
|
|
private func closeSizeLimitedFile() {
|
|
|
|
self.sizeLimitedFile.synchronizeAndCloseFile()
|
|
|
|
self.sizeLimitedFile = nil
|
2018-08-14 09:18:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private func rotateLogFiles() throws {
|
2018-08-26 13:30:53 +02:00
|
|
|
let logrotate = logrotateFactory.makeInstance(fileURL: fileURL, rotations: rotations)
|
2018-08-14 09:18:03 +02:00
|
|
|
try logrotate.rotate()
|
|
|
|
}
|
|
|
|
}
|
2018-08-26 13:30:53 +02:00
|
|
|
|
|
|
|
private class FileRotateFactory: LogrotateFactory {
|
|
|
|
func makeInstance(fileURL: URL, rotations: Int) -> Logrotate {
|
|
|
|
return FileRotate(fileURL: fileURL, rotations: rotations, fileSystem: FileManager.default)
|
|
|
|
}
|
|
|
|
}
|
2018-08-28 19:14:55 +02:00
|
|
|
|
|
|
|
private class FileWriterFactory: SizeLimitedFileFactory {
|
|
|
|
func makeInstance(fileURL: URL, fileSizeLimit: UInt64) throws -> SizeLimitedFile {
|
2019-10-30 19:52:06 +01:00
|
|
|
return try SizeLimitedFileImpl(fileURL: fileURL, fileSizeLimit: fileSizeLimit, fileFactory: FileHandleFactory())
|
2018-08-28 19:14:55 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-28 20:01:26 +02:00
|
|
|
|
|
|
|
private class FileHandleFactory: FileFactory {
|
|
|
|
func makeInstance(forWritingTo: URL) throws -> File {
|
|
|
|
return try FileHandle(forWritingTo: forWritingTo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension FileHandle: File {
|
|
|
|
|
2019-10-30 18:55:31 +01:00
|
|
|
func swift_write(_ data: Data) throws {
|
|
|
|
try __write(data, error: ())
|
|
|
|
}
|
2018-08-28 20:01:26 +02:00
|
|
|
}
|