simplelog::CombinedLogger panicking at runtime - rust

I'm using the log and simplelog crates. Previously this code worked:
use simplelog::*;
CombinedLogger::init(
vec![
TermLogger::new(LevelFilter::Warn, Config::default()).unwrap(),
WriteLogger::new(LevelFilter::Info, Config::default(), File::create("main.log").unwrap()),
]
).unwrap();
But now it throws this error at runtime:
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', /checkout/src/libcore/option.rs:335:21
stack backtrace:
Cargo.toml dependencies:
log = "0.4.1"
simplelog = "0.5.0"

The documentation of TermLogger::new is not good, because it does not explain why it returns an Option.
I read the code: the function can fail because of term::stdout() or term::stderr(). Reading the documentation, your code fails because a terminal couldn't be opened.

Related

Can't build RustDesk - Android

I tried to build RustDesk project following documentation at documentation - android build
After executing:
VCPKG_ROOT=/opt/vcpkg ANDROID_NDK_HOME=/opt/android-ndk-r22b flutter/ndk_arm64.sh
I cp-ed vcpkg and android-ndk-r22b from home to /opt to make sure that path is right
I'm getting this one error message:
[2023-01-13T09:59:47Z INFO cargo_ndk::cli] Using NDK at path: /opt/android-ndk-r22b (ANDROID_NDK_HOME)
[2023-01-13T09:59:47Z INFO cargo_ndk::cli] NDK API level: 21
[2023-01-13T09:59:47Z INFO cargo_ndk::cli] Building targets: arm64-v8a
[2023-01-13T09:59:47Z INFO cargo_ndk::cli] Building arm64-v8a (aarch64-linux-android)
warning: function `patch` is never used
--> libs/hbb_common/src/config.rs:239:4
|
239 | fn patch(path: PathBuf) -> PathBuf {
| ^^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: `hbb_common` (lib) generated 1 warning
Compiling rustdesk v1.2.0 (/home/mwitek/rustdesk)
error: failed to run custom build command for `rustdesk v1.2.0 (/home/mwitek/rustdesk)`
Caused by:
process didn't exit successfully: `/home/mwitek/rustdesk/target/release/build/rustdesk-9caa4dac4ffa2754/build-script-build` (exit status: 101)
--- stdout
cargo:rustc-link-search=/opt/vcpkg/installed/arm64-android/lib
cargo:rustc-link-lib=oboe
cargo:rustc-link-lib=c++
cargo:rustc-link-lib=OpenSLES
cargo:rerun-if-changed=src/flutter_ffi.rs
--- stderr
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Failed to run build_runner for /home/mwitek/rustdesk/flutter: ', build.rs:108:56
stack backtrace:
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
And to be honest I don't know how to get rid of it, I already tried to understand what is wrong from code-side but I can only find this section in build.rs:
fn install_oboe() {
let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
if target_os != "android" {
return;
}
let mut target_arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap();
if target_arch == "x86_64" {
target_arch = "x64".to_owned();
} else if target_arch == "aarch64" {
target_arch = "arm64".to_owned();
} else {
target_arch = "arm".to_owned();
}
let target = format!("{}-android", target_arch);
let vcpkg_root = std::env::var("VCPKG_ROOT").unwrap();
let mut path: std::path::PathBuf = vcpkg_root.into();
path.push("installed");
path.push(target);
println!(
"{}",
format!(
"cargo:rustc-link-search={}",
path.join("lib").to_str().unwrap()
)
);
println!("cargo:rustc-link-lib=oboe");
println!("cargo:rustc-link-lib=c++");
println!("cargo:rustc-link-lib=OpenSLES");
// I always got some strange link error with oboe, so as workaround, put oboe.cc into oboe src: src/common/AudioStreamBuilder.cpp
// also to avoid libc++_shared not found issue, cp ndk's libc++_shared.so to jniLibs, e.g.
// ./flutter_hbb/android/app/src/main/jniLibs/arm64-v8a/libc++_shared.so
// let include = path.join("include");
//cc::Build::new().file("oboe.cc").include(include).compile("oboe_wrapper");
}
But it doesn't look like anything for me
Also here is function that's generating exception:
fn gen_flutter_rust_bridge() {
let llvm_path = match std::env::var("LLVM_HOME") {
Ok(path) => Some(vec![path]),
Err(_) => None,
};
// Tell Cargo that if the given file changes, to rerun this build script.
println!("cargo:rerun-if-changed=src/flutter_ffi.rs");
// settings for fbr_codegen
let opts = lib_flutter_rust_bridge_codegen::Opts {
// Path of input Rust code
rust_input: "src/flutter_ffi.rs".to_string(),
// Path of output generated Dart code
dart_output: "flutter/lib/generated_bridge.dart".to_string(),
// Path of output generated C header
c_output: Some(vec!["flutter/macos/Runner/bridge_generated.h".to_string()]),
// for other options lets use default
llvm_path,
..Default::default()
};
// run fbr_codegen
lib_flutter_rust_bridge_codegen::frb_codegen(opts).unwrap();
}
Where it's last line is the one that throws this error.
lib_flutter_rust_bridge_codegen::frb_codegen(opts).unwrap();
I know this question really specific but documentation and self-investigation does not helping.
Also my OS properties: Ubuntu 20.04 x64 [Freshly installed]

Rust plotters doesn't draw

I'm trying to run one of the examples from rust's plotters user guide. For some reason, whenI run the example, no image is written out to images/. I looked through the whole project structure, no images are written to examples/ or otherwise.
Here's the output from cargo run:
plotters % cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.06s
Running `target/debug/plotters`
Here's the code:
use plotters::prelude::*;
fn main() {
let root_drawing_area = BitMapBackend::new("images/0.1.png", (1024, 768))
.into_drawing_area();
root_drawing_area.fill(&WHITE).unwrap();
let mut chart = ChartBuilder::on(&root_drawing_area)
.build_cartesian_2d(-3.14..3.14, -1.2..1.2)
.unwrap();
chart.draw_series(LineSeries::new(
(-314..314).map(|x| x as f64 / 100.0).map(|x| (x, x.sin())),
&RED
)).unwrap();
root_drawing_area.present().unwrap();
}
Am I missing something?
Edit:
I added the explicit call to root_drawing_area.present().unwrap();, now I get a helpful error message:
% cargo run
Compiling plotters v0.1.0 (/Users/sledge/Sandbox/plotters)
Finished dev [unoptimized + debuginfo] target(s) in 0.82s
Running `target/debug/plotters`
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: BackendError(DrawingError(ImageError(IoError(Os { code: 2, kind: NotFound, message: "No such file or directory" }))))', src/main.rs:18:33
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Compiling bevy_dylib v0.5.0 error: linking with `cc` failed: exit status: 1

On Mac freshly upgraded to Monterey, I'm getting the following when attempting to cargo run a trivial Bevy program. I've reinstalled XCode CLTs like recommended here and other places. I've tried messing around with some of the cargo.yml with no success.
Compiling bevy_dylib v0.5.0
error: linking with `cc` failed: exit status: 1
note: "cc" "-Wl,-exported_symbols_list,/var/folders/rp/lky8r76j5v5dk0rqg_yzk35w0000gn/T/rustcDYBmaq/list"
"-m64" "-arch" "x86_64" <......... many pages of warnings>
ld: warning: object file (/Users/BWStearns/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy-glsl-to-spirv-0.2.1/build/osx/libSPIRV-Tools-opt.glsltospirv.a(const_folding_rules.cpp.o)) was built for newer macOS version (10.13) than being linked (10.7)
<many more pages of warnings>
"_CGDisplayCreateUUIDFromDisplayID", referenced from:
_$LT$winit..platform_impl..platform..monitor..MonitorHandle$u20$as$u20$core..cmp..PartialEq$GT$::eq::h541b069daf520ec9 in libwinit-4f8b93ad49cc21f8.rlib(winit-4f8b93ad49cc21f8.winit.355ded65-cgu.6.rcgu.o)
_$LT$winit..platform_impl..platform..monitor..MonitorHandle$u20$as$u20$core..cmp..Ord$GT$::cmp::h951d61f6bd1d7a5f in libwinit-4f8b93ad49cc21f8.rlib(winit-4f8b93ad49cc21f8.winit.355ded65-cgu.6.rcgu.o)
winit::platform_impl::platform::monitor::MonitorHandle::ns_screen::h3207f8aed4eae22f in libwinit-4f8b93ad49cc21f8.rlib(winit-4f8b93ad49cc21f8.winit.355ded65-cgu.6.rcgu.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: could not compile `bevy_dylib` due to previous error
The cargo.yml is like this:
[package]
name = "my_bevy_game"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bevy = { version = "0.5.0", features = ["dynamic"] }
I just ran into this starting this week. I am pretty sure this exact code was compiling fine a couple weeks ago. Any help would be greatly appreciated.
main.rs looks like
use bevy::prelude::*;
struct Person;
struct Name(String);
fn add_people(mut commands: Commands) {
commands.spawn().insert(Person).insert(Name("Elaina Proctor".to_string()));
commands.spawn().insert(Person).insert(Name("Renzo Hume".to_string()));
commands.spawn().insert(Person).insert(Name("Zayna Nieves".to_string()));
}
fn greet_people(query: Query<&Name, With<Person>>) {
for name in query.iter() {
println!("hello {}!", name.0);
}
}
pub struct HelloPlugin;
impl Plugin for HelloPlugin {
fn build(&self, app: &mut AppBuilder) {
app.add_startup_system(add_people.system())
.add_system(greet_people.system());
}
}
fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_plugin(HelloPlugin)
.run();
}
Update:
Definitely something about the environment on my machine because on a different macbook it worked fine.
So I found a solution if you discover this issue and the code works on other machines. Uninstall rust with rustup self uninstall and then renistall with the standard script curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh and then you should be good.

Crate cannot find path

I am trying to use this crate to generate an ethereum address: https://docs.rs/ethkey/0.2.5/ethkey/
use ethkey::prelude::*;
fn main() {
let key = EthAccount::load_or_generate("~/", "passwd")
.expect("should load or generate new eth key");
println!("{:?}", key.address())
}
This is the example from the documentation and it doesnt seem to work.
I get the error below:
cargo run Compiling ethkey v0.1.0
(/Users/samueldare/Documents/Code/Thor/ethkey)
Finished dev [unoptimized + debuginfo] target(s) in 1.34s
Running target/debug/ethkey thread 'main' panicked at 'should load or generate new eth key: Error(IoError(Os { code: 2, kind:
NotFound, message: "No such file or directory" }), State { next_error:
None, backtrace: InternalBacktrace { backtrace: None } })',
src/libcore/result.rs:999:5 note: run with RUST_BACKTRACE=1
environment variable to display a backtrace.
Ive use ~/ as a last attempt to generate the key file in rust, but it still doesnt seem to work.
I will appreciate any pointers with this
The first argument to load_or_generate() takes a std::path::Path with no closing slash ( '/' ). Remove the slash:
fn main() {
let key = EthAccount::load_or_generate("~", "passwd")
.expect("should load or generate new eth key");
println!("{:?}", key.address())
}
Sample output:
05:47 ethtest (master) ✗ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.04s
Running `target/debug/ethtest`
Address(0x8248af6d1765e559509060b88e540a3567c42d20)
05:47 ethtest (master) ✗

How to obtain the value of a configuration flag?

Is there a way of obtaining the value of a configuration flag? For example, I would like to get the value of target_os as str/String, without resorting to the following if-else-if chain:
if cfg!(target_os = "windows") {
"windows"
} else if cfg!(target_os = "linux") {
"linux"
// ...
} else {
"unknown"
}
No. You can get some of them by tricking Cargo into telling you. If you place the following into a build script:
use std::env;
fn main() {
for (key, value) in env::vars() {
if key.starts_with("CARGO_CFG_") {
println!("{}: {:?}", key, value);
}
}
panic!("stop and dump stdout");
}
...it will display the cfg flags Cargo is aware of. The panic! is just there as an easy way to get Cargo to actually show the output instead of hiding it. For reference, the output this produces looks like this:
Compiling dump-cfg v0.1.0 (file:///F:/Programming/Rust/sandbox/cargo-test/dump-cfg)
error: failed to run custom build command for `dump-cfg v0.1.0 (file:///F:/Programming/Rust/sandbox/cargo-test/dump-cfg)`
process didn't exit successfully: `F:\Programming\Rust\sandbox\cargo-test\dump-cfg\target\debug\build\dump-cfg-8b04f9ac3818f82a\build-script-build` (exit code: 101)
--- stdout
CARGO_CFG_TARGET_POINTER_WIDTH: "64"
CARGO_CFG_TARGET_ENV: "msvc"
CARGO_CFG_TARGET_OS: "windows"
CARGO_CFG_TARGET_ENDIAN: "little"
CARGO_CFG_TARGET_FAMILY: "windows"
CARGO_CFG_TARGET_ARCH: "x86_64"
CARGO_CFG_TARGET_HAS_ATOMIC: "16,32,64,8,ptr"
CARGO_CFG_TARGET_FEATURE: "sse,sse2"
CARGO_CFG_WINDOWS: ""
CARGO_CFG_TARGET_VENDOR: "pc"
CARGO_CFG_DEBUG_ASSERTIONS: ""
--- stderr
thread 'main' panicked at 'stop', build.rs:9
note: Run with `RUST_BACKTRACE=1` for a backtrace.
You can extract the values you're interested in from this list, and dump them to a generated source file, which you can then import (using #[path] or include!) into your package's source.
For target_os specifically, and also for just target_family and target_arch, there are corresponding &str constants in std::env::consts::{OS, FAMILY, ARCH}.

Resources