aviutl2 0.47.0

AviUtl2 SDKのRustラッパー
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! AviUtl2のロガーへのインターフェースを提供します。
//!
//! # Examples
//!
//! AviUtl2のロガーに直接書き込むことができます。
//!
//! ```rust
//! aviutl2::logger::write_plugin_log("This is a plugin log message.").unwrap();
//! aviutl2::logger::write_info_log("This is an info log message.").unwrap();
//! aviutl2::logger::write_warn_log("This is a warning log message.").unwrap();
//! aviutl2::logger::write_error_log("This is an error log message.").unwrap();
//! aviutl2::logger::write_verbose_log("This is a verbose log message.").unwrap();
//!
//! aviutl2::lprintln!("This is a plugin log message.");  // デフォルトはpluginログに出力
//! aviutl2::lprintln!(plugin, "This is also a plugin log message.");
//! aviutl2::lprintln!(info, "This is an info log message.");
//! aviutl2::lprintln!(warn, "This is a warning log message.");
//! aviutl2::lprintln!(error, "This is an error log message.");
//! aviutl2::lprintln!(verbose, "This is a verbose log message.");
//!
//! aviutl2::ldbg!(42); // dbg!マクロに相当
//! ```
//!
//! [`tracing`]クレートと組み合わせることもできます。
//!
//! ```rust
//! aviutl2::tracing_subscriber::fmt()
//!     .with_max_level(if cfg!(debug_assertions) {
//!         tracing::Level::DEBUG
//!     } else {
//!         tracing::Level::INFO
//!     })
//!     .event_format(aviutl2::logger::AviUtl2Formatter)
//!     .with_writer(aviutl2::logger::AviUtl2LogWriter)
//!     .init();
//!
//! tracing::info!("This is an info log message using tracing.");
//! ```

use crate::common::{CWString, NullByteError};
use tracing_log::NormalizeEvent;
use tracing_subscriber::fmt::FormatFields;

// NOTE:
// InitializeLoggerは可能な限り早く実行されるらしいので、まぁ捨てられるログはないとしていいはず...

/// [`tracing_subscriber::fmt::FormatEvent`]を実装する構造体。
///
/// AviUtl2風のログフォーマットでイベントをフォーマットします。
#[derive(Debug, Clone, Default)]
pub struct AviUtl2Formatter;

impl<C, N> tracing_subscriber::fmt::FormatEvent<C, N> for AviUtl2Formatter
where
    C: tracing::Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
    N: for<'a> tracing_subscriber::fmt::FormatFields<'a> + 'static,
{
    fn format_event(
        &self,
        ctx: &tracing_subscriber::fmt::FmtContext<'_, C, N>,
        mut writer: tracing_subscriber::fmt::format::Writer<'_>,
        event: &tracing::Event<'_>,
    ) -> std::fmt::Result {
        let mut writer = tracing_subscriber::fmt::format::Writer::new(&mut writer);
        let meta = event.normalized_metadata();
        let meta = meta.as_ref().unwrap_or_else(|| event.metadata());
        let target = meta.target();
        write!(writer, "[{target}] ")?;
        ctx.format_fields(writer.by_ref(), event)?;
        writer.write_str("\n")?;
        Ok(())
    }
}

/// [`tracing_subscriber::fmt::MakeWriter`]を実装する構造体。
///
/// AviUtl2のログに書き込みます。
#[derive(Debug, Clone, Default)]
pub struct AviUtl2LogWriter;

impl tracing_subscriber::fmt::MakeWriter<'_> for AviUtl2LogWriter {
    type Writer = LockedInternalWriter;

    fn make_writer(&self) -> Self::Writer {
        LockedInternalWriter::plugin()
    }

    fn make_writer_for(&'_ self, meta: &tracing::Metadata<'_>) -> Self::Writer {
        match *meta.level() {
            tracing::Level::ERROR => LockedInternalWriter::error(),
            tracing::Level::WARN => LockedInternalWriter::warn(),
            tracing::Level::INFO => LockedInternalWriter::info(),
            tracing::Level::DEBUG | tracing::Level::TRACE => LockedInternalWriter::verbose(),
        }
    }
}

static INTERNAL_WRITER_MUTEX_PLUGIN: std::sync::LazyLock<std::sync::Mutex<InternalWriter>> =
    std::sync::LazyLock::new(|| {
        std::sync::Mutex::new(InternalWriter::new(InternalWriterLevel::Plugin))
    });
static INTERNAL_WRITER_MUTEX_INFO: std::sync::LazyLock<std::sync::Mutex<InternalWriter>> =
    std::sync::LazyLock::new(|| {
        std::sync::Mutex::new(InternalWriter::new(InternalWriterLevel::Info))
    });
static INTERNAL_WRITER_MUTEX_WARN: std::sync::LazyLock<std::sync::Mutex<InternalWriter>> =
    std::sync::LazyLock::new(|| {
        std::sync::Mutex::new(InternalWriter::new(InternalWriterLevel::Warn))
    });
static INTERNAL_WRITER_MUTEX_ERROR: std::sync::LazyLock<std::sync::Mutex<InternalWriter>> =
    std::sync::LazyLock::new(|| {
        std::sync::Mutex::new(InternalWriter::new(InternalWriterLevel::Error))
    });
static INTERNAL_WRITER_MUTEX_VERBOSE: std::sync::LazyLock<std::sync::Mutex<InternalWriter>> =
    std::sync::LazyLock::new(|| {
        std::sync::Mutex::new(InternalWriter::new(InternalWriterLevel::Verbose))
    });

pub struct LockedInternalWriter {
    mutex: &'static std::sync::Mutex<InternalWriter>,
}

impl LockedInternalWriter {
    pub fn plugin() -> Self {
        Self {
            mutex: &INTERNAL_WRITER_MUTEX_PLUGIN,
        }
    }

    pub fn info() -> Self {
        Self {
            mutex: &INTERNAL_WRITER_MUTEX_INFO,
        }
    }

    pub fn warn() -> Self {
        Self {
            mutex: &INTERNAL_WRITER_MUTEX_WARN,
        }
    }

    pub fn error() -> Self {
        Self {
            mutex: &INTERNAL_WRITER_MUTEX_ERROR,
        }
    }

    pub fn verbose() -> Self {
        Self {
            mutex: &INTERNAL_WRITER_MUTEX_VERBOSE,
        }
    }
}
impl std::io::Write for LockedInternalWriter {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        let mut writer = self.mutex.lock().unwrap();
        writer.write(buf)
    }

    fn flush(&mut self) -> std::io::Result<()> {
        let mut writer = self.mutex.lock().unwrap();
        writer.flush()
    }
}

enum InternalWriterLevel {
    Plugin,
    Info,
    Warn,
    Error,
    Verbose,
}

struct InternalWriter {
    level: InternalWriterLevel,
    buffer: Vec<u8>,
}
impl InternalWriter {
    fn new(level: InternalWriterLevel) -> Self {
        Self {
            level,
            buffer: Vec::new(),
        }
    }
}

impl std::io::Write for InternalWriter {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.buffer.extend_from_slice(buf);
        self.flush()?;
        Ok(buf.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        while let Some(pos) = self.buffer.iter().position(|&b| b == b'\n') {
            let line = self.buffer.drain(..=pos).collect::<Vec<u8>>();
            let line = String::from_utf8_lossy(&line);
            let line = line.trim_end_matches('\n');
            match self.level {
                InternalWriterLevel::Plugin => {
                    let _ = write_plugin_log(line);
                }
                InternalWriterLevel::Info => {
                    let _ = write_info_log(line);
                }
                InternalWriterLevel::Warn => {
                    let _ = write_warn_log(line);
                }
                InternalWriterLevel::Error => {
                    let _ = write_error_log(line);
                }
                InternalWriterLevel::Verbose => {
                    let _ = write_verbose_log(line);
                }
            }
        }
        Ok(())
    }
}

/// プラグイン用ログに出力する[`dbg!`]マクロ。
///
/// # See Also
/// <https://github.com/rust-lang/rust/blob/29483883eed69d5fb4db01964cdf2af4d86e9cb2/library/std/src/macros.rs#L352>
#[macro_export]
macro_rules! ldbg {
    () => {
        $crate::lprintln!(verbose, "[{}:{}:{}]", ::std::file!(), ::std::line!(), ::std::column!());
    };
    ($val:expr $(,)?) => {
        match $val {
            tmp => {
                $crate::lprintln!(verbose, "[{}:{}:{}] {} = {:#?}",
                    ::std::file!(),
                    ::std::line!(),
                    ::std::column!(),
                    ::std::stringify!($val),
                    &&tmp as &dyn ::std::fmt::Debug,
                );
                tmp
            }
        }
    };
    ($($val:expr),+ $(,)?) => {
        ($($crate::ldbg!($val)),+,)
    };
}

/// プラグイン用ログに出力する[`println!`]マクロ。
///
/// ```rust
/// # use aviutl2::lprintln;
/// lprintln!("This is a plugin log message.");  // デフォルトはpluginログに出力
/// lprintln!(plugin, "This is also a plugin log message.");
/// lprintln!(info, "This is an info log message.");
/// lprintln!(warn, "This is a warning log message.");
/// lprintln!(error, "This is an error log message.");
/// lprintln!(verbose, "This is a verbose log message.");
/// ```
#[macro_export]
macro_rules! lprintln {
    (plugin, $($arg:tt)*) => {
        ::std::mem::drop($crate::logger::write_plugin_log(&format!($($arg)*)));
    };
    (info, $($arg:tt)*) => {
        ::std::mem::drop($crate::logger::write_info_log(&format!($($arg)*)));
    };
    (warn, $($arg:tt)*) => {
        ::std::mem::drop($crate::logger::write_warn_log(&format!($($arg)*)));
    };
    (error, $($arg:tt)*) => {
        ::std::mem::drop($crate::logger::write_error_log(&format!($($arg)*)));
    };
    (verbose, $($arg:tt)*) => {
        ::std::mem::drop($crate::logger::write_verbose_log(&format!($($arg)*)));
    };
    ($($arg:tt)*) => {
        $crate::lprintln!(plugin, $($arg)*);
    };
}

#[cfg(feature = "wrap_log")]
fn log_length_limit(kind_length: usize) -> usize {
    static DLL_LENGTH: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
    let dll_length = *DLL_LENGTH.get_or_init(|| {
        process_path::get_dylib_path()
            .map_or(0, |path| path.file_name().unwrap().to_string_lossy().len())
    });
    // [01/23 08:43:47] [VERBOSE] [Plugin::vi5.aux2] ...
    1023 - 35 - dll_length - kind_length
}
#[cfg(not(feature = "wrap_log"))]
fn log_length_limit(_kind_length: usize) -> usize {
    // wrap_logが無効な場合は制限なし
    usize::MAX
}

fn split_into_chunks(message: &str, kind_length: usize) -> Vec<String> {
    // 二分探索みたいなことをすればもっと効率的にできるけど面倒なので...
    let log_length_limit = log_length_limit(kind_length);
    let mut chunks = Vec::with_capacity(message.len() / log_length_limit + 1);
    let mut current_chunk = String::new();
    for letter in message.chars() {
        let letter_len = letter.len_utf8();
        if current_chunk.len() + letter_len > log_length_limit {
            chunks.push(std::mem::take(&mut current_chunk));
        }
        current_chunk.push(letter);
    }
    if !current_chunk.is_empty() {
        chunks.push(current_chunk);
    }
    chunks
}

/// プラグイン用ログにメッセージを書き込みます。
///
/// # Note
///
/// ロガーが初期化されていない場合は何も行いません。
///
/// # See Also
///
/// - [`ldbg!`]
/// - [`lprintln!`]
pub fn write_plugin_log(message: &str) -> Result<(), NullByteError> {
    with_logger_handle(|handle| unsafe {
        for chunk in split_into_chunks(message, "PLUGIN".len()) {
            let wide_message = CWString::new(&chunk)?;
            ((*handle).log)(handle, wide_message.as_ptr());
        }
        Ok(())
    })
    .unwrap_or(Ok(()))
}

#[duplicate::duplicate_item(
    level       function_name       log_method;
    ["ERROR"]   [write_error_log]   [error];
    ["WARN"]    [write_warn_log]    [warn];
    ["INFO"]    [write_info_log]    [info];
    ["VERBOSE"] [write_verbose_log] [verbose];
)]
#[doc = concat!("ログに", level, "レベルのメッセージを書き込みます。")]
///
/// # Note
///
/// ロガーが初期化されていない場合は何も行いません。
///
/// # See Also
///
/// - [`ldbg!`]
/// - [`lprintln!`]
pub fn function_name(message: &str) -> Result<(), NullByteError> {
    with_logger_handle(|handle| unsafe {
        for chunk in split_into_chunks(message, level.len()) {
            let wide_message = CWString::new(&chunk)?;
            ((*handle).log_method)(handle, wide_message.as_ptr());
        }
        Ok(())
    })
    .unwrap_or(Ok(()))
}

struct InternalLoggerHandle(*mut aviutl2_sys::logger2::LOG_HANDLE);
unsafe impl Send for InternalLoggerHandle {}

static LOGGER_HANDLE: std::sync::OnceLock<std::sync::Mutex<InternalLoggerHandle>> =
    std::sync::OnceLock::new();

#[doc(hidden)]
pub fn __initialize_logger(handle: *mut aviutl2_sys::logger2::LOG_HANDLE) {
    let internal_handle = InternalLoggerHandle(handle);
    LOGGER_HANDLE
        .set(std::sync::Mutex::new(internal_handle))
        .unwrap_or_else(|_| {
            panic!("Logger has already been initialized");
        });
}

#[doc(hidden)]
pub fn __initialize_logger_unwind(handle: *mut aviutl2_sys::logger2::LOG_HANDLE) {
    if let Err(panic_info) =
        crate::utils::catch_unwind_with_panic_info(|| __initialize_logger(handle))
    {
        crate::tracing::error!("Panic occurred during InitializeLogger: {}", panic_info);
        let _ = crate::logger::write_error_log(&panic_info);
    }
}

impl InternalLoggerHandle {
    fn ptr(&self) -> *mut aviutl2_sys::logger2::LOG_HANDLE {
        self.0
    }
}

fn with_logger_handle<F, T>(f: F) -> Option<T>
where
    F: FnOnce(*mut aviutl2_sys::logger2::LOG_HANDLE) -> T,
{
    let handle = LOGGER_HANDLE.get()?;
    let handle = handle.lock().unwrap();
    let handle_ptr = handle.ptr();
    Some(f(handle_ptr))
}

#[cfg(test)]
mod tests {
    #[test]
    fn aviutl2_formatter_does_not_emit_ansi_escape_codes() {
        struct TestWriter(std::sync::Arc<std::sync::Mutex<Vec<u8>>>);

        impl std::io::Write for TestWriter {
            fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
                std::io::Write::write(&mut *self.0.lock().unwrap(), buf)
            }

            fn flush(&mut self) -> std::io::Result<()> {
                std::io::Write::flush(&mut *self.0.lock().unwrap())
            }
        }

        let output = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
        let test_output = output.clone();
        let subscriber = tracing_subscriber::fmt()
            .with_ansi(true)
            .event_format(super::AviUtl2Formatter)
            .with_writer(move || TestWriter(test_output.clone()))
            .finish();

        tracing::subscriber::with_default(subscriber, || {
            tracing::info!(answer = 42, "test message");
        });

        let output = output.lock().unwrap();
        assert!(!output.contains(&b'\x1b'));
        let output = std::str::from_utf8(&output).unwrap();
        assert!(output.contains("test message"));
        assert!(output.contains("answer=42"));
    }

    #[test]
    fn test_can_compile_ldbg() {
        let x = 42;
        ldbg!();
        ldbg!(x);
        ldbg!(x + 1, x * 2);
    }

    #[test]
    fn test_can_compile_lprintln() {
        lprintln!("This is a test log message.");
        lprintln!(info, "This is an info log message.");
        lprintln!(warn, "This is a warning log message.");
        lprintln!(error, "This is an error log message.");
        lprintln!(verbose, "This is a verbose log message.");
    }

    #[test]
    #[cfg(feature = "wrap_log")]
    fn test_split_into_chunks() {
        let message = "a".repeat(5000);
        let chunks = super::split_into_chunks(&message, "VERBOSE".len());
        let dylib_name = process_path::get_dylib_path()
            .and_then(|path| {
                path.file_name()
                    .map(|name| name.to_string_lossy().into_owned())
            })
            .unwrap();
        for chunk in chunks {
            assert!(chunk.len() <= super::log_length_limit("VERBOSE".len()));
            assert!(
                format!(
                    "[01/23 08:43:47] [VERBOSE] [Plugin::{}] {}",
                    dylib_name, chunk
                )
                .len()
                    <= 1023
            );
        }
    }
}