logly 0.0.4

High-performance, structured logging library with async support, rotation, filtering, and GPU/CPU optimization
Documentation
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! GPU/CUDA acceleration for high-performance logging
//!
//! Provides optional GPU acceleration using CUDA for high-throughput logging scenarios.
//! Requires the `gpu` feature flag and CUDA toolkit to be installed.
//!
//! This module uses the cudarc driver API to allocate GPU memory and transfer log data
//! to the device for high-throughput scenarios. The implementation uses:
//! - `CudaContext` for device management (similar to CPU's GlobalAlloc)
//! - `CudaSlice<T>` for GPU memory allocation (similar to CPU's `Vec<T>`)
//! - `htod_sync_copy` for host-to-device memory transfers
//!
//! # Features
//!
//! - Automatic CUDA device initialization
//! - Graceful fallback to CPU-only logging if GPU unavailable
//! - Thread-safe enable/disable controls
//! - Synchronous memory transfers for reliability
//!
//! # Example
//!
//! ```no_run
//! use logly::GpuLogger;
//!
//! let gpu = GpuLogger::new(1024 * 1024)?; // 1MB buffer
//! if gpu.is_available() {
//!     gpu.enable()?;
//!     let data = b"log message";
//!     gpu.write_to_gpu(data)?;
//! }
//! # Ok::<(), logly::LoglyError>(())
//! ```
//!
//! # CUDA Version Support
//!
//! Supports CUDA 11.4-11.8, 12.0-12.9, and 13.0 via cudarc.

use crate::error::{LoglyError, Result};
use parking_lot::RwLock;
use std::sync::Arc;

/// GPU logger for CUDA-accelerated logging operations.
///
/// Manages GPU device initialization, buffer allocation, and data transfer using
/// the cudarc driver API. The device is initialized on creation and can be
/// enabled/disabled at runtime.
///
/// # Thread Safety
///
/// This struct is thread-safe and can be shared across threads using Arc.
/// The enabled state is protected by RwLock for concurrent access.
///
/// # Memory Management
///
/// Uses `CudaContext::htod_sync_copy` for synchronous host-to-device transfers.
/// Each write allocates a new `CudaSlice<u8>` on the device.
pub struct GpuLogger {
    /// CUDA context and stream (boxed to avoid exposing cudarc types)
    /// Only available when compiled with `gpu` feature
    #[cfg(feature = "gpu")]
    ctx_stream: Option<Box<dyn std::any::Any + Send + Sync>>,
    /// Whether GPU logging is currently enabled (thread-safe)
    enabled: Arc<RwLock<bool>>,
    /// Size of the GPU buffer in bytes (for informational purposes)
    #[allow(dead_code)]
    buffer_size: usize,
}

impl GpuLogger {
    /// Creates a new GPU logger with the specified buffer size.
    ///
    /// Attempts to initialize CUDA device 0. If initialization fails,
    /// the logger will be created but GPU functionality will be unavailable.
    ///
    /// # Arguments
    ///
    /// * `buffer_size` - Size of the GPU buffer in bytes (informational)
    ///
    /// # Returns
    ///
    /// A new GpuLogger instance. Always succeeds, even if GPU is unavailable.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use logly::GpuLogger;
    ///
    /// let gpu = GpuLogger::new(1024 * 1024)?; // 1MB buffer
    /// if gpu.is_available() {
    ///     println!("GPU logging available");
    /// }
    /// # Ok::<(), logly::LoglyError>(())
    /// ```
    pub fn new(buffer_size: usize) -> Result<Self> {
        #[cfg(feature = "gpu")]
        {
            let ctx_stream = std::panic::catch_unwind(|| cudarc::driver::CudaContext::new(0))
                .ok()
                .and_then(|r| r.ok())
                .map(|ctx| {
                    let stream = ctx.default_stream();
                    Box::new((ctx, stream)) as Box<dyn std::any::Any + Send + Sync>
                });
            let is_available = ctx_stream.is_some();

            Ok(Self {
                ctx_stream,
                enabled: Arc::new(RwLock::new(is_available)),
                buffer_size,
            })
        }

        #[cfg(not(feature = "gpu"))]
        {
            Ok(Self {
                enabled: Arc::new(RwLock::new(false)),
                buffer_size,
            })
        }
    }

    /// Checks if GPU acceleration is available.
    ///
    /// Returns true only if:
    /// - Compiled with `gpu` feature
    /// - CUDA device initialization succeeded
    ///
    /// # Returns
    ///
    /// `true` if CUDA device is initialized and available, `false` otherwise
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use logly::GpuLogger;
    ///
    /// let gpu = GpuLogger::new(1024)?;
    /// if gpu.is_available() {
    ///     gpu.enable()?;
    /// }
    /// # Ok::<(), logly::LoglyError>(())
    /// ```
    pub fn is_available(&self) -> bool {
        #[cfg(feature = "gpu")]
        {
            self.ctx_stream.is_some()
        }

        #[cfg(not(feature = "gpu"))]
        {
            false
        }
    }

    /// Checks if GPU logging is currently enabled.
    ///
    /// Note: This only checks the enabled flag, not GPU availability.
    /// Use `is_available()` to check if GPU is actually usable.
    ///
    /// # Returns
    ///
    /// `true` if GPU logging is enabled, `false` otherwise
    ///
    /// # Thread Safety
    ///
    /// This method is thread-safe and uses a read lock.
    pub fn is_enabled(&self) -> bool {
        *self.enabled.read()
    }

    /// Enables GPU logging.
    ///
    /// # Returns
    ///
    /// - `Ok(())` if GPU is available and enabled successfully
    /// - `Err(LoglyError::GpuError)` if GPU is not available or feature not compiled
    ///
    /// # Errors
    ///
    /// Returns error if:
    /// - Not compiled with `gpu` feature
    /// - CUDA device initialization failed
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use logly::GpuLogger;
    ///
    /// let gpu = GpuLogger::new(1024)?;
    /// match gpu.enable() {
    ///     Ok(_) => println!("GPU enabled"),
    ///     Err(e) => eprintln!("GPU not available: {}", e),
    /// }
    /// # Ok::<(), logly::LoglyError>(())
    /// ```
    pub fn enable(&self) -> Result<()> {
        #[cfg(feature = "gpu")]
        {
            if self.ctx_stream.is_none() {
                return Err(LoglyError::GpuError(
                    "CUDA device not available".to_string(),
                ));
            }
            *self.enabled.write() = true;
            Ok(())
        }

        #[cfg(not(feature = "gpu"))]
        {
            Err(LoglyError::GpuError(
                "GPU feature not enabled. Compile with --features gpu".to_string(),
            ))
        }
    }

    /// Disables GPU logging.
    ///
    /// After calling this, `write_to_gpu()` will become a no-op.
    /// Can be re-enabled with `enable()`.
    ///
    /// # Thread Safety
    ///
    /// This method is thread-safe and uses a write lock.
    pub fn disable(&self) {
        *self.enabled.write() = false;
    }

    /// Writes log data to GPU memory (only available with gpu feature).
    ///
    /// Uses `CudaContext::htod_sync_copy` to perform synchronous host-to-device
    /// memory transfer. Allocates a new `CudaSlice<u8>` for each write.
    ///
    /// # Arguments
    ///
    /// * `data` - Byte slice to write to GPU
    ///
    /// # Returns
    ///
    /// - `Ok(())` if write succeeds or GPU is disabled (no-op)
    /// - `Err(LoglyError::GpuError)` if GPU write fails
    ///
    /// # Behavior
    ///
    /// - If GPU is disabled: Returns Ok without doing anything
    /// - If GPU is enabled: Performs synchronous copy to device
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use logly::GpuLogger;
    ///
    /// let gpu = GpuLogger::new(1024)?;
    /// gpu.enable()?;
    /// gpu.write_to_gpu(b"log message")?;
    /// # Ok::<(), logly::LoglyError>(())
    /// ```
    #[cfg(feature = "gpu")]
    pub fn write_to_gpu(&self, data: &[u8]) -> Result<()> {
        if !self.is_enabled() {
            return Ok(());
        }

        if let Some(ref ctx_stream_box) = self.ctx_stream {
            type CtxStream = (
                Arc<cudarc::driver::CudaContext>,
                Arc<cudarc::driver::CudaStream>,
            );
            if let Some((_ctx, stream)) = ctx_stream_box.downcast_ref::<CtxStream>() {
                match stream.memcpy_stod(data) {
                    Ok(_buffer) => Ok(()),
                    Err(e) => Err(LoglyError::GpuError(format!(
                        "Failed to copy to GPU: {:?}",
                        e
                    ))),
                }
            } else {
                Err(LoglyError::GpuError(
                    "Invalid CUDA context type".to_string(),
                ))
            }
        } else {
            Err(LoglyError::GpuError(
                "CUDA device not available".to_string(),
            ))
        }
    }

    /// Writes log data to GPU memory (stub when gpu feature is disabled).
    ///
    /// This is a no-op stub that always returns an error when the `gpu`
    /// feature is not compiled.
    ///
    /// # Arguments
    ///
    /// * `_data` - Byte slice (ignored)
    ///
    /// # Returns
    ///
    /// Always returns `Err(LoglyError::GpuError)` indicating feature not enabled
    ///
    /// # Note
    ///
    /// To use GPU logging, compile with `--features gpu`
    #[cfg(not(feature = "gpu"))]
    pub fn write_to_gpu(&self, _data: &[u8]) -> Result<()> {
        Err(LoglyError::GpuError("GPU feature not enabled".to_string()))
    }

    /// Returns information about GPU logging status.
    ///
    /// Provides human-readable information about GPU availability,
    /// device details, buffer size, and current status.
    ///
    /// # Returns
    ///
    /// A formatted string containing:
    /// - GPU availability status
    /// - Device information (if available)
    /// - Buffer size
    /// - Active/Inactive status
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use logly::GpuLogger;
    ///
    /// let gpu = GpuLogger::new(1024 * 1024)?;
    /// println!("{}", gpu.get_info());
    /// # Ok::<(), logly::LoglyError>(())
    /// ```
    pub fn get_info(&self) -> String {
        #[cfg(feature = "gpu")]
        {
            if self.ctx_stream.is_some() {
                format!(
                    "GPU Logging: Enabled\nDevice: CUDA Device 0\nBuffer Size: {} bytes\nStatus: {}",
                    self.buffer_size,
                    if self.is_enabled() {
                        "Active"
                    } else {
                        "Inactive"
                    }
                )
            } else {
                "GPU Logging: Not Available (CUDA device initialization failed)".to_string()
            }
        }

        #[cfg(not(feature = "gpu"))]
        {
            "GPU Logging: Not Available (compile with --features gpu)".to_string()
        }
    }
}

impl Default for GpuLogger {
    fn default() -> Self {
        Self::new(1024 * 1024).unwrap_or_else(|_| {
            #[cfg(feature = "gpu")]
            {
                Self {
                    ctx_stream: None,
                    enabled: Arc::new(RwLock::new(false)),
                    buffer_size: 1024 * 1024,
                }
            }
            #[cfg(not(feature = "gpu"))]
            {
                Self {
                    enabled: Arc::new(RwLock::new(false)),
                    buffer_size: 1024 * 1024,
                }
            }
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_gpu_logger_creation() {
        let gpu = GpuLogger::new(1024 * 1024);
        assert!(gpu.is_ok());
    }

    #[test]
    fn test_gpu_logger_default() {
        // Default should never panic even without CUDA
        let gpu = GpuLogger::default();
        assert_eq!(gpu.buffer_size, 1024 * 1024);
    }

    #[test]
    fn test_gpu_availability() {
        // Should not panic if CUDA is unavailable
        if let Ok(gpu) = GpuLogger::new(1024) {
            let _ = gpu.is_available();
        }
    }

    #[test]
    fn test_gpu_enable_disable() {
        if let Ok(gpu) = GpuLogger::new(1024) {
            gpu.disable();
            assert!(!gpu.is_enabled());
        }
    }

    #[test]
    fn test_gpu_info() {
        if let Ok(gpu) = GpuLogger::new(1024) {
            let info = gpu.get_info();
            assert!(!info.is_empty());
            assert!(info.contains("GPU Logging"));
        }
    }

    #[test]
    fn test_gpu_write_when_disabled() {
        if let Ok(gpu) = GpuLogger::new(1024) {
            gpu.disable();
            let data = b"test log data";
            let result = gpu.write_to_gpu(data);
            #[cfg(feature = "gpu")]
            assert!(result.is_ok());
            #[cfg(not(feature = "gpu"))]
            assert!(result.is_err());
        }
    }

    #[cfg(not(feature = "gpu"))]
    #[test]
    fn test_gpu_not_available_without_feature() {
        let gpu = GpuLogger::new(1024).unwrap();
        assert!(!gpu.is_available());
        assert!(gpu.enable().is_err());
    }

    #[cfg(feature = "gpu")]
    #[test]
    fn test_gpu_write_to_gpu() {
        if let Ok(gpu) = GpuLogger::new(1024) {
            if gpu.is_available() {
                let _ = gpu.enable();
                let data = b"test log data";
                let _ = gpu.write_to_gpu(data);
            }
        }
    }
}