]> Sergey Matveev's repositories - public-inbox.git/commitdiff
lei: use standard warn() in more places
authorEric Wong <e@80x24.org>
Wed, 13 Oct 2021 07:00:35 +0000 (07:00 +0000)
committerEric Wong <e@80x24.org>
Wed, 13 Oct 2021 19:51:18 +0000 (19:51 +0000)
warn() is easier to augment with context information, and
frankly unavoidable in the presence of 3rd-party libraries
we don't control.

15 files changed:
lib/PublicInbox/Fetch.pm
lib/PublicInbox/LEI.pm
lib/PublicInbox/LeiBlob.pm
lib/PublicInbox/LeiConvert.pm
lib/PublicInbox/LeiForgetExternal.pm
lib/PublicInbox/LeiImport.pm
lib/PublicInbox/LeiImportKw.pm
lib/PublicInbox/LeiInput.pm
lib/PublicInbox/LeiMailSync.pm
lib/PublicInbox/LeiMirror.pm
lib/PublicInbox/LeiP2q.pm
lib/PublicInbox/LeiRediff.pm
lib/PublicInbox/LeiRemote.pm
lib/PublicInbox/LeiViewText.pm
lib/PublicInbox/LeiXSearch.pm

index 5ada1f49e4dc28ba39e2c0278208869575e4456a..e5756fb664e44cd774a6daa20214cd6c0291a943 100644 (file)
@@ -52,7 +52,7 @@ sub do_manifest ($$$) {
                $m0 = eval {
                        PublicInbox::LeiMirror::decode_manifest($fh, $mf, $mf)
                };
-               $lei->err($@) if $@;
+               warn($@) if $@;
        }
        my ($bn) = ($fn =~ m!/([^/]+)\z!);
        my $curl_cmd = $lei->{curl}->for_uri($lei, $muri, qw(-R -o), $bn);
index 183cb545fe55276053c0d9110eef0823d1d08d11..bd8a6bef632b9c1252c6b70ea258bd5a028c3196 100644 (file)
@@ -471,6 +471,7 @@ sub _drop_wq {
 # pronounced "exit": x_it(1 << 8) => exit(1); x_it(13) => SIGPIPE
 sub x_it ($$) {
        my ($self, $code) = @_;
+       local $current_lei = $self;
        # make sure client sees stdout before exit
        $self->{1}->autoflush(1) if $self->{1};
        stop_pager($self);
@@ -504,6 +505,7 @@ sub qfin { # show message on finalization (LeiFinmsg)
 
 sub fail_handler ($;$$) {
        my ($lei, $code, $io) = @_;
+       local $current_lei = $lei;
        close($io) if $io; # needed to avoid warnings on SIGPIPE
        _drop_wq($lei);
        x_it($lei, $code // (1 << 8));
@@ -521,6 +523,7 @@ sub sigint_reap {
 
 sub fail ($$;$) {
        my ($self, $buf, $exit_code) = @_;
+       local $current_lei = $self;
        $self->{failed}++;
        warn($buf, "\n") if defined $buf;
        $self->{pkt_op_p}->pkt_do('fail_handler') if $self->{pkt_op_p};
@@ -541,6 +544,7 @@ sub puts ($;@) { out(shift, map { "$_\n" } @_) }
 
 sub child_error { # passes non-fatal curl exit codes to user
        my ($self, $child_error, $msg) = @_; # child_error is $?
+       local $current_lei = $self;
        $child_error ||= 1 << 8;
        warn($msg, "\n") if defined $msg;
        if ($self->{pkt_op_p}) { # to top lei-daemon
@@ -1019,7 +1023,7 @@ sub poke_mua { # forces terminal MUAs to wake up and hopefully notice new mail
                        $cmd = [ Text::ParseWords::shellwords($cmd) ];
                        send($sock, exec_buf($cmd, {}), MSG_EOR) if $sock;
                } else {
-                       err($self, "W: unsupported --alert=$op"); # non-fatal
+                       warn("W: unsupported --alert=$op\n"); # non-fatal
                }
        }
 }
@@ -1068,7 +1072,7 @@ sub start_pager {
 # display a message for user before spawning full-screen $VISUAL
 sub pgr_err {
        my ($self, @msg) = @_;
-       return $self->err(@msg) unless $self->{sock} && -t $self->{2};
+       return warn(@msg) unless $self->{sock} && -t $self->{2};
        start_pager($self, { LESS => 'RX' }); # no 'F' so we prompt
        print { $self->{2} } @msg;
        $self->{2}->autoflush(1);
@@ -1118,6 +1122,7 @@ sub accept_dispatch { # Listener {post_accept} callback
 
 sub dclose {
        my ($self) = @_;
+       local $current_lei = $self;
        delete $self->{-progress};
        _drop_wq($self) if $self->{failed};
        $self->close if $self->{-event_init_done}; # PublicInbox::DS::close
@@ -1369,6 +1374,7 @@ sub DESTROY {
 sub wq_done_wait { # dwaitpid callback
        my ($arg, $pid) = @_;
        my ($wq, $lei) = @$arg;
+       local $current_lei = $lei;
        my $err_type = $lei->{-err_type};
        $? and $lei->child_error($?,
                        $err_type ? "$err_type errors during $lei->{cmd}" : ());
@@ -1383,6 +1389,7 @@ sub fchdir {
 
 sub wq_eof { # EOF callback for main daemon
        my ($lei) = @_;
+       local $current_lei = $lei;
        my $wq1 = delete $lei->{wq1} // return $lei->fail; # already failed
        $wq1->wq_wait_old($wq1->can('_wq_done_wait') // \&wq_done_wait, $lei);
 }
@@ -1423,7 +1430,7 @@ sub refresh_watches {
                $seen{$url} = undef;
                my $state = $cfg->get_1("watch.$url.state");
                if (!watch_state_ok($state)) {
-                       $lei->err("watch.$url.state=$state not supported");
+                       warn("watch.$url.state=$state not supported\n");
                        next;
                }
                if ($url =~ /\Amaildir:(.+)/i) {
@@ -1492,6 +1499,7 @@ sub lms {
 
 sub sto_done_request {
        my ($lei, $sock) = @_;
+       local $current_lei = $lei;
        eval {
                if ($sock //= $lei->{sock}) { # issue, async wait
                        $lei->{sto}->wq_io_do('done', [ $sock ]);
@@ -1499,14 +1507,14 @@ sub sto_done_request {
                        my $wait = $lei->{sto}->wq_do('done');
                }
        };
-       $lei->err($@) if $@;
+       warn($@) if $@;
 }
 
 sub cfg_dump ($$) {
        my ($lei, $f) = @_;
        my $ret = eval { PublicInbox::Config->git_config_dump($f, $lei->{2}) };
        return $ret if !$@;
-       $lei->err($@);
+       warn($@);
        undef;
 }
 
index b94f67a029ff3bbd967dea8487c67d59f93b2730..a3ddbbcec211c1916033a36608bede6e442db4ef 100644 (file)
@@ -40,7 +40,7 @@ sub solver_user_cb { # called by solver when done
        # don't try to support all the git-show(1) options for non-blob,
        # this is just a convenience:
        $type ne 'blob' and
-               $lei->err("# $oid is a $type of $size bytes in:\n#\t$gd");
+               warn "# $oid is a $type of $size bytes in:\n#\t$gd\n";
 
        my $cmd = [ 'git', "--git-dir=$gd", 'show', $oid ];
        my $rdr = { 1 => $lei->{1}, 2 => $lei->{2} };
index 6550c242b1a2514ced857e2d956bfa0a8fdc0ad5..9e98edc391a0e70c4227e549c64ed92a154c7630 100644 (file)
@@ -41,7 +41,7 @@ sub process_inputs { # via wq_do
        delete $lei->{1};
        delete $self->{wcb}; # commit
        my $nr = delete($lei->{-nr_write}) // 0;
-       $lei->err("# converted $nr messages") if $lei->{opt}->{verbose};
+       $lei->qerr("# converted $nr messages");
 }
 
 sub lei_convert { # the main "lei convert" method
index 7a4bbcf80a3af3ab47266c16de8298b0cfb5a4ae..07f0ac803c88778b1c9865059c6cee85fc1b328d 100644 (file)
@@ -20,10 +20,9 @@ sub lei_forget_external {
                        if ($? == 0) {
                                $lei->qerr("# $l forgotten ");
                        } elsif (($? >> 8) == 5) {
-                               $lei->err("# $l not found");
+                               warn("# $l not found\n");
                        } else {
-                               $lei->err("# --unset $key error");
-                               return $lei->x_it($?);
+                               $lei->child_error($?, "# --unset $key error");
                        }
                }
        }
index 69d63ab6b39756f96a89deb49422fb9eaadcb444..2f8fd6c64f69f4348a6bcb77335ba8bcdf32d85d 100644 (file)
@@ -38,7 +38,7 @@ sub pmdir_cb { # called via wq_io_do from LeiPmdir->each_mdir_fn
        my $lse = $self->{lse} //= $self->{lei}->{sto}->search;
        my $lms = $self->{-lms_ro} //= $self->{lei}->lms; # may be 0 or undef
        my @oidbin = $lms ? $lms->name_oidbin($folder, $bn) : ();
-       @oidbin > 1 and $self->{lei}->err("W: $folder/*/$$bn not unique:\n",
+       @oidbin > 1 and warn("W: $folder/*/$$bn not unique:\n",
                                map { "\t".unpack('H*', $_)."\n" } @oidbin);
        my %seen;
        my @docids = sort { $a <=> $b } grep { !$seen{$_}++ }
@@ -100,9 +100,8 @@ sub do_import_index ($$@) {
                my $nproc = $self->detect_nproc;
                $j = $nproc if $j > $nproc;
        }
-       if ($lei->{opt}->{'new-only'} && (!$net || !$net->{imap_order})) {
-               $lei->err('# --new-only is only for IMAP');
-       }
+       ($lei->{opt}->{'new-only'} && (!$net || !$net->{imap_order})) and
+               warn "# --new-only is only for IMAP\n";
        my $ops = {};
        $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
        $lei->{-eml_noisy} = 1;
index c35c5c266c4b93ffc36d665b4f3954a5d8feb82f..8359f3386668b19101a8060cd932d20422d7cc35 100644 (file)
@@ -36,7 +36,7 @@ sub ck_update_kw { # via wq_io_do
        my ($self, $url, $uid, $kw) = @_;
        my @oidbin = $self->{-lms_ro}->num_oidbin($url, $uid);
        my $uid_url = "$url/;UID=$uid";
-       @oidbin > 1 and $self->{lei}->err("W: $uid_url not unique:\n",
+       @oidbin > 1 and warn("W: $uid_url not unique:\n",
                                map { "\t".unpack('H*', $_)."\n" } @oidbin);
        my %seen;
        my @docids = sort { $a <=> $b } grep { !$seen{$_}++ }
index 83479221000bbd970ce5260d7f71e9c84fe5160e..6a90e7e1e756fa02bfe68c0496edcfe336f3257d 100644 (file)
@@ -343,8 +343,8 @@ $input is `eml', not --in-format=$in_fmt
 --mail-sync specified but no inputs support it
 
                # non-fatal if some inputs support support sync
-               $lei->err("# --mail-sync will only be used for @{$sync->{ok}}");
-               $lei->err("# --mail-sync is not supported for: @{$sync->{no}}");
+               warn("# --mail-sync will only be used for @{$sync->{ok}}\n");
+               warn("# --mail-sync is not supported for: @{$sync->{no}}\n");
        }
        if ($net) {
                $net->{-can_die} = 1;
index f7e37ad9ca808ce3b618d651366c3e2a0e28aeb3..f2f1e3ed265838e384ad303b8e084c9575719e03 100644 (file)
@@ -466,7 +466,7 @@ sub arg2folder {
 # using `$res' instead of `$orig'
 EOM
                        } else {
-                               $lei->err($res) if defined $res;
+                               warn($res, "\n") if defined $res;
                                push @no, $orig;
                        }
                } elsif (m!\A(?:nntps?|s?news)://!i) {
@@ -478,7 +478,7 @@ EOM
 # using `$res' instead of `$orig'
 EOM
                        } else {
-                               $lei->err($res) if defined $res;
+                               warn($res, "\n") if defined $res;
                                push @no, $orig;
                        }
                } else {
index 4be8f70ae4015b63c6cd8622715dc67f69a045ea..ec41bec6f16b51547a9ab56bdea4279b6635e3a9 100644 (file)
@@ -20,7 +20,7 @@ sub _wq_done_wait { # dwaitpid callback (via wq_eof)
        if ($?) {
                $lei->child_error($?);
        } elsif (!unlink($f)) {
-               $lei->err("unlink($f): $!") unless $!{ENOENT};
+               warn("unlink($f): $!\n") unless $!{ENOENT};
        } else {
                if ($lei->{cmd} ne 'public-inbox-clone') {
                        $lei->lazy_cb('add-external', '_finish_'
@@ -120,7 +120,7 @@ sub _try_config {
                -d $dst or die "mkpath($dst): $!\n";
        }
        my $err = _get_txt($self, qw(_/text/config/raw inbox.config.example));
-       return $self->{lei}->err($err) if $err;
+       return warn($err, "\n") if $err;
        my $f = "$self->{dst}/inbox.config.example";
        chmod((stat($f))[2] & 0444, $f) or die "chmod(a-w, $f): $!";
        my $cfg = PublicInbox::Config->git_config_dump($f, $self->{lei}->{2});
@@ -151,7 +151,7 @@ sub index_cloned_inbox {
        my ($self, $iv) = @_;
        my $lei = $self->{lei};
        my $err = _get_txt($self, qw(description description));
-       $lei->err($err) if $err; # non fatal
+       warn($err, "\n") if $err; # non fatal
        eval { set_description($self) };
        warn $@ if $@;
 
@@ -380,7 +380,7 @@ sub try_manifest {
        my ($path_pfx, $v1_path, @v2_epochs) = deduce_epochs($m, $path);
        if (@v2_epochs) {
                # It may be possible to have v1 + v2 in parallel someday:
-               $lei->err(<<EOM) if defined $v1_path;
+               warn(<<EOM) if defined $v1_path;
 # `$v1_path' appears to be a v1 inbox while v2 epochs exist:
 # @v2_epochs
 # ignoring $v1_path (use --inbox-version=1 to force v1 instead)
index c0c4563d6fbb86a1638bf1917bf83f41d9a5525b..5c2ce0a13dfc85eda2bf28017f5fa0ac31712754 100644 (file)
@@ -135,7 +135,7 @@ sub do_p2q { # via wq_do
        if ($lei->{opt}->{debug}) {
                my $json = ref(PublicInbox::Config->json)->new;
                $json->utf8->canonical->pretty;
-               $lei->err($json->encode($lei->{qterms}));
+               print { $lei->{2} } $json->encode($lei->{qterms});
        }
        my (@q, %seen);
        for my $pfx (@want) {
index decb721b7a4c47e9eaf3acbba51fcfbaa913be78..740cbcee334fa0cc26bd9a13f56cd186f4de4e1f 100644 (file)
@@ -30,7 +30,7 @@ sub rediff_user_cb { # called by solver when done
 
        # don't try to support all the git-show(1) options for non-blob,
        # this is just a convenience:
-       $type ne 'blob' and return $lei->err(<<EOF);
+       $type ne 'blob' and return warn(<<EOF);
 # $oid is a $type of $size bytes in:
 # $git->{git_dir} (wanted: $oid_want)
 EOF
index 346aa6a40132292c69681139ed0e0f89b11955b8..7782aa9dbfa132bef82301130a3450fd5d6a8b30 100644 (file)
@@ -75,7 +75,7 @@ sub smsg_eml {
        if (my $bref = $self->{lei}->ale->git->cat_file($smsg->{blob})) {
                return PublicInbox::Eml->new($bref);
        }
-       $self->{lei}->err("E: $self->{uri} $smsg->{blob} gone <$smsg->{mid}>");
+       warn("E: $self->{uri} $smsg->{blob} gone <$smsg->{mid}>\n");
        undef;
 }
 
index c469d1ea5b1ce77a084b1e19a952cb90797054d4..2dad3b780fa13af5da27ae23c8d7ef5cb372ec78 100644 (file)
@@ -77,7 +77,7 @@ sub new {
        my $cfg = PublicInbox::Config::config_fh_parse($r, "\0", "\n");
        waitpid($pid, 0);
        if ($?) {
-               $lei->err("# git-config failed, no color (non-fatal)");
+               warn "# git-config failed, no color (non-fatal)\n";
                return $self;
        }
        $self->{-colored} = \&my_colored;
index ae9f5881676d0a07e40d301c9c81b5356f13d772..fee1b859f9c64d2d22292b4b0a1fb4729ba0186b 100644 (file)
@@ -16,6 +16,7 @@ use PublicInbox::Spawn qw(popen_rd spawn which);
 use PublicInbox::MID qw(mids);
 use PublicInbox::Smsg;
 use PublicInbox::Eml;
+use PublicInbox::LEI;
 use Fcntl qw(SEEK_SET F_SETFL O_APPEND O_RDWR);
 use PublicInbox::ContentHash qw(git_sha);
 use POSIX qw(strftime);
@@ -392,11 +393,11 @@ sub query_remote_mboxrd {
                $err = '';
                if (-s $cerr) {
                        seek($cerr, 0, SEEK_SET) or
-                                       $lei->err("seek($cmd stderr): $!");
+                                       warn "seek($cmd stderr): $!";
                        $err = do { local $/; <$cerr> } //
-                                       "read($cmd stderr): $!";
+                                       warn "read($cmd stderr): $!";
                        truncate($cerr, 0) or
-                                       $lei->err("truncate($cmd stderr): $!");
+                                       warn "truncate($cmd stderr): $!";
                }
                next if (($? >> 8) == 22 && $err =~ /\b404\b/);
                $uri->query_form(q => $qstr);
@@ -416,6 +417,7 @@ sub xsearch_done_wait { # dwaitpid callback
 
 sub query_done { # EOF callback for main daemon
        my ($lei) = @_;
+       local $PublicInbox::LEI::current_lei = $lei;
        my $l2m = delete $lei->{l2m};
        $l2m->wq_wait_old(\&xsearch_done_wait, $lei) if $l2m;
        if (my $lxs = delete $lei->{lxs}) {
@@ -462,6 +464,7 @@ Error closing $lei->{ovv}->{dst}: $!
 
 sub do_post_augment {
        my ($lei) = @_;
+       local $PublicInbox::LEI::current_lei = $lei;
        my $l2m = $lei->{l2m} or return; # client disconnected
        $lei->fchdir or return;
        my $err;
@@ -497,9 +500,10 @@ sub concurrency {
        $nl + $nr;
 }
 
-sub start_query ($;$) { # always runs in main (lei-daemon) process
-       my ($self, $l2m) = @_;
-       if ($self->{opt_threads} || ($l2m && !$self->{opt_sort})) {
+sub start_query ($$) { # always runs in main (lei-daemon) process
+       my ($self, $lei) = @_;
+       local $PublicInbox::LEI::current_lei = $lei;
+       if ($self->{opt_threads} || ($lei->{l2m} && !$self->{opt_sort})) {
                for my $ibxish (locals($self)) {
                        $self->wq_io_do('query_one_mset', [], $ibxish);
                }
@@ -521,9 +525,10 @@ sub start_query ($;$) { # always runs in main (lei-daemon) process
 }
 
 sub incr_start_query { # called whenever an l2m shard starts do_post_auth
-       my ($self, $l2m) = @_;
+       my ($self, $lei) = @_;
+       my $l2m = $lei->{l2m};
        return if ++$self->{nr_start_query} != $l2m->{-wq_nr_workers};
-       start_query($self, $l2m);
+       start_query($self, $lei);
 }
 
 sub ipc_atfork_child {
@@ -545,7 +550,7 @@ sub do_query {
                'l2m_progress' => [ \&l2m_progress, $lei ],
                'x_it' => [ $lei ],
                'child_error' => [ $lei ],
-               'incr_start_query' => [ $self, $l2m ],
+               'incr_start_query' => [ $self, $lei ],
        };
        $lei->{auth}->op_merge($ops, $l2m) if $l2m && $lei->{auth};
        my $end = $lei->pkt_op_pair;
@@ -586,7 +591,7 @@ sub do_query {
        if ($l2m) {
                $l2m->net_merge_all_done($lei) unless $lei->{auth};
        } else {
-               start_query($self);
+               start_query($self, $lei);
        }
        $lei->event_step_init; # wait for shutdowns
        $lei->wait_wq_events($op_c, $ops);