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
use std::{ffi::OsStr, iter::once};
use crate::{
sink::{helper, Sink},
Record, Result, StringBuf,
};
pub struct WinDebugSink {
common_impl: helper::CommonImpl,
}
impl WinDebugSink {
#[must_use]
pub fn builder() -> WinDebugSinkBuilder {
WinDebugSinkBuilder {
common_builder_impl: helper::CommonBuilderImpl::new(),
}
}
#[allow(clippy::new_without_default)]
#[deprecated(
since = "0.3.0",
note = "it may be removed in the future, use `WinDebugSink::builder()` instead"
)]
#[must_use]
pub fn new() -> WinDebugSink {
WinDebugSink::builder().build().unwrap()
}
}
impl Sink for WinDebugSink {
fn log(&self, record: &Record) -> Result<()> {
#[cfg(windows)] use std::os::windows::ffi::OsStrExt;
if !self.should_log(record.level()) {
return Ok(());
}
let mut string_buf = StringBuf::new();
self.common_impl
.formatter
.read()
.format(record, &mut string_buf)?;
let wide: Vec<u16> = OsStr::new(&string_buf)
.encode_wide()
.chain(once(0))
.collect();
let wide = wide.as_ptr();
unsafe { winapi::um::debugapi::OutputDebugStringW(wide) }
Ok(())
}
fn flush(&self) -> Result<()> {
Ok(())
}
helper::common_impl!(@Sink: common_impl);
}
pub struct WinDebugSinkBuilder {
common_builder_impl: helper::CommonBuilderImpl,
}
impl WinDebugSinkBuilder {
helper::common_impl!(@SinkBuilder: common_builder_impl);
pub fn build(self) -> Result<WinDebugSink> {
let sink = WinDebugSink {
common_impl: helper::CommonImpl::from_builder(self.common_builder_impl),
};
Ok(sink)
}
}