mirror of
https://github.com/velopack/velopack.git
synced 2025-10-25 15:19:22 +00:00
Add back C++ logging callbacks
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -2297,6 +2297,7 @@ dependencies = [
|
||||
"log",
|
||||
"log-derive",
|
||||
"serde_json",
|
||||
"simplelog",
|
||||
"velopack",
|
||||
]
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ velopack.workspace = true
|
||||
anyhow.workspace = true
|
||||
lazy_static.workspace = true
|
||||
log.workspace = true
|
||||
simplelog.workspace = true
|
||||
log-derive.workspace = true
|
||||
libc.workspace = true
|
||||
serde_json.workspace = true
|
||||
|
||||
@@ -179,6 +179,13 @@ typedef void (*vpkc_progress_callback_t)(void *p_user_data, size_t progress);
|
||||
*/
|
||||
typedef void (*vpkc_hook_callback_t)(void *p_user_data, const char *psz_app_version);
|
||||
|
||||
/**
|
||||
* Log callback function.
|
||||
*/
|
||||
typedef void (*vpkc_log_callback_t)(void *p_user_data,
|
||||
const char *psz_level,
|
||||
const char *psz_message);
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // __cplusplus
|
||||
@@ -392,6 +399,12 @@ void vpkc_app_set_hook_restarted(vpkc_hook_callback_t cb_restarted);
|
||||
*/
|
||||
size_t vpkc_get_last_error(char *psz_error, size_t c_error);
|
||||
|
||||
/**
|
||||
* Set a custom log callback. This will be called for all log messages generated by the Velopack library.
|
||||
*/
|
||||
void vpkc_set_logger(vpkc_log_callback_t cb_log,
|
||||
void *p_user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif // __cplusplus
|
||||
|
||||
@@ -447,7 +447,7 @@ pub extern "C" fn vpkc_app_run(p_user_data: *mut c_void) {
|
||||
default_logfile_path(LocationContext::FromCurrentExe)
|
||||
};
|
||||
|
||||
init_logging("lib-cpp", Some(&log_file), false, false);
|
||||
init_logging("lib-cpp", Some(&log_file), false, false, Some(create_shared_logger()));
|
||||
app.run();
|
||||
}
|
||||
|
||||
@@ -540,4 +540,10 @@ pub extern "C" fn vpkc_app_set_hook_restarted(cb_restarted: vpkc_hook_callback_t
|
||||
pub extern "C" fn vpkc_get_last_error(psz_error: *mut c_char, c_error: size_t) -> size_t {
|
||||
let error = get_last_error();
|
||||
return_cstr(psz_error, c_error, &error)
|
||||
}
|
||||
}
|
||||
|
||||
/// Set a custom log callback. This will be called for all log messages generated by the Velopack library.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn vpkc_set_logger(cb_log: vpkc_log_callback_t, p_user_data: *mut c_void) {
|
||||
set_log_callback(cb_log, p_user_data);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
use anyhow::Result;
|
||||
use std::sync::RwLock;
|
||||
use log::{Level, LevelFilter, Log, Metadata, Record};
|
||||
use simplelog::{Config, SharedLogger};
|
||||
use std::ffi::{c_void, CString};
|
||||
use std::sync::{Mutex, RwLock};
|
||||
use velopack::locator::VelopackLocatorConfig;
|
||||
|
||||
use crate::types::*;
|
||||
@@ -19,6 +22,7 @@ pub struct AppOptions {
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
static ref LAST_ERROR: RwLock<String> = RwLock::new(String::new());
|
||||
static ref LOG_CALLBACK: Mutex<(vpkc_log_callback_t, usize)> = Mutex::new((None, 0));
|
||||
pub static ref VELOPACK_APP: RwLock<AppOptions> = RwLock::new(Default::default());
|
||||
}
|
||||
|
||||
@@ -61,3 +65,65 @@ where
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_log_callback(callback: vpkc_log_callback_t, user_data: *mut c_void) {
|
||||
let mut log_callback = LOG_CALLBACK.lock().unwrap();
|
||||
*log_callback = (callback, user_data as usize);
|
||||
}
|
||||
|
||||
pub fn log_message(level: &str, message: &str) {
|
||||
let log_callback = LOG_CALLBACK.lock().unwrap();
|
||||
let (callback, user_data) = *log_callback;
|
||||
if let Some(callback) = callback {
|
||||
let c_level = CString::new(level).unwrap();
|
||||
let c_message = CString::new(message).unwrap();
|
||||
callback(user_data as *mut c_void, c_level.as_ptr(), c_message.as_ptr());
|
||||
}
|
||||
}
|
||||
|
||||
struct LoggerImpl {}
|
||||
|
||||
impl SharedLogger for LoggerImpl {
|
||||
fn level(&self) -> LevelFilter {
|
||||
LevelFilter::max()
|
||||
}
|
||||
|
||||
fn config(&self) -> Option<&Config> {
|
||||
None
|
||||
}
|
||||
|
||||
fn as_log(self: Box<Self>) -> Box<dyn Log> {
|
||||
Box::new(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Log for LoggerImpl {
|
||||
fn enabled(&self, metadata: &Metadata) -> bool {
|
||||
metadata.level() <= log::max_level()
|
||||
}
|
||||
|
||||
fn log(&self, record: &Record) {
|
||||
if !self.enabled(record.metadata()) {
|
||||
return;
|
||||
}
|
||||
|
||||
let text = format!("{}", record.args());
|
||||
|
||||
let level = match record.level() {
|
||||
Level::Error => "error",
|
||||
Level::Warn => "warn",
|
||||
Level::Info => "info",
|
||||
Level::Debug => "debug",
|
||||
Level::Trace => "trace",
|
||||
}
|
||||
.to_string();
|
||||
|
||||
log_message(&level, &text);
|
||||
}
|
||||
|
||||
fn flush(&self) {}
|
||||
}
|
||||
|
||||
pub fn create_shared_logger() -> Box<dyn SharedLogger> {
|
||||
Box::new(LoggerImpl {})
|
||||
}
|
||||
|
||||
@@ -22,6 +22,9 @@ pub type vpkc_update_source_t = c_void;
|
||||
/// Progress callback function.
|
||||
pub type vpkc_progress_callback_t = Option<extern "C" fn(p_user_data: *mut c_void, progress: size_t)>;
|
||||
|
||||
/// Log callback function.
|
||||
pub type vpkc_log_callback_t = Option<extern "C" fn(p_user_data: *mut c_void, psz_level: *const c_char, psz_message: *const c_char)>;
|
||||
|
||||
/// VelopackApp startup hook callback function.
|
||||
pub type vpkc_hook_callback_t = Option<extern "C" fn(p_user_data: *mut c_void, psz_app_version: *const c_char)>;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user