【问题标题】:Swift writing log file in yosemite在优胜美地快速写入日志文件
【发布时间】:2020-06-30 08:27:16
【问题描述】:

我有一个将字符串写入文件的程序。它在 macos mojave 中完美运行,但在优胜美地中却不行。

使用 Xcode 11 和 Swift 5

代码是 (Log.swift)

import Foundation
open class Log {

open var maxFileSize: UInt64 = 102400
open var maxFileCount = 365

///The directory in which the log files will be written
open var directory = Log.defaultDirectory() {
    didSet {
        directory = NSString(string: directory).expandingTildeInPath

        let fileManager = FileManager.default
        if !fileManager.fileExists(atPath: directory) {
            do {
                try fileManager.createDirectory(atPath: directory, withIntermediateDirectories: true, attributes: nil)
            } catch {
                NSLog("Couldn't create directory at \(directory)")
            }
        }
    }
}

open var currentPath: String {
    return "\(directory)/\(logName(0))"
}

///The name of the log files
open var name = "logfile"

///Whether or not logging also prints to the console
open var printToConsole = true

///logging singleton
open class var logger: Log {

    struct Static {
        static let instance: Log = Log()
    }
    return Static.instance
}
//the date formatter
var dateFormatter: DateFormatter {
    let formatter = DateFormatter()
    formatter.timeStyle = .medium
    formatter.dateStyle = .medium
    return formatter
}

///write content to the current log file.
open func write(_ text: String) {
    let path = currentPath
    let fileManager = FileManager.default
    if !fileManager.fileExists(atPath: path) {
        do {
            try "".write(toFile: path, atomically: true, encoding: String.Encoding.utf8)
        } catch _ {
        }
    }
    if let fileHandle = FileHandle(forWritingAtPath: path) {
        let dateStr = dateFormatter.string(from: Date())
        let writeText = "[\(dateStr)]: \(text)\n"
        fileHandle.seekToEndOfFile()
        fileHandle.write(writeText.data(using: String.Encoding.utf8)!)
        fileHandle.closeFile()
        }
}

///Recursive method call to rename log files
func rename(_ index: Int) {
    let fileManager = FileManager.default
    let path = "\(directory)/\(logName(index))"
    let newPath = "\(directory)/\(logName(index+1))"
    if fileManager.fileExists(atPath: newPath) {
        rename(index+1)
    }
    do {
        try fileManager.moveItem(atPath: path, toPath: newPath)
    } catch _ {
    }
}

///gets the log name
func logName(_ num :Int) -> String {
    return "\(name)-\(num).log"
}

///get the default log directory
class func defaultDirectory() -> String {
    var path = ""
    let fileManager = FileManager.default
        let urls = fileManager.urls(for: .libraryDirectory, in: .userDomainMask)
        //let urls = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask)
        if let url = urls.last {
            path = "\(url.path)/TestingLog/Logs"
        }
    if !fileManager.fileExists(atPath: path) && path != ""  {
        do {
            try fileManager.createDirectory(atPath: path, withIntermediateDirectories: false, attributes: nil)
        } catch _ {
        }
    }
    return path
}

}

///Writes content to the current log file
public func logw(_ text: String) {
    Log.logger.write(text)
}

在另一个 swift 文件中,我使用 logw("test1") 在 applicationsupport 目录中的文件中打印 test1。

是什么问题导致它不能在优胜美地写字?

我需要它不删除文件或文件中的文本,并使其在写入日志时继续附加它。

【问题讨论】:

    标签: swift osx-yosemite


    【解决方案1】:

    我可以从你上面的代码中看到

    open func write(_ text: String) {
        let path = currentPath
        let fileManager = FileManager.default
        if !fileManager.fileExists(atPath: path) {
            do {
                try "".write(toFile: path, atomically: true, encoding: String.Encoding.utf8)
            } catch _ {
            }
        }
    }
    

    方法传递给日志的字符串值不在任何地方使用。 .你能澄清一下吗

    【讨论】:

      猜你喜欢
      • 2015-01-01
      • 1970-01-01
      • 2013-02-23
      • 1970-01-01
      • 1970-01-01
      • 2017-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多