Last active
August 20, 2021 08:08
-
-
Save divi255/7f632ec3d60b617ce27b79380647140a to your computer and use it in GitHub Desktop.
Logging from embedded Python to Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Made similar proxy for Qt->Rust: PR, code.