Is there a way to log the thread or queue that a method is running on / called from? Something like:
- (void)foo
{
NSLog(@"Running on %@ queue, %@ thread", queue, thread);
}
You can get the current thread with +[NSThread currentThread]
. That could have a name
property, but if you didn't set one don't rely on it.
Queues are trickier because there are different meanings of "queue". A queue could be an NSOperationQueue
, and you can grab its name
from +[NSOperationQueue currentQueue]
(again, assuming you set it).
Then there are dispatch queues. You can get the current queue with dispatch_get_current_queue()
, but be warned that this function will succeed even if called from code that isn't associated with a queue(!). In that case it returns the default background queue Queues are labeled, so you can call dispatch_queue_get_label()
and if you created the queue with a label, you will get that.
So basically, yes you can get the queue or thread—with the proviso that all code has an associated dispatch queue even if it isn't code that was dispatched. You can also usually get meaningful names for these threads and queues, which is handy for debugging: but it's your responsibility to name them.
Here's some Swift code that I'm currently using. This is partly based on another answer I previously posted here at Stack Overflow: https://stackoverflow.com/a/41294559/253938
/// Struct to contain either the thread name or an (arbitrary) thread number for the current thread.
/// This is partly inspired by code in BaseDestination.swift in XCGLogger. Main thread is
/// arbitrarily given thread number 0. If no thread name can be determined then the memory address
/// of the current Thread object is arbitrarily used as the thread number.
///
/// Re use of "__dispatch_queue_get_label(nil)" (seen in XCGLogger) see here:
/// https://stackoverflow.com/questions/40186868/get-gcd-label-in-swift-3
internal struct ThreadInfo : CustomStringConvertible {
var threadName : String? = nil
var threadNumber : Int64? = nil
/// Initializer.
public init() {
// Process main thread (call it thread 0) and threads whose name can be determined
if Thread.isMainThread {
threadNumber = 0
}
else if let threadName = Thread.current.name, !threadName.isEmpty {
self.threadName = threadName
}
else if let queueName = String(validatingUTF8: __dispatch_queue_get_label(nil)),
!queueName.isEmpty {
threadName = queueName
}
else {
// Convert the memory address of the current Thread object into an Int64 and use it as the
// (arbitrary) thread number
let objPtr = Unmanaged.passUnretained(Thread.current).toOpaque()
let onePtr = UnsafeMutableRawPointer(bitPattern: 1)! // Absolute memory location 1
let rawAddress : Int64 = onePtr.distance(to: objPtr) + 1 // May include high-order bits
threadNumber = rawAddress % (256 * 1024 * 1024 * 1024) // Remove possible high-order bits
}
}
/// Method to implement CustomStringConvertible, returning either the thread name if possible or
/// else the (arbitrary) thread number.
public var description: String {
get {
return
threadName != nil ? String(describing: threadName!) : String(describing: threadNumber!)
}
}
}
To use this just instantiate ThreadInfo while running on the thread in question. Then you can display ThreadInfo or embed it in logging data.
let threadInfo = ThreadInfo()
print(threadInfo)
To get the thread, you can use
NSLog(@"Running on %@ thread", [NSThread currentThread]);
You can get the current dispatch queue like this:
dispatch_queue_t dispatch_get_current_queue(void);
But the header has the following warnings:
Recommended for debugging and logging purposes only:
The code must not make any assumptions about the queue returned, unless it is one of the global queues or a queue the code has itself created. The code must not assume that synchronous execution onto a queue is safe from deadlock if that queue is not the one returned by dispatch_get_current_queue().
so YMMV.
[NSThread currentThread]
, as seen here.