Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions c2pa_c_ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
thiserror = "1.0.64"
tokio = { version = "1.36", features = ["rt-multi-thread", "rt"] }
log = "0.4"
fern = "0.6"
chrono = "0.4" # For timestamps

[dev-dependencies]
tempfile = "3.7.0"
Expand Down
52 changes: 51 additions & 1 deletion c2pa_c_ffi/src/c_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ use std::{
ptr,
};

use chrono::Local;
use fern::Dispatch;

/// Validates that a buffer size is within safe bounds and doesn't cause integer overflow
/// when used with pointer arithmetic.
///
Expand Down Expand Up @@ -65,7 +68,7 @@ unsafe fn safe_slice_from_raw_parts(

if !is_safe_buffer_size(len, ptr) {
return Err(Error::Other(format!(
"Buffer size {len} is invalid for parameter '{param_name}'",
"Buffer size {len} is invalid for parameter '{param_name}'"
)));
}

Expand Down Expand Up @@ -297,6 +300,43 @@ pub unsafe extern "C" fn c2pa_version() -> *mut c_char {
to_c_string(version)
}

/// Sets up file logging.
/// The logger will append any logged text to the end of the log_file provided.
///
/// # Safety
/// Reads from NULL-terminated C strings.
#[no_mangle]
pub unsafe extern "C" fn c2pa_init_file_logging(log_file: *const c_char) -> c_int {
let log_file = from_cstr_or_return_int!(log_file);

// Attempt to open the log file, return -1 if it fails (e.g., parent directory doesn't exist)
let log_file_handle = match fern::log_file(log_file) {
Ok(handle) => handle,
Err(_) => return -1,
};

let result = Dispatch::new()
.format(|out, message, record| {
out.finish(format_args!(
"[{}][{}] {}",
Local::now().format("%Y-%m-%d %H:%M:%S"),
record.level(),
message
))
})
.level(log::LevelFilter::Info)
.chain(std::io::stdout())
// Log to a file
.chain(log_file_handle)
.apply();

if result.is_ok() {
0
} else {
-1
}
}

/// Returns the last error message.
///
/// # Safety
Expand Down Expand Up @@ -1785,6 +1825,10 @@ mod tests {
#[test]
#[cfg(feature = "file_io")]
fn test_c2pa_sign_file_null_source_path() {
let log_path_str = "c2pa_test.log";
let log_path = CString::new(log_path_str).unwrap();
unsafe { c2pa_init_file_logging(log_path.as_ptr()) };

let dest_path = CString::new("/tmp/output.jpg").unwrap();
let manifest = CString::new("{}").unwrap();
let signer_info = C2paSignerInfo {
Expand All @@ -1806,6 +1850,12 @@ mod tests {
let error = unsafe { c2pa_error() };
let error_str = unsafe { CString::from_raw(error) };
assert_eq!(error_str.to_str().unwrap(), "NullParameter: source_path");

use std::fs;
let content = fs::read(log_path_str);
assert!(content.is_ok());
assert!(content.unwrap().is_empty());
let _ = fs::remove_file(log_path_str);
}

#[test]
Expand Down