2024-08-11 03:48:20 +02:00
|
|
|
//! One of the goals of the bot is to never save user-images to disk (for
|
|
|
|
//! performance and safety reasons), opting to perform operations in-memory
|
|
|
|
//! instead.
|
|
|
|
//!
|
|
|
|
//! While great in practice, this makes debugging much harder. This module
|
|
|
|
//! allows for a convenient way to throw images into a `logs` directory with
|
|
|
|
//! a simple env var.
|
|
|
|
|
2024-08-11 19:49:46 +02:00
|
|
|
use std::{env, ops::Deref, sync::OnceLock, time::Instant};
|
2024-08-11 03:14:02 +02:00
|
|
|
|
|
|
|
use image::{DynamicImage, EncodableLayout, ImageBuffer, PixelWithColorType};
|
|
|
|
|
|
|
|
use crate::context::Error;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn should_save_debug_images() -> bool {
|
|
|
|
env::var("SHIMMERING_DEBUG_IMGS")
|
|
|
|
.map(|s| s == "1")
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
2024-08-11 19:49:46 +02:00
|
|
|
#[inline]
|
|
|
|
fn get_startup_time() -> Instant {
|
|
|
|
static CELL: OnceLock<Instant> = OnceLock::new();
|
|
|
|
*CELL.get_or_init(|| Instant::now())
|
|
|
|
}
|
|
|
|
|
2024-08-11 03:14:02 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn debug_image_log(image: &DynamicImage) -> Result<(), Error> {
|
|
|
|
if should_save_debug_images() {
|
2024-08-11 19:49:46 +02:00
|
|
|
image.save(format!(
|
|
|
|
"./logs/{:0>15}.png",
|
|
|
|
get_startup_time().elapsed().as_nanos()
|
|
|
|
))?;
|
2024-08-11 03:14:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn debug_image_buffer_log<P, C>(image: &ImageBuffer<P, C>) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
P: PixelWithColorType,
|
|
|
|
[P::Subpixel]: EncodableLayout,
|
|
|
|
C: Deref<Target = [P::Subpixel]>,
|
|
|
|
{
|
|
|
|
if should_save_debug_images() {
|
2024-08-11 19:49:46 +02:00
|
|
|
image.save(format!(
|
|
|
|
"./logs/{:0>15}.png",
|
|
|
|
get_startup_time().elapsed().as_nanos()
|
|
|
|
))?;
|
2024-08-11 03:14:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|