Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to Rust & PyO3 #11

Open
ds-ak opened this issue Nov 29, 2022 · 3 comments
Open

Port to Rust & PyO3 #11

ds-ak opened this issue Nov 29, 2022 · 3 comments

Comments

@ds-ak
Copy link

ds-ak commented Nov 29, 2022

This is just a tracker to port this to Rust and PyO3. I need to use this in Enola and as a result we can also use this in other languages that rust offers bindings to.

https://github.com/PyO3/pyo3

@gauravsaini
Copy link

Non working port . Might start from here

`use pyo3::prelude::;
use pyo3::types::
;
use std::rc::Weak;

#[pyclass]
struct AKLogger {
name: String,
parent: Weak,
logger: Logger,
slkr: Option,
slack_token: Option,
slack_level: Option,
level: i32,
filename: Option,
}

#[pymethods]
impl AKLogger {
#[new]
fn new(obj: &PyRawObject, name: &str, parent: Option<&AKLogger>) -> PyResult<()> {
let gil = Python::acquire_gil();
let py = gil.python();
let logging = py.import("logging")?;

     let root_logger = match parent {
         Some(parent) => parent.logger.get_child(name)?,
         None => logging.get_logger("root")?,
     };

     obj.init(AKLogger {
         name: name.to_owned(),
         parent,
         logger: root_logger,
         slkr: None,
         slack_token: None,
         slack_level: None,
         level: logging.NOTSET?,
     });
 }

 #[getter]
 fn name(&self) -> PyResult<&str> {
     Ok(&self.name)
 }

 fn set_name(&mut self, name: &str) {
     self.name = name.to_owned();
 }

 fn get_logger(&self, name: &str) -> PyResult<AKLogger> {
     let gil = Python::acquire_gil();
     let py = gil.python();
     let logging = py.import("logging")?;

     let logger = self.logger.get_child(name)?;
     let aklogger = AKLogger {
         name: name.to_owned(),
         parent: Some(self),
         logger,
         slkr: None,
         slack_token: None,
         slack_level: None,
         level: logging.NOTSET?,
     };

     Ok(aklogger)
 }

 fn set_level(&mut self, level: i32) {
     let gil = Python::acquire_gil();
     let py = gil.python();
     let logging = py.import("logging")?;

     let level = logging._check_level(level)?;
     self.level = level;
     self.logger.set_level(level)?;
 }

 #[getter]
 fn level(&self) -> PyResult<i32> {
     Ok(self.level)
 }

 fn enable_slack(&mut self, token: &str) -> PyResult<()> {
     let gil = Python::acquire_gil();
     let py = gil.python();
     let slacker = py.import("slacker")?;
     let slkr = slacker.Slacker(token)?;
     slkr.api.test()?;

     self.slkr = Some(slkr);
     self.slack_token = Some(token.to_owned());
     Ok(())
 }

 fn disable_slack(&mut self) {
     self.slkr = None;
     self.slack_token = None;
 }

 #[getter]
 fn slack_level(&self) -> PyResult<i32> {
    Ok(self.slack_level.unwrap_or(self.level))
}

fn set_slack_level(&mut self, level: i32) -> PyResult<()> {
    let gil = Python::acquire_gil();
    let py = gil.python();
    let logging = py.import("logging")?;

    let level = logging._check_level(level)?;
    self.slack_level = Some(level);
    Ok(())
}

fn log_to_file(&mut self, file_name: &str) -> PyResult<()> {
    let gil = Python::acquire_gil();
    let py = gil.python();
    let logging = py.import("logging")?;

    let handler = logging.FileHandler(file_name)?;
    self.logger.add_handler(handler)?;
    Ok(())
}

fn log(&self, level: i32, summary: &str, details: Option<&str>, channel: &str,
       force_push_slack: bool) -> PyResult<()> {
    let gil = Python::acquire_gil();
    let py = gil.python();
    let logging = py.import("logging")?;

    let level_name = logging._level_to_name[level]?;
    let level_color = logging._level_to_color[level]?;

    let message = match details {
        Some(details) => format!("{}\n{}", summary, details),
        None => summary.to_owned(),
    };

    self.logger.log(level, &message)?;

    if let Some(slkr) = self.slkr {
                   let slack_level = self.slack_level.unwrap_or(self.level);
                   if level >= slack_level || force_push_slack {
                    let attachment = PyList::new(py, &[
                        PyDict::new(py, &[
                            ("color", level_color),
                            ("text", message),
                            ]),
                        ]);
       
                       slkr.chat.post_message(channel, "", attachment)?;
                   }
                }

    Ok(())
}

#[pyfn(m, "debug")]
fn debug_py(&self, summary: &str, details: Option<&str>, channel: &str,
            force_push_slack: bool) -> PyResult<()> {
    self.log(logging.DEBUG, summary, details, channel, force_push_slack)
}

#[pyfn(m, "info")]
fn info_py(&self, summary: &str, details: Option<&str>, channel: &str,
           force_push_slack: bool) -> PyResult<()> {
    self.log(logging.INFO, summary, details, channel, force_push_slack)
}

#[pyfn(m, "warning")]
fn warning_py(&self, summary: &str, details: Option<&str>, channel: &str,
              force_push_slack: bool) -> PyResult<()> {
    self.log(logging.WARNING, summary, details, channel, force_push_slack)
}

#[pyfn(m, "error")]
fn error_py(&self, summary: &str, details: Option<&str>, channel: &str,
             force_push_slack: bool) -> PyResult<()> {
    self.log(logging.ERROR, summary, details, channel, force_push_slack)
}

#[pyfn(m, "critical")]
fn critical_py(&self, summary: &str, details: Option<&str>, channel: &str,
               force_push_slack: bool) -> PyResult<()> {
    self.log(logging.CRITICAL, summary, details, channel, force_push_slack)
}

}

#[pymodinit]
fn init_aklogger(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::()?;

 Ok(())

}`

@ds-ak
Copy link
Author

ds-ak commented Dec 3, 2022

Oh wow, thanks for your effort @gauravsaini This is indeed an excellent starting point :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants