From c96ec10ab83e5d2ad1ed7c381edcf5a57793555f Mon Sep 17 00:00:00 2001 From: Aaqib Hussain Date: Fri, 25 Aug 2023 15:19:30 +0200 Subject: [PATCH 1/4] Added a centralized logger to turn off or on xcode console logs generated by NFX. --- README.md | 10 ++++++++++ netfox.xcodeproj/project.pbxproj | 6 ++++++ netfox/Core/NFX.swift | 10 +++++++--- netfox/Core/NFXHTTPModel.swift | 5 +++-- netfox/Core/NFXHelper.swift | 9 +++++---- netfox/Core/NFXLogger.swift | 19 +++++++++++++++++++ netfox_ios_demo/AppDelegate.swift | 2 +- 7 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 netfox/Core/NFXLogger.swift diff --git a/README.md b/README.md index 8cf36abc..c970c3d2 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,16 @@ NFX.sharedInstance().ignoreURL("the_url") ``` Tip: You can use the url of the host (for example "https://www.github.com") to ignore all paths of it +## Hide NFX Xcode Console logs + +To present the errors thrown by the NFX. There is some logging done to the console internally. In order to hide those you can just do +```swift +NFX.sharedInstance().shouldShowConsoleLogs(false) +NFX.sharedInstance().start() +``` +Before you actually start your NFX instance so that you don't see any noise generated by the library. + + ## Features - Search: You can easily search among requests via diff --git a/netfox.xcodeproj/project.pbxproj b/netfox.xcodeproj/project.pbxproj index ccbf7109..b5de368c 100644 --- a/netfox.xcodeproj/project.pbxproj +++ b/netfox.xcodeproj/project.pbxproj @@ -9,6 +9,8 @@ /* Begin PBXBuildFile section */ 026274D9214C6BC400AE1BDF /* WKWebViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 026274D8214C6BC400AE1BDF /* WKWebViewController.swift */; }; 3708AD5A22D4A9DD0050DB7D /* NFXURLDetailsControllerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3708AD5922D4A9DD0050DB7D /* NFXURLDetailsControllerViewController.swift */; }; + 38B166512A98D6420007423A /* NFXLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 38B166502A98D6420007423A /* NFXLogger.swift */; }; + 38B166522A98D6420007423A /* NFXLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 38B166502A98D6420007423A /* NFXLogger.swift */; }; 719756B3279C7EC500402E07 /* netfox_ios.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B3BC020F1C09CDA000C17F3A /* netfox_ios.framework */; }; 719756B4279C7EC500402E07 /* netfox_ios.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = B3BC020F1C09CDA000C17F3A /* netfox_ios.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 8201A39D204E3E3F00AB2C3D /* NFXAuthenticationChallengeSender.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8201A39C204E3E3F00AB2C3D /* NFXAuthenticationChallengeSender.swift */; }; @@ -99,6 +101,7 @@ /* Begin PBXFileReference section */ 026274D8214C6BC400AE1BDF /* WKWebViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WKWebViewController.swift; sourceTree = ""; }; 3708AD5922D4A9DD0050DB7D /* NFXURLDetailsControllerViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NFXURLDetailsControllerViewController.swift; sourceTree = ""; }; + 38B166502A98D6420007423A /* NFXLogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NFXLogger.swift; sourceTree = ""; }; 8201A39C204E3E3F00AB2C3D /* NFXAuthenticationChallengeSender.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NFXAuthenticationChallengeSender.swift; sourceTree = ""; }; 8229AD621F8FB34300A9D613 /* netfox_ios_demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = netfox_ios_demo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 8229AD641F8FB34300A9D613 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; @@ -236,6 +239,7 @@ B3F8BA7F1C833ABC00F9FBEA /* NFXSettingsController.swift */, B3F8BA801C833ABC00F9FBEA /* NFXStatisticsController.swift */, B3F8BA811C833ABC00F9FBEA /* NFXWindowController.swift */, + 38B166502A98D6420007423A /* NFXLogger.swift */, ); path = Core; sourceTree = ""; @@ -459,6 +463,7 @@ B3F8BAB11C833AC700F9FBEA /* NFXSettingsController_iOS.swift in Sources */, B3F8BAB01C833AC700F9FBEA /* NFXListController_iOS.swift in Sources */, 3708AD5A22D4A9DD0050DB7D /* NFXURLDetailsControllerViewController.swift in Sources */, + 38B166512A98D6420007423A /* NFXLogger.swift in Sources */, B3F8BA8E1C833ABC00F9FBEA /* NFXHelper.swift in Sources */, B3F8BA861C833ABC00F9FBEA /* NFXConstants.swift in Sources */, B3F8BAAD1C833AC700F9FBEA /* NFXHelper_iOS.swift in Sources */, @@ -507,6 +512,7 @@ B3F8BA991C833ABC00F9FBEA /* NFXListController.swift in Sources */, B3F8D6801C833B1700F9FBEA /* NFXListController_OSX.swift in Sources */, B3F8BA971C833ABC00F9FBEA /* NFXInfoController.swift in Sources */, + 38B166522A98D6420007423A /* NFXLogger.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/netfox/Core/NFX.swift b/netfox/Core/NFX.swift index eaf2eab9..170a6215 100755 --- a/netfox/Core/NFX.swift +++ b/netfox/Core/NFX.swift @@ -49,6 +49,7 @@ open class NFX: NSObject { fileprivate var ignoredURLs = [String]() fileprivate var ignoredURLsRegex = [NSRegularExpression]() fileprivate var lastVisitDate: Date = Date() + fileprivate let logger: NFXLogger = .shared internal var cacheStoragePolicy = URLCache.StoragePolicy.notAllowed @@ -103,8 +104,12 @@ open class NFX: NSObject { #endif } + @objc open func shouldShowConsoleLogs(_ status: Bool) { + logger.debugLogs = status + } + fileprivate func showMessage(_ msg: String) { - print("netfox \(nfxVersion) - [https://github.com/kasketis/netfox]: \(msg)") + logger.log("netfox \(nfxVersion) - [https://github.com/kasketis/netfox]: \(msg)") } internal func isEnabled() -> Bool { @@ -170,8 +175,7 @@ open class NFX: NSObject { hideNFX() } - @objc open func toggle() - { + @objc open func toggle() { guard self.started else { return } toggleNFX() } diff --git a/netfox/Core/NFXHTTPModel.swift b/netfox/Core/NFXHTTPModel.swift index bddf6172..71d319d5 100755 --- a/netfox/Core/NFXHTTPModel.swift +++ b/netfox/Core/NFXHTTPModel.swift @@ -46,6 +46,7 @@ fileprivate func < (lhs: T?, rhs: T?) -> Bool { @objc public var shortTypeString: String { return shortType.rawValue } @objc public var noResponse = true + private let logger: NFXLogger = .shared func saveRequest(_ request: URLRequest) { requestDate = Date() @@ -166,7 +167,7 @@ fileprivate func < (lhs: T?, rhs: T?) -> Bool { do { try dataString.write(to: fileURL, atomically: true, encoding: .utf8) } catch let error { - print("[NFX]: Failed to save data to [\(fileURL)] - \(error.localizedDescription)") + logger.log("[NFX]: Failed to save data to [\(fileURL)] - \(error.localizedDescription)") } } @@ -174,7 +175,7 @@ fileprivate func < (lhs: T?, rhs: T?) -> Bool { do { return try Data(contentsOf: fileURL) } catch let error { - print("[NFX]: Failed to load data from [\(fileURL)] - \(error.localizedDescription)") + logger.log("[NFX]: Failed to load data from [\(fileURL)] - \(error.localizedDescription)") return nil } } diff --git a/netfox/Core/NFXHelper.swift b/netfox/Core/NFXHelper.swift index 0894a6ef..a7e6ab17 100644 --- a/netfox/Core/NFXHelper.swift +++ b/netfox/Core/NFXHelper.swift @@ -339,12 +339,13 @@ struct NFXPath { static let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory()) static let nfxDirURL = tmpDirURL.appendingPathComponent("NFX", isDirectory: true) static let sessionLogURL = nfxDirURL.appendingPathComponent(sessionLogName) + static let logger: NFXLogger = .shared static func createNFXDirIfNotExist() { do { try FileManager.default.createDirectory(at: nfxDirURL, withIntermediateDirectories: true, attributes: nil) } catch let error { - print("[NFX]: failed to create working dir - \(error.localizedDescription)") + logger.log("[NFX]: failed to create working dir - \(error.localizedDescription)") } } @@ -354,7 +355,7 @@ struct NFXPath { do { try FileManager.default.removeItem(at: nfxDirURL) } catch let error { - print("[NFX]: failed to delete working dir - \(error.localizedDescription)") + logger.log("[NFX]: failed to delete working dir - \(error.localizedDescription)") } } @@ -394,7 +395,7 @@ extension String { try fileHandle.seekToEnd() try fileHandle.write(contentsOf: data) } catch let error { - print("[NFX]: Failed to append [\(self.prefix(128))] to \(fileURL), trying to create new file - \(error.localizedDescription)") + NFXLogger.shared.log("[NFX]: Failed to append [\(self.prefix(128))] to \(fileURL), trying to create new file - \(error.localizedDescription)") write(to: fileURL) } } else { @@ -408,7 +409,7 @@ extension String { do { try write(to: fileURL, atomically: true, encoding: .utf8) } catch let error { - print("[NFX]: Failed to save [\(self.prefix(128))] to \(fileURL) - \(error.localizedDescription)") + NFXLogger.shared.log("[NFX]: Failed to save [\(self.prefix(128))] to \(fileURL) - \(error.localizedDescription)") } } diff --git a/netfox/Core/NFXLogger.swift b/netfox/Core/NFXLogger.swift new file mode 100644 index 00000000..0ffd7494 --- /dev/null +++ b/netfox/Core/NFXLogger.swift @@ -0,0 +1,19 @@ +// +// NFXLogger.swift +// netfox +// +// Copyright © 2016 netfox. All rights reserved. +// + +import Foundation +@objc +final class NFXLogger: NSObject { + static let shared = NFXLogger() + + var debugLogs: Bool = true + + @objc public func log(_ message: String) { + guard debugLogs else { return } + print(message) + } +} diff --git a/netfox_ios_demo/AppDelegate.swift b/netfox_ios_demo/AppDelegate.swift index d6eb8231..8c3bd2d3 100644 --- a/netfox_ios_demo/AppDelegate.swift +++ b/netfox_ios_demo/AppDelegate.swift @@ -16,8 +16,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { + NFX.sharedInstance().shouldShowConsoleLogs(false) NFX.sharedInstance().start() - return true } } From df40938e4d85f7e92c8de58c0d12ec4906ef3338 Mon Sep 17 00:00:00 2001 From: Aaqib Hussain Date: Fri, 25 Aug 2023 16:34:23 +0200 Subject: [PATCH 2/4] disabled logs by default in start --- netfox/Core/NFX.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/netfox/Core/NFX.swift b/netfox/Core/NFX.swift index 170a6215..d410bc6c 100755 --- a/netfox/Core/NFX.swift +++ b/netfox/Core/NFX.swift @@ -83,6 +83,7 @@ open class NFX: NSObject { enable() fileStorageInit() showMessage(Constants.startedMessage.rawValue) + logger.debugLogs = false #if os(OSX) addNetfoxToMainMenu() #endif From fd711ec91ee6f523e4667c995d9edd068c008a4c Mon Sep 17 00:00:00 2001 From: Aaqib Hussain Date: Fri, 25 Aug 2023 16:44:26 +0200 Subject: [PATCH 3/4] Revert "disabled logs by default in start" This reverts commit df40938e4d85f7e92c8de58c0d12ec4906ef3338. --- netfox/Core/NFX.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/netfox/Core/NFX.swift b/netfox/Core/NFX.swift index d410bc6c..170a6215 100755 --- a/netfox/Core/NFX.swift +++ b/netfox/Core/NFX.swift @@ -83,7 +83,6 @@ open class NFX: NSObject { enable() fileStorageInit() showMessage(Constants.startedMessage.rawValue) - logger.debugLogs = false #if os(OSX) addNetfoxToMainMenu() #endif From efa52acb21d69f522ba216f89bf4de01e8ec03bb Mon Sep 17 00:00:00 2001 From: Aaqib Hussain Date: Sun, 27 Aug 2023 10:25:58 +0200 Subject: [PATCH 4/4] removed @objc from NFXLogger --- netfox/Core/NFXLogger.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/netfox/Core/NFXLogger.swift b/netfox/Core/NFXLogger.swift index 0ffd7494..472f4f51 100644 --- a/netfox/Core/NFXLogger.swift +++ b/netfox/Core/NFXLogger.swift @@ -6,8 +6,9 @@ // import Foundation -@objc + final class NFXLogger: NSObject { + static let shared = NFXLogger() var debugLogs: Bool = true