Advertisement
Maurizio-Ciullo

Libreria Library Logger

Apr 12th, 2023
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © HeWhoMustNotBeNamed
  3. //                                       ░▒            
  4. //                                  ▒▒▒   ▒▒      
  5. //                              ▒▒▒▒▒     ▒▒      
  6. //                      ▒▒▒▒▒▒▒░     ▒     ▒▒          
  7. //                  ▒▒▒▒▒▒           ▒     ▒▒          
  8. //             ▓▒▒▒       ▒        ▒▒▒▒▒▒▒▒▒▒▒  
  9. //   ▒▒▒▒▒▒▒▒▒▒▒ ▒        ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒        
  10. //   ▒  ▒       ░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░        
  11. //   ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░▒▒▒▒▒▒▒▒        
  12. //   ▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ ▒▒                      
  13. //    ▒▒▒▒▒         ▒▒▒▒▒▒▒                            
  14. //                 ▒▒▒▒▒▒▒▒▒                          
  15. //                ▒▒▒▒▒ ▒▒▒▒▒                          
  16. //               ░▒▒▒▒   ▒▒▒▒▓      ████████╗██████╗ ███████╗███╗   ██╗██████╗  ██████╗ ███████╗ ██████╗ ██████╗ ██████╗ ███████╗
  17. //              ▓▒▒▒▒     ▒▒▒▒      ╚══██╔══╝██╔══██╗██╔════╝████╗  ██║██╔══██╗██╔═══██╗██╔════╝██╔════╝██╔═══██╗██╔══██╗██╔════╝
  18. //              ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒        ██║   ██████╔╝█████╗  ██╔██╗ ██║██║  ██║██║   ██║███████╗██║     ██║   ██║██████╔╝█████╗
  19. //             ▒▒▒▒▒       ▒▒▒▒▒       ██║   ██╔══██╗██╔══╝  ██║╚██╗██║██║  ██║██║   ██║╚════██║██║     ██║   ██║██╔═══╝ ██╔══╝  
  20. //            ▒▒▒▒▒         ▒▒▒▒▒      ██║   ██║  ██║███████╗██║ ╚████║██████╔╝╚██████╔╝███████║╚██████╗╚██████╔╝██║     ███████╗
  21. //             ▒▒             ▒                        
  22. //@version=5
  23. // @description Logger Library based on types and methods.
  24. // https://www.tradingview.com/script/XQClwWbb-Logger/?offer_id=10&aff_id=15271
  25. // https://www.youtube.com/watch?v=0hDTYCd-u3w&t=290s
  26. library("Libreria Library Logger", overlay=true)
  27. // @type Log Object holding log entry
  28. // @field level Logging level. Valid values - `TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`, `FATAL`, `CRITICAL`
  29. // @field message Logging message
  30. // @field bartime bar time at which log is recorded
  31. // @field bar bar index at which log is recorded
  32. export type Log
  33.     string level
  34.     string message
  35.     int bartime = time
  36.     int bar = bar_index
  37.    
  38. method lpush(array<Log> this, Log item, int maxItems=10)=>
  39.     this.push(item)
  40.     while(this.size() > maxItems)
  41.         this.shift()
  42.  
  43. method lunshift(array<Log> this, Log item, int maxItems=10)=>
  44.     this.unshift(item)
  45.     while(this.size() > maxItems)
  46.         this.pop()
  47.  
  48. // @type Logger object which can be used for logging purposes
  49. // @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
  50. // @field pageSize size of each page of logs which can be shown on UI. Default is 10
  51. // @field maxEntries max size logs to be stored
  52. // @field pageNumber current page number of logs to display on chart
  53. // @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
  54. // @field textColor text color of debug messages. Default is color.white
  55. // @field showOnlyLast If set, shows the logs derived only from last bar. Default is true
  56. // @field minimumLevel Minimum level of logs to be considered for logging. Valid values in ascending order - `TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`, `FATAL`, `CRITICAL`
  57. // @field realTime Print logs based on real time bar. This should be set to true for debugging indicators and false for debugging strategies.
  58. // @field debugTable table containing debug messages. It will be set in init method. Hence no need to pass this in constructor
  59. // @field logs Array of Log containing logging messages. It will be set in init method. Hence no need to pass this in constructor
  60. export type Logger
  61.     string position = position.bottom_right
  62.     int pageSize = 10
  63.     int maxEntries = 1000
  64.     int pageNumber = 0
  65.     string textSize = size.small
  66.     color textColor = color.white
  67.     bool showOnlyLast = true
  68.     string minimumLevel = 'INFO'
  69.     bool realTime = true
  70.     table debugTable
  71.     array<Log> logs
  72.  
  73. getLevelWeight(string level)=>
  74.     level == 'TRACE'? 1 :
  75.      level == 'DEBUG'? 2 :
  76.      level == 'INFO'? 3 :
  77.      level == 'WARN'? 4 :
  78.      level == 'ERROR'? 5 :
  79.      level == 'FATAL' or level =='CRITICAL'? 6 : 0
  80.  
  81. method print(Logger this, int index, int row)=>
  82.     if(barstate.islast and this.realTime) or ((barstate.islastconfirmedhistory or barstate.islast) and not this.realTime)
  83.         Log log = this.logs.get(index)
  84.         bgcolor = log.level == 'TRACE'? color.aqua :
  85.                          log.level == 'DEBUG' ? color.purple :
  86.                          log.level == 'INFO' ? color.lime :
  87.                          log.level == 'WARN' ? color.yellow :
  88.                          log.level == 'ERROR' ? color.orange :
  89.                          log.level == 'FATAL' or log.level == 'CRITICAL' ? color.red :
  90.                          color.silver
  91.         barTimeString = str.tostring(year(log.bartime), '0000') + '/' + str.tostring(month(log.bartime), '00') + '/' +
  92.                              str.tostring(dayofmonth(log.bartime), '00') +
  93.                              (timeframe.isintraday ? '-' + str.tostring(hour(log.bartime), '00') + ':' + str.tostring(minute(log.bartime), '00') + ':' + str.tostring(second(log.bartime), '00') : '')
  94.    
  95.         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)
  96.         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)
  97.         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)
  98.         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)
  99.         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)
  100.    
  101. method populateLogs(Logger this)=>
  102.     mRow = 0
  103.     startIndex = math.max(math.min(this.pageSize * this.pageNumber, this.logs.size()-1-this.pageSize), 0)
  104.     endIndex = math.min((this.pageSize * (this.pageNumber+1))-1, this.logs.size()-1)
  105.    
  106.     this.debugTable.clear(0, 0, 4, this.pageSize)
  107.     for i = startIndex to this.logs.size() == 0 ? na : endIndex
  108.         this.print(i, mRow)
  109.         mRow+=1
  110.  
  111. // @function init will initialize logger table and log stream array
  112. // @param this Logger object
  113. // @returns void
  114. export method init(Logger this)=>
  115.     if(na(this.debugTable) or na(this.logs))
  116.         this.debugTable := table.new(position=this.position, columns=5, rows=this.pageSize+1, border_width=0)
  117.         this.logs := array.new<Log>()
  118.     if(this.showOnlyLast)
  119.         this.logs.clear()
  120.         this.populateLogs()
  121.  
  122. // @function setPage will set current page number of logs to display
  123. // @param this Logger object
  124. // @param pageNumber - Page number of logs to display
  125. // @returns void
  126. export method setPage(Logger this, int pageNumber)=>
  127.     if(this.logs.size() < pageNumber)
  128.         this.pageNumber := pageNumber
  129.     this.populateLogs()
  130.  
  131. // @function nextPage will incremement page number to display on screen
  132. // @param this Logger object
  133. // @returns void
  134. export method nextPage(Logger this)=>
  135.     if(this.logs.size() < this.pageNumber+1)
  136.         this.pageNumber := this.pageNumber + 1
  137.     this.populateLogs()
  138.  
  139. // @function previousPage will decrement page number to display on screen
  140. // @param this Logger object
  141. // @returns void        
  142. export method previousPage(Logger this)=>
  143.     if(this.pageNumber > 0)
  144.         this.pageNumber := this.pageNumber - 1
  145.     this.populateLogs()
  146.  
  147. // @function log will record message to be logged and repopulate logs displayed
  148. // @param this Logger object
  149. // @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
  150. // @param message log message to be recorded
  151. // @returns void
  152. export method log(Logger this, string level, string message)=>
  153.     currentLevel = getLevelWeight(level)
  154.     minLevel = getLevelWeight(this.minimumLevel)
  155.     if(currentLevel >=minLevel)
  156.         Log log = Log.new(level, message)
  157.         this.logs.lunshift(log, this.maxEntries)
  158.         this.populateLogs()
  159.  
  160. // @function trace will record message to be logged with level 'TRACE'
  161. // @param this Logger object
  162. // @param message log message to be recorded
  163. // @returns void  
  164. export method trace(Logger this, string message)=>this.log('TRACE', message)
  165. // @function debug will record message to be logged with level 'DEBUG'
  166. // @param this Logger object
  167. // @param message log message to be recorded
  168. // @returns void  
  169. export method debug(Logger this, string message)=>this.log('DEBUG', message)
  170. // @function info will record message to be logged with level 'INFO'
  171. // @param this Logger object
  172. // @param message log message to be recorded
  173. // @returns void  
  174. export method info(Logger this, string message)=>this.log('INFO', message)
  175. // @function warn will record message to be logged with level 'WARN'
  176. // @param this Logger object
  177. // @param message log message to be recorded
  178. // @returns void  
  179. export method warn(Logger this, string message)=>this.log('WARN', message)
  180. // @function error will record message to be logged with level 'ERROR'
  181. // @param this Logger object
  182. // @param message log message to be recorded
  183. // @returns void  
  184. export method error(Logger this, string message)=>this.log('ERROR', message)
  185. // @function fatal will record message to be logged with level 'FATAL'
  186. // @param this Logger object
  187. // @param message log message to be recorded
  188. // @returns void  
  189. export method fatal(Logger this, string message)=>this.log('FATAL', message)
  190.  
  191. //**************** Example code ************************//
  192. var logger = Logger.new()
  193. logger.init()
  194. logger.trace('TEST Trace Level')
  195. logger.debug('TEST DEBUG Level')
  196. logger.info('TEST INFO Level')
  197. logger.warn('TEST WARN Level')
  198. logger.error('TEST ERROR Level')
  199. logger.fatal('TEST FATAL Level')
  200. //**************** Example code ************************//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement