Skip to content

Commit aa18d2a

Browse files
committed
feat: log to steam logs
1 parent e72a547 commit aa18d2a

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

bin/driver-proxy/src/driver/server_tracked_provider.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ use std::{os::raw::c_char, sync::Mutex};
33
use crate::{
44
driver_context::{try_init_driver_context, DRIVER_CONTEXT},
55
factory::{get_hmd_driver_factory, TOKIO_RUNTIME},
6+
log::try_init_driver_log,
67
setting,
78
settings::Setting,
89
try_vr,
910
};
1011
use cppvtbl::{impl_vtables, HasVtable, VtableRef, WithVtables};
1112
use once_cell::sync::Lazy;
13+
use openvr::{IVRDriverLogVtable, IVRDriverLog_Version};
1214
use tokio::task::LocalSet;
1315
use tracing::info;
1416
use valve_pm::{start_manager, StationCommand, StationControl, StationState};
@@ -36,8 +38,13 @@ impl IServerTrackedDeviceProvider for ServerTrackedProvider {
3638
&self,
3739
pDriverContext: *const cppvtbl::VtableRef<IVRDriverContextVtable>,
3840
) -> EVRInitError {
39-
let _ = try_init_driver_context(unsafe { &*pDriverContext });
41+
try_init_driver_context(unsafe { &*pDriverContext });
4042
let context = DRIVER_CONTEXT.get().expect("context just initialized");
43+
let logger: *const cppvtbl::VtableRef<IVRDriverLogVtable> = context
44+
.get_generic_interface(IVRDriverLog_Version)
45+
.expect("always able to initialize driver log")
46+
.cast();
47+
try_init_driver_log(unsafe { &*logger });
4148

4249
let power_management = POWER_MANAGEMENT.get();
4350
*self.standby_state.lock().expect("lock") = match power_management {
@@ -56,8 +63,8 @@ impl IServerTrackedDeviceProvider for ServerTrackedProvider {
5663
break 'stations;
5764
}
5865
let Ok(manager) = TOKIO_RUNTIME.block_on(start_manager()) else {
59-
break 'stations;
60-
};
66+
break 'stations;
67+
};
6168
let stations: Vec<_> = stations
6269
.iter()
6370
.filter_map(|line| {

bin/driver-proxy/src/factory.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use std::{
44
ptr::null,
55
};
66

7-
use crate::{server_tracked_provider::SERVER_TRACKED_DEVICE_PROVIDER, Error, Result};
7+
use crate::{
8+
log::LogWriter, server_tracked_provider::SERVER_TRACKED_DEVICE_PROVIDER, Error, Result,
9+
};
810
use cppvtbl::{HasVtable, VtableRef};
911
use libloading::{Library, Symbol};
1012
use once_cell::sync::{Lazy, OnceCell};
@@ -36,7 +38,12 @@ pub static TOKIO_RUNTIME: Lazy<Runtime> =
3638

3739
fn HmdDriverFactory_impl(iface: *const c_char) -> Result<*const c_void> {
3840
// May be already installed
39-
if tracing_subscriber::fmt().without_time().try_init().is_ok() {
41+
if tracing_subscriber::fmt()
42+
.without_time()
43+
.with_writer(LogWriter::default)
44+
.try_init()
45+
.is_ok()
46+
{
4047
// This magic string is also used for installation detection!
4148
info!("https://patreon.com/0lach");
4249
}

bin/driver-proxy/src/log.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use crate::openvr::{IVRDriverLog, IVRDriverLogVtable};
77

88
static DRIVER_LOG: OnceCell<&'static VtableRef<IVRDriverLogVtable>> = OnceCell::new();
99

10-
struct LogWriter(Vec<u8>);
10+
#[derive(Default)]
11+
pub struct LogWriter(Vec<u8>);
1112
impl LogWriter {
1213
fn flush_line(&mut self) {
1314
if let Some(driver) = DRIVER_LOG.get() {
@@ -27,11 +28,16 @@ impl Write for LogWriter {
2728
self.flush_line();
2829
buf = &buf[pos + 1..];
2930
}
30-
self.0.extend_from_slice(&buf);
31+
self.0.extend_from_slice(buf);
3132
Ok(buf.len())
3233
}
3334

3435
fn flush(&mut self) -> std::io::Result<()> {
36+
self.flush_line();
3537
Ok(())
3638
}
3739
}
40+
41+
pub fn try_init_driver_log(log: &'static VtableRef<IVRDriverLogVtable>) {
42+
let _ = DRIVER_LOG.set(log);
43+
}

0 commit comments

Comments
 (0)