1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Cross-platform logging utilities for the exfiltrate library.
//!
//! This module provides a simple, platform-agnostic logging interface that writes
//! to the appropriate output stream depending on the target platform. On standard
//! platforms, it writes to stderr, while on WebAssembly it uses the browser's
//! console API.
//!
//! # Overview
//!
//! The module is designed to provide basic logging functionality without the overhead
//! of a full logging framework. It's particularly useful for debugging and error
//! reporting in both native and WebAssembly environments.
//!
//! # Platform Behavior
//!
//! - **Native platforms**: Messages are written to stderr using `eprintln!`
//! - **WebAssembly**: Messages are written to the browser console using `console.log`
//!
//! # Examples
//!
//! ```
//! # mod logging {
//! # pub fn log(str: &str) {
//! # eprintln!("{}", str);
//! # }
//! # }
//! # use logging::log;
//!
//! // Simple logging
//! log("Application started");
//!
//! // Logging with formatted strings
//! let port = 8080;
//! log(&format!("Server listening on port {}", port));
//!
//! // Error reporting
//! match std::fs::read_to_string("config.json") {
//! Ok(_) => log("Configuration loaded"),
//! Err(e) => log(&format!("Failed to load config: {}", e)),
//! }
//! ```
/// Logs a message to the platform-appropriate output stream.
///
/// This function provides cross-platform logging that automatically selects
/// the correct output mechanism based on the compilation target:
/// - On native platforms, writes to stderr
/// - On WebAssembly, writes to the browser console
///
/// # Arguments
///
/// * `str` - The message to log. This should be a string slice containing
/// the formatted message to output.
///
/// # Platform-specific behavior
///
/// ## Native platforms
/// Messages are written to stderr with a newline appended. This ensures
/// that log messages don't interfere with stdout, which may be used for
/// program output or inter-process communication.
///
/// ## WebAssembly
/// Messages are written to the browser's console using the Web API.
/// This makes debugging output visible in the browser's developer tools.
///
/// # Examples
///
/// ## Basic usage
/// ```
/// # mod logging {
/// # pub fn log(str: &str) {
/// # eprintln!("{}", str);
/// # }
/// # }
/// # use logging::log;
///
/// log("Starting initialization");
/// log("Initialization complete");
/// ```
///
/// ## With runtime values
/// ```
/// # mod logging {
/// # pub fn log(str: &str) {
/// # eprintln!("{}", str);
/// # }
/// # }
/// # use logging::log;
///
/// let user_count = 42;
/// log(&format!("Active users: {}", user_count));
///
/// let config = "production";
/// log(&format!("Running in {} mode", config));
/// ```
///
/// ## Error handling patterns
/// ```
/// # mod logging {
/// # pub fn log(str: &str) {
/// # eprintln!("{}", str);
/// # }
/// # }
/// # use logging::log;
/// use std::fs::File;
///
/// match File::open("data.txt") {
/// Ok(_) => log("File opened successfully"),
/// Err(e) => log(&format!("Error opening file: {}", e)),
/// }
/// ```
///
/// # Performance considerations
///
/// This function performs I/O operations and should not be called in
/// performance-critical code paths. For high-frequency logging, consider
/// batching messages or using a more sophisticated logging framework
/// like logwise.