Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce new public Reporter trait that is used from CLI types to report file status #309

Merged
merged 7 commits into from
Sep 13, 2023

Conversation

hendrikvanantwerpen
Copy link
Collaborator

Fixes #307 by introducing a public Reporter trait that users can implement for custom logging.

Approach

File status is reported through a Reporter trait. To keep the burden on implementors limited, we require a predicatable way of calling reporters:

  • files can be skipped, indicated by a single call to skipped, or
  • files are processed, indicated by a pair of calls to started and on of (succeeded, failed, or canceled).

Implementation

  • A ConsoleReporter type is provided that prints to the console and allows control over what is logged.

  • The implementations if Indexer and Querier take some short-cuts, and violate this protocol. That is solved by the CLIFileLogger utility class, that retains that part of the logic from the old Logger implementation.

  • The Querier has also been updated to use the reporter for all its output. Previously, it was still printing directly to the console in some cases.

The `Querier` is a reusable type that should not assume it is used in a
console output where printing things is okay. This change ensures all
output is reported to the reporter only.
@hendrikvanantwerpen hendrikvanantwerpen requested a review from a team as a code owner August 11, 2023 15:06
@hendrikvanantwerpen hendrikvanantwerpen self-assigned this Aug 11, 2023
@github-actions
Copy link

Performance Summary

Comparing base 134b7bc with head 0fcb846 on typescript_benchmark benchmark. For details see workflow artifacts. Note that performance is tested on the last commits with changes in stack-graphs, not on every commit.

Before
--------------------------------------------------------------------------------
Command:            base/target/release/tree-sitter-stack-graphs-typescript index -D base.sqlite --max-file-time=30 --hide-error-details -- base/data/typescript_benchmark
Massif arguments:   --massif-out-file=perf.out
ms_print arguments: --x=72 --y=12 base-perf-results/perf.out
--------------------------------------------------------------------------------


    MB
715.8^                                                                      # 
     |                                                                      # 
     |                                                  @@::              : # 
     |                             @@                   @ :               :@# 
     |                             @                    @ :               :@# 
     |                            :@                   :@ :             :::@# 
     |                    :     :::@                   :@ :             : :@# 
     |                    :   ::: :@         :        @:@ :             : :@# 
     |                    :  @: : :@       :::        @:@ :            @: :@#:
     |                  @@:::@: : :@  :  ::: :        @:@ :            @: :@#:
     |                  @ :: @: : :@  :::@ : : @    ::@:@ :      ::::  @: :@#:
     |                  @ :: @: : :@  :: @ : : @::: : @:@ : :::::: :   @: :@#:
   0 +----------------------------------------------------------------------->Gi
     0                                                                   50.79
After
--------------------------------------------------------------------------------
Command:            head/target/release/tree-sitter-stack-graphs-typescript index -D head.sqlite --max-file-time=30 --hide-error-details -- head/data/typescript_benchmark
Massif arguments:   --massif-out-file=perf.out
ms_print arguments: --x=72 --y=12 head-perf-results/perf.out
--------------------------------------------------------------------------------


    MB
715.8^                                                                     #  
     |                                                   @                 #  
     |                                                 @@@               @@#  
     |                             @                   @@@               @ #: 
     |                             @                   @@@               @ #: 
     |                           ::@                  :@@@              :@ #: 
     |                    :    ::: @                  :@@@              :@ #: 
     |                    : :::: : @        ::        :@@@::            :@ #: 
     |                   @: : :: : @      :::        ::@@@:           :::@ #: 
     |                   @::: :: : @  :  :: :        ::@@@:           : :@ #::
     |                  @@::: :: : @  ::@:: : ::     ::@@@:        :: : :@ #::
     |                  @@::: :: : @  ::@:: : ::::@::::@@@: :::: :::  : :@ #::
   0 +----------------------------------------------------------------------->Gi
     0                                                                   51.82

Copy link
Member

@dcreager dcreager left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Left a couple of optional suggestions

Comment on lines 36 to 41
use crate::CancellationFlag;

use super::util::reporter::ConsoleReporter;
use super::util::reporter::Level;
use super::util::CLIFileReporter;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would suggest not having both crate references and super references — can you change these to crate::util::[etc]? (Unless there's a reason for the super references that I missed)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is a good suggestion. Perhaps VSCode does this automatically? At least I don't remember doing this inpurpose.

@@ -373,7 +398,8 @@ impl TestArgs {
filter: &dyn Filter,
success: bool,
cancellation_flag: &dyn CancellationFlag,
) -> anyhow::Result<()> {
) -> anyhow::Result<Vec<String>> {
let mut outputs = Vec::with_capacity(3);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like outputs is only ever used by joining the string elements into a larger string delimited by newlines. Is it worth building up a single already-joined String here? String implements std::fmt::Write, so all of your outputs.push(format!(...)) calls would turn into write!(&mut output, ...).

That said, this suggestion might be premature optimization, since this code path is only called when running our test suite.

///
/// For each file, either
/// - [`skipped`] is called once, or
/// - [`started`] and one of [`succeeded`], [`failed`], or [`canceled`] are called.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"and one" ⇒ "and then one" to describe the required ordering of calls

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that's better!

@dcreager
Copy link
Member

Does this need a changelog entry?

@github-actions
Copy link

Performance Summary

Comparing base 134b7bc with head 0fcb846 on typescript_benchmark benchmark. For details see workflow artifacts. Note that performance is tested on the last commits with changes in stack-graphs, not on every commit.

Before
--------------------------------------------------------------------------------
Command:            base/target/release/tree-sitter-stack-graphs-typescript index -D base.sqlite --max-file-time=30 --hide-error-details -- base/data/typescript_benchmark
Massif arguments:   --massif-out-file=perf.out
ms_print arguments: --x=72 --y=12 base-perf-results/perf.out
--------------------------------------------------------------------------------


    MB
715.8^                                                                   #    
     |                                                @                  #    
     |                                                @                 :#    
     |                                               :@                 :#    
     |                                               :@               :::#    
     |                         :@                  :::@              :: :#:   
     |                   :    ::@                  : :@:::           :: :#::: 
     |                   :  ::::@         :       :: :@:: @          :: :#::: 
     |                   ::@: ::@:      :::       :: :@:: @          :: :#::: 
     |                 @@::@: ::@:  :  :: :       :: :@:: @          :: :#::: 
     |                 @ ::@: ::@:  ::::: :      ::: :@:: @    :: ::::: :#::: 
     |                 @ ::@: ::@:  :: :: :::::@@::: :@:: @::@@: :: ::: :#::::
   0 +----------------------------------------------------------------------->Gi
     0                                                                   55.11
After
--------------------------------------------------------------------------------
Command:            head/target/release/tree-sitter-stack-graphs-typescript index -D head.sqlite --max-file-time=30 --hide-error-details -- head/data/typescript_benchmark
Massif arguments:   --massif-out-file=perf.out
ms_print arguments: --x=72 --y=12 head-perf-results/perf.out
--------------------------------------------------------------------------------


    MB
715.8^                                                                   #    
     |                                                @@                 #    
     |                                              @@@                  #    
     |                                              @@@                 :#:   
     |                                              @@@                ::#:   
     |                         :@@                  @@@                ::#:   
     |                  ::    ::@                 ::@@@              @@::#: : 
     |                  :  :::::@         :       : @@@ :            @ ::#::::
     |                  :  :: ::@       :::       : @@@ :            @ ::#::::
     |                  : ::: ::@   :  :: :    :  : @@@ :::         :@ ::#::::
     |                 @: ::: ::@   ::::: :    :  : @@@ ::     ::   :@ ::#::::
     |                 @: ::: ::@   :: :: ::::::  : @@@ :: ::@@: :: :@ ::#::::
   0 +----------------------------------------------------------------------->Gi
     0                                                                   54.93

@hendrikvanantwerpen hendrikvanantwerpen merged commit a20a97b into main Sep 13, 2023
9 checks passed
@hendrikvanantwerpen hendrikvanantwerpen deleted the expose-logger branch September 13, 2023 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Simplify cli::util::Logger and make public
2 participants