|
@@ -125,6 +125,28 @@ public func bf_getCurrentViewController() -> UIViewController? {
|
|
|
return currentVC
|
|
|
}
|
|
|
|
|
|
+final class LogDestination: TextOutputStream {
|
|
|
+ static let shared = LogDestination()
|
|
|
+
|
|
|
+ private var path: String = {
|
|
|
+ var path = ""
|
|
|
+ if let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
|
|
|
+ path = documentDirectoryPath + "/\(appRunIdentify)" + "_logfile.txt"
|
|
|
+ }
|
|
|
+ return path
|
|
|
+ }()
|
|
|
+
|
|
|
+ func write(_ string: String) {
|
|
|
+ if let data = string.data(using: .utf8), let fileHandle = FileHandle(forWritingAtPath: path) {
|
|
|
+ defer {
|
|
|
+ fileHandle.closeFile()
|
|
|
+ }
|
|
|
+ fileHandle.seekToEndOfFile()
|
|
|
+ fileHandle.write(data)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
/** 打印
|
|
|
type = 1 : 胡志强
|
|
|
type = 2 :王成
|
|
@@ -132,22 +154,28 @@ public func bf_getCurrentViewController() -> UIViewController? {
|
|
|
|
|
|
*/
|
|
|
public func BFLog<T>(_ type: Int = 0, _ file: String = #file, _ line: Int = #line, message: T) {
|
|
|
+ var printStr = ""
|
|
|
+
|
|
|
#if DEBUG
|
|
|
let filePath = (file as NSString).lastPathComponent
|
|
|
let dateFmt = DateFormatter()
|
|
|
dateFmt.dateFormat = "HH:mm:ss:SSSS"
|
|
|
let msg = "\(filePath) (L:\(line)) \(message)"
|
|
|
if type == 1 {
|
|
|
- print("hhz-\(dateFmt.string(from: Date())) \(msg)")
|
|
|
+ printStr = "hhz-\(dateFmt.string(from: Date())) \(msg)"
|
|
|
} else if type == 2 {
|
|
|
- print("ak-\(dateFmt.string(from: Date())) \(msg)")
|
|
|
+ printStr = "ak-\(dateFmt.string(from: Date())) \(msg)"
|
|
|
} else if type == 3 {
|
|
|
- print("ww-\(dateFmt.string(from: Date())) \(msg)")
|
|
|
- } else {
|
|
|
- print("\(dateFmt.string(from: Date())) \(msg)")
|
|
|
+ printStr = "ww-\(dateFmt.string(from: Date())) \(msg)"
|
|
|
+ } else if type == 0{
|
|
|
+ printStr = "\(dateFmt.string(from: Date())) \(msg)"
|
|
|
}
|
|
|
+ print(printStr)
|
|
|
#else
|
|
|
- writeIntoFile()
|
|
|
+ if printStr.count > 0{
|
|
|
+ var dest = LogDestination.shared
|
|
|
+ print(printStr, to: &dest)
|
|
|
+ }
|
|
|
// let filePath = (file as NSString).lastPathComponent;
|
|
|
// let dateFmt = DateFormatter()
|
|
|
// dateFmt.dateFormat = "HH:mm:ss:SSSS"
|
|
@@ -156,13 +184,6 @@ public func BFLog<T>(_ type: Int = 0, _ file: String = #file, _ line: Int = #lin
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
-func writeIntoFile() {
|
|
|
- if let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
|
|
|
- let filePath = documentDirectoryPath + "/\(appRunIdentify)" + "_logfile.txt"
|
|
|
- freopen(filePath.cString(using: .ascii), "a", stderr)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
// MARK: 获取公共参数
|
|
|
|
|
|
public func commonParams() -> [String: Any] {
|