Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 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 self.fds.contains_key(target) {
  16. Ok(self.fds.get_mut(target).unwrap())
  17. } else {
  18. let mut file_path = self.log_dir.clone();
  19. file_path.push(&format!("{}.txt", target));
  20. let fd = OpenOptions::new().append(true).open(file_path)?;
  21. self.fds.insert(target.to_owned(), fd);
  22. return self.get_file(target)
  23. }
  24. }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement