Skip to content

[feature-request] Logpoints output structure object #4329

Open

Description

@danielchan303

Logging and debugging with Map is very troublesome in dart. It's impossible to debug the map as a long string in the log (especially API response). Hopefully, it is possible to log as expandable object. Please refer to #2137 .

Screenshot 2023-01-04 at 10 15 54 PM

Something like the chrome debugger would be great.

Activity

  1. added
    is enhancement
    in debugging
    on Jan 4, 2023
  2. DanTup commented on Jan 4, 2023

    @DanTup
    Member

    @danielchan303 to be clear, you can use inspect() from dart:developer in the same way as using console.log in the example above. I presume you're specifically trying to have this printed without needing to modify the code?

    Something worth noting is that these expressions are expanded lazily. So if you did:

    var a = {'a': 'b'};
    // logpoint here for expression "a"
    b['b'] = 'c';
    // logpoint here for expression "a"
    

    Both of these logpoints would include b when expanded, because they would refer to the same Map instance and it's only going to be evaluated at the time that you expand it. Is this what you're expecting?

  3. added
    awaiting info
    on Jan 4, 2023
  4. danielchan303 commented on Jan 4, 2023

    @danielchan303
    Author

    Yes, it would be great that the object can be printed without needing to modify the code.

    I have to say the case that you state is a bit unexpected. It would be better to print what is the state of that object at the point that it is inspected.

    By the way, although inspect can output expandable object, unlike the console.log it can only accept one param. As a result, I cannot print the tag together with it and hard to filter in the debug console.

  5. DanTup commented on Jan 4, 2023

    @DanTup
    Member

    I have to say the case that you state is a bit unexpected. It would be better to print what is the state of that object at the point that it is inspected.

    I agree this would be useful, but unfortunately I don't think it's practical. We'd have to traverse the whole of the object - effectively making a deep clone (and handling cycles) pre-emptively in case you expand it later. The VM would have to be kept paused while this happens, so it could also be quite slow if called a lot.

    This is how the debug console and inspect() already work today. VS Code only asks for the next level of data as it's expanded. Here you can see that I inspect(a) when it only contains a and then later I add b to it, and b shows up when expanding:

    Jan-04-2023 15-41-02

    If you want to capture a snapshot of the data, the best thing is to create a function that builds that snapshot/clone, and then use that (eg. if you created a toJson() method on an object that created a new Map that isn't modified with the original object and then evaluated a.toJson(), it may do what you want).

  6. danielchan303 commented on Jan 4, 2023

    @danielchan303
    Author

    Thank you for your advice. May be I can do something like this:

        var a = {'a': 'b'};
        inspect({...a});
        a['b'] = 'c';
        inspect({...a});
    

    I am not familiar with the underlying mechanism, thank you for pointing out that achieving this may result in slow performance.

    So would it be possible that developer can inspect data with logpoint in vscode so there won't be inspect everywhere and slow down the app in production? In this way, I can also add a tag.
    Screenshot 2023-01-05 at 12 09 21 AM

  7. DanTup commented on Jan 4, 2023

    @DanTup
    Member

    So would it be possible that developer can inspect data with logpoint in vscode so there won't be inspect everywhere and slow down the app in production? In this way, I can also add a tag.

    You can do what's shown in your screenshot, but it would just call to toString() method on the map. You could convert this to JSON in the expression which won't give you an expandable string, but will show a dump of the content at that point in time:

    Jan-04-2023 16-17-16

    This is a simple map, but you could make an equivalent of the dumpMap function that handles your own types and builds some string representation of them.

  8. danielchan303 commented on Jan 4, 2023

    @danielchan303
    Author

    Output that is expandable is just too important, it will be too much if it is cannot be collapsed.

    It is pretty sad that the network log in flutter devtools doesn't work at all, if it can print network log with expandable output then it will be great, but it is what it is. Pretty sad.

    Hopefully, it is possible to print expandable output with logpoints in vscode.

    In the mean time, I will use inspect and breakpoint to inspect the state. Thank you for your help.

  9. DanTup commented on Jan 4, 2023

    @DanTup
    Member

    It is pretty sad that the network log in flutter devtools doesn't work at all,

    I would encourage you to file an issue at https://github.com/flutter/devtools/ with details of the issue you're having so it can be looked at :-)

    Hopefully, it is possible to print expandable output with logpoints in vscode.

    The problem isn't so much making it expandable, but rather making a copy of that data and ensuring it's not garbage collected. Although you're using a simple Map, the debugger works with all types.. when you expand these objects you could be traversing a large graph of objects that might include things like Files or HttpRequests or database connections.

    If you only care about simple data structures that can he JSON serialised then the problem is a bit simpler, but even if you make a copy it's possible the garbage collector will have cleaned it up before it can be examined:

    Screenshot 2023-01-04 at 16 39 45

    I was trying to think of some possible ways to handle this for simple JSON types, but I just noticed in the DAP specification:

        /**
         * If an attribute `variablesReference` exists and its value is > 0, the
         * output contains objects which can be retrieved by passing
         * `variablesReference` to the `variables` request as long as execution
         * remains suspended. See 'Lifetime of Object References' in the Overview
         * section for details.
         */
        variablesReference?: number;
    

    "as long as execution remains suspended" means that it's not actually valid to have expanding variables work once the debugger is unpaused, so any dumping of variables in a collapsable way is not guaranteed to work if the thread is not paused (this means the existing inspect functionality also isn't really to-spec).

    In the mean time, I will use inspect and breakpoint to inspect the state.

    Unfortunately I think this (or using string representations) are the only really feasible solutions right now. I'll keep this issue open in the backlog to collect 👍 's and if there's significant demand for something here, maybe we can try and come up with something (or if required, file feature requests with VS Code / DAP for some improved way to handle this).

  10. removed
    awaiting info
    on Jan 4, 2023
  11. added this to the Backlog milestone on Jan 4, 2023
  12. dawidope commented on Jan 19, 2023

    @dawidope

    @danielchan303 I have exactly the same problem. Very often during analysing all app states, responses and so on I cannot find what I want in the log because everything is mixed. Of course after time consuming analysis I can find what I want but it isn't pleasure.
    I was thinking about using using groups from:
    https://microsoft.github.io/debug-adapter-protocol/specification
    I would like to be able to collapse some long logs using this feature. How we can achive it? My idea is to allow users to use some special tags. In logToUser method:
    https://github.com/Dart-Code/Dart-Code/blob/master/src/debug/dart_debug_impl.ts#L2262
    we could add additional parser which would be able to detect special tags. Something similar what is done for detecting async gap or stack frame. After that we could add groups properties to OutputEvent, which we want to group.
    I did some modifications in Dart-Code extension to handle opening/closing group tags and I prepared method in Dart to parse json. Demo below.

    Peek 2023-01-19 22-09
    As you can see it works pretty well. I'm sure that I can write the lib which is able to parse json with nice colors or to look like the output from inspect() method.
    What do you think about that idea? @DanTup

  13. DanTup commented on Jan 23, 2023

    @DanTup
    Member

    @dawidope I think there are a few different things being mixed together here. Rendering as a group is not the complicated part of what's requested above - but rather, producing the full object of data from any arbitrary instance at a point in time.

    In your example, your data being printed is a simple Map, where all the data is available up front and has no cycles. This is a much simpler problem than doing this for arbitrary instances, since they might reference large object graphics and have cycles. The only feasible way to enumerate them to support this is lazily (which can be done by sending a variablesReference with the `OutputEvent). However, this won't capture the data at the point in time where the log point is hit, but rather the data at each level as you expand each level.

    As an example, consider this code:

    var a = {'a': 'a'};
    a['b'] = a;
    printJson(a);
    

    It's not clear to me what this would output (I suspect a naive implementation that tries to handle the whole Map in one go will get stuck in a loop). And if the Map contains objects that have references to many other objects (and the tree goes hundreds of levels deep), it's also not clear to me what would happen - would the VM remain paused while the entire tree is built?

    I think if you want to examine potentially-large data structures, it should be done by pausing and using the other debugging tools. If you just want to dump some specific types of data (such as simple maps), then I think this is already possible with your own helper/methods (as noted in #4329 (comment)).

  14. dawidope commented on Jan 23, 2023

    @dawidope

    @DanTup ok understand. My impression was that the author of the issue wants to achive expanding/collapsing not only objects but also normal logs.
    My goal was to implement the feature which allows to create logs which can be collapsed. For example: during analysing Bloc states it would be nice to have just the simple log about current and next state and when you want to check some details about states you can just expand the interesting line. Thanks to that you are able to analyse data at the point in time where the event occures. Something similar like here:
    image
    Another example: I have the log which gives the information about request and response. I don't want to see all details everytime, but I can always click arrow and get it.
    The gif which I gave, shows just the example usage of that feature. How you use it it depends what you want.
    Maybe I chose bad issue and I should create a new one which is connected with expanding/collapsing logs.

  15. DanTup commented on Jan 23, 2023

    @DanTup
    Member

    @dawidope if you want a "point in time" view of your data, you can only do this by capturing a snapshot of the data at that time. That's what was discussed above (for example #4329 (comment)). I don't think it's feasible for the debugger to do this for you, because arbitrary objects could have references to huge graphs that could take a lot of time to traverse.

    I think if you have data you know is "simple" you could produce a Map from it (capturing the state at that time) and then inspect() that?

  16. dawidope commented on Jan 23, 2023

    @dawidope

    @DanTup as I wrote the main goal was to achive collapsing in the debug console. Without that printing (or using logpoints) even not so big Map and reading logs is a nightmare but it is an another issue I think.

    Just one note about inspect(). There is a problem with timeline. For example:

    var a = 'prefsOfStateA';
    var b = 'prefsOfStateB';
    var c = 'prefsOfStateC';
    var d = 'prefsOfStateD';
    print('stateA');
    inspect(a);
    print('stateB');
    inspect(b);
    print('stateC');
    inspect(c);
    print('stateD');
    inspect(d);
    

    When I run it I will get:
    image
    Even if you make some hack to avoid cleaning variables by GC there is another problem - order. In the case when you have lot of logs from different states and events you don't know which <inspected variable> belongs to which print.

  17. DanTup commented on Jan 23, 2023

    @DanTup
    Member

    @dawidope

    @DanTup as I wrote the main goal was to achive collapsing in the debug console. Without that printing (or using logpoints) even not so big Map and reading logs is a nightmare but it is an another issue I think.

    I understand. My point was that we don't necessarily need to use manual groupings just to get expand/collapse, it comes for free with "variables" (which is what inspect() is using).

    there is another problem - order. In the case when you have lot of logs from different states and events you don't know which <inspected variable> belongs to which print.

    Yeah, this is an issue with inspect because it's async. That's why I suggested <inspected variable> is not very useful. It would be nice if we could provide a better string. For example if you had been able to do:

    inspect('stateA', stateA);

    And the <inspected variable> text had instead said <inspected: stateA> or something, then I think that would improve things.

    But it's not completely clear to me what the goal is here, whether it's to have expand/collapse on simple JSON strings, to have them for Maps, or to have them for arbitrary objects. Each of these would need to be solved in different ways and would have different characteristics (in particular around whether they could provide snapshots of data at a point in time, or would show the "live" data as you expanded them).

    I would suggest opening a new issue that is much more specific than this one about your request, with as much detail about the use cases as possible so we can see what the possibilities are.

    Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in debuggingis enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions