Rengaw

crash_counter

Jan 14th, 2021
436
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use app_dirs::{AppDataType, AppInfo};
  2. use std::env;
  3. use std::fs;
  4. use std::path::PathBuf;
  5. use std::str;
  6. use std::sync::mpsc;
  7. use std::sync::mpsc::RecvError;
  8. use std::time::Duration;
  9.  
  10. use notify::{RecursiveMode, Watcher};
  11.  
  12. // type Sender = mpsc::Sender<notify::DebouncedEvent>;
  13. type Receiver = mpsc::Receiver<notify::DebouncedEvent>;
  14. type Event = notify::DebouncedEvent;
  15.  
  16. const APP_INFO: AppInfo = AppInfo {
  17.     name: "crash_counter",
  18.     author: "Wagner",
  19. };
  20.  
  21. struct AppPaths {
  22.     crashes_dir: PathBuf,
  23.     count_file: PathBuf,
  24. }
  25.  
  26. fn main() {
  27.     let paths = AppPaths {
  28.         crashes_dir: get_crashes_dir(),
  29.         count_file: get_count_file(),
  30.     };
  31.  
  32.     // Create a channel to receive the events.
  33.     let (tx, rx) = mpsc::channel();
  34.  
  35.     // Create a watcher object, delivering debounced events.
  36.     // The notification back-end is selected based on the platform.
  37.     let mut watcher = notify::watcher(tx, Duration::from_secs(10)).unwrap();
  38.  
  39.     watcher
  40.         .watch(&paths.crashes_dir, RecursiveMode::Recursive)
  41.         .unwrap();
  42.  
  43.     receive(rx, &paths);
  44. }
  45.  
  46. fn get_crashes_dir() -> PathBuf {
  47.     let temp_dir = env::temp_dir();
  48.     let crashes_dir = temp_dir.join("Unity").join("Editor").join("Crashes");
  49.     if !crashes_dir.exists() {
  50.         println!("Creating directory: {:?}", crashes_dir);
  51.         fs::create_dir_all(&crashes_dir)
  52.             .expect(&format!("Failed to create directory: {:?}", &crashes_dir));
  53.     }
  54.     crashes_dir
  55. }
  56.  
  57. fn get_count_file() -> PathBuf {
  58.     let count_file = app_dirs::app_root(AppDataType::UserData, &APP_INFO)
  59.         .expect("Couldn't create app directory.")
  60.         .join("count.txt");
  61.  
  62.     if !count_file.exists() {
  63.         std::fs::write(&count_file, b"0")
  64.             .expect(&format!("Failed to write file: {:?}", count_file));
  65.     }
  66.  
  67.     count_file
  68. }
  69.  
  70. fn receive(rx: Receiver, paths: &AppPaths) {
  71.     println!("Listening for events...");
  72.     loop {
  73.         match rx.recv() {
  74.             Ok(event) => handle_event(event, paths),
  75.             Err(error) => handle_recv_error(error),
  76.         }
  77.     }
  78. }
  79.  
  80. fn handle_event(event: Event, paths: &AppPaths) {
  81.     let read_result = std::fs::read(&paths.count_file);
  82.     let count = match read_result {
  83.         Ok(contents) => str::from_utf8(&contents)
  84.             .expect(&format!("Failed to read file: {:?}", paths.count_file))
  85.             .parse::<u128>()
  86.             .expect(&format!("Failed to parse file: {:?}", paths.count_file)),
  87.         Err(_) => 0,
  88.     } + 1;
  89.  
  90.     std::fs::write(&paths.count_file, count.to_string())
  91.         .expect(&format!("Failed to write file: {:?}", paths.count_file));
  92.  
  93.     println!("{:?} | count = {}", event, count);
  94. }
  95.  
  96. fn handle_recv_error(error: RecvError) {
  97.     println!("Watch error: {:?}", error);
  98. }
  99.  
RAW Paste Data