From e77a1b26b2d418d05ce7c633fcc3763f25d7f665 Mon Sep 17 00:00:00 2001 From: Jeremy Leibs Date: Thu, 25 May 2023 12:34:16 +0200 Subject: [PATCH] Clean up warnings printed when rr.init hasn't been called (#2209) The recording handle work changed the behavior of some of our warnings when calling a log API without having called `rr.init`. Before: ``` [2023-05-25T09:09:33Z WARN re_sdk::global] There is no currently active Data recording available for the current thread (ThreadId(1)): have you called `set_global()` and/or `set_thread_local()` first? WARNING:rerun:Rerun is disabled - log_text_box() call ignored ``` After: ``` WARNING:rerun:Rerun is disabled - log_text_box() call ignored. You must call rerun.init before using log APIs. ``` PR Build Summary: https://build.rerun.io/pr/2209 --- crates/re_sdk/src/global.rs | 27 +++++++++++++++++++ rerun_py/rerun_sdk/rerun/__init__.py | 5 +++- rerun_py/rerun_sdk/rerun/log/log_decorator.py | 4 ++- rerun_py/rerun_sdk/rerun/sinks.py | 2 +- rerun_py/src/python_bridge.rs | 4 +-- 5 files changed, 37 insertions(+), 5 deletions(-) diff --git a/crates/re_sdk/src/global.rs b/crates/re_sdk/src/global.rs index 5e15ad4c9ff7..caa00e842653 100644 --- a/crates/re_sdk/src/global.rs +++ b/crates/re_sdk/src/global.rs @@ -65,6 +65,33 @@ impl RecordingStream { rec } + // Internal implementation of `get()` that doesn't print a warning if no recording is found. + // Used from python-bridge. + #[inline] + #[doc(hidden)] + pub fn get_quiet( + which: RecordingType, + overrides: Option, + ) -> Option { + let rec = overrides.or_else(|| { + Self::get_any(RecordingScope::ThreadLocal, which) + .or_else(|| Self::get_any(RecordingScope::Global, which)) + }); + + if rec.is_none() { + // NOTE: This is the one and only place where a warning about missing active recording + // should be printed, don't stutter! + re_log::debug_once!( + "There is no currently active {which} recording available \ + for the current thread ({:?}): have you called `set_global()` and/or \ + `set_thread_local()` first?", + std::thread::current().id(), + ); + } + + rec + } + // --- Global --- /// Returns the currently active recording of the specified type in the global scope, if any. diff --git a/rerun_py/rerun_sdk/rerun/__init__.py b/rerun_py/rerun_sdk/rerun/__init__.py index a36b5480803e..cd5c88b3d789 100644 --- a/rerun_py/rerun_sdk/rerun/__init__.py +++ b/rerun_py/rerun_sdk/rerun/__init__.py @@ -366,7 +366,10 @@ def start_web_viewer_server(port: int = 0) -> None: """ if not bindings.is_enabled(): - logging.warning("Rerun is disabled - self_host_assets() call ignored") + logging.warning( + "Rerun is disabled - start_web_viewer_server() call ignored. You must call rerun.init before starting the" + + " web viewer server." + ) return bindings.start_web_viewer_server(port) diff --git a/rerun_py/rerun_sdk/rerun/log/log_decorator.py b/rerun_py/rerun_sdk/rerun/log/log_decorator.py index 815bbd94f6d5..0d1db629c5f0 100644 --- a/rerun_py/rerun_sdk/rerun/log/log_decorator.py +++ b/rerun_py/rerun_sdk/rerun/log/log_decorator.py @@ -32,7 +32,9 @@ def wrapper(*args: Any, **kwargs: Any) -> Any: recording = RecordingStream.to_native(kwargs.get("recording")) if not bindings.is_enabled(recording): # NOTE: use `warnings` which handles runtime deduplication. - warnings.warn(f"Rerun is disabled - {func.__name__}() call ignored") + warnings.warn( + f"Rerun is disabled - {func.__name__}() call ignored. You must call rerun.init before using log APIs." + ) return if rerun.strict_mode(): diff --git a/rerun_py/rerun_sdk/rerun/sinks.py b/rerun_py/rerun_sdk/rerun/sinks.py index d06464732218..ad854e38d1ac 100644 --- a/rerun_py/rerun_sdk/rerun/sinks.py +++ b/rerun_py/rerun_sdk/rerun/sinks.py @@ -50,7 +50,7 @@ def save(path: str, recording: Optional[RecordingStream] = None) -> None: """ if not bindings.is_enabled(): - logging.warning("Rerun is disabled - save() call ignored") + logging.warning("Rerun is disabled - save() call ignored. You must call rerun.init before saving a recording.") return recording = RecordingStream.to_native(recording) diff --git a/rerun_py/src/python_bridge.rs b/rerun_py/src/python_bridge.rs index 0dc3bdd33e42..da18e9155e11 100644 --- a/rerun_py/src/python_bridge.rs +++ b/rerun_py/src/python_bridge.rs @@ -274,7 +274,7 @@ fn get_recording_id(recording: Option<&PyRecordingStream>) -> Option { /// specified recording otherwise, if any. #[pyfunction] fn get_data_recording(recording: Option<&PyRecordingStream>) -> Option { - RecordingStream::get( + RecordingStream::get_quiet( rerun::RecordingType::Data, recording.map(|rec| rec.0.clone()), ) @@ -345,7 +345,7 @@ fn set_thread_local_data_recording( /// specified recording otherwise, if any. #[pyfunction] fn get_blueprint_recording(overrides: Option<&PyRecordingStream>) -> Option { - RecordingStream::get( + RecordingStream::get_quiet( rerun::RecordingType::Blueprint, overrides.map(|rec| rec.0.clone()), )