Skip to content

Commit c1f73e6

Browse files
author
Hussein Habibi Juybari
committed
Enhance network interface discovery and logging; prefer 'en0' interface and integrate OSLog for improved logging performance
1 parent b13c4dc commit c1f73e6

File tree

5 files changed

+93
-40
lines changed

5 files changed

+93
-40
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
---
99

10+
### 0.8.0 - 2025-10-28
11+
- Providing one URL for having access to LogTap server
12+
- Move code from print to os_log to be able to see the logs in Console app too.
13+
1014
### 0.7.0 - 2025-10-22
1115
- Fix issue to adding LogTapFramework via SPM
1216
- Fix issue showing logs in webpage

LogTapSample/LogTapSample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
Add to your dependencies:
5252
```swift
5353
dependencies: [
54-
.package(url: "https://github.com/Husseinhj/LogTapFramework.git", from: "0.7.0")
54+
.package(url: "https://github.com/Husseinhj/LogTapFramework.git", from: "0.8.0")
5555
]
5656
```
5757

Sources/LogTapFramework/LogTap.swift

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -164,23 +164,40 @@ public final class LogTap {
164164

165165
public func urls() -> [String] {
166166
// best-effort LAN IPv4 list
167-
var addrs: [String] = []
168-
var ifaddrPtr: UnsafeMutablePointer<ifaddrs>?
169-
if getifaddrs(&ifaddrPtr) == 0, let first = ifaddrPtr {
170-
var ptr: UnsafeMutablePointer<ifaddrs>? = first
171-
while ptr != nil {
172-
let ifa = ptr!.pointee
173-
if let sa = ifa.ifa_addr, sa.pointee.sa_family == UInt8(AF_INET) {
174-
var addr = [CChar](repeating: 0, count: Int(NI_MAXHOST))
175-
if getnameinfo(ifa.ifa_addr, socklen_t(ifa.ifa_addr.pointee.sa_len), &addr, socklen_t(addr.count), nil, 0, NI_NUMERICHOST) == 0 {
176-
let ip = String(cString: addr)
177-
if ip != "127.0.0.1" { addrs.append(ip) }
178-
}
179-
}
180-
ptr = ifa.ifa_next
181-
}
182-
freeifaddrs(first)
167+
var addrs: [(iface: String, ip: String)] = []
168+
var ifaddrPtr: UnsafeMutablePointer<ifaddrs>?
169+
if getifaddrs(&ifaddrPtr) == 0, let first = ifaddrPtr {
170+
var ptr: UnsafeMutablePointer<ifaddrs>? = first
171+
while ptr != nil {
172+
let ifa = ptr!.pointee
173+
// Skip interfaces that are down or loopback
174+
let name = String(cString: ifa.ifa_name)
175+
let flags = Int32(ifa.ifa_flags)
176+
let isUp = (flags & IFF_UP) == IFF_UP
177+
let isLoopback = (flags & IFF_LOOPBACK) == IFF_LOOPBACK
178+
if !isUp || isLoopback {
179+
ptr = ifa.ifa_next
180+
continue
181+
}
182+
183+
if let sa = ifa.ifa_addr, sa.pointee.sa_family == UInt8(AF_INET) {
184+
var addr = [CChar](repeating: 0, count: Int(NI_MAXHOST))
185+
if getnameinfo(ifa.ifa_addr, socklen_t(ifa.ifa_addr.pointee.sa_len), &addr, socklen_t(addr.count), nil, 0, NI_NUMERICHOST) == 0 {
186+
let ip = String(cString: addr)
187+
if ip != "127.0.0.1" {
188+
addrs.append((iface: name, ip: ip))
189+
}
190+
}
191+
}
192+
ptr = ifa.ifa_next
193+
}
194+
freeifaddrs(first)
195+
}
196+
// Prefer en0 (typical Wi‑Fi interface on iOS/macOS) if present
197+
if let en0 = addrs.first(where: { $0.iface == "en0" }) {
198+
return ["http://\(en0.ip):\(config.port)/"]
183199
}
184-
return addrs.map { "http://\($0):\(config.port)/" }
185-
}
186-
}
200+
// Otherwise return all discovered addresses
201+
return addrs.map { "http://\($0.ip):\(config.port)/" }
202+
}
203+
}

Sources/LogTapFramework/Logger/LogTapLogger.swift

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Created by Hussein Habibi Juybari on 06.09.25.
66
//
77

8+
import os
89
import Foundation
910

1011
public enum BuildMode {
@@ -15,6 +16,33 @@ public final class LogTapLogger {
1516
public static let shared = LogTapLogger()
1617
private init() {}
1718

19+
// Default OSLog instance used as fallback
20+
private let osLog = OSLog(subsystem: Bundle.main.bundleIdentifier ?? "LogTap", category: "LogTapLogger")
21+
// Cache OSLog objects per tag/category to avoid recreating them repeatedly.
22+
private var osLogCache: [String: OSLog] = [:]
23+
private let osLogCacheQueue = DispatchQueue(label: "LogTapLogger.osLogCache")
24+
25+
private func osLog(forTag tag: String) -> OSLog {
26+
// Use a quick thread-safe cache lookup/creation
27+
return osLogCacheQueue.sync {
28+
if let existing = osLogCache[tag] { return existing }
29+
let entry = OSLog(subsystem: Bundle.main.bundleIdentifier ?? "LogTap", category: tag)
30+
osLogCache[tag] = entry
31+
return entry
32+
}
33+
}
34+
35+
private func osLogType(for level: LogLevel) -> OSLogType {
36+
switch level {
37+
case .verbose: return .debug
38+
case .debug: return .debug
39+
case .info: return .info
40+
case .warn: return .default
41+
case .error: return .error
42+
case .assert: return .fault
43+
}
44+
}
45+
1846
public var debugMode: Bool = true
1947
public var allowReleaseLogging: Bool = false
2048
public var minLevel: LogLevel = .debug
@@ -54,7 +82,11 @@ public final class LogTapLogger {
5482
)
5583
LogTap.shared.emit(ev)
5684
// Also forward to OSLog/print for convenience:
57-
print("[\(level.rawValue)] \(autoTag): \(msg)")
85+
// Use os_log so logs show up in the unified logging system (Console / log command).
86+
let formatted = "[\(level.rawValue)] \(autoTag): \(msg)"
87+
// Use the auto-generated tag as the OSLog category (falls back to default osLog if tag empty)
88+
let categoryLog = autoTag.isEmpty ? osLog : osLog(forTag: autoTag)
89+
os_log("%{public}@", log: categoryLog, type: osLogType(for: level), formatted)
5890
}
5991

6092
internal func v(_ msg: @autoclosure () -> String, tag: String? = nil) { log(.verbose, msg(), tag: tag) }

0 commit comments

Comments
 (0)