Debugging win32 by printing values


#1

I’d like to be able to print info from inside a rust win32 app. But because I use #![windows_subsystem = "windows"], there is no stdout or stderr to print to.
So, I tried logging:

#![windows_subsystem = "windows"]

#[macro_use] extern crate log;
extern crate simplelog;
extern crate winapi;

use simplelog::*;
use std::fs::File;
    // Lots elided

quick_main!(run);

fn run() -> Result<()> {
    WriteLogger::new(LevelFilter::Info, Config::default(), File::create("debug.log").unwrap());
    info!("starting up");
    // Lots elided including more info!() calls
}

This creates an empty debug.log file, but never writes to it.
I don’t need (or want) to use a debugger & was hoping to be able to print to a file.


#2

According to simplelog crate documentation, you need to call WriteLogger::init instead of WriteLogger::new. The init() function registers the logger globally, while new() simply creates a logger object that can be passed to e.g. a CombinedLogger.


#3

I think virtually all log backends require their init() to be called before logging starts.


#4

Thank you very much! I’d looked at that example but only needed the WriteLogger and didn’t notice that the CombinedLogger used init and that I’d need that. It works perfectly now.


#5

Note that you can also just call AllocConsole() to manually open a console window, after which println should work.