Skip to content

Commit

Permalink
fix: improve logging performance by switch from print to NSLog (#165)
Browse files Browse the repository at this point in the history
* fix: improve logging performance by switch from print to NSLog

* bump: version to 2.9.1
  • Loading branch information
CAMOBAP authored Oct 13, 2024
1 parent 4152d62 commit 66b750e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 47 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.9.1

- Fix: performance issue on building a timestamp prefix in `print` (`HCaptchaLog`)

# 2.9.0

- Fix: emit `.htmlLoadError` if `HCaptcha` WebView is not loaded in specified timeout (5 sec default)
Expand Down
6 changes: 3 additions & 3 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PODS:
- AppSwizzle (1.3.1)
- HCaptcha/Core (2.9.0)
- HCaptcha/RxSwift (2.9.0):
- HCaptcha/Core (2.9.1)
- HCaptcha/RxSwift (2.9.1):
- HCaptcha/Core
- RxSwift (~> 6.2.0)
- RxBlocking (6.2.0):
Expand Down Expand Up @@ -37,7 +37,7 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
AppSwizzle: db36e436f56110d93e5ae0147683435df593cabc
HCaptcha: 7f68727bcf0b01d7022585485e3c577bdfb11d87
HCaptcha: e97d412ae88e01c09cbbdb1582a710f389403ae8
RxBlocking: 0b29f7d2079109a8de49c411381bed7c33ef1eeb
RxCocoa: 4baf94bb35f2c0ab31bc0cb9f1900155f646ba42
RxRelay: e72dbfd157807478401ef1982e1c61c945c94b2f
Expand Down
2 changes: 1 addition & 1 deletion HCaptcha.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'HCaptcha'
s.version = '2.9.0'
s.version = '2.9.1'
s.summary = 'HCaptcha for iOS'
s.swift_version = '5.0'

Expand Down
61 changes: 26 additions & 35 deletions HCaptcha/Classes/HCaptchaDebugInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,15 @@

import Foundation
import CommonCrypto
import ObjectiveC.runtime
import UIKit

extension String {
private extension String {
func jsSanitize() -> String {
return self.replacingOccurrences(of: ".", with: "_")
}
}

private func updateInfoFor(_ image: String, _ ctx: UnsafeMutablePointer<CC_MD5_CTX>, depth: UInt32 = 16) {
var count: UInt32 = 0
if let imagePtr = (image as NSString).utf8String {
let classes = objc_copyClassNamesForImage(imagePtr, &count)
for cls in UnsafeBufferPointer<UnsafePointer<CChar>>(start: classes, count: Int(min(depth, count))) {
CC_MD5_Update(ctx, cls, CC_LONG(strlen(cls)))
}
classes?.deallocate()
var isSystemFramework: Bool {
return self.contains("/System/Library/") || self.contains("/usr/lib/")
}
}

Expand All @@ -41,7 +33,6 @@ private func bundleShortVersion() -> String {
}

class HCaptchaDebugInfo {

public static let json: String = HCaptchaDebugInfo.buildDebugInfoJson()

private class func buildDebugInfoJson() -> String {
Expand All @@ -53,39 +44,39 @@ class HCaptchaDebugInfo {
}

private class func buildDebugInfo() -> [String] {
let depth: UInt32 = 16
var depsCount = 0
var sysCount = 0
let depsCtx = UnsafeMutablePointer<CC_MD5_CTX>.allocate(capacity: 1)
let sysCtx = UnsafeMutablePointer<CC_MD5_CTX>.allocate(capacity: 1)
let appCtx = UnsafeMutablePointer<CC_MD5_CTX>.allocate(capacity: 1)
CC_MD5_Init(depsCtx)
CC_MD5_Init(sysCtx)
CC_MD5_Init(appCtx)

for framework in Bundle.allFrameworks {
guard let frameworkPath = URL(string: framework.bundlePath) else { continue }
let frameworkBin = frameworkPath.deletingPathExtension().lastPathComponent
let image = frameworkPath.appendingPathComponent(frameworkBin).absoluteString
let systemFramework = image.contains("/Library/PrivateFrameworks/") ||
image.contains("/System/Library/Frameworks/")
let loadedCount = Int(min(objc_getClassList(nil, 0), 1024))
if loadedCount > 0 {
let classes = UnsafeMutablePointer<AnyClass?>.allocate(capacity: loadedCount)
defer { classes.deallocate() }

if systemFramework && sysCount < depth {
sysCount += 1
} else if !systemFramework && depsCount < depth {
depsCount += 1
} else if sysCount < depth || depsCount < depth {
continue
} else {
break
}
_ = objc_getClassList(AutoreleasingUnsafeMutablePointer(classes), Int32(loadedCount))

let md5Ctx = systemFramework ? sysCtx : depsCtx
updateInfoFor(image, md5Ctx)
}
for idx in 0..<loadedCount {
if let `class` = classes[idx] {
var info = Dl_info()
if dladdr(unsafeBitCast(`class`, to: UnsafeRawPointer.self), &info) != 0,
let imagePathPtr = info.dli_fname {
let imagePath = String(cString: imagePathPtr)

var md5Ctx = depsCtx
if imagePath.isSystemFramework {
md5Ctx = sysCtx
} else if let execPath = Bundle.main.executablePath, imagePath.hasPrefix(execPath) {
md5Ctx = appCtx
}

if let executablePath = Bundle.main.executablePath {
updateInfoFor(executablePath, appCtx)
let className = NSStringFromClass(`class`)
CC_MD5_Update(md5Ctx, className, CC_LONG(className.count))
}
}
}
}

let depsHash = getFinalHash(depsCtx)
Expand Down
12 changes: 4 additions & 8 deletions HCaptcha/Classes/HCaptchaLog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// HCaptchaEvent.swift
// HCaptcha
//
// Copyright © 2023 HCaptcha. All rights reserved.
// Copyright © 2024 HCaptcha. All rights reserved.
//

import Foundation
import os

/** Internal SDK logger level
*/
Expand Down Expand Up @@ -50,17 +51,12 @@ internal class HCaptchaLogger {
}

let formattedMessage = String(format: message, arguments: args)
let logMessage = "\(timestamp) \(threadId) HCaptcha/\(level.description): \(formattedMessage)"
let logMessage = "\(threadId) HCaptcha/\(level.description): \(formattedMessage)"

print(logMessage)
NSLog(logMessage)
#endif
}

private static var timestamp: String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.sss"
return dateFormatter.string(from: Date())
}

private static var threadId: String {
return Thread.isMainThread ? "main" : "\(pthread_self())"
Expand Down

0 comments on commit 66b750e

Please sign in to comment.