SHARE
TWEET

Untitled

a guest Oct 13th, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::collections::HashMap;
  2. use std::fs;
  3. use std::fs::{File, OpenOptions};
  4. use std::io;
  5. use std::io::Result;
  6. use std::path::PathBuf;
  7.  
  8. pub struct Logger {
  9.     log_dir: PathBuf,
  10.     fds: HashMap<String, File>,
  11. }
  12.  
  13. impl Logger {
  14.     fn get_file(&mut self, target: &str) -> Result<&mut File> {
  15.         if let Some(fd) = self.fds.get_mut(target) {
  16.             return Ok(fd);
  17.         }
  18.  
  19.         let mut file_path = self.log_dir.clone();
  20.         file_path.push(&format!("{}.txt", target));
  21.         let fd = OpenOptions::new().append(true).open(file_path)?;
  22.         self.fds.insert(target.to_owned(), fd);
  23.         self.get_file(target)
  24.     }
  25. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top