Skip to content

Commit

Permalink
Merge pull request #86 from hotwired/disable-logging-by-default
Browse files Browse the repository at this point in the history
Expose a new `TurboLog.debugLoggingEnabled` API that apps can enable to see debug logs
  • Loading branch information
jayohms authored Jun 30, 2022
2 parents 4b31358 + 57d6f83 commit 2ace9eb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
7 changes: 6 additions & 1 deletion Demo/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import UIKit
import Turbo

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
true
#if DEBUG
TurboLog.debugLoggingEnabled = true
#endif

return true
}

// MARK: UISceneSession Lifecycle
Expand Down
9 changes: 9 additions & 0 deletions Docs/Advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ One place you'll definitely want multiple sessions is for modals since non full-

A `UITabBarController` based app should work with a single session, but you may want to use multiple session there as well. A good rule of thumb is that you'll likely want a separate session for each navigation context you have (most likely a session per `UINavigationController`).

## Enable Debug Logging
During development, you may want to see what `turbo-ios` is doing behind the scenes. To enable debug logging, you can set the `TurboLog.debugLoggingEnabled` flag to `true`. Debug logging should always be disabled in your production app. For example:

```swift
#if DEBUG
TurboLog.debugLoggingEnabled = true
#endif
```

## Native <-> JavaScript Integration

You can send messages from your native app to the web view by using `session.webView.evaluateJavaScript()` method. You can receive messages by adding a `WKScriptMessageHandler` to the web view and implementing the required protocols. That allows for two-way async communication.
Expand Down
21 changes: 13 additions & 8 deletions Source/Logging.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import Foundation

public struct TurboLog {
public static var debugLoggingEnabled = false
}

/// Simple function to help in debugging, a noop in Release builds
func debugLog(_ text: String, _ arguments: [String: Any] = [:]) {
#if DEBUG
func debugLog(_ message: String, _ arguments: [String: Any] = [:]) {
let timestamp = Date()

print("\(timestamp) \(text) \(arguments)")

#endif
log("\(timestamp) \(message) \(arguments)")
}

func debugPrint(_ message: String) {
#if DEBUG
print(message)
#endif
log(message)
}

private func log(_ message: String) {
if TurboLog.debugLoggingEnabled {
print(message)
}
}

0 comments on commit 2ace9eb

Please sign in to comment.