Skip to content

Commit

Permalink
Added: Logs
Browse files Browse the repository at this point in the history
  • Loading branch information
mbnuqw committed Jun 16, 2020
1 parent 806dc0c commit 73422f0
Show file tree
Hide file tree
Showing 22 changed files with 224 additions and 3 deletions.
2 changes: 2 additions & 0 deletions addon/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import MsgActions from './actions/msg.js'
import ProxyActions from './actions/proxy.js'
import FaviconsActions from './actions/favicons.js'
import StorageActions from './actions/storage.js'
import LogsActions from './actions/logs.js'
import MiscActions from './actions/misc.js'

const Actions = {
Expand All @@ -17,6 +18,7 @@ const Actions = {
...ProxyActions,
...FaviconsActions,
...StorageActions,
...LogsActions,
...MiscActions,
}

Expand Down
67 changes: 67 additions & 0 deletions addon/actions/logs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Log info
*/
function infoLog(msg) {
if (!this._logs) this._logs = []
this._logs.push(`${Date.now()} [INFO::background] ${msg}`)
}

/**
* Log warn
*/
function warnLog(msg) {
if (!this._logs) this._logs = []
this._logs.push(`${Date.now()} [WARN::background] ${msg}`)
}

/**
* Log err
*/
function errLog(msg) {
if (!this._logs) this._logs = []
this._logs.push(`${Date.now()} [ERR::background] ${msg}`)
}

/**
* Writes logs
*/
function log(logs) {
if (!this._logs) this._logs = []
this._logs.push(...logs)

if (this._logs.length > 123) {
browser.storage.local.set({ logs: this._logs })
this._logs = []
}
}

/**
* Returns all logs
*/
async function getLogs() {
let storage = { logs: [] }
try {
storage = await browser.storage.local.get(storage)
} catch (err) {
// ...
}

let logs = storage.logs

if (this._logs) logs = logs.concat(this._logs)

return logs
}

function initLogs() {
window.getLogs = this.actions.getLogs
}

export default {
infoLog,
warnLog,
errLog,
log,
getLogs,
initLogs,
}
4 changes: 4 additions & 0 deletions addon/actions/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ function initToolbarButton() {
if (info && info.button === 1) return browser.runtime.openOptionsPage()
else browser.sidebarAction.open()
})

this.actions.infoLog('Toolbar button initialized')
}

/**
Expand All @@ -34,6 +36,8 @@ async function loadPermissions() {
if (c.userAgentActive) c.userAgentActive = false
}
}

this.actions.infoLog('Permissions loaded')
}

function onMenuHidden() {
Expand Down
10 changes: 8 additions & 2 deletions addon/actions/msg.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ function initMessaging() {
}
firstSidebarInitHandlers = null
}

this.actions.infoLog(`Sidebar (win: ${info.windowId}) connected`)
}

// Handle disconnect
Expand All @@ -57,12 +59,16 @@ function initMessaging() {
let targetPort = connectedSidebars[info.windowId]
if (info.instanceType === 'sidebar' && targetPort) {
let window = this.windows[info.windowId]
delete window.sidebarPort

if (this.settings.markWindow) browser.windows.update(info.windowId, { titlePreface: '' })
if (window) {
delete window.sidebarPort
if (this.settings.markWindow) browser.windows.update(info.windowId, { titlePreface: '' })
}

targetPort.onMessage.removeListener(onSidebarMsg)
delete connectedSidebars[info.windowId]

this.actions.infoLog(`Sidebar (win: ${info.windowId}) disconnected`)
}
})
})
Expand Down
2 changes: 2 additions & 0 deletions addon/actions/snapshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ async function scheduleSnapshots() {
else nextTimeout = interval - elapsed

Actions.scheduleNextSnapshot(nextTimeout)

Actions.infoLog('Auto snapshots scheduled')
}

/**
Expand Down
2 changes: 2 additions & 0 deletions addon/actions/windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ async function getWindows() {
async function onWindowCreated(window) {
this.windows[window.id] = window
window.tabs = await browser.tabs.query({ windowId: window.id })
this.actions.infoLog('Window created: ' + window.id)
}

/**
* Handle window remove
*/
function onWindowRemoved(windowId) {
delete this.windows[windowId]
this.actions.infoLog('Window removed: ' + windowId)
}

function setupWindowsListeners() {
Expand Down
3 changes: 3 additions & 0 deletions addon/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ void (async function main() {
state.images = {}
state.windows = {}

Actions.initLogs()
Actions.infoLog('Initialization start')

// Init first-need stuff
Actions.initToolbarButton()
Actions.initGlobalMessaging()
Expand Down
1 change: 1 addition & 0 deletions src/actions/containers.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ async function loadContainers() {
}

this.state.containers = containers
this.actions.infoLog('Containers loaded')

if (saveNeeded) this.actions.saveContainers()
}
Expand Down
65 changes: 65 additions & 0 deletions src/actions/logs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
interface ActionContextTEMP {
actions: { [key: string]: (...args: any[]) => any }
state: {
instanceType: string
bg: WEPort
windowId: string
}

_logs: string[] | undefined
_logTimeout: number | undefined
}

enum LogLvl {
Info = 'INFO',
Warn = 'WARN',
Err = 'ERR',
}

/**
* Log info
*/
function infoLog(this: ActionContextTEMP, msg: string): void {
this.actions.log(LogLvl.Info, msg)
}

/**
* Log warn
*/
function warnLog(this: ActionContextTEMP, msg: string): void {
this.actions.log(LogLvl.Warn, msg)
}

/**
* Log err
*/
function errLog(this: ActionContextTEMP, msg: string): void {
this.actions.log(LogLvl.Err, msg)
}

/**
* Log msg at given level in mem and send
*/
const LOG_DEBOUNCE = 1000
function log(this: ActionContextTEMP, lvl: LogLvl, msg: string): void {
if (!this._logs) this._logs = []
const log = `${Date.now()} [${lvl}::${this.state.instanceType}-${this.state.windowId}] ${msg}`
this._logs.push(log)

if (this._logTimeout) window.clearTimeout(this._logTimeout)
this._logTimeout = setTimeout(() => {
if (this.state.bg && !this.state.bg.error) {
this.state.bg.postMessage({ action: 'log', args: [this._logs] })
} else {
browser.runtime.sendMessage({ instanceType: 'bg', action: 'log', args: [this._logs] })
}
this._logs = []
}, LOG_DEBOUNCE)
}

export default {
infoLog,
warnLog,
errLog,
log,
}
1 change: 1 addition & 0 deletions src/actions/panels.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ async function loadPanels() {

this.state.panels = normPanels
this.state.panelsMap = panelsMap
this.actions.infoLog('Panels loaded')

if (saveNeeded) this.actions.savePanels()
}
Expand Down
2 changes: 2 additions & 0 deletions src/actions/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ async function loadSettings() {
if (settings[key] === undefined) continue
this.state[key] = settings[key]
}

this.actions.infoLog('Settings loaded')
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/actions/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ function initTheme() {

themeLinkEl.href = `../themes/${this.state.theme}/${this.state.instanceType}.css`
setTimeout(() => EventBus.$emit('dynVarChange'), 120)

this.actions.infoLog('Theme initialized: ' + this.state.theme)
}

/**
Expand All @@ -35,6 +37,7 @@ async function loadCustomCSS() {
if (!ans || !ans[fieldName]) return

this.actions.applyCustomCSS(ans[fieldName])
this.actions.infoLog('Custom CSS loaded')
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/page.settings/actions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import FaviconsActions from '../actions/favicons'
import MenuActions from '../actions/menu'
import LogsActions from '../actions/logs'
import Store from './store'
import State from './store/state'
import KeybindingsActions from './actions/keybindings'
Expand All @@ -17,6 +18,7 @@ const Actions = {
...StylesActions,
...ContainersActions,
...PanelsActions,
...LogsActions,
...MiscActions,
}

Expand Down
6 changes: 6 additions & 0 deletions src/page.settings/components/settings.help.vue
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,12 @@ export default {
dbg.bookmarks = err.toString()
}
try {
dbg.logs = await browser.runtime.sendMessage({ instanceType: 'bg', action: 'getLogs' })
} catch (err) {
dbg.logs = err.toString()
}
return dbg
},
Expand Down
2 changes: 2 additions & 0 deletions src/sidebar/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import TabsActions from './actions/tabs'
import BookmarksActions from './actions/bookmarks'
import StylesActions from './actions/styles'
import CtxMenuActions from './actions/menu'
import LogsActions from '../actions/logs'
import MiscActions from './actions/misc'

const Actions = {
Expand All @@ -19,6 +20,7 @@ const Actions = {
...BookmarksActions,
...StylesActions,
...CtxMenuActions,
...LogsActions,
...MiscActions,
}

Expand Down
1 change: 1 addition & 0 deletions src/sidebar/actions/bookmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ async function loadBookmarks() {

this.state.bookmarks = bookmarks[0].children
this.state.bookmarksCount = count
this.actions.infoLog('Bookmarks loaded')
panel.loading = 'ok'
setTimeout(() => {
panel.loading = false
Expand Down
2 changes: 2 additions & 0 deletions src/sidebar/actions/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ async function loadWindowInfo() {
w => w.id !== this.state.windowId && w.type === 'normal'
)
})

this.actions.infoLog('Windows info loaded')
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/sidebar/actions/panels.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ async function updatePanels(newPanels) {
function updatePanelsTabs() {
let lastIndex = this.getters.pinnedTabs.length
let tabsCount = this.state.tabs.length
let eatenTabs = []
let tab, i
for (let panel of this.state.panels) {
if (panel.type === 'bookmarks') continue
Expand All @@ -119,6 +120,7 @@ function updatePanelsTabs() {
} else if (tab.panelId === null) {
tab.panelId = panel.id
panel.tabs.push(tab)
eatenTabs.push(tab)
} else {
break
}
Expand All @@ -134,6 +136,11 @@ function updatePanelsTabs() {
panel.endIndex = panel.startIndex
}
}

if (eatenTabs.length) {
let info = eatenTabs.map(t => `${t.panelId} <- n${t.index} #${t.id}`).join('\n ')
this.actions.warnLog('Tabs were eaten by panel:\n ' + info)
}
}

function getPanelStartIndex(id) {
Expand Down
Loading

0 comments on commit 73422f0

Please sign in to comment.