@@ -25,8 +25,7 @@ use tracing_subscriber::registry::LookupSpan;
2525
2626use nix:: fcntl:: { flock, FlockArg } ;
2727use std:: fs:: OpenOptions ;
28- use std:: io:: ErrorKind ;
29- use std:: io:: { Error , Write } ;
28+ use std:: io:: { self , Error , ErrorKind , Write } ;
3029use std:: os:: fd:: AsRawFd ;
3130use std:: os:: unix:: fs:: MetadataExt ;
3231use std:: path:: Path ;
@@ -42,13 +41,6 @@ const EVENT_PREFIX: &str = concat!("azure-init-", env!("CARGO_PKG_VERSION"));
4241///
4342/// This visitor is primarily used in the `on_event` method of the `EmitKVPLayer`
4443/// to extract event messages and log them as key-value pairs.
45- ///
46- /// # Example
47- /// ```rust
48- /// let mut event_message = String::new();
49- /// let mut visitor = StringVisitor { string: &mut event_message };
50- /// event.record(&mut visitor);
51- /// ```
5244pub struct StringVisitor < ' a > {
5345 string : & ' a mut String ,
5446}
9587 ///
9688 /// # Example
9789 /// ```rust
90+ /// use tracing::{event, Level};
9891 /// event!(Level::INFO, msg = "Event message");
9992 /// ```
10093 fn on_event ( & self , event : & tracing:: Event < ' _ > , ctx : TracingContext < ' _ , S > ) {
@@ -182,7 +175,9 @@ pub fn handle_kvp_operation(
182175 let event_key = generate_event_key ( event_level, event_name, span_id) ;
183176
184177 let encoded_kvp = encode_kvp_item ( & event_key, event_value) ;
185- write_to_kvp_file ( file_path, & encoded_kvp) ;
178+ if let Err ( e) = write_to_kvp_file ( file_path, & encoded_kvp) {
179+ eprintln ! ( "Error writing to KVP file: {}" , e) ;
180+ }
186181}
187182
188183/// Generates a unique event key by combining the event level, name, and span ID.
@@ -207,25 +202,50 @@ fn generate_event_key(
207202/// This function appends the provided encoded KVP data to the specified file and ensures
208203/// that file locking is handled properly to avoid race conditions. It locks the file exclusively,
209204/// writes the data, flushes the output, and then unlocks the file.
210- fn write_to_kvp_file ( file_path : & Path , encoded_kvp : & Vec < Vec < u8 > > ) {
211- let mut file = OpenOptions :: new ( )
212- . append ( true )
213- . create ( true )
214- . open ( file_path)
215- . expect ( "Failed to open log file" ) ;
205+ fn write_to_kvp_file (
206+ file_path : & Path ,
207+ encoded_kvp : & Vec < Vec < u8 > > ,
208+ ) -> io:: Result < ( ) > {
209+ let mut file =
210+ match OpenOptions :: new ( ) . append ( true ) . create ( true ) . open ( file_path) {
211+ Ok ( file) => file,
212+ Err ( e) => {
213+ eprintln ! ( "Failed to open log file: {}" , e) ;
214+ return Err ( e) ; // Return the error if the file can't be opened
215+ }
216+ } ;
216217
217218 let fd = file. as_raw_fd ( ) ;
218- flock ( fd, FlockArg :: LockExclusive ) . expect ( "Failed to lock the file" ) ;
219+ if let Err ( e) = flock ( fd, FlockArg :: LockExclusive ) {
220+ eprintln ! ( "Failed to lock the file: {}" , e) ;
221+ return Err ( io:: Error :: new (
222+ io:: ErrorKind :: Other ,
223+ "File locking failed" ,
224+ ) ) ;
225+ }
219226
220- // Dereference kvp to get a slice &[u8]
227+ // Write the encoded KVP data
221228 for kvp in encoded_kvp {
222- file. write_all ( & kvp[ ..] )
223- . expect ( "Failed to write to log file" ) ;
229+ if let Err ( e) = file. write_all ( & kvp[ ..] ) {
230+ eprintln ! ( "Failed to write to log file: {}" , e) ;
231+ return Err ( e) ; // Return the error if writing fails
232+ }
224233 }
225234
226- file. flush ( ) . expect ( "Failed to flush the log file" ) ;
235+ if let Err ( e) = file. flush ( ) {
236+ eprintln ! ( "Failed to flush the log file: {}" , e) ;
237+ return Err ( e) ; // Return the error if flushing fails
238+ }
227239
228- flock ( fd, FlockArg :: Unlock ) . expect ( "Failed to unlock the file" ) ;
240+ if let Err ( e) = flock ( fd, FlockArg :: Unlock ) {
241+ eprintln ! ( "Failed to unlock the file: {}" , e) ;
242+ return Err ( io:: Error :: new (
243+ io:: ErrorKind :: Other ,
244+ "File unlocking failed" ,
245+ ) ) ;
246+ }
247+
248+ Ok ( ( ) )
229249}
230250
231251/// Encodes a key-value pair (KVP) into one or more byte slices. If the value
0 commit comments