Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions sentry-contexts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ hostname = "0.3.0"
[target."cfg(not(windows))".dependencies]
uname = "0.1.1"

[target."cfg(windows)".dependencies]
os_info = "3.5.0"

[build-dependencies]
rustc_version = "0.4.0"

Expand Down
111 changes: 70 additions & 41 deletions sentry-contexts/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,52 +115,59 @@ pub fn server_name() -> Option<String> {
}

/// Returns the OS context
#[cfg(not(windows))]
pub fn os_context() -> Option<Context> {
#[cfg(not(windows))]
{
use uname::uname;
if let Ok(info) = uname() {
#[cfg(target_os = "macos")]
{
Some(
OsContext {
name: Some("macOS".into()),
kernel_version: Some(info.version),
version: model_support::get_macos_version(),
build: model_support::get_macos_build(),
..Default::default()
}
.into(),
)
}
#[cfg(not(target_os = "macos"))]
{
Some(
OsContext {
name: Some(info.sysname),
kernel_version: Some(info.version),
version: Some(info.release),
..Default::default()
}
.into(),
)
}
} else {
None
use uname::uname;
if let Ok(info) = uname() {
#[cfg(target_os = "macos")]
{
Some(
OsContext {
name: Some("macOS".into()),
kernel_version: Some(info.version),
version: model_support::get_macos_version(),
build: model_support::get_macos_build(),
..Default::default()
}
.into(),
)
}
}
#[cfg(windows)]
{
Some(
OsContext {
name: Some(PLATFORM.into()),
..Default::default()
}
.into(),
)
#[cfg(not(target_os = "macos"))]
{
Some(
OsContext {
name: Some(info.sysname),
kernel_version: Some(info.version),
version: Some(info.release),
..Default::default()
}
.into(),
)
}
} else {
None
}
}

/// Returns the OS context
#[cfg(windows)]
pub fn os_context() -> Option<Context> {
use os_info::Version;
let version = match os_info::get().version() {
Version::Unknown => None,
version => Some(version.to_string()),
};

Some(
OsContext {
name: Some(PLATFORM.into()),
version,
..Default::default()
}
.into(),
)
}

/// Returns the rust info.
pub fn rust_context() -> Context {
RuntimeContext {
Expand All @@ -187,3 +194,25 @@ pub fn device_context() -> Context {
}
.into()
}

#[cfg(test)]
mod tests {
use super::*;
#[cfg(windows)]
#[test]
fn windows_os_version_not_empty() {
let context = os_context();
match context {
Some(Context::Os(os_context)) => {
// verify the version is a non-empty string
let version = os_context.version.expect("OS version to be some");
assert!(!version.is_empty());

// verify the version is not equal to the unknown OS version
let unknown_version = os_info::Version::Unknown.to_string();
assert_ne!(version, unknown_version);
}
_ => unreachable!("os_context() should return a Context::Os"),
}
}
}