From c15615132cb35293d76a2c8ae4056b1187c2ca93 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Fri, 13 Sep 2024 12:33:52 +0200 Subject: [PATCH 1/2] align logging with zenoh-c --- docs/commons.rst | 3 ++- include/zenoh/api/logging.hxx | 20 ++++++++++++++++---- tests/zenohc/config.cxx | 2 +- zenoh-c | 2 +- zenoh-pico | 2 +- 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/docs/commons.rst b/docs/commons.rst index f06d90f7..867896a2 100644 --- a/docs/commons.rst +++ b/docs/commons.rst @@ -79,4 +79,5 @@ Sample Logging ------- -.. doxygenfunction:: init_logging +.. doxygenfunction:: try_init_log_from_env +.. doxygenfunction:: init_log_from_env_or diff --git a/include/zenoh/api/logging.hxx b/include/zenoh/api/logging.hxx index 0d642a5f..ced72c2d 100644 --- a/include/zenoh/api/logging.hxx +++ b/include/zenoh/api/logging.hxx @@ -17,12 +17,24 @@ namespace zenoh { #ifdef ZENOHCXX_ZENOHC -/// @brief Enable logging. +/// @brief Initializes the zenoh runtime logger, using rust environment settings. +/// E.g.: `RUST_LOG=info` will enable logging at info level. Similarly, you can set the variable to `error` or `debug`. +/// Note that if the environment variable is not set, then logging will not be enabled. +/// See https://docs.rs/env_logger/latest/env_logger/index.html for accepted filter format. +/// @note zenoh-c only. +inline void try_init_log_from_env() { ::zc_try_init_log_from_env(); } + +/// @brief Initializes the zenoh runtime logger, using rust environment settings or the provided fallback filter. +/// E.g.: `RUST_LOG=info` will enable logging at info level. Similarly, you can set the variable to `error` or `debug`. /// -/// User may set environment variable RUST_LOG to values *debug* | *info* | *warn* | *error* to show diagnostic output. +/// Note that if the environment variable is not set, then fallback filter will be used instead. +/// See https://docs.rs/env_logger/latest/env_logger/index.html for accepted filter format. /// -/// @note zenoh-c only -inline void init_logging() { ::zc_init_logging(); } +/// @param fallback_filter: The fallback filter if the `RUST_LOG` environment variable is not set. +/// @note zenoh-c only. +inline void init_log_from_env_or(const std::string& fallback_filter) { + ::zc_init_log_from_env_or(fallback_filter.c_str()); +} #endif } // namespace zenoh \ No newline at end of file diff --git a/tests/zenohc/config.cxx b/tests/zenohc/config.cxx index b6aba6bf..7b45e43b 100644 --- a/tests/zenohc/config.cxx +++ b/tests/zenohc/config.cxx @@ -36,7 +36,7 @@ void test_config_to_string() { } int main(int argc, char** argv) { - init_logging(); + init_log_from_env_or("error"); test_config_insert(); test_config_to_string(); // TODO: add more tests diff --git a/zenoh-c b/zenoh-c index c453da54..280e47e8 160000 --- a/zenoh-c +++ b/zenoh-c @@ -1 +1 @@ -Subproject commit c453da549a0933ba27090bc929e310ca99758b84 +Subproject commit 280e47e8f57af04afe67f0897f3307317d240ed3 diff --git a/zenoh-pico b/zenoh-pico index c5106acd..0a62ea80 160000 --- a/zenoh-pico +++ b/zenoh-pico @@ -1 +1 @@ -Subproject commit c5106acd10c9d43a792b2f615da3a896bc167622 +Subproject commit 0a62ea80b48e2e56e910248fc3fe93180e01af93 From 5908c96949bbd55eddfabee4914c44d499b69610 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Fri, 13 Sep 2024 14:24:15 +0200 Subject: [PATCH 2/2] add logging to all the examples --- examples/simple/universal/z_simple.cxx | 3 +++ examples/simple/zenohc/zc_simple.cxx | 1 + examples/universal/z_delete.cxx | 3 +++ examples/universal/z_get.cxx | 3 +++ examples/universal/z_get_attachment.cxx | 3 +++ examples/universal/z_get_channel.cxx | 3 +++ examples/universal/z_get_channel_non_blocking.cxx | 3 +++ examples/universal/z_info.cxx | 3 +++ examples/universal/z_ping.cxx | 3 +++ examples/universal/z_pong.cxx | 3 +++ examples/universal/z_pub.cxx | 3 +++ examples/universal/z_pub_attachment.cxx | 3 +++ examples/universal/z_pub_delete.cxx | 3 +++ examples/universal/z_pub_thr.cxx | 3 +++ examples/universal/z_put.cxx | 3 +++ examples/universal/z_queryable.cxx | 3 +++ examples/universal/z_queryable_attachment.cxx | 3 +++ examples/universal/z_scout.cxx | 3 +++ examples/universal/z_sub.cxx | 3 +++ examples/universal/z_sub_attachment.cxx | 3 +++ examples/universal/z_sub_thr.cxx | 3 +++ examples/zenohc/z_get_liveliness.cxx | 3 +++ examples/zenohc/z_get_shm.cxx | 3 +++ examples/zenohc/z_liveliness.cxx | 3 +++ examples/zenohc/z_ping_shm.cxx | 3 +++ examples/zenohc/z_pub_shm.cxx | 3 +++ examples/zenohc/z_pub_shm_thr.cxx | 3 +++ examples/zenohc/z_queryable_shm.cxx | 3 +++ examples/zenohc/z_sub_liveliness.cxx | 3 +++ examples/zenohc/z_sub_shm.cxx | 3 +++ zenoh-c | 2 +- 31 files changed, 89 insertions(+), 1 deletion(-) diff --git a/examples/simple/universal/z_simple.cxx b/examples/simple/universal/z_simple.cxx index 373306a2..81867066 100644 --- a/examples/simple/universal/z_simple.cxx +++ b/examples/simple/universal/z_simple.cxx @@ -162,6 +162,9 @@ class CustomSubscriber { int main(int, char**) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif Config config = Config::create_default(); auto session = Session::open(std::move(config)); diff --git a/examples/simple/zenohc/zc_simple.cxx b/examples/simple/zenohc/zc_simple.cxx index 20495eee..37a7e936 100644 --- a/examples/simple/zenohc/zc_simple.cxx +++ b/examples/simple/zenohc/zc_simple.cxx @@ -18,6 +18,7 @@ using namespace zenoh; int main(int, char **) { + init_log_from_env_or("error"); Config config = Config::create_default(); auto session = Session::open(std::move(config)); session.put("demo/example/simple", "Simple!"); diff --git a/examples/universal/z_delete.cxx b/examples/universal/z_delete.cxx index b4757a65..316b7ce6 100644 --- a/examples/universal/z_delete.cxx +++ b/examples/universal/z_delete.cxx @@ -71,6 +71,9 @@ int _main(int argc, char **argv) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/universal/z_get.cxx b/examples/universal/z_get.cxx index 568e18a0..fc4da7cd 100644 --- a/examples/universal/z_get.cxx +++ b/examples/universal/z_get.cxx @@ -103,6 +103,9 @@ int _main(int argc, char **argv) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/universal/z_get_attachment.cxx b/examples/universal/z_get_attachment.cxx index 409b622a..64993866 100644 --- a/examples/universal/z_get_attachment.cxx +++ b/examples/universal/z_get_attachment.cxx @@ -98,6 +98,9 @@ int _main(int argc, char **argv) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/universal/z_get_channel.cxx b/examples/universal/z_get_channel.cxx index 3e396d09..734eb356 100644 --- a/examples/universal/z_get_channel.cxx +++ b/examples/universal/z_get_channel.cxx @@ -83,6 +83,9 @@ int _main(int argc, char **argv) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/universal/z_get_channel_non_blocking.cxx b/examples/universal/z_get_channel_non_blocking.cxx index 6dc738f7..b9f7e0df 100644 --- a/examples/universal/z_get_channel_non_blocking.cxx +++ b/examples/universal/z_get_channel_non_blocking.cxx @@ -97,6 +97,9 @@ int _main(int argc, char **argv) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/universal/z_info.cxx b/examples/universal/z_info.cxx index 42f50525..52c02838 100644 --- a/examples/universal/z_info.cxx +++ b/examples/universal/z_info.cxx @@ -73,6 +73,9 @@ int _main(int argc, char** argv) { int main(int argc, char** argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif return _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/universal/z_ping.cxx b/examples/universal/z_ping.cxx index 7cbb45c4..cdc92ff0 100644 --- a/examples/universal/z_ping.cxx +++ b/examples/universal/z_ping.cxx @@ -142,6 +142,9 @@ struct args_t parse_args(int argc, char** argv) { int main(int argc, char** argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif return _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/universal/z_pong.cxx b/examples/universal/z_pong.cxx index e45089d4..ad7ede37 100644 --- a/examples/universal/z_pong.cxx +++ b/examples/universal/z_pong.cxx @@ -36,6 +36,9 @@ int _main(int, char **) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/universal/z_pub.cxx b/examples/universal/z_pub.cxx index d29929a7..258836f1 100644 --- a/examples/universal/z_pub.cxx +++ b/examples/universal/z_pub.cxx @@ -101,6 +101,9 @@ int _main(int argc, char **argv) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/universal/z_pub_attachment.cxx b/examples/universal/z_pub_attachment.cxx index 4a06f31c..1fe2f8ef 100644 --- a/examples/universal/z_pub_attachment.cxx +++ b/examples/universal/z_pub_attachment.cxx @@ -90,6 +90,9 @@ int _main(int argc, char **argv) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/universal/z_pub_delete.cxx b/examples/universal/z_pub_delete.cxx index 8eb481f0..c59b1650 100644 --- a/examples/universal/z_pub_delete.cxx +++ b/examples/universal/z_pub_delete.cxx @@ -76,6 +76,9 @@ int _main(int argc, char **argv) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/universal/z_pub_thr.cxx b/examples/universal/z_pub_thr.cxx index 322fb786..95a07dac 100644 --- a/examples/universal/z_pub_thr.cxx +++ b/examples/universal/z_pub_thr.cxx @@ -81,6 +81,9 @@ int _main(int argc, char **argv) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/universal/z_put.cxx b/examples/universal/z_put.cxx index addc2bdd..a2ba8aa0 100644 --- a/examples/universal/z_put.cxx +++ b/examples/universal/z_put.cxx @@ -88,6 +88,9 @@ int _main(int argc, char **argv) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/universal/z_queryable.cxx b/examples/universal/z_queryable.cxx index 4620ec56..92fdf83d 100644 --- a/examples/universal/z_queryable.cxx +++ b/examples/universal/z_queryable.cxx @@ -106,6 +106,9 @@ int _main(int argc, char **argv) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/universal/z_queryable_attachment.cxx b/examples/universal/z_queryable_attachment.cxx index d41f9e55..e5680c2a 100644 --- a/examples/universal/z_queryable_attachment.cxx +++ b/examples/universal/z_queryable_attachment.cxx @@ -101,6 +101,9 @@ int _main(int argc, char **argv) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/universal/z_scout.cxx b/examples/universal/z_scout.cxx index eb8362e4..4f51f0b0 100644 --- a/examples/universal/z_scout.cxx +++ b/examples/universal/z_scout.cxx @@ -111,6 +111,9 @@ int _main(int argc, char **argv) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/universal/z_sub.cxx b/examples/universal/z_sub.cxx index 6d0b7d17..4ce1ea65 100644 --- a/examples/universal/z_sub.cxx +++ b/examples/universal/z_sub.cxx @@ -97,6 +97,9 @@ int _main(int argc, char **argv) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/universal/z_sub_attachment.cxx b/examples/universal/z_sub_attachment.cxx index 41acc4df..a99ef165 100644 --- a/examples/universal/z_sub_attachment.cxx +++ b/examples/universal/z_sub_attachment.cxx @@ -85,6 +85,9 @@ int _main(int argc, char **argv) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/universal/z_sub_thr.cxx b/examples/universal/z_sub_thr.cxx index 50b5b462..60f6f305 100644 --- a/examples/universal/z_sub_thr.cxx +++ b/examples/universal/z_sub_thr.cxx @@ -122,6 +122,9 @@ int _main(int argc, char **argv) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/zenohc/z_get_liveliness.cxx b/examples/zenohc/z_get_liveliness.cxx index 0696ef39..404a63a3 100644 --- a/examples/zenohc/z_get_liveliness.cxx +++ b/examples/zenohc/z_get_liveliness.cxx @@ -79,6 +79,9 @@ int _main(int argc, char **argv) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/zenohc/z_get_shm.cxx b/examples/zenohc/z_get_shm.cxx index 963d8787..ca63367b 100644 --- a/examples/zenohc/z_get_shm.cxx +++ b/examples/zenohc/z_get_shm.cxx @@ -94,6 +94,9 @@ int _main(int argc, char **argv) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/zenohc/z_liveliness.cxx b/examples/zenohc/z_liveliness.cxx index aa508d97..c72a4dfe 100644 --- a/examples/zenohc/z_liveliness.cxx +++ b/examples/zenohc/z_liveliness.cxx @@ -68,6 +68,9 @@ int _main(int argc, char **argv) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/zenohc/z_ping_shm.cxx b/examples/zenohc/z_ping_shm.cxx index f7da459c..755f9c6a 100644 --- a/examples/zenohc/z_ping_shm.cxx +++ b/examples/zenohc/z_ping_shm.cxx @@ -147,6 +147,9 @@ struct args_t parse_args(int argc, char** argv) { int main(int argc, char** argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif return _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/zenohc/z_pub_shm.cxx b/examples/zenohc/z_pub_shm.cxx index 5ebff2ba..152f2d3d 100644 --- a/examples/zenohc/z_pub_shm.cxx +++ b/examples/zenohc/z_pub_shm.cxx @@ -87,6 +87,9 @@ int _main(int argc, char **argv) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/zenohc/z_pub_shm_thr.cxx b/examples/zenohc/z_pub_shm_thr.cxx index f4e9943f..e22e1842 100644 --- a/examples/zenohc/z_pub_shm_thr.cxx +++ b/examples/zenohc/z_pub_shm_thr.cxx @@ -68,6 +68,9 @@ int _main(int argc, char **argv) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/zenohc/z_queryable_shm.cxx b/examples/zenohc/z_queryable_shm.cxx index b672258f..a8fbee8d 100644 --- a/examples/zenohc/z_queryable_shm.cxx +++ b/examples/zenohc/z_queryable_shm.cxx @@ -105,6 +105,9 @@ int _main(int argc, char **argv) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/zenohc/z_sub_liveliness.cxx b/examples/zenohc/z_sub_liveliness.cxx index e4392d6e..653fde88 100644 --- a/examples/zenohc/z_sub_liveliness.cxx +++ b/examples/zenohc/z_sub_liveliness.cxx @@ -75,6 +75,9 @@ int _main(int argc, char **argv) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/examples/zenohc/z_sub_shm.cxx b/examples/zenohc/z_sub_shm.cxx index 3e870ef7..66f1a3b7 100644 --- a/examples/zenohc/z_sub_shm.cxx +++ b/examples/zenohc/z_sub_shm.cxx @@ -107,6 +107,9 @@ int _main(int argc, char **argv) { int main(int argc, char **argv) { try { +#ifdef ZENOHCXX_ZENOHC + init_log_from_env_or("error"); +#endif _main(argc, argv); } catch (ZException e) { std::cout << "Received an error :" << e.what() << "\n"; diff --git a/zenoh-c b/zenoh-c index 280e47e8..d05e7a82 160000 --- a/zenoh-c +++ b/zenoh-c @@ -1 +1 @@ -Subproject commit 280e47e8f57af04afe67f0897f3307317d240ed3 +Subproject commit d05e7a823010365014db49c1e9f536ef8394977a