Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © HeWhoMustNotBeNamed
- // ░▒
- // ▒▒▒ ▒▒
- // ▒▒▒▒▒ ▒▒
- // ▒▒▒▒▒▒▒░ ▒ ▒▒
- // ▒▒▒▒▒▒ ▒ ▒▒
- // ▓▒▒▒ ▒ ▒▒▒▒▒▒▒▒▒▒▒
- // ▒▒▒▒▒▒▒▒▒▒▒ ▒ ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
- // ▒ ▒ ░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░
- // ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░▒▒▒▒▒▒▒▒
- // ▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ ▒▒
- // ▒▒▒▒▒ ▒▒▒▒▒▒▒
- // ▒▒▒▒▒▒▒▒▒
- // ▒▒▒▒▒ ▒▒▒▒▒
- // ░▒▒▒▒ ▒▒▒▒▓ ████████╗██████╗ ███████╗███╗ ██╗██████╗ ██████╗ ███████╗ ██████╗ ██████╗ ██████╗ ███████╗
- // ▓▒▒▒▒ ▒▒▒▒ ╚══██╔══╝██╔══██╗██╔════╝████╗ ██║██╔══██╗██╔═══██╗██╔════╝██╔════╝██╔═══██╗██╔══██╗██╔════╝
- // ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ ██║ ██████╔╝█████╗ ██╔██╗ ██║██║ ██║██║ ██║███████╗██║ ██║ ██║██████╔╝█████╗
- // ▒▒▒▒▒ ▒▒▒▒▒ ██║ ██╔══██╗██╔══╝ ██║╚██╗██║██║ ██║██║ ██║╚════██║██║ ██║ ██║██╔═══╝ ██╔══╝
- // ▒▒▒▒▒ ▒▒▒▒▒ ██║ ██║ ██║███████╗██║ ╚████║██████╔╝╚██████╔╝███████║╚██████╗╚██████╔╝██║ ███████╗
- // ▒▒ ▒
- //@version=5
- // @description Logger Library based on types and methods.
- // https://www.tradingview.com/script/XQClwWbb-Logger/?offer_id=10&aff_id=15271
- // https://www.youtube.com/watch?v=0hDTYCd-u3w&t=290s
- library("Libreria Library Logger", overlay=true)
- // @type Log Object holding log entry
- // @field level Logging level. Valid values - `TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`, `FATAL`, `CRITICAL`
- // @field message Logging message
- // @field bartime bar time at which log is recorded
- // @field bar bar index at which log is recorded
- export type Log
- string level
- string message
- int bartime = time
- int bar = bar_index
- method lpush(array<Log> this, Log item, int maxItems=10)=>
- this.push(item)
- while(this.size() > maxItems)
- this.shift()
- method lunshift(array<Log> this, Log item, int maxItems=10)=>
- this.unshift(item)
- while(this.size() > maxItems)
- this.pop()
- // @type Logger object which can be used for logging purposes
- // @field position position on chart where logs can be shown. Valid values are table position values. Make sure that the script does not have any other table at this position
- // @field pageSize size of each page of logs which can be shown on UI. Default is 10
- // @field maxEntries max size logs to be stored
- // @field pageNumber current page number of logs to display on chart
- // @field textSize size of text on debug table to be shown. default is size.small. Other options - size.tiny, size.normal, size.large, size.huge, size.auto
- // @field textColor text color of debug messages. Default is color.white
- // @field showOnlyLast If set, shows the logs derived only from last bar. Default is true
- // @field minimumLevel Minimum level of logs to be considered for logging. Valid values in ascending order - `TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`, `FATAL`, `CRITICAL`
- // @field realTime Print logs based on real time bar. This should be set to true for debugging indicators and false for debugging strategies.
- // @field debugTable table containing debug messages. It will be set in init method. Hence no need to pass this in constructor
- // @field logs Array of Log containing logging messages. It will be set in init method. Hence no need to pass this in constructor
- export type Logger
- string position = position.bottom_right
- int pageSize = 10
- int maxEntries = 1000
- int pageNumber = 0
- string textSize = size.small
- color textColor = color.white
- bool showOnlyLast = true
- string minimumLevel = 'INFO'
- bool realTime = true
- table debugTable
- array<Log> logs
- getLevelWeight(string level)=>
- level == 'TRACE'? 1 :
- level == 'DEBUG'? 2 :
- level == 'INFO'? 3 :
- level == 'WARN'? 4 :
- level == 'ERROR'? 5 :
- level == 'FATAL' or level =='CRITICAL'? 6 : 0
- method print(Logger this, int index, int row)=>
- if(barstate.islast and this.realTime) or ((barstate.islastconfirmedhistory or barstate.islast) and not this.realTime)
- Log log = this.logs.get(index)
- bgcolor = log.level == 'TRACE'? color.aqua :
- log.level == 'DEBUG' ? color.purple :
- log.level == 'INFO' ? color.lime :
- log.level == 'WARN' ? color.yellow :
- log.level == 'ERROR' ? color.orange :
- log.level == 'FATAL' or log.level == 'CRITICAL' ? color.red :
- color.silver
- barTimeString = str.tostring(year(log.bartime), '0000') + '/' + str.tostring(month(log.bartime), '00') + '/' +
- str.tostring(dayofmonth(log.bartime), '00') +
- (timeframe.isintraday ? '-' + str.tostring(hour(log.bartime), '00') + ':' + str.tostring(minute(log.bartime), '00') + ':' + str.tostring(second(log.bartime), '00') : '')
- this.debugTable.cell(column=0, row=row, text=str.tostring(index), bgcolor=color.new(bgcolor, 50), text_color=this.textColor, text_size=this.textSize, text_halign = text.align_left)
- this.debugTable.cell(column=1, row=row, text=str.tostring(log.bar), bgcolor=color.new(bgcolor, 60), text_color=this.textColor, text_size=this.textSize, text_halign = text.align_left)
- this.debugTable.cell(column=2, row=row, text=barTimeString, bgcolor=color.new(bgcolor, 70), text_color=this.textColor, text_size=this.textSize, text_halign = text.align_left)
- this.debugTable.cell(column=3, row=row, text=log.level, bgcolor=color.new(bgcolor, 80), text_color=this.textColor, text_size=this.textSize, text_halign = text.align_left)
- this.debugTable.cell(column=4, row=row, text=log.message, bgcolor=color.new(bgcolor, 90), text_color=this.textColor, text_size=this.textSize, text_halign = text.align_left)
- method populateLogs(Logger this)=>
- mRow = 0
- startIndex = math.max(math.min(this.pageSize * this.pageNumber, this.logs.size()-1-this.pageSize), 0)
- endIndex = math.min((this.pageSize * (this.pageNumber+1))-1, this.logs.size()-1)
- this.debugTable.clear(0, 0, 4, this.pageSize)
- for i = startIndex to this.logs.size() == 0 ? na : endIndex
- this.print(i, mRow)
- mRow+=1
- // @function init will initialize logger table and log stream array
- // @param this Logger object
- // @returns void
- export method init(Logger this)=>
- if(na(this.debugTable) or na(this.logs))
- this.debugTable := table.new(position=this.position, columns=5, rows=this.pageSize+1, border_width=0)
- this.logs := array.new<Log>()
- if(this.showOnlyLast)
- this.logs.clear()
- this.populateLogs()
- // @function setPage will set current page number of logs to display
- // @param this Logger object
- // @param pageNumber - Page number of logs to display
- // @returns void
- export method setPage(Logger this, int pageNumber)=>
- if(this.logs.size() < pageNumber)
- this.pageNumber := pageNumber
- this.populateLogs()
- // @function nextPage will incremement page number to display on screen
- // @param this Logger object
- // @returns void
- export method nextPage(Logger this)=>
- if(this.logs.size() < this.pageNumber+1)
- this.pageNumber := this.pageNumber + 1
- this.populateLogs()
- // @function previousPage will decrement page number to display on screen
- // @param this Logger object
- // @returns void
- export method previousPage(Logger this)=>
- if(this.pageNumber > 0)
- this.pageNumber := this.pageNumber - 1
- this.populateLogs()
- // @function log will record message to be logged and repopulate logs displayed
- // @param this Logger object
- // @param level logging level. Can be `TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`, `FATAL`, `CRITICAL`. Logs only if log level is higher than Loggers minimul log level set
- // @param message log message to be recorded
- // @returns void
- export method log(Logger this, string level, string message)=>
- currentLevel = getLevelWeight(level)
- minLevel = getLevelWeight(this.minimumLevel)
- if(currentLevel >=minLevel)
- Log log = Log.new(level, message)
- this.logs.lunshift(log, this.maxEntries)
- this.populateLogs()
- // @function trace will record message to be logged with level 'TRACE'
- // @param this Logger object
- // @param message log message to be recorded
- // @returns void
- export method trace(Logger this, string message)=>this.log('TRACE', message)
- // @function debug will record message to be logged with level 'DEBUG'
- // @param this Logger object
- // @param message log message to be recorded
- // @returns void
- export method debug(Logger this, string message)=>this.log('DEBUG', message)
- // @function info will record message to be logged with level 'INFO'
- // @param this Logger object
- // @param message log message to be recorded
- // @returns void
- export method info(Logger this, string message)=>this.log('INFO', message)
- // @function warn will record message to be logged with level 'WARN'
- // @param this Logger object
- // @param message log message to be recorded
- // @returns void
- export method warn(Logger this, string message)=>this.log('WARN', message)
- // @function error will record message to be logged with level 'ERROR'
- // @param this Logger object
- // @param message log message to be recorded
- // @returns void
- export method error(Logger this, string message)=>this.log('ERROR', message)
- // @function fatal will record message to be logged with level 'FATAL'
- // @param this Logger object
- // @param message log message to be recorded
- // @returns void
- export method fatal(Logger this, string message)=>this.log('FATAL', message)
- //**************** Example code ************************//
- var logger = Logger.new()
- logger.init()
- logger.trace('TEST Trace Level')
- logger.debug('TEST DEBUG Level')
- logger.info('TEST INFO Level')
- logger.warn('TEST WARN Level')
- logger.error('TEST ERROR Level')
- logger.fatal('TEST FATAL Level')
- //**************** Example code ************************//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement