1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use super::errors::{Error, Result};
use crate::errors::AppError;
use ffi_utils::{catch_unwind_cb, FfiResult, ReprC, FFI_RESULT_OK};
use safe_core::utils::logging;
use std::ffi::CString;
use std::os::raw::{c_char, c_void};
#[no_mangle]
pub unsafe extern "C" fn app_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<()> {
if output_file_name_override.is_null() {
logging::init(false)?;
} else {
let output_file_name_override = String::clone_from_repr_c(output_file_name_override)?;
logging::init_with_output_file(false, output_file_name_override)?;
}
o_cb(user_data, FFI_RESULT_OK);
Ok(())
});
}
#[no_mangle]
pub unsafe extern "C" fn app_config_dir_path(
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<()> {
let config_dir = safe_core::config_dir()?;
let config_dir_path = CString::new(
config_dir
.into_os_string()
.into_string()
.map_err(|_| AppError::Unexpected("Couldn't convert OsString".to_string()))
.map_err(Error::from)?
.into_bytes(),
)?;
o_cb(user_data, FFI_RESULT_OK, config_dir_path.as_ptr());
Ok(())
})
}
#[cfg(test)]
mod tests {
use super::*;
use ffi_utils::test_utils::{call_0, call_1};
use log::{debug, error};
use safe_core::config_dir;
use std::env;
use std::fs::{self, File};
use std::io::Read;
use std::path::PathBuf;
use std::str::FromStr;
use std::thread;
use std::time::Duration;
use unwrap::unwrap;
#[test]
fn config_dir_path() {
let path: String = unsafe { unwrap!(call_1(|ud, cb| app_config_dir_path(ud, cb),)) };
let expected_path = unwrap!(unwrap!(config_dir()).into_os_string().into_string());
assert_eq!(path, expected_path);
}
#[test]
fn file_logging() {
setup_log_config();
let log_file_name = unwrap!(PathBuf::from_str("AppClient.log"));
let file_name = unwrap!(CString::new(unwrap!(log_file_name
.clone()
.into_os_string()
.into_string())));
unsafe {
unwrap!(call_0(|ud, cb| app_init_logging(
file_name.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(1));
let log_path = unwrap!(config_dir()).join(log_file_name);
let mut log_file = unwrap!(File::open(log_path));
let mut file_content = String::new();
let written = unwrap!(log_file.read_to_string(&mut file_content));
assert!(written > 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 config_dir = unwrap!(config_dir());
unwrap!(fs::create_dir_all(config_dir.clone()));
if current_dir.as_path() != config_dir.as_path() {
current_dir.push("sample_log_file/log.toml");
config_dir.push("log.toml");
let _ = unwrap!(fs::copy(current_dir, config_dir));
}
}
}