Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement