Last active
October 25, 2024 18:16
-
-
Save kkroesch/c259ee803f8b1e25194408c9ca0e07f9 to your computer and use it in GitHub Desktop.
Rust Tracing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! ## Dependencies | |
//! [dependencies] | |
//! tracing = "0.1" | |
//! tracing-subscriber = { version = "0.3", features = ["fmt", "env-filter"] } | |
use tracing::{debug, error, info, instrument, warn}; | |
use tracing_subscriber::{fmt, EnvFilter}; | |
#[instrument(level = "trace")] | |
fn sieve_of_eratosthenes(limit: usize) -> Vec<usize> { | |
let mut is_prime = vec![true; limit + 1]; | |
is_prime[0] = false; | |
is_prime[1] = false; | |
let sqrt_limit = (limit as f64).sqrt() as usize + 1; | |
for num in 2..sqrt_limit { | |
if is_prime[num] { | |
for multiple in (num * num..=limit).step_by(num) { | |
is_prime[multiple] = false; | |
} | |
} | |
} | |
debug!("Sieving done. Now collecting."); | |
is_prime | |
.iter() | |
.enumerate() | |
.filter_map(|(num, &prime)| if prime { Some(num) } else { None }) | |
.collect() | |
} | |
#[instrument(level = "trace")] | |
fn mandelbrot(width: usize, height: usize, max_iter: usize) -> Vec<u32> { | |
let mut pixels = vec![0; width * height]; | |
for y in 0..height { | |
let cy = y as f64 / height as f64 * 2.0 - 1.0; | |
for x in 0..width { | |
let cx = x as f64 / width as f64 * 3.5 - 2.5; | |
let mut zx = 0.0; | |
let mut zy = 0.0; | |
let mut iter = 0; | |
while zx * zx + zy * zy < 4.0 && iter < max_iter { | |
let temp = zx * zx - zy * zy + cx; | |
zy = 2.0 * zx * zy + cy; | |
zx = temp; | |
iter += 1; | |
} | |
pixels[y * width + x] = iter as u32; | |
} | |
} | |
pixels | |
} | |
fn main() { | |
let subscriber = tracing_subscriber::fmt() | |
// Use a more compact, abbreviated log format | |
.compact() | |
// Configure Log legel via environment variable, for example `RUST_LOG=trace` | |
.with_env_filter(EnvFilter::from_default_env()) | |
//.with_max_level(tracing::Level::TRACE) | |
// Log entrance and exit of functions | |
.with_span_events(fmt::format::FmtSpan::ENTER | fmt::format::FmtSpan::EXIT) | |
// Display source code file paths | |
.with_file(true) | |
// Display source code line numbers | |
.with_line_number(true) | |
// Display the thread ID an event was recorded on | |
.with_thread_ids(true) | |
// Don't display the event's target (module path) | |
.with_target(false) | |
// Build the subscriber | |
.finish(); | |
tracing::subscriber::set_global_default(subscriber).expect("Cannot configure subscriber."); | |
let limit = 10_000_000; | |
let primes = sieve_of_eratosthenes(limit); | |
let num_primes = primes.len(); | |
warn!("Found {num_primes} prime numbers."); | |
let width = 1920; | |
let height = 1080; | |
let max_iter = 1000; | |
let pixels = mandelbrot(width, height, max_iter); | |
let num_pixels = pixels.len(); | |
info!("Calculated {num_pixels} pixels for Mandelbrot set."); | |
//println!("{:?}", primes); | |
error!("The program has terminated."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment