Getting the current queue and thread name?

The Xcode console logs an identifier indicating the current thread that's running when a message is print()ed from Swift. Some more complex logging systems (e.g. swift-log and console-kit) don't provide that information, and I'm trying to write a logging back-end for swift-log that will.

Thing is, I don't see any way to get the current queue name in Swift. I tried

dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL)

But I get "cannot convert value of type '()' to expected argument type 'DispatchQueue?'". Passing nil instead results in "'dispatch_queue_get_label' has been replaced by property 'DispatchQueue.label'".

This is not an unreasonable thing to want to do, and it poses no safety concerns.

Try this:

import Dispatch

let queue = DispatchQueue(label: "hello-cruel-world")

func main() {
    queue.async {
        print(String(cString: __dispatch_queue_get_label(nil)))
        exit(0)
    }
    dispatchMain()
}

main()

There’s a lot to unpack here:

  • DISPATCH_CURRENT_QUEUE_LABEL is just a macro that returns NULL and is not imported into Swift. Rather, just call dispatch_queue_get_label with nil.

  • In general, dispatch_queue_get_label has been replaced by the label property on a DispatchQueue object.

  • However, that doesn’t cover the case where you pass in NULL to get the current queue’s label. AFAICT there’s no Swift-friendly way to do that and I encourage you to file an enhancement request for that. For example, it might be nice to have a currentQueueLabel static property on DispatchQueue itself.

    Please post your bug number, just for the record.

  • In the meantime, work around this by calling __dispatch_queue_get_label. When a routine, dispatch_queue_get_label, is marked as being refined in Swift, the unrefined version is available via the __ prefix. This doesn’t show up in code completion, but you can happily use it.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hi Quinn (@eskimo),

The String(cString: __dispatch_queue_get_label(nil)) works great under macOS, but it fails to compile under Linux. Looking through the https://github.com/apple/swift-corelibs-libdispatch/tree/main/src/swift there does not seem to be any mention of this.

Thoughts? Martin

The C underpinnings of the Swift libraries on Linux are not considered to be API. For example, Foundation is considered API on Linux but not Core Foundation. Likewise for Dispatch. If the Dispatch API, the Swift stuff, doesn’t have what you need, you can’t fall back to the C API like you can on Apple platforms.

So, if you need this functionality on Linux my advice is:

  1. File a bug against swift-corelibs-libdispatch.

  2. Optionally, fix that bug and submit a pull request.

It would also make sense to file a bug against the Dispatch Swift API on Apple platforms. You shouldn’t need to drop down to the C API to get this functionality.

Please post your bug number, well bug numbers, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

@eskimo Enhancement request: FB11800863

Getting the current queue and thread name?
 
 
Q