Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a centralized logger to turn off or on xcode console logs. #259

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions netfox.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -99,6 +101,7 @@
/* Begin PBXFileReference section */
026274D8214C6BC400AE1BDF /* WKWebViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WKWebViewController.swift; sourceTree = "<group>"; };
3708AD5922D4A9DD0050DB7D /* NFXURLDetailsControllerViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NFXURLDetailsControllerViewController.swift; sourceTree = "<group>"; };
38B166502A98D6420007423A /* NFXLogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NFXLogger.swift; sourceTree = "<group>"; };
8201A39C204E3E3F00AB2C3D /* NFXAuthenticationChallengeSender.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NFXAuthenticationChallengeSender.swift; sourceTree = "<group>"; };
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 = "<group>"; };
Expand Down Expand Up @@ -236,6 +239,7 @@
B3F8BA7F1C833ABC00F9FBEA /* NFXSettingsController.swift */,
B3F8BA801C833ABC00F9FBEA /* NFXStatisticsController.swift */,
B3F8BA811C833ABC00F9FBEA /* NFXWindowController.swift */,
38B166502A98D6420007423A /* NFXLogger.swift */,
);
path = Core;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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;
};
Expand Down
10 changes: 7 additions & 3 deletions netfox/Core/NFX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -170,8 +175,7 @@ open class NFX: NSObject {
hideNFX()
}

@objc open func toggle()
{
@objc open func toggle() {
guard self.started else { return }
toggleNFX()
}
Expand Down
5 changes: 3 additions & 2 deletions netfox/Core/NFXHTTPModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ fileprivate func < <T : Comparable>(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()
Expand Down Expand Up @@ -166,15 +167,15 @@ fileprivate func < <T : Comparable>(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)")
}
}

@objc public func readRawData(from fileURL: URL) -> Data? {
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
}
}
Expand Down
9 changes: 5 additions & 4 deletions netfox/Core/NFXHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}
}

Expand All @@ -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)")
}
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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)")
}
}

Expand Down
19 changes: 19 additions & 0 deletions netfox/Core/NFXLogger.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 1 addition & 1 deletion netfox_ios_demo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down