audit_log.rs: handle Results in LogContext handling

The Rust liblog_event_list API used to silently ignore any errors
reported by liblog. aosp/2617613 attempts to make the operations
propagate the failure instead.

Note that this introduces a subtle behavior change: when *creating the
log record* fails, the API with Results does not allow submitting a
partially constructed log. Otherwise, the result of the write operation
is ignored as it was before.

Bug: 282691103
Test: m
Test: atest keystore2_test
Change-Id: I7c43100149b4ca831050af0a9229b95d2f7f8392
diff --git a/keystore2/src/audit_log.rs b/keystore2/src/audit_log.rs
index 07509d3..0e5dfeb 100644
--- a/keystore2/src/audit_log.rs
+++ b/keystore2/src/audit_log.rs
@@ -20,7 +20,7 @@
     Domain::Domain, KeyDescriptor::KeyDescriptor,
 };
 use libc::uid_t;
-use log_event_list::{LogContext, LogIdSecurity};
+use log_event_list::{LogContext, LogContextError, LogIdSecurity};
 
 const TAG_KEY_GENERATED: u32 = 210024;
 const TAG_KEY_IMPORTED: u32 = 210025;
@@ -60,27 +60,28 @@
 pub fn log_key_integrity_violation(key: &KeyDescriptor) {
     with_log_context(TAG_KEY_INTEGRITY_VIOLATION, |ctx| {
         let owner = key_owner(key.domain, key.nspace, key.nspace as i32);
-        ctx.append_str(key.alias.as_ref().map_or("none", String::as_str)).append_i32(owner)
+        ctx.append_str(key.alias.as_ref().map_or("none", String::as_str))?.append_i32(owner)
     })
 }
 
 fn log_key_event(tag: u32, key: &KeyDescriptor, calling_app: uid_t, success: bool) {
     with_log_context(tag, |ctx| {
         let owner = key_owner(key.domain, key.nspace, calling_app as i32);
-        ctx.append_i32(i32::from(success))
-            .append_str(key.alias.as_ref().map_or("none", String::as_str))
+        ctx.append_i32(i32::from(success))?
+            .append_str(key.alias.as_ref().map_or("none", String::as_str))?
             .append_i32(owner)
     })
 }
 
 fn with_log_context<F>(tag: u32, f: F)
 where
-    F: Fn(LogContext) -> LogContext,
+    F: Fn(LogContext) -> Result<LogContext, LogContextError>,
 {
     if let Some(ctx) = LogContext::new(LogIdSecurity, tag) {
-        let event = f(ctx);
-        LOGS_HANDLER.queue_lo(move |_| {
-            event.write();
-        });
+        if let Ok(event) = f(ctx) {
+            LOGS_HANDLER.queue_lo(move |_| {
+                let _result = event.write();
+            });
+        }
     }
 }