Improved set/get log config.
This commit is contained in:
parent
091a64a86b
commit
cdd80c8570
1 changed files with 19 additions and 11 deletions
|
@ -29,12 +29,14 @@ class App.Log
|
||||||
|
|
||||||
class _Singleton
|
class _Singleton
|
||||||
constructor: ->
|
constructor: ->
|
||||||
@moduleColorsMap = {}
|
@moduleColorsMap = {}
|
||||||
@currentConfig = {}
|
@currentConfig = {}
|
||||||
|
@currentConfigReady = {}
|
||||||
if window.localStorage
|
if window.localStorage
|
||||||
raw = window.localStorage.getItem('log_config')
|
raw = window.localStorage.getItem('log_config')
|
||||||
if raw
|
if raw
|
||||||
@currentConfig = JSON.parse(raw)
|
@currentConfig = JSON.parse(raw)
|
||||||
|
@configReady()
|
||||||
|
|
||||||
# example config to enable debugging
|
# example config to enable debugging
|
||||||
#@config('module', 'i18n|websocket')
|
#@config('module', 'i18n|websocket')
|
||||||
|
@ -46,6 +48,13 @@ class _Singleton
|
||||||
if data && (data.browser is 'Chrome' || ( data.browser is 'Firefox' && data.version >= 31.0 ) )
|
if data && (data.browser is 'Chrome' || ( data.browser is 'Firefox' && data.version >= 31.0 ) )
|
||||||
@colorSupport = true
|
@colorSupport = true
|
||||||
|
|
||||||
|
configReady: ->
|
||||||
|
for type, value of @currentConfig
|
||||||
|
if type is 'module' || type is 'content'
|
||||||
|
@currentConfigReady[type] = new RegExp(value, 'i')
|
||||||
|
else
|
||||||
|
@currentConfigReady[type] = value
|
||||||
|
|
||||||
config: (type = undefined, value = undefined) ->
|
config: (type = undefined, value = undefined) ->
|
||||||
|
|
||||||
# get
|
# get
|
||||||
|
@ -54,23 +63,22 @@ class _Singleton
|
||||||
return @currentConfig[type]
|
return @currentConfig[type]
|
||||||
return @currentConfig
|
return @currentConfig
|
||||||
|
|
||||||
# set
|
# set runtime config
|
||||||
if type is 'module' || type is 'content'
|
@currentConfig[type] = value
|
||||||
@currentConfig[type] = new RegExp(value, 'i')
|
@configReady()
|
||||||
else
|
|
||||||
@currentConfig[type] = value
|
|
||||||
if window.localStorage
|
if window.localStorage
|
||||||
window.localStorage.setItem('log_config', JSON.stringify(@currentConfig))
|
window.localStorage.setItem('log_config', JSON.stringify(@currentConfig))
|
||||||
|
|
||||||
log: ( module, level, args ) ->
|
log: ( module, level, args ) ->
|
||||||
if level is 'debug'
|
if level is 'debug'
|
||||||
return if !@currentConfig.module && !@currentConfig.content
|
return if !@currentConfigReady.module && !@currentConfigReady.content
|
||||||
return if @currentConfig.module && !module.match(@currentConfig.module)
|
return if @currentConfigReady.module && !module.match(@currentConfigReady.module)
|
||||||
return if @currentConfig.content && !args.toString().match(@currentConfig.content)
|
return if @currentConfigReady.content && !args.toString().match(@currentConfigReady.content)
|
||||||
@_log( module, level, args )
|
@_log( module, level, args )
|
||||||
|
|
||||||
_log: ( module, level, args ) ->
|
_log: ( module, level, args ) ->
|
||||||
prefixLength = 28
|
prefixLength = 32
|
||||||
prefix = "App.#{module}(#{level})"
|
prefix = "App.#{module}(#{level})"
|
||||||
if prefix.length < prefixLength
|
if prefix.length < prefixLength
|
||||||
prefix += Array(prefixLength - prefix.length).join(' ')
|
prefix += Array(prefixLength - prefix.length).join(' ')
|
||||||
|
|
Loading…
Reference in a new issue