Search code examples
javascriptqtwebkitqtwebkitconsole.log

Is there a JavaScript console limit in WebKit?


I want to limit the memory or the number of entries in the JavaScript / Web Inspector console.

Is it already limited somehow (except by available and accessible memory)?

If not, is it possible to just clear out the oldest entries in the console log while retaining newer ones, i.e. something like console.clear(10000)?

Is there anything like a limit or a selective clear() in any JavaScript engine?

The issue is that I want to log debug information in a single-page app in an embedded web view but do not want to constantly leak memory this way.


Solution

  • While some JavaScript developer tools might implement an internal limit, WebKit's Web Inspector apparently does not, at least it does not expose any documented interface to it.

    So basically there are two feasible solutions (besides patching WebKit or using a tool that supports a limit), as Cerbrus and Jan Dvorak have also mentioned.

    • Create a proxy to the various console methods to buffer any log output and dump on request only, and limit the internal buffer. A simple ring buffer is not very fast, so an alternative would be to use two buffers where the older one is discarded and overwritten when the current becomes full. The drawback of using a buffer is that you cannot immediately expand objects but only on first access, which may lose vital debug data if they get overwritten (Qt 5's console expands simple objects and arrays during logging).

    • Periodically clear the console to limit memory usage. However, the console may be cleared at the wrong time.