chiark / gitweb /
More sophisticated handling of program name
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 2 Jun 2021 17:50:23 +0000 (18:50 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 2 Jun 2021 17:50:53 +0000 (18:50 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/bin/otter.rs
src/config.rs
src/utils.rs

index 59d3e1679ebfb96306132faabd90a5daa86097f9..10d6333b6cd365b14916b6a91c9c9b99ae2459ab 100644 (file)
@@ -8,7 +8,6 @@ pub type MgmtChannel = ClientMgmtChannel;
 
 // xxx ssh keys: need a force option to set key for non ssh: account
 // xxx make default account be ssh:<user>: rather than unix:<user>: if we are passed --ssh
-// xxx make remote otter have a different error message prefix
 
 use otter::imports::*;
 
@@ -1951,6 +1950,8 @@ mod mgmtchannel_proxy {
 
   #[throws(AE)]
   fn call(SCCA{ out, ma, args,.. }:SCCA) {
+    set_program_name("otter (remote)".into());
+
     let args = parse_args::<Args,_>(args, &subargs, &ok_id, None);
     let mut chan = connect_chan(&ma)?;
 
index 4b7d1e78eff10860878c340c7ca399d79f658856..b8d9d57864a547e8c236fa0036a90f27c6efe69e 100644 (file)
@@ -99,6 +99,8 @@ pub enum PathResolveContext {
 }
 impl Default for PathResolveContext { fn default() -> Self { Self::Noop } }
 
+static PROGRAM_NAME: RwLock<String> = parking_lot::const_rwlock(String::new());
+
 impl PathResolveMethod {
   #[throws(io::Error)]
   fn chdir(self, cd: &str) -> PathResolveContext {
@@ -342,3 +344,26 @@ impl Default for WholeServerConfig {
     spec.resolve(default()).expect("empty spec into config")
   }
 }
+
+pub fn set_program_name(s: String) {
+  *PROGRAM_NAME.write() = s;
+}
+
+pub fn program_name() -> String {
+  {
+    let set = PROGRAM_NAME.read();
+    if set.len() > 0 { return set.clone() }
+  }
+
+  let mut w = PROGRAM_NAME.write();
+  if w.len() > 0 { return w.clone() }
+
+  let new = env::args().next().expect("expected at least 0 arguments");
+  let new = match new.rsplit_once('/') {
+    Some((_path,leaf)) => leaf.to_owned(),
+    None => new,
+  };
+  let new = if new.len() > 0 { new } else { "otter".to_owned() };
+  *w = new.clone();
+  new
+}
index de0fced07409d8b9dcc6320e5f1fd66d1aa363fa..ce0b78328e266123f4ead58fde95e3eb650cb87e 100644 (file)
@@ -673,18 +673,18 @@ impl anyhow::Error {
   fn d(&self) -> AnyhowDisplay<'_> { AnyhowDisplay(self) }
 
   fn end_process(self, estatus: u8) -> ! {
-    #[derive(Default,Debug)] struct Sol { any: bool }
+    #[derive(Default,Debug)] struct Sol { any: bool, progname: String }
     impl Sol {
       fn nl(&mut self) {
         if self.any { eprintln!("") };
         self.any = false;
       }
       fn head(&mut self) {
-        if ! self.any { eprint!("otter: error"); }
+        if ! self.any { eprint!("{}: error", &self.progname); }
         self.any = true
       }
     }
-    let mut sol: Sol = default();
+    let mut sol: Sol = Sol { any: false, progname: program_name() };
     self.for_each(&mut |s|{
       let long = s.len() > 80;
       if long && sol.any { sol.nl() }