session-ios/SessionUtilitiesKit/General/Logging.swift

45 lines
1.7 KiB
Swift
Raw Normal View History

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
//
// stringlint:disable
import Foundation
2021-10-05 06:45:54 +02:00
import SignalCoreKit
2020-11-05 02:07:21 +01:00
private extension DispatchQueue {
static var isDBWriteQueue: Bool {
/// The `dispatch_queue_get_label` function is used to get the label for a given DispatchQueue, in Swift this
/// was replaced with the `label` property on a queue instance but you used to be able to just pass `nil` in order
/// to get the name of the current queue - it seems that there might be a hole in the current design where there isn't
/// a built-in way to get the label of the current queue natively in Swift
///
/// On a positive note it seems that we can safely call `__dispatch_queue_get_label(nil)` in order to do this,
/// it won't appear in auto-completed code but works properly
///
/// For more information see
/// https://developer.apple.com/forums/thread/701313?answerId=705773022#705773022
/// https://forums.swift.org/t/gcd-getting-current-dispatch-queue-name-with-swift-3/3039/2
return (String(cString: __dispatch_queue_get_label(nil)) == "\(Storage.queuePrefix).writer")
}
}
2020-11-05 02:15:57 +01:00
public func SNLog(_ message: String) {
let logPrefixes: String = [
"Session",
(Thread.isMainThread ? "Main" : nil),
(DispatchQueue.isDBWriteQueue ? "DBWrite" : nil)
]
.compactMap { $0 }
.joined(separator: ", ")
2020-11-05 02:07:21 +01:00
#if DEBUG
print("[\(logPrefixes)] \(message)")
2020-11-05 02:07:21 +01:00
#endif
OWSLogger.info("[\(logPrefixes)] \(message)")
2020-11-05 02:07:21 +01:00
}
public func SNLogNotTests(_ message: String) {
guard ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] == nil else { return }
SNLog(message)
}