-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 67-remove-k3-pow
- Loading branch information
Showing
16 changed files
with
156 additions
and
21 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod initialization; | ||
mod log; | ||
mod post_impl; | ||
|
||
#[repr(C)] | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
use std::ffi::c_char; | ||
|
||
use log::{Level, Log, Metadata, Record}; | ||
|
||
/// FFI-safe borrowed Rust &str | ||
#[repr(C)] | ||
pub struct StringView { | ||
pub ptr: *const c_char, | ||
pub len: usize, | ||
} | ||
|
||
impl StringView { | ||
#[cfg(test)] | ||
pub unsafe fn to_str<'a>(&self) -> &'a str { | ||
let bytes = std::slice::from_raw_parts(self.ptr as _, self.len); | ||
std::str::from_utf8_unchecked(bytes) | ||
} | ||
} | ||
|
||
impl<'a> From<&'a str> for StringView { | ||
fn from(s: &'a str) -> Self { | ||
Self { | ||
ptr: s.as_ptr() as _, | ||
len: s.len(), | ||
} | ||
} | ||
} | ||
|
||
#[repr(C)] | ||
pub struct ExternCRecord { | ||
pub level: Level, | ||
pub message: StringView, | ||
pub module_path: StringView, | ||
pub file: StringView, | ||
pub line: i64, | ||
} | ||
|
||
struct ExternCLog { | ||
callback: extern "C" fn(&ExternCRecord), | ||
level: Level, | ||
} | ||
|
||
impl ExternCLog { | ||
fn new(level: Level, callback: extern "C" fn(&ExternCRecord)) -> Self { | ||
Self { level, callback } | ||
} | ||
} | ||
|
||
impl Log for ExternCLog { | ||
fn enabled(&self, metadata: &Metadata) -> bool { | ||
metadata.level() <= self.level | ||
} | ||
|
||
fn log(&self, record: &Record) { | ||
let msg = record.args().to_string(); | ||
let c_record = ExternCRecord { | ||
level: record.level(), | ||
message: StringView::from(msg.as_str()), | ||
module_path: StringView::from(record.module_path().unwrap_or("")), | ||
file: StringView::from(record.file().unwrap_or("")), | ||
line: record.line().map(|u| u as i64).unwrap_or(-1_i64), | ||
}; | ||
|
||
(self.callback)(&c_record); | ||
} | ||
|
||
fn flush(&self) {} | ||
} | ||
|
||
/// Set a logging callback function | ||
/// The function is idempotent, calling it more then once will have no effect. | ||
/// Returns 0 if the callback was set successfully, 1 otherwise. | ||
#[no_mangle] | ||
pub extern "C" fn set_logging_callback( | ||
level: Level, | ||
callback: extern "C" fn(&ExternCRecord), | ||
) -> i32 { | ||
match log::set_boxed_logger(Box::new(ExternCLog::new(level, callback))) | ||
.map(|()| log::set_max_level(level.to_level_filter())) | ||
{ | ||
Ok(_) => 0, | ||
Err(e) => { | ||
eprintln!("Failed to set logger ({e})"); | ||
1 | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::log::ExternCRecord; | ||
|
||
#[test] | ||
fn logging_callback() { | ||
extern "C" fn log_cb(record: &ExternCRecord) { | ||
assert_eq!("Hello, logger", unsafe { record.message.to_str() }); | ||
assert_eq!(log::Level::Info, record.level); | ||
} | ||
|
||
super::set_logging_callback(log::Level::Info, log_cb); | ||
log::info!("Hello, logger"); | ||
log::trace!("Trace log level is disabled"); | ||
|
||
assert_eq!(1, super::set_logging_callback(log::Level::Warn, log_cb)); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ scrypt-ocl = { path = "../scrypt-ocl" } | |
rayon = "1.7.0" | ||
eyre = "0.6.8" | ||
rand = "0.8.5" | ||
env_logger = "0.10.0" |
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
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
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
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
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
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
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