TradingView
AlphaViz
11 mai 2023 23:27

JavaScript-style Debug Console 

S&P 500SP

Description

This library provides a JavaScript-style debug console to Pine Coders. It supports the most commonly used utilities from the WHATWG Console Standard including the following:

 • console.log
 • console.debug
 • console.info
 • console.warn
 • console.error
 • console.assert
 • console.count
 • console.countReset
 • console.group
 • console.groupEnd
 • console.clear

In addition to the WHATWG standard, this library also supports the following methods:

 • console.show
 • console.hide

FEATURES

 • Follows the WHATWG Console Standard, which is widely adopted by all major JavaScript runtimes including browsers and Node.js.
 • Provides an out-of-box UI with pre-configured theming, ensuring a clean and professional-looking console.
 • Allows for easy UI customizations to fit your personal preferences.
 • Has extremely simple import and initialization, making it easy to integrate with your existing codebase.

USAGE

1. Import this library:

import algotraderdev/Console/1


2. Initialize the console object:

var console = Console.new() // You can also specify optional params to customize the look & feel. var console = Console.new( position = position.bottom_right, max_rows = 50, width = 0, text_size = size.normal, background_color = #000000CC, timestamp_color = #AAAAAA, info_message_color = #DDDDDD, debug_message_color = #AAAAAA, warn_message_color = #FFEB3B, error_message_color = #ff3c00)


3. Use the console object to debug your code. Here are some examples:

// Basic logging console.log('hello world!') // prints 'hello world' console.warn('warn') // prints 'warn' in yellow console.error('error') // prints 'error' in red console.clear() // clears the console // Assertion console.assert(a.isEmpty(), 'array should be empty') // prints 'assertion failed: array should be empty' if the array is not empty // Counter console.count('fooFunction') // prints 'fooFunction: 1' console.count('fooFunction') // prints 'fooFunction: 2' console.countReset('fooFunction') // resets the counter console.count('fooFunction') // prints 'fooFunction: 1' // Group console.log('A') console.group() console.log('B') console.group() console.log('C') console.log('D') console.groupEnd() console.log('E') console.groupEnd() console.log('F') // prints // A //   B //     C //     D //   E // F // Hide and show console.hide() console.show()

Notes de version

Improve performance by not using `var` for constants.

Notes de version

It seems that PineScript has released a new version that's not backward compatible and led to the following error in the script: "Invalid object name: log. Namespaces of built-ins cannot be used."
As a result, I had to modify the variable names in the script to bypass the issue.

Notes de version

Always set the timezone as `syminfo.timezone` when calling `str.format_time`. It seems that in a new version of PineScript compiler the timezone parameter is no longer optional.
Commentaires
adolgov
Some recommedations:

export type Console table table varip Log[] logs

varip makes logs non-rollbackable, i.e. all "intrabar" generated logs will persist
d1g1talshad0w
Something recently changed in the last Trading View update. Using this library or even other logger libraries cause the code to not compile. It has something to do with using 'log' namespace. This is concerning because Trading View refuses to put proper console logging into the editor and now they have broken every library out there that attempts to provide this functionality.
AlphaViz
Hi @d1g1talshad0w, you're correct that a latest update of TradingView / PineScript compiler broke this library. I've worked around the issue by not using the 'log' symbol. You can update the library to v3: import algotraderdev/Console/3
wibatrade
If i use your lib, i get an error:
Error on bar 0: Incorrect `timezone` value for the str.format_time() function: "yyyy-MM-dd". The value must be a string in either UTC/GMT notation (e.g. "UTC-5", "GMT+0530") or an IANA time zone database name (e.g. "America/New_York").

this is my usage:

//@version=5
indicator("TEST", overlay = true, max_lines_count = 500,max_boxes_count=500, max_labels_count = 500)
import algotraderdev/Console/3

var console = Console.new()
console.log('Hello!')
AlphaViz
@wibatrade, Hi, thanks for the report. The issue was caused by an undocumented change in PineScript's builtin str.format_time function. I've updated this library to accommodate that change. You can update the script to version 4 and it should start working again. Thanks!

import algotraderdev/Console/4
CamKartel
This (great!) library stopped working today: "Invalid object name: log. Namespaces of built-ins cannot be used."

Importing algotraderdev/Console/2 into another script causes the above compilation error.

If this is not a temporarily fluke (and Pine Script is breaking backwards compatibility with published scripts), then the following changes must be made to line 102 - 1017. I prefixed variables named 'log' with an underscore, thus '_log'.

// @Function Flushes all the stored logs to the underlying table.
method _flush(Console this) =>
for (i, _log) in this.logs // replace parens by square brackets
this._write(i, _log)

// @Function Writes a message with the specified level to the console.
// @param level The log level.
// @param msg The message to write to the console.
method _log(Console this, int level, string msg) =>
// If the number of existing logs in the console exceeds the 'max_rows' limit, the oldest
// log is removed to make room for the new log.
if this.logs.size() == this.max_rows
this.logs.shift()
this._flush()

Log _log = Log.new(timestamp(), level, this.indent + msg)
this._write(this.logs.size(), _log)
this.logs.push(_log)
AlphaViz
Hi @CamKartel, Thanks for flagging this issue! I've released a new version of this library following your fix!
IkinovInvest
U DID IT !!! Well done mate thank u a lot
Trendoscope
Pretty similar to mine but more detailed.

tradingview.com/script/XQClwWbb-Logger/

The constants, you can make it non var. var is expensive generally.
AlphaViz
@HeWhoMustNotBeNamed, Great to see a comment from you. Thanks for the tip! :)
Plus