use crate::Level;
use crate::log_record::LogRecord;
pub fn debuginternal_pre(file: &'static str, line: u32, column: u32) -> LogRecord {
let mut record = crate::log_record::LogRecord::new(Level::DebugInternal);
let read_ctx = crate::context::Context::current();
read_ctx._log_prelude(&mut record);
record.log("DEBUG: ");
record.log(file);
record.log_owned(format!(":{}:{} ", line, column));
record.log_timestamp();
record
}
pub fn debuginternal_sync_post(record: LogRecord) {
let global_loggers = crate::hidden::global_loggers();
for logger in global_loggers {
logger.finish_log_record(record.clone());
}
}
pub async fn debuginternal_async_post(record: LogRecord) {
let global_loggers = crate::hidden::global_loggers();
for logger in global_loggers {
logger.finish_log_record_async(record.clone()).await;
}
}
pub fn info_sync_pre(file: &'static str, line: u32, column: u32) -> LogRecord {
let mut record = crate::log_record::LogRecord::new(Level::Info);
let read_ctx = crate::context::Context::current();
read_ctx._log_prelude(&mut record);
record.log("INFO: ");
record.log(file);
record.log_owned(format!(":{}:{} ", line, column));
record.log_timestamp();
record
}
pub fn info_sync_post(record: LogRecord) {
let global_loggers = crate::hidden::global_loggers();
for logger in global_loggers {
logger.finish_log_record(record.clone());
}
}
pub async fn info_async_post(record: LogRecord) {
let global_loggers = crate::hidden::global_loggers();
for logger in global_loggers {
logger.finish_log_record_async(record.clone()).await;
}
}
pub fn warn_sync_pre(file: &'static str, line: u32, column: u32) -> LogRecord {
let mut record = crate::log_record::LogRecord::new(Level::Warning);
let read_ctx = crate::context::Context::current();
read_ctx._log_prelude(&mut record);
record.log("WARN: ");
record.log(file);
record.log_owned(format!(":{}:{} ", line, column));
record.log_timestamp();
record
}
pub fn warn_sync_post(record: LogRecord) {
let global_loggers = crate::hidden::global_loggers();
for logger in global_loggers {
logger.finish_log_record(record.clone());
}
}
pub fn trace_sync_pre(file: &'static str, line: u32, column: u32) -> LogRecord {
let mut record = crate::log_record::LogRecord::new(Level::Trace);
let read_ctx = crate::context::Context::current();
read_ctx._log_prelude(&mut record);
record.log("TRACE: ");
record.log(file);
record.log_owned(format!(":{}:{} ", line, column));
record.log_timestamp();
record
}
pub fn trace_sync_post(record: LogRecord) {
let global_loggers = crate::hidden::global_loggers();
for logger in global_loggers {
logger.finish_log_record(record.clone());
}
}
pub async fn trace_async_post(record: LogRecord) {
let global_loggers = crate::hidden::global_loggers();
for logger in global_loggers {
logger.finish_log_record_async(record.clone()).await;
}
}
pub fn error_sync_pre(file: &'static str, line: u32, column: u32) -> LogRecord {
let mut record = crate::log_record::LogRecord::new(Level::Error);
let read_ctx = crate::context::Context::current();
read_ctx._log_prelude(&mut record);
record.log("ERROR: ");
record.log(file);
record.log_owned(format!(":{}:{} ", line, column));
record.log_timestamp();
record
}
pub fn error_sync_post(record: LogRecord) {
let global_loggers = crate::hidden::global_loggers();
for logger in global_loggers {
logger.finish_log_record(record.clone());
}
}
pub async fn error_async_post(record: LogRecord) {
let global_loggers = crate::hidden::global_loggers();
for logger in global_loggers {
logger.finish_log_record_async(record.clone()).await;
}
}
pub fn perfwarn_begin_pre(file: &'static str, line: u32, column: u32) -> LogRecord {
let start = crate::sys::Instant::now();
let mut record = crate::log_record::LogRecord::new(Level::PerfWarn);
let read_ctx = crate::context::Context::current();
read_ctx._log_prelude(&mut record);
record.log("PERFWARN: BEGIN ");
record.log(file);
record.log_owned(format!(":{}:{} ", line, column));
record.log_time_since(start);
record
}
pub fn perfwarn_begin_post(
record: LogRecord,
name: &'static str,
) -> crate::interval::PerfwarnInterval {
let global_loggers = crate::hidden::global_loggers();
for logger in global_loggers {
logger.finish_log_record(record.clone());
}
crate::interval::PerfwarnInterval::new(name, crate::sys::Instant::now())
}
pub fn perfwarn_begin_if_post(
record: LogRecord,
name: &'static str,
threshold: crate::sys::Duration,
) -> crate::interval::PerfwarnIntervalIf {
crate::interval::PerfwarnIntervalIf::new(name, crate::sys::Instant::now(), threshold, record)
}
pub fn perfwarn_begin_if_pre(file: &'static str, line: u32, column: u32) -> LogRecord {
let mut record = crate::log_record::LogRecord::new(Level::PerfWarn);
let read_ctx = crate::context::Context::current();
read_ctx._log_prelude(&mut record);
record.log("PERFWARN: ");
record.log(file);
record.log_owned(format!(":{}:{} ", line, column));
record
}
pub fn mandatory_sync_pre(file: &'static str, line: u32, column: u32) -> LogRecord {
let mut record = crate::log_record::LogRecord::new(Level::Mandatory);
let read_ctx = crate::context::Context::current();
read_ctx._log_prelude(&mut record);
record.log("MANDATORY: ");
record.log(file);
record.log_owned(format!(":{}:{} ", line, column));
record.log_timestamp();
record
}
pub fn mandatory_sync_post(record: LogRecord) {
let global_loggers = crate::hidden::global_loggers();
for logger in global_loggers {
logger.finish_log_record(record.clone());
}
}
pub async fn mandatory_async_post(record: LogRecord) {
let global_loggers = crate::hidden::global_loggers();
for logger in global_loggers {
logger.finish_log_record_async(record.clone()).await;
}
}
pub fn profile_sync_pre(file: &'static str, line: u32, column: u32) -> LogRecord {
let mut record = crate::log_record::LogRecord::new(Level::Profile);
let read_ctx = crate::context::Context::current();
read_ctx._log_prelude(&mut record);
record.log("PROFILE: ");
record.log(file);
record.log_owned(format!(":{}:{} ", line, column));
record.log_timestamp();
record
}
pub fn profile_sync_post(record: LogRecord) {
let global_loggers = crate::hidden::global_loggers();
for logger in global_loggers {
logger.finish_log_record(record.clone());
}
}
pub async fn profile_async_post(record: LogRecord) {
let global_loggers = crate::hidden::global_loggers();
for logger in global_loggers {
logger.finish_log_record_async(record.clone()).await;
}
}
pub fn profile_begin_pre(file: &'static str, line: u32, column: u32) -> (u64, LogRecord) {
let id = crate::interval::next_profile_id();
let start = crate::sys::Instant::now();
let mut record = crate::log_record::LogRecord::new(Level::Profile);
let read_ctx = crate::context::Context::current();
read_ctx._log_prelude(&mut record);
record.log_owned(format!("PROFILE: BEGIN [id={}] ", id));
record.log(file);
record.log_owned(format!(":{}:{} ", line, column));
record.log_time_since(start);
(id, record)
}
pub fn profile_begin_post(
id: u64,
record: LogRecord,
name: &'static str,
) -> crate::interval::ProfileInterval {
let global_loggers = crate::hidden::global_loggers();
for logger in global_loggers {
logger.finish_log_record(record.clone());
}
crate::interval::ProfileInterval::new(id, name, crate::sys::Instant::now())
}
#[cfg(test)]
mod tests {
use logwise::context::Context;
use logwise_proc::{debuginternal_sync, info_sync, warn_sync};
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_warn_sync() {
crate::context::Context::reset("test_warn_sync".to_string());
info_sync!("test_warn_sync");
warn_sync!("test_warn_sync Hello {world}!", world = 23);
}
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn perfwarn() {
use logwise::perfwarn;
Context::reset("test_perfwarn".to_string());
info_sync!("test_perfwarn");
let _: i32 = perfwarn!("test_perfwarn interval name", {
23
});
}
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_debuginternal_sync() {
crate::context::Context::reset("test_debuginternal_sync".to_string());
debuginternal_sync!("test_debuginternal_sync");
}
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_log_rich() {
let val = false;
crate::context::Context::reset("test_log_rich".to_string());
debuginternal_sync!("Hello {world}!", world = val);
}
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_log_custom() {
crate::context::Context::reset("test_log_custom".to_string());
#[derive(Debug)]
#[allow(dead_code)]
struct S(i32);
let s = S(23);
debuginternal_sync!("{s}!", s = logwise::privacy::LogIt(&s));
}
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[allow(clippy::let_underscore_future)] fn test_log_info_async() {
crate::context::Context::reset("test_log_info_async".to_string());
let _ = async {
logwise::info_async!("test_log_info_async");
};
}
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_trace() {
crate::context::Context::reset("test_trace".to_string());
logwise::trace_sync!("test_trace");
}
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_mandatory_sync() {
crate::context::Context::reset("test_mandatory_sync".to_string());
logwise::mandatory_sync!("mandatory debug value={val}", val = 42);
}
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_profile_sync() {
crate::context::Context::reset("test_profile_sync".to_string());
logwise::profile_sync!("profile timing={ms}ms", ms = 100);
}
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_profile_begin() {
crate::context::Context::reset("test_profile_begin".to_string());
let interval = logwise::profile_begin!("test_operation {param}", param = "foo");
let _ = (0..100).sum::<i32>();
drop(interval);
}
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_profile_begin_nested() {
crate::context::Context::reset("test_profile_begin_nested".to_string());
let outer = logwise::profile_begin!("outer_operation");
{
let inner = logwise::profile_begin!("inner_operation");
let _ = (0..50).sum::<i32>();
drop(inner);
}
drop(outer);
}
#[logwise::profile]
fn profiled_function() -> i32 {
(0..100).sum()
}
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_profile_attribute() {
crate::context::Context::reset("test_profile_attribute".to_string());
let result = profiled_function();
assert_eq!(result, 4950);
}
#[logwise::profile]
fn profiled_with_args(x: i32, y: i32) -> i32 {
x + y
}
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_profile_attribute_with_args() {
crate::context::Context::reset("test_profile_attribute_with_args".to_string());
let result = profiled_with_args(10, 20);
assert_eq!(result, 30);
}
#[logwise::profile]
fn profiled_early_return(x: i32) -> i32 {
if x < 0 {
return -1;
}
x * 2
}
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn test_profile_attribute_early_return() {
crate::context::Context::reset("test_profile_attribute_early_return".to_string());
assert_eq!(profiled_early_return(-5), -1);
assert_eq!(profiled_early_return(10), 20);
}
}