Skip to content

Commit

Permalink
Merge pull request #215 from DenisBiryukov91/align-logging
Browse files Browse the repository at this point in the history
align logging with zenoh-c
  • Loading branch information
milyin authored Sep 13, 2024
2 parents c25dbcd + 5908c96 commit 0094b57
Show file tree
Hide file tree
Showing 35 changed files with 109 additions and 8 deletions.
3 changes: 2 additions & 1 deletion docs/commons.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,5 @@ Sample
Logging
-------

.. doxygenfunction:: init_logging
.. doxygenfunction:: try_init_log_from_env
.. doxygenfunction:: init_log_from_env_or
3 changes: 3 additions & 0 deletions examples/simple/universal/z_simple.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
1 change: 1 addition & 0 deletions examples/simple/zenohc/zc_simple.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down
3 changes: 3 additions & 0 deletions examples/universal/z_delete.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/universal/z_get.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/universal/z_get_attachment.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/universal/z_get_channel.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/universal/z_get_channel_non_blocking.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/universal/z_info.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/universal/z_ping.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/universal/z_pong.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/universal/z_pub.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/universal/z_pub_attachment.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/universal/z_pub_delete.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/universal/z_pub_thr.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/universal/z_put.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/universal/z_queryable.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/universal/z_queryable_attachment.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/universal/z_scout.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/universal/z_sub.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/universal/z_sub_attachment.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/universal/z_sub_thr.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/zenohc/z_get_liveliness.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/zenohc/z_get_shm.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/zenohc/z_liveliness.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/zenohc/z_ping_shm.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/zenohc/z_pub_shm.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/zenohc/z_pub_shm_thr.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/zenohc/z_queryable_shm.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/zenohc/z_sub_liveliness.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions examples/zenohc/z_sub_shm.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
20 changes: 16 additions & 4 deletions include/zenoh/api/logging.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion tests/zenohc/config.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion zenoh-pico

0 comments on commit 0094b57

Please sign in to comment.