use crate::util;
use std::{error::Error, io};
use std::collections::HashMap;
use std::time::Duration;
use tokio::task_local;
use tracing::{debug, error, info, span, warn, Level, Span, event};
use tracing_subscriber::fmt::format::{FmtSpan, Format};
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use redgold_schema::{EasyJson, error_info, error_message, task_local, task_local_map};
use redgold_schema::structs::ErrorInfo;
use crate::api::public_api::run_server;
use crate::core::relay::Relay;
#[tracing::instrument]
pub fn nested_shave(hello: String) {
info!("Hello from nested shave omg");
}
#[tracing::instrument]
pub fn shave(yak: usize) -> Result<(), Box<dyn Error + 'static>> {
info!("hello! I'm gonna shave a yak from within the instrument tracing function.");
info!("yak message 2");
debug!(excitement = "yay!", "hello! I'm gonna shave a yak.");
if yak == 3 {
warn!("could not locate yak!");
return Err(io::Error::new(io::ErrorKind::Other, "shaving yak failed!").into());
} else {
debug!("yak shaved successfully");
}
nested_shave("yo yo yo".to_string());
Ok(())
}
task_local! {
pub static ONE: u32;
#[allow(unused)]
static TWO: f32;
static NUMBER: u32;
}
async fn some_other_async() {
println!("some other async number get: {}", NUMBER.get().to_string());
}
#[ignore]
#[tokio::test]
pub async fn debug() {
use tracing_subscriber::EnvFilter;
let fmt_layer = tracing_subscriber::fmt::Layer::default();
let filter_layer = EnvFilter::new("sqlx=WARN,warp=WARN,rocket=WARN,redgold=DEBUG");
tracing_subscriber::registry()
.with(filter_layer)
.with(fmt_layer)
.init();
shave(0).expect("shaving yaks, really?");
log::info!("Log crate");
}
pub fn init_tracing(log_level: &str) {
use tracing_subscriber::EnvFilter;
let fmt_layer = tracing_subscriber::fmt::Layer::default()
.compact()
.with_ansi(false);
let filter_layer = EnvFilter::new(format!(
"sqlx=ERROR,warp=WARN,rocket=ERROR,redgold={}", log_level));
tracing_subscriber::registry()
.with(filter_layer)
.with(fmt_layer)
.init();
}
async fn debug_task() -> ErrorInfo {
error_info("yo")
}
#[ignore]
#[tokio::test]
pub async fn debug_task_local() {
let r = task_local("test", "asdf", debug_task()).await;
println!("r: {}", r.json_pretty_or());
}
#[tracing::instrument(fields(other_param))]
pub async fn nested_func(nested_param: &str, opt_param: Option<&str>) {
Span::current().record("other_param", Some("filled"));
Span::current().record("outer_empty", Some("filled_from_inner_does_not_work"));
tracing::info!("Nested func info");
error!("error info: {}", error_info("test_error_info").json_or())
}
#[tracing::instrument(fields(outer_empty))]
pub async fn some_trace_func(param1: &str) {
tracing::info!("outer function info");
let span = Span::current();
span.record("outer_empty", Some("filled_from_outer"));
let mut hm: HashMap<String, String> = HashMap::default();
hm.insert("func_error_key".to_string(), "func_error_value_initial".to_string());
task_local_map(hm, nested_func("nested_param", None)).await
}
#[ignore]
#[tokio::test]
pub async fn debug_span_inject() {
init_tracing("DEBUG");
let mut hm: HashMap<String, String> = HashMap::default();
hm.insert("error_key".to_string(), "error_value_initial".to_string());
task_local_map(hm, some_trace_func("param1")).await;
}
#[tracing::instrument(fields(outer_empty))]
pub async fn some_boring_fun() {
tokio::time::sleep(Duration::from_secs(1)).await;
event!(Level::INFO, "Boring function info");
}
#[ignore]
#[tokio::test]
pub async fn test_perf_timing() {
init_tracing("DEBUG");
some_boring_fun().await;
}