use super::AuthError;
use config_file_handler::FileHandler;
use ffi_utils::{catch_unwind_cb, from_c_str, FfiResult, FFI_RESULT_OK};
use maidsafe_utilities::log;
use std::ffi::CString;
use std::os::raw::{c_char, c_void};
#[no_mangle]
pub unsafe extern "C" fn auth_init_logging(
output_file_name_override: *const c_char,
user_data: *mut c_void,
o_cb: extern "C" fn(user_data: *mut c_void, result: *const FfiResult),
) {
catch_unwind_cb(user_data, o_cb, || -> Result<(), AuthError> {
if output_file_name_override.is_null() {
log::init(false)?;
} else {
let output_file_name_override = from_c_str(output_file_name_override)?;
log::init_with_output_file(false, output_file_name_override)?;
}
o_cb(user_data, FFI_RESULT_OK);
Ok(())
});
}
#[no_mangle]
pub unsafe extern "C" fn auth_output_log_path(
output_file_name: *const c_char,
user_data: *mut c_void,
o_cb: extern "C" fn(user_data: *mut c_void, result: *const FfiResult, log_path: *const c_char),
) {
catch_unwind_cb(user_data, o_cb, || -> Result<(), AuthError> {
let op_file = from_c_str(output_file_name)?;
let fh = FileHandler::<()>::new(&op_file, true)
.map_err(|e| AuthError::Unexpected(format!("{}", e)))?;
let op_file_path = CString::new(
fh.path()
.to_path_buf()
.into_os_string()
.into_string()
.map_err(|_| AuthError::Unexpected("Couldn't convert OsString".to_string()))?
.into_bytes(),
)?;
o_cb(user_data, FFI_RESULT_OK, op_file_path.as_ptr());
Ok(())
})
}
#[cfg(test)]
mod tests {
use super::*;
use config_file_handler::current_bin_dir;
use ffi_utils::test_utils::{call_0, call_1};
use std::env;
use std::fs::{self, File};
use std::io::Read;
use std::thread;
use std::time::Duration;
#[test]
fn output_log_path() {
let name = "_test path";
let path_str = unwrap!(CString::new(name));
let path: String = unsafe {
unwrap!(call_1(|ud, cb| auth_output_log_path(
path_str.as_ptr(),
ud,
cb
),))
};
assert!(path.contains(name));
}
#[test]
fn file_logging() {
setup_log_config();
let mut current_exe_path = unwrap!(current_bin_dir());
current_exe_path.push("auth-log-output.log");
let log_file_path = unwrap!(CString::new(unwrap!(current_exe_path
.clone()
.into_os_string()
.into_string())));
unsafe {
unwrap!(call_0(|ud, cb| auth_init_logging(
log_file_path.as_ptr(),
ud,
cb
),));
}
let debug_msg = "This is a sample debug message".to_owned();
let junk_msg = "This message should not exist in the log file".to_owned();
error!("{}", debug_msg);
debug!("{}", junk_msg);
thread::sleep(Duration::from_secs(10));
let mut log_file = unwrap!(File::open(current_exe_path));
let mut file_content = String::new();
let num_bytes = unwrap!(log_file.read_to_string(&mut file_content));
assert!(num_bytes > 0);
assert!(file_content.contains(&debug_msg[..]));
assert!(!file_content.contains(&junk_msg[..]));
}
fn setup_log_config() {
let mut current_dir = unwrap!(env::current_dir());
let mut current_bin_dir = unwrap!(current_bin_dir());
if current_dir.as_path() != current_bin_dir.as_path() {
current_dir.push("sample_log_file/log.toml");
current_bin_dir.push("log.toml");
let _ = unwrap!(fs::copy(current_dir, current_bin_dir));
}
}
}