]> ruderich.org/simon Gitweb - fcscs/fcscs.git/blobdiff - bin/fcscs
write debug output to ~/.config/fcscs/log
[fcscs/fcscs.git] / bin / fcscs
index 9c33fe95ca06d3c9f7bcc05e261d620af02923d4..7543a9df7807c10ed3a89dda3a89f714f8b9e0f9 100755 (executable)
--- a/bin/fcscs
+++ b/bin/fcscs
@@ -344,7 +344,18 @@ package Screen {
 sub debug {
     my ($config, $module, @args) = @_;
 
 sub debug {
     my ($config, $module, @args) = @_;
 
-    say STDERR "$module: @args" if $config->{setting}{debug};
+    return if not $config->{setting}{debug};
+
+    state $fh; # only open the file once per run
+    if (not defined $fh) {
+        # Ignore errors if the directory doesn't exist.
+        if (not open $fh, '>', "$ENV{HOME}/.config/fcscs/log") {
+            $fh = undef; # a failed open still writes a value to $fh
+            return;
+        }
+    }
+
+    say $fh "$module: @args";
     return;
 }
 
     return;
 }
 
@@ -461,9 +472,6 @@ sub run_in_background {
         my $pid = fork;
         defined $pid or die $!;
         if ($pid == 0) { # child
         my $pid = fork;
         defined $pid or die $!;
         if ($pid == 0) { # child
-            # Disable debug mode as writing will fail with closed STDERR.
-            $config->{setting}{debug} = 0;
-
             $sub->();
         }
         exit;
             $sub->();
         }
         exit;
@@ -904,7 +912,7 @@ Defaults in parentheses.
 
 =over
 
 
 =over
 
-=item B<debug>          enable debug mode (redirect stderr when enabling) (C<0>)
+=item B<debug>          enable debug mode, writes to I<~/.config/fcscs/log> (C<0>)
 
 =item B<initial_mode>   start in this mode, must be a valid mode mapping (C<\&mapping_mode_url>)
 
 
 =item B<initial_mode>   start in this mode, must be a valid mode mapping (C<\&mapping_mode_url>)
 
@@ -1034,6 +1042,7 @@ Used as mappings, see L</MAPPINGS> above.
 
 Used as handler to yank, paste selection or open URL in browser.
 
 
 Used as handler to yank, paste selection or open URL in browser.
 
+    debug()
     get_regex_matches()
     select_match()
     run_command()
     get_regex_matches()
     select_match()
     run_command()
@@ -1066,6 +1075,8 @@ package Fcscs {
     sub handler_paste { return main::handler_paste(@_); }
     sub handler_url { return main::handler_url(@_); }
 
     sub handler_paste { return main::handler_paste(@_); }
     sub handler_url { return main::handler_url(@_); }
 
+    sub debug { return main::debug(@_); }
+
     sub get_regex_matches { return main::get_regex_matches(@_); }
     sub select_match { return main::select_match(@_); }
 
     sub get_regex_matches { return main::get_regex_matches(@_); }
     sub select_match { return main::select_match(@_); }
 
@@ -1211,11 +1222,20 @@ RESULT:
             }
 
             debug \%config, 'input', 'running handler';
             }
 
             debug \%config, 'input', 'running handler';
-            my $handler = $config{state}{handler};                 # set by user
-            $handler = $result->{match}->{handler} unless defined $handler; # set by match
-            $handler = $result->{handler} unless defined $handler; # set by mapping
-            $handler = $config{handler}{yank} unless defined $handler; # fallback
-            $handler->($screen, \%config, $result->{match});
+
+            # Choose handler with falling priority.
+            my @handlers = (
+                $config{state}{handler},     # set by user
+                $result->{match}->{handler}, # set by match
+                $result->{handler},          # set by mapping
+                $config{handler}{yank},      # fallback
+            );
+            foreach my $handler (@handlers) {
+                next unless defined $handler;
+
+                $handler->($screen, \%config, $result->{match});
+                last;
+            }
             last;
         }
 
             last;
         }