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

Errors from example code #47

Closed
kief opened this issue Dec 23, 2021 · 6 comments
Closed

Errors from example code #47

kief opened this issue Dec 23, 2021 · 6 comments
Labels
CVE-2020-26235 Unsound behaviour with local timezone access

Comments

@kief
Copy link

kief commented Dec 23, 2021

Hi,

I'm new to Rust, so may be missing something obvious to more experienced folks.

I created a project following the usage example, but get the error use of undeclared crate or module 'log' when I run cargo run. When I run cargo run --example init I get the error no example target named 'init'.

My project has a fail src/main.rs with a copy/paste of the example code:

use simple_logger::SimpleLogger;

fn main() {
    SimpleLogger::new().init().unwrap();

    log::warn!("This is an example message.");
}

My Cargo.toml is:

[package]
name = "example"
version = "0.1.0"
edition = "2021"

[dependencies]
simple_logger = "1.16.0"

When I run cargo run:

$ cargo run
    Updating crates.io index
   Compiling libc v0.2.112
   Compiling log v0.4.14
   Compiling cfg-if v1.0.0
   Compiling time-macros v0.2.3
   Compiling itoa v0.4.8
   Compiling lazy_static v1.4.0
   Compiling atty v0.2.14
   Compiling colored v1.9.3
   Compiling time v0.3.5
   Compiling simple_logger v1.16.0
   Compiling example v0.1.0 (/Users/kief/projects/rust/example)
error[E0433]: failed to resolve: use of undeclared crate or module `log`
 --> src/main.rs:6:5
  |
6 |     log::warn!("This is an example message.");
  |     ^^^ use of undeclared crate or module `log`

For more information about this error, try `rustc --explain E0433`.
error: could not compile `example` due to previous error

Is the example is missing something (like a use statement) that a more experienced Rustacian would know to include? I've tried adding use 'log' and including a log crate in my Cargo.toml, but then I get entirely different error messages. I figured I should make sure I'm on the right track with the example and not going down a completely different rabbithole.

The example documentation also suggests running cargo run --example init. When I do this, I get this

$ cargo run --example init
error: no example target named `init`

Again, this smells like I'm missing a "Rust 101" concept. Clues would be appreciated!

@borntyping
Copy link
Owner

I think you maybe just need to add log to your [dependencies] in Cargo.toml, but my rust knowledge is very outdated these days.

@kief
Copy link
Author

kief commented Dec 23, 2021

When I do that, I get the thread 'main' panicked at 'Could not determine the UTC offset on this system. error. I've tried Googling that error (and seen there was an issue that was fixed in the current version of simple_logger), and it sounds like something else going on. If it doesn't seem like I'm doing something obviously wrong in using the example code then I guess I can close this issue and dig into it.

@borntyping
Copy link
Owner

That's progress, at least the code compiles now! That second error actually is a known issue in the time crate, which means unfortunately the default example doesn't work in all contexts.

There should be a bit more detail to the error message - you're likely either running in a multi-threaded environment or on an OS the time crate does not support. I don't think cargo run --example ... uses threads by default, so it's likely that you're using an OS that time doesn't support (sys/local_offset_at/mod.rs in time)

There's a list of workarounds for simple_logger, depending on how you'd prefer to deal with the issue (mostly taken from #43).

  • Use UTC time (SimpleLogger::new().with_utc_timestamps().init().unwrap();)
  • Disable timestamps (SimpleLogger::new().without_timestamps().init().unwrap();)
  • Use the time crates unsafe features: RUSTFLAGS="--cfg unsound_local_offset" (docs)

@jakewan
Copy link

jakewan commented Dec 24, 2021

@kief I thought I recognized that username! Thank you for Infrastructure as Code! 😄

@kief
Copy link
Author

kief commented Dec 27, 2021

Thanks @jakewan, glad you found it useful! And thanks @borntyping. The error message doesn't give much more detail than your suggestions (MacOS 11.6.1):

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.08s
     Running `target/debug/example`
thread 'main' panicked at 'Could not determine the UTC offset on this system. Possible causes are that the time crate does not implement "local_offset_at" on your system, or that you are running in a multi-threaded environment and the time crate is returning "None" from "local_offset_at" to avoid unsafe behaviour. See the time crate's documentation for more information. (https://time-rs.github.io/internal-api/time/index.html#feature-flags): IndeterminateOffset', /Users/kief/.cargo/registry/src/github.com-1ecc6299db9ec823/simple_logger-1.16.0/src/lib.rs:409:85
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I'll find a way to work around it. Clearly not an issue with simple_logger, so, . Thanks!

@borntyping
Copy link
Owner

UTC timestamps are now the default in simple_logger 2.0.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CVE-2020-26235 Unsound behaviour with local timezone access
Projects
None yet
Development

No branches or pull requests

3 participants