terminal user interface to jujutsu. Focused on speed and clarity
9
fork

Configure Feed

Select the types of activity you want to include in your feed.

never throw during log init

+50 -48
+50 -48
jj_tui/lib/logging.ml
··· 63 63 ;; 64 64 65 65 (** Removes old log files, keeping only the 20 most recent *) 66 - let cleanup_logs () = 67 - let state_home = Unix.getenv "XDG_STATE_HOME" in 68 - let jj_tui_dir = Filename.concat state_home "jj_tui" in 66 + let cleanup_logs log_path = 69 67 let log_files = 70 - Sys.readdir jj_tui_dir 68 + Sys.readdir log_path 71 69 |> Array.to_list 72 70 |> List.filter (fun file -> Filename.check_suffix file ".log") 73 71 |> List.sort (fun a b -> String.compare b a) ··· 78 76 (fun i file -> 79 77 if i >= 20 80 78 then ( 81 - let file_path = Filename.concat jj_tui_dir file in 79 + let file_path = Filename.concat log_path file in 82 80 Unix.unlink file_path)) 83 81 log_files 84 82 ;; ··· 133 131 | a -> 134 132 a 135 133 with 136 - |_-> 134 + | _ -> 137 135 None 138 136 ;; 139 137 140 138 let init_logging () = 141 - (*creates or opens the log file*) 142 - let get_log_file_channel () = 143 - get_log_dir () 144 - |> Option.map @@ fun state_home -> 145 - let jj_tui_dir = Filename.concat state_home "jj_tui" in 146 - (try Unix.mkdir jj_tui_dir 0o755 with Unix.Unix_error (Unix.EEXIST, _, _) -> ()); 147 - let timestamp = Unix.time () |> Unix.localtime in 148 - let timestamp_str = 149 - Printf.sprintf 150 - "%04d%02d%02d_%02d%02d%02d" 151 - (timestamp.tm_year + 1900) 152 - (timestamp.tm_mon + 1) 153 - timestamp.tm_mday 154 - timestamp.tm_hour 155 - timestamp.tm_min 156 - timestamp.tm_sec 157 - in 158 - let log_file = 159 - Filename.concat jj_tui_dir (Printf.sprintf "log_%s.log" timestamp_str) 160 - in 161 - let log_channel = open_out_gen [ Open_append; Open_creat ] 0o644 log_file in 162 - log_channel 163 - in 164 - (* log our logs into the log file*) 165 - let log_chan = get_log_file_channel () in 166 - match log_chan with 167 - | Some log_chan -> 168 - (*Make a mutex for logging to prevent concurrency and threading issues *) 169 - let logging_mutex = Picos_std_sync.Mutex.create () in 170 - Logs.set_reporter_mutex 171 - ~lock:(fun () -> Picos_std_sync.Mutex.lock logging_mutex) 172 - ~unlock:(fun () -> Picos_std_sync.Mutex.unlock logging_mutex); 173 - let log_formatter = Format.formatter_of_out_channel log_chan in 174 - let reporter = reporter log_formatter in 175 - Logs.set_level (Some Debug); 176 - Logs.set_reporter reporter; 177 - (*make sure everything is working*) 178 - [%log info "Logging initialized"]; 179 - cleanup_logs (); 180 - [%log debug "Old logs cleaned up"] 181 - | None -> 139 + try 182 140 (*just no logging if we can't find a log file*) 183 141 (*TODO: log to stderr *) 184 - () 142 + match get_log_dir () with 143 + | Some log_dir -> 144 + (*creates or opens the log file*) 145 + let get_log_file_channel () = 146 + let jj_tui_dir = Filename.concat log_dir "jj_tui" in 147 + (try Unix.mkdir jj_tui_dir 0o755 with 148 + | Unix.Unix_error (Unix.EEXIST, _, _) -> 149 + ()); 150 + let timestamp = Unix.time () |> Unix.localtime in 151 + let timestamp_str = 152 + Printf.sprintf 153 + "%04d%02d%02d_%02d%02d%02d" 154 + (timestamp.tm_year + 1900) 155 + (timestamp.tm_mon + 1) 156 + timestamp.tm_mday 157 + timestamp.tm_hour 158 + timestamp.tm_min 159 + timestamp.tm_sec 160 + in 161 + let log_file = 162 + Filename.concat jj_tui_dir (Printf.sprintf "log_%s.log" timestamp_str) 163 + in 164 + let log_channel = open_out_gen [ Open_append; Open_creat ] 0o644 log_file in 165 + log_channel 166 + in 167 + (* log our logs into the log file*) 168 + let log_chan = get_log_file_channel () in 169 + (*Make a mutex for logging to prevent concurrency and threading issues *) 170 + let logging_mutex = Picos_std_sync.Mutex.create () in 171 + Logs.set_reporter_mutex 172 + ~lock:(fun () -> Picos_std_sync.Mutex.lock logging_mutex) 173 + ~unlock:(fun () -> Picos_std_sync.Mutex.unlock logging_mutex); 174 + let log_formatter = Format.formatter_of_out_channel log_chan in 175 + let reporter = reporter log_formatter in 176 + Logs.set_level (Some Debug); 177 + Logs.set_reporter reporter; 178 + (*make sure everything is working*) 179 + [%log info "Logging initialized"]; 180 + cleanup_logs log_dir; 181 + [%log debug "Old logs cleaned up"] 182 + | None -> 183 + Printf.eprintf "Logging couldn't be initialized" 184 + with 185 + | e -> 186 + Printf.eprintf "Logging couldn't be initialized. Exn: %s" (Printexc.to_string e) 185 187 ;; 186 188 end 187 189