Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: re_sdk no longer depends on re_viewer #1507

Merged
merged 30 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
da739d5
Refactor: move the session log sink to its own file
emilk Mar 4, 2023
0fc0583
Turn LogSink into a trait
emilk Mar 4, 2023
5913ce8
Move spawning of native viewer to `rerun` crate
emilk Mar 4, 2023
74a2cee
Move the `show()` method to `rerun` crate
emilk Mar 4, 2023
1a3fdc7
re_sdk no longer depend directly on re_viewer
emilk Mar 4, 2023
de0370d
Prevent re_viewer from accidentally depending on re_web_viewer_server
emilk Mar 4, 2023
ab2847e
Improve doc-strings
emilk Mar 4, 2023
593d02b
Remove the native_viewer feature from re_sdk
emilk Mar 4, 2023
bfabb19
Remove direct dependency of rerun_py on re_sdk_comms
emilk Mar 4, 2023
bd2cdc8
Small doc-fixes
emilk Mar 4, 2023
20967f6
Move `clap` hookup from `re_sdk` to `rerun`
emilk Mar 4, 2023
6f0c874
Move the `session.serve()` method to trait `rerun::WebViewerSessionExt`
emilk Mar 4, 2023
ead647d
cleanup
emilk Mar 4, 2023
bde8830
Remove cyclic-dependency-protection because it caused weird warning:
emilk Mar 4, 2023
9cd90c9
Fix web-viewer
emilk Mar 4, 2023
dc0a971
Fix python build
emilk Mar 4, 2023
c04001e
Fix remove_dir_all RUSTSEC
emilk Mar 4, 2023
9f50b60
Merge branch 'emilk/remove-remove_dir_all' into emilk/rust-sdk-refactor
emilk Mar 4, 2023
cc63258
Trim re_sdk dependency list
emilk Mar 4, 2023
6f01eec
Fix doc-tests
emilk Mar 4, 2023
d8043dd
Fix doclinks
emilk Mar 4, 2023
0f1f436
Document rust_version
emilk Mar 4, 2023
98705aa
Better docs for set_sink
emilk Mar 4, 2023
e087a74
Hide `global_session()` behind the `global` feature flag
emilk Mar 5, 2023
62023b5
Fix typos
emilk Mar 5, 2023
501fe30
Merge branch 'main' into emilk/rust-sdk-refactor
teh-cmc Mar 6, 2023
454f78f
Proper rustc & LLVM versions (#1512)
teh-cmc Mar 6, 2023
c9de301
Rename "global" feature to "global_session"
emilk Mar 6, 2023
1dd0333
Replace extension trait with `serve_web_viewer` method
emilk Mar 6, 2023
f5d960a
Merge branch 'main' into emilk/rust-sdk-refactor
emilk Mar 6, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/typos.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# https://github.com/crate-ci/typos
# Add exceptions to _typos.toml
# install and run locally: cargo install typos-cli && typos

name: Spell Check
on: [pull_request]
Expand Down
10 changes: 3 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/re_log_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ pub enum RecordingSource {

/// The official Rerun Rust Logging SDK
RustSdk {
/// `CARGO_PKG_RUST_VERSION` = the MSRV specified by out crate. TODO(#1509): remove or improve
rust_version: String,
},

Expand Down
39 changes: 13 additions & 26 deletions crates/re_sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,64 +17,51 @@ all-features = true


[features]
default = ["demo", "glam", "image", "native_viewer"]

## Enable telemetry using our analytics SDK.
analytics = ["re_web_viewer_server?/analytics", "re_viewer?/analytics"]
default = ["demo", "glam", "image"]

## Enable the `demo` module (helpers for Rerun examples).
demo = []

## Add support for some math operations using [`glam`](https://crates.io/crates/glam/).
glam = ["re_log_types/glam"]

## Add the `global_session` method.
global = ["dep:once_cell", "dep:parking_lot"]
emilk marked this conversation as resolved.
Show resolved Hide resolved

## Integration with the [`image`](https://crates.io/crates/image/) crate.
image = ["re_log_types/image"]

## Support for the native viewer.
native_viewer = ["image", "dep:re_viewer"]

## Support serving a web viewer over HTTP.
##
## Enabling this adds quite a bit to the compile time and binary size,
## since it requires compiling and bundling the viewer as wasm.
##
## You also need to install some additional tools, which you can do by running
## [`scripts/setup_web.sh`](https://github.com/rerun-io/rerun/blob/main/scripts/setup_web.sh).
web_viewer = ["dep:re_ws_comms", "dep:re_web_viewer_server", "dep:webbrowser"]
# Add a tokio runtime to the `Session` type.
tokio_runtime = ["dep:tokio"]


[dependencies]
re_build_info.workspace = true
re_error.workspace = true
re_log.workspace = true
re_log_types.workspace = true
re_log.workspace = true
re_memory.workspace = true
re_sdk_comms = { workspace = true, features = ["client"] }
re_smart_channel.workspace = true
re_string_interner.workspace = true

anyhow.workspace = true
arrow2.workspace = true
crossbeam = "0.8"
document-features = "0.2"
lazy_static.workspace = true
nohash-hasher = "0.2"
once_cell = "1.12"
parking_lot = "0.12"
thiserror.workspace = true

# Optional dependencies:
re_ws_comms = { workspace = true, optional = true, features = ["server"] }
re_viewer = { workspace = true, default-features = false, optional = true }
re_web_viewer_server = { workspace = true, optional = true }

webbrowser = { version = "0.8", optional = true }
once_cell = { version = "1.12", optional = true }
parking_lot = { version = "0.12", optional = true }

# Native dependencies:
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
clap = { workspace = true, features = ["derive"] }
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
tokio = { workspace = true, optional = true, features = [
"macros",
"rt-multi-thread",
] }


[dev-dependencies]
Expand Down
32 changes: 28 additions & 4 deletions crates/re_sdk/src/demo_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub fn grid(from: glam::Vec3, to: glam::Vec3, n: usize) -> impl Iterator<Item =
/// * `angular_step`: The factor applied between each step along the trigonometric circle.
/// * `angular_offset`: Offsets the starting position on the trigonometric circle.
/// * `z_step`: The factor applied between between each step along the Z axis.
#[cfg(all(feature = "glam", feature = "native_viewer"))]
#[cfg(feature = "glam")]
pub fn color_spiral(
num_points: usize,
radius: f32,
Expand All @@ -91,10 +91,34 @@ pub fn color_spiral(
.collect();

let colors = (0..num_points)
.map(move |i| {
re_viewer::external::re_renderer::colormap_turbo_srgb(i as f32 / num_points as f32)
})
.map(move |i| colormap_turbo_srgb(i as f32 / num_points as f32))
.collect();

(points, colors)
}

/// Returns sRGB polynomial approximation from Turbo color map, assuming `t` is normalized.
fn colormap_turbo_srgb(t: f32) -> [u8; 4] {
#![allow(clippy::excessive_precision)]
use glam::{Vec2, Vec4, Vec4Swizzles};

const R4: Vec4 = Vec4::new(0.13572138, 4.61539260, -42.66032258, 132.13108234);
const G4: Vec4 = Vec4::new(0.09140261, 2.19418839, 4.84296658, -14.18503333);
const B4: Vec4 = Vec4::new(0.10667330, 12.64194608, -60.58204836, 110.36276771);

const R2: Vec2 = Vec2::new(-152.94239396, 59.28637943);
const G2: Vec2 = Vec2::new(4.27729857, 2.82956604);
const B2: Vec2 = Vec2::new(-89.90310912, 27.34824973);

debug_assert!((0.0..=1.0).contains(&t));

let v4 = glam::vec4(1.0, t, t * t, t * t * t);
let v2 = v4.zw() * v4.z;

[
((v4.dot(R4) + v2.dot(R2)) * 255.0) as u8,
((v4.dot(G4) + v2.dot(G2)) * 255.0) as u8,
((v4.dot(B4) + v2.dot(B2)) * 255.0) as u8,
255,
]
}
5 changes: 4 additions & 1 deletion crates/re_sdk/src/file_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::Context as _;

use re_log_types::LogMsg;

/// Stream log messages to a file.
pub struct FileWriter {
// None = quit
tx: std::sync::mpsc::Sender<Option<LogMsg>>,
Expand Down Expand Up @@ -50,8 +51,10 @@ impl FileWriter {
join_handle: Some(join_handle),
})
}
}

pub fn write(&self, msg: LogMsg) {
impl crate::LogSink for FileWriter {
fn send(&mut self, msg: LogMsg) {
self.tx.send(Some(msg)).ok();
}
}
23 changes: 12 additions & 11 deletions crates/re_sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@
// Send data to a rerun session
#[cfg(not(target_arch = "wasm32"))]
mod file_writer;

#[cfg(feature = "global")]
mod global;

mod log_sink;
mod msg_sender;
#[cfg(feature = "web_viewer")]
mod remote_viewer_server;
mod session;

#[cfg(feature = "global")]
pub use self::global::{global_session, global_session_with_default_enabled};

pub use self::msg_sender::{MsgSender, MsgSenderError};
pub use self::session::Session;

#[cfg(not(target_arch = "wasm32"))]
pub mod clap;
pub use log_sink::{BufferedSink, LogSink, TcpSink};

#[cfg(feature = "demo")]
pub mod demo_util;
Expand Down Expand Up @@ -67,9 +69,6 @@ pub mod external {
pub use re_memory;
pub use re_sdk_comms;

#[cfg(feature = "native_viewer")]
pub use re_viewer;

#[cfg(feature = "glam")]
pub use re_log_types::external::glam;

Expand Down Expand Up @@ -106,22 +105,24 @@ fn get_rerun_env() -> Option<bool> {
///
/// Also adds some helpful logging.
fn decide_logging_enabled(default_enabled: bool) -> bool {
// We use `info_once` so that we can call this function
// multiple times without spamming the log.
match get_rerun_env() {
Some(true) => {
re_log::info!(
re_log::info_once!(
"Rerun Logging is enabled by the '{RERUN_ENV_VAR}' environment variable."
);
true
}
Some(false) => {
re_log::info!(
re_log::info_once!(
"Rerun Logging is disabled by the '{RERUN_ENV_VAR}' environment variable."
);
false
}
None => {
if !default_enabled {
re_log::info!(
re_log::info_once!(
"Rerun Logging has been disabled. Turn it on with the '{RERUN_ENV_VAR}' environment variable."
);
}
Expand Down
83 changes: 83 additions & 0 deletions crates/re_sdk/src/log_sink.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use re_log_types::LogMsg;

/// Where the SDK sends its log messages.
pub trait LogSink: Send {
/// Send this log message.
fn send(&mut self, msg: LogMsg);

/// Send all these log messages.
fn send_all(&mut self, messages: Vec<LogMsg>) {
for msg in messages {
self.send(msg);
}
}

/// Drain all buffered [`LogMsg`]es and return them.
fn drain_backlog(&mut self) -> Vec<LogMsg> {
vec![]
}

/// Wait until all logged data have been sent to the remove server (if any).
fn flush(&self) {}

/// If the TCP session is disconnected, allow it to quit early and drop unsent messages.
fn drop_msgs_if_disconnected(&self) {}
}

// ----------------------------------------------------------------------------

/// Store log messages in memory until you call [`LogSink::drain_backlog`].
#[derive(Default)]
pub struct BufferedSink(Vec<LogMsg>);

impl BufferedSink {
/// An empty buffer.
pub fn new() -> Self {
Self::default()
}
}

impl LogSink for BufferedSink {
fn send(&mut self, msg: LogMsg) {
self.0.push(msg);
}

fn send_all(&mut self, mut messages: Vec<LogMsg>) {
self.0.append(&mut messages);
}

fn drain_backlog(&mut self) -> Vec<LogMsg> {
std::mem::take(&mut self.0)
}
}

// ----------------------------------------------------------------------------

/// Stream log messages to a Rerun TCP server.
pub struct TcpSink {
client: re_sdk_comms::Client,
}

impl TcpSink {
/// Connect to the given address in a background thread.
/// Retries until successful.
pub fn new(addr: std::net::SocketAddr) -> Self {
Self {
client: re_sdk_comms::Client::new(addr),
}
}
}

impl LogSink for TcpSink {
fn send(&mut self, msg: LogMsg) {
self.client.send(msg);
}

fn flush(&self) {
self.client.flush();
}

fn drop_msgs_if_disconnected(&self) {
self.client.drop_if_disconnected();
}
}
Loading