You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Use logger in a dynamically loaded shared library #66
Since a dynamically loaded shared library is compiled separately from the executable. The linker would need to resolve the logger in the library to the instance in the executable at run-time when we invoke "dlopen".
Having a way to ask dynamic library not allocate but to look for in run-time would make log usable in dynamically loaded library.
I know. Configure both the main program and the shared library to dynamically rather than statically link to their dependencies and the dynamic linker should make everything work when you dlopen the plugin.
@sfackler say I have executable Foo, and dynamic library Bar, which is loaded by Foo using "dlopen" at run-time. Do you mean I should let Bar link to crate Log dynamically?
Use cargo rustc -- -C link-args='-rdynamic' to compile the application with logger. ldd will dynamically link the library logger symbol to the application's location at run-time.
Closing this.
I stumbled across this thinking its the perfect solution for my issue, but I could not get this to work.
I have a main application that initializes a logger, and then plugins to that application that are compiled as shared libraries and loaded at runtime using the libloading crate. Any logging statements using the macros from within these plugins are not picked up by my logger, even when using the compilation notes above. I'm sorry to bring up this issue years later, but any help would be appreciated. Thanks!
For what its worth - I switched to using env_logger crate. Then, in both my main application and my dynamically loaded libraries I call env_logger::builder().format_timestamp(None).init(); at the top, and everything seems to log the way I was originally hoping. Feels clunky, but it works for me.
When the application opens the dll, just call the init_logger in the application domain like this
init_logger(log_param());
log_param function called in the application, so the function points to the application function.
init function called in the dll, but the param given from the application, so they are the same.
Activity
sfackler commentedon Dec 8, 2015
If you compile the main application and the shared library to both be dynamically linked to the log crate, they should work together.
WiSaGaN commentedon Dec 8, 2015
The shared library is compiled separately. It's used with a "dlopen" in executable.
sfackler commentedon Dec 8, 2015
I know. Configure both the main program and the shared library to dynamically rather than statically link to their dependencies and the dynamic linker should make everything work when you dlopen the plugin.
WiSaGaN commentedon Dec 9, 2015
@sfackler say I have executable Foo, and dynamic library Bar, which is loaded by Foo using "dlopen" at run-time. Do you mean I should let Bar link to crate Log dynamically?
WiSaGaN commentedon May 24, 2016
Use
cargo rustc -- -C link-args='-rdynamic'to compile the application with logger.lddwill dynamically link the library logger symbol to the application's location at run-time.Closing this.
aaeATS commentedon Sep 1, 2020
I stumbled across this thinking its the perfect solution for my issue, but I could not get this to work.
I have a main application that initializes a logger, and then plugins to that application that are compiled as shared libraries and loaded at runtime using the libloading crate. Any logging statements using the macros from within these plugins are not picked up by my logger, even when using the compilation notes above. I'm sorry to bring up this issue years later, but any help would be appreciated. Thanks!
jlgerber commentedon Mar 11, 2021
I have stumbled across this as well.
aaeATS commentedon Mar 11, 2021
For what its worth - I switched to using env_logger crate. Then, in both my main application and my dynamically loaded libraries I call
env_logger::builder().format_timestamp(None).init();at the top, and everything seems to log the way I was originally hoping. Feels clunky, but it works for me.Good luck
lazytiger commentedon Jun 17, 2021
Just put this file in your application project.
And then write a function like this in your dll project to be called in the application.
When the application opens the dll, just call the init_logger in the application domain like this
log_param function called in the application, so the function points to the application function.
init function called in the dll, but the param given from the application, so they are the same.
Merge pull request rust-lang#66 from ryankurte/macos-use-zip