Skip to content

Instantly share code, notes, and snippets.

@divi255
Last active August 20, 2021 08:08
Show Gist options
  • Save divi255/7f632ec3d60b617ce27b79380647140a to your computer and use it in GitHub Desktop.
Save divi255/7f632ec3d60b617ce27b79380647140a to your computer and use it in GitHub Desktop.
Logging from embedded Python to Rust
import logging
def rust_emit_log(*args):
# Override from Rust with pyfunction
pass
class RustLogHandler(logging.Handler):
def emit(self, record):
msg = f'{record.name}: {record.getMessage()}'
rust_emit_log(msg, record.levelno)
root_logger = logging.getLogger()
for h in root_logger.handlers:
root_logger.removeHandler(h)
handler = RustLogHandler()
root_logger.addHandler(handler)
"""
Rust logger function example
use log::{debug, info, warn, error};
#[pyfunction]
fn rust_emit_log(_py: Python, message: String, level: u8) {
match level {
20 => info!("{}", message),
30 => warn!("{}", message),
_ => {
if level <= 10 {
debug!("{}", message);
} else {
error!("{}", message);
}
}
}
}
"""
@ratijas
Copy link

ratijas commented Aug 20, 2021

Made similar proxy for Qt->Rust: PR, code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment