Skip to content

Commit

Permalink
fix: improve logging performance by switch from print to NSLog
Browse files Browse the repository at this point in the history
  • Loading branch information
CAMOBAP committed Oct 13, 2024
1 parent 4152d62 commit d2920ac
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 44 deletions.
62 changes: 26 additions & 36 deletions HCaptcha/Classes/HCaptchaDebugInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,14 @@

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 +32,6 @@ private func bundleShortVersion() -> String {
}

class HCaptchaDebugInfo {

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

private class func buildDebugInfoJson() -> String {
Expand All @@ -53,39 +43,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 i in 0..<loadedCount {
if let `class` = classes[i] {
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 d2920ac

Please sign in to comment.