Skip to content

Commit d6fa06a

Browse files
authored
examples: Initialize env_logger to actually see log output (#145)
`log` only provides the facade, and needs an explicitly initialized logger to emit output to `stdout`. The `env_logger` crate is typically used across the Rust ecosystem, but unfortunately initializes with a default of `off` unless the `RUST_LOG` variable is set: default it to `trace` instead (we mostly log on `info` though).
1 parent e5a641b commit d6fa06a

File tree

6 files changed

+28
-15
lines changed

6 files changed

+28
-15
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ ash = { version = "0.37", default-features = false, features = ["debug", "loaded
5050
ash-window = "0.12"
5151
raw-window-handle = "0.5"
5252
winit = { version = "0.27", features = ["x11", "wayland"] }
53+
env_logger = "0.10"
5354

5455
[target.'cfg(windows)'.dev-dependencies]
5556
winapi = { version = "0.3.9", features = ["d3d12", "d3d12sdklayers", "dxgi1_6", "winerror", "impl-default", "impl-debug", "winuser", "windowsx", "libloaderapi"] }

examples/d3d12-buffer-winrs.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use gpu_allocator::d3d12::{
33
AllocationCreateDesc, Allocator, AllocatorCreateDesc, ResourceCategory,
44
};
55
use gpu_allocator::MemoryLocation;
6-
use log::error;
6+
use log::*;
77
use windows::core::{Interface, Result};
88
use windows::Win32::{
99
Foundation::E_NOINTERFACE,
@@ -55,15 +55,15 @@ fn create_d3d12_device(dxgi_factory: &IDXGIFactory6) -> Option<ID3D12Device> {
5555
let mut device = None;
5656
match unsafe { D3D12CreateDevice(&adapter4, feature_level, &mut device) } {
5757
Ok(()) => {
58-
println!("Using D3D12 feature level: {}", feature_level_name);
58+
info!("Using D3D12 feature level: {}", feature_level_name);
5959
Some(device.unwrap())
6060
}
6161
Err(e) if e.code() == E_NOINTERFACE => {
6262
error!("ID3D12Device interface not supported");
6363
None
6464
}
6565
Err(e) => {
66-
println!(
66+
info!(
6767
"D3D12 feature level {} not supported: {}",
6868
feature_level_name, e
6969
);
@@ -80,6 +80,8 @@ fn create_d3d12_device(dxgi_factory: &IDXGIFactory6) -> Option<ID3D12Device> {
8080
}
8181

8282
fn main() -> Result<()> {
83+
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("trace")).init();
84+
8385
let dxgi_factory = unsafe { CreateDXGIFactory2(0) }?;
8486

8587
let device = create_d3d12_device(&dxgi_factory).expect("Failed to create D3D12 device.");
@@ -132,7 +134,7 @@ fn main() -> Result<()> {
132134
drop(resource);
133135

134136
allocator.free(allocation).unwrap();
135-
println!("Allocation and deallocation of GpuOnly memory was successful.");
137+
info!("Allocation and deallocation of GpuOnly memory was successful.");
136138
}
137139

138140
// Test allocating Cpu to Gpu memory
@@ -180,7 +182,7 @@ fn main() -> Result<()> {
180182
drop(resource);
181183

182184
allocator.free(allocation).unwrap();
183-
println!("Allocation and deallocation of CpuToGpu memory was successful.");
185+
info!("Allocation and deallocation of CpuToGpu memory was successful.");
184186
}
185187

186188
// Test allocating Gpu to Cpu memory
@@ -228,7 +230,7 @@ fn main() -> Result<()> {
228230
drop(resource);
229231

230232
allocator.free(allocation).unwrap();
231-
println!("Allocation and deallocation of CpuToGpu memory was successful.");
233+
info!("Allocation and deallocation of CpuToGpu memory was successful.");
232234
}
233235

234236
Ok(())

examples/d3d12-buffer.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn create_d3d12_device(
6868
};
6969
match hr {
7070
winapi::shared::winerror::S_OK => {
71-
println!("Using D3D12 feature level: {}.", feature_level_name);
71+
info!("Using D3D12 feature level: {}.", feature_level_name);
7272
Some(device)
7373
}
7474
winapi::shared::winerror::E_NOINTERFACE => {
@@ -93,6 +93,8 @@ fn create_d3d12_device(
9393
}
9494

9595
fn main() {
96+
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("trace")).init();
97+
9698
let dxgi_factory = {
9799
let mut dxgi_factory: *mut all_dxgi::IDXGIFactory6 = std::ptr::null_mut();
98100
let hr = unsafe {
@@ -167,7 +169,7 @@ fn main() {
167169
unsafe { resource.as_ref().unwrap().Release() };
168170

169171
allocator.free(allocation).unwrap();
170-
println!("Allocation and deallocation of GpuOnly memory was successful.");
172+
info!("Allocation and deallocation of GpuOnly memory was successful.");
171173
}
172174

173175
// Test allocating Cpu to Gpu memory
@@ -219,7 +221,7 @@ fn main() {
219221
unsafe { resource.as_ref().unwrap().Release() };
220222

221223
allocator.free(allocation).unwrap();
222-
println!("Allocation and deallocation of CpuToGpu memory was successful.");
224+
info!("Allocation and deallocation of CpuToGpu memory was successful.");
223225
}
224226

225227
// Test allocating Gpu to Cpu memory
@@ -271,7 +273,7 @@ fn main() {
271273
unsafe { resource.as_ref().unwrap().Release() };
272274

273275
allocator.free(allocation).unwrap();
274-
println!("Allocation and deallocation of CpuToGpu memory was successful.");
276+
info!("Allocation and deallocation of CpuToGpu memory was successful.");
275277
}
276278

277279
drop(allocator); // Explicitly drop before destruction of device.

examples/d3d12-visualization/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![windows_subsystem = "windows"]
22
//! Example showcasing [`winapi`] interop with [`gpu-allocator`] which is driven by the [`windows`] crate.
3+
use log::info;
34
use raw_window_handle::HasRawWindowHandle;
45

56
use gpu_allocator::d3d12::{Allocator, AllocatorCreateDesc, ToWindows};
@@ -128,6 +129,8 @@ fn transition_resource(
128129
}
129130

130131
fn main() {
132+
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("trace")).init();
133+
131134
// Disable automatic DPI scaling by windows
132135
unsafe { winuser::SetProcessDPIAware() };
133136

@@ -151,7 +154,7 @@ fn main() {
151154
std::thread::spawn(move || {
152155
let mut dxgi_factory_flags = 0;
153156
if ENABLE_DEBUG_LAYER && enable_d3d12_debug_layer() {
154-
println!("Enabled D3D12 debug layer");
157+
info!("Enabled D3D12 debug layer");
155158
dxgi_factory_flags |= all_dxgi::DXGI_CREATE_FACTORY_DEBUG;
156159
}
157160

examples/vulkan-buffer.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use ash::vk;
2+
use log::info;
23

34
use std::default::Default;
45
use std::ffi::CString;
@@ -9,9 +10,11 @@ use gpu_allocator::vulkan::{
910
use gpu_allocator::MemoryLocation;
1011

1112
fn main() {
13+
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("trace")).init();
14+
1215
let entry = unsafe { ash::Entry::load() }.unwrap();
1316

14-
// Create vulkan instance
17+
// Create Vulkan instance
1518
let instance = {
1619
let app_name = CString::new("Vulkan gpu-allocator test").unwrap();
1720

@@ -128,7 +131,7 @@ fn main() {
128131

129132
unsafe { device.destroy_buffer(test_buffer, None) };
130133

131-
println!("Allocation and deallocation of GpuOnly memory was successful.");
134+
info!("Allocation and deallocation of GpuOnly memory was successful.");
132135
}
133136

134137
// Test allocating Cpu to Gpu memory
@@ -161,7 +164,7 @@ fn main() {
161164

162165
unsafe { device.destroy_buffer(test_buffer, None) };
163166

164-
println!("Allocation and deallocation of CpuToGpu memory was successful.");
167+
info!("Allocation and deallocation of CpuToGpu memory was successful.");
165168
}
166169

167170
// Test allocating Gpu to Cpu memory
@@ -194,7 +197,7 @@ fn main() {
194197

195198
unsafe { device.destroy_buffer(test_buffer, None) };
196199

197-
println!("Allocation and deallocation of GpuToCpu memory was successful.");
200+
info!("Allocation and deallocation of GpuToCpu memory was successful.");
198201
}
199202

200203
drop(allocator); // Explicitly drop before destruction of device and instance.

examples/vulkan-visualization/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ mod helper;
1212
use helper::record_and_submit_command_buffer;
1313

1414
fn main() -> ash::prelude::VkResult<()> {
15+
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("trace")).init();
16+
1517
let entry = unsafe { ash::Entry::load() }.unwrap();
1618

1719
let event_loop = winit::event_loop::EventLoop::new();

0 commit comments

Comments
 (0)