Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional custom format specifiers to DDLogMessageFormat #1412

Open
696GrocuttT opened this issue Mar 10, 2024 · 1 comment
Open

Add optional custom format specifiers to DDLogMessageFormat #1412

696GrocuttT opened this issue Mar 10, 2024 · 1 comment

Comments

@696GrocuttT
Copy link

It would be helpful to be able to specify custom format strings when using string interpolation to generate messages. This is especially true when handling floating point numbers. At first sight it looks like this could be done by changing the methods in
DDLogMessageFormat.StringInterpolation from this style:

    @inlinable
    public mutating func appendInterpolation(_ flt: Float) {
        storage.addValue(flt, withSpecifier: "%f")
    }

to this style:

    @inlinable
    public mutating func appendInterpolation(_ flt: Float, specifier: String = "%f") {
        storage.addValue(flt, withSpecifier: specifier)
    }

This would enable formatting control when using string interpolation like this:

    DDLogDebug("my float value is \(myFloat, specifier: "%.2f")")
@ffried
Copy link
Member

ffried commented Mar 19, 2024

Unfortunately it's not that easy. With your proposed solution, the following code would be accepted by the compiler:

DDLogDebug("Some float value: \(someFloat, specifier: "whatever")")

Which would then lead to a runtime crash.

The proper way would be to add fully typed format specifiers, similar to what OSLog does.

In the meantime, I'd recommend using FormatStyle if possible. This will lead to the parameter being recorded as string, though. However, since we're not (yet) doing much with the concrete values (except for recording the format), that's fine for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants