AAkira / Napier

Logging library for Kotlin Multiplatform
Apache License 2.0
785 stars 34 forks source link

Cannot access Antilog from swift. #43

Closed stumi01 closed 4 years ago

stumi01 commented 4 years ago

Usecase: We want to create subclass of the Antilog abstract class in swift.

The warning is the following:

Imported declaration 'CommonNapierNapierLevel' could not be mapped to 'NapierNapier.Level'
5:28
__attribute__((swift_name("NapierNapier.Level")))

Because of it we cannot use anything which relates to the Napier.Level enum from swift code, therefore the Antilog superclass is empty.

As a workaround we created a similar class like the CrashlyticsAntilog in the example but would be better a bit if the library would not produce a warning.

RomeuG commented 4 years ago

@AAkira

Any news on this?

AAkira commented 4 years ago

@stumi01 @RomeuG

Do you mean to define a separate Level that is currently nested in the Napier class?

@ThreadLocal
object Napier {

    enum class Level {
        VERBOSE,
        ...
    }
   ....
}
enum class Level {
   VERBOSE,
    ...
}

@ThreadLocal
object Napier {
   ....
}
stumi01 commented 4 years ago

I think yes, that would solve the issue because the name problem comes from the fact that the Level enum is embedded inside the Napier object but I'm not 100% sure.

AAkira commented 4 years ago

@stumi01 @RomeuG

I published it as 1.5.0-alpha1. Please check it.

Napier.Level has been renamed to LogLevel.

horita-yuya commented 4 years ago

@AAkira @stumi01

Hi, I tried with the version.

import UIKit
import Common

final class SwiftAntilog: NapierAntilog {
    override func performLog(priority: NapierLogLevel, tag: String?, throwable: KotlinThrowable?, message: String?) {
        print("Swift: \(message ?? "")")
    }
}

final class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        NapierProxyKt.releaseBuild(antilog: SwiftAntilog())

        Sample().hello()

        DispatchQueue.global(qos: .background).async {
            // Without the below code, SwiftAntilog won't appear because Napier is ThreadLocal object.
//            NapierProxyKt.releaseBuild(antilog: SwiftAntilog())
            Sample().hello()
        }
    }
}

// Console
// Swift: Hello napier
// Swift: optional tag

Is this behavior you expect?

You can try this code with mpp-sample in Napier repository.

stumi01 commented 4 years ago

@AAkira Perfect, thank you.

AAkira commented 4 years ago

@horita-yuya Thank you for your support! 🥺

RomeuG commented 4 years ago

@AAkira

I guess there isn't any version of this patch for Kotlin 1.3.72 ?

AAkira commented 4 years ago

Actually, I can publish it for 1.3.72. But I don't recommend using the old Kotlin version in KMP.

RomeuG commented 4 years ago

Can you do it, if possible?

I understand what you mean, but in my current project, it is a bit early to update to 1.4.0. It's not like the differences are that huge from 1.3.72.

Regardless, I leave that to you, if you want to publish or not. :-)

AAkira commented 4 years ago

I'm sorry I don't want to publish it. 🙇‍♂️

As you know, it's a very complicated updated from Kotlin 1.3.x to 1.4.x. I have to revert this if I publish it. https://github.com/AAkira/Napier/pull/46/files

RomeuG commented 4 years ago

It's okay, I have to live with the warning for a while then. Thank you.