繁体   English   中英

从没有控制台窗口执行的 Javascript 代码记录到文件

[英]Log to a file from Javascript code executed with no console window

使用 Cinnamon 小程序( 示例)。 这些使用由cjs解释器解释的 Javascript,并且在没有控制台窗口的情况下运行。

如何从我正在处理的代码中将某些内容记录到文件或控制台?

我无法将测试代码放入单独的 Javascript 文件并使用cjs <the file>运行它,因为我无法访问 Cinnamon 提供给小程序的模块。

日志记录

从 javascript 登录:

global.log('my message')

要检查事物,包括可能具有循环引用的对象,请定义此函数:

const inspect = (function() {
    function replace_circular_references(show_private_keys) {
        let fullpaths = new Map(), cleanpaths= new Map(), rootobject = null
        return function(key, value) {
            let decycled; {
                if (!show_private_keys && key.startsWith('_')) return undefined
                let path = fullpaths.get(this) + (
                    Array.isArray(this) ? `[${key}]` : `.${key}` )
                let is_complex = value === Object(value)
                if (is_complex) fullpaths.set(value, path)
                let cleanpath = cleanpaths.get(value) || ''
                if (!cleanpath && is_complex)
                    cleanpaths.set(value, path.replace(/undefined\.\.?/, ''))
                decycled = cleanpath ?
                    `${cleanpath[0] == '[' ? '(this)' : '(this).'}${cleanpath}`
                    : value
                if (rootobject === null) rootobject = value
                else if (decycled === rootobject) { decycled = "(this)" }
            }
            return decycled
        }
    }
    return function (nameofthing, thing, show_private_keys=true, fold=null) {
        try {
            let thingrepr; {
                thingrepr = JSON.stringify(
                    thing, replace_circular_references(show_private_keys), '\t'
                ).replace(
                    /"([^\n"]+)"[:] /g, "$1: "  // prettify keys
                ).replace(
                    /"(\(this\)[^\n"]*)"/g, "$1"  // prettify circular refs
                )
                if (fold !== null) {  // maybe fold away some things
                    for (const key of fold) {
                        thingrepr = thingrepr.replace(
                            new RegExp(`^([ ]*)${key}: [{][^]*?\\n\\1[}]`, 'gm'),
                            "$1" + key + ": {...}"
                        ).replace(
                            new RegExp(`^([ ]*)${key}: \\[[^]*?\\n\\1\\]`, 'gm'),
                            "$1" + key + ": [...]"
                        )
                    }
                }
            }
            global.log(`\n${nameofthing} = ${thingrepr}`)
        } catch (error) {
            global.log(`${nameofthing} = (oops, can not do that)`)
            global.log(error)
            global.log(error.message)
        }
    }
})()

并像这样使用它:

inspect('myobject', myobject)

或者像这样,隐藏以下划线开头的键:

inspect('myobject', myobject, false)

或者像这样,折叠您不感兴趣的特定键:

inspect('myobject', myobject, true, ['key1', 'key2', ...])

查看日志

要查看记录的消息,请使用控制台或 GUI(或两者)。

使用控制台

tail -f ~/.xsession-errors

或者,在 Cinnamon 3.8.8 之前:

tail -f ~/.cinnamon/glass.log 

ctrl + c中止记录。

要重新加载小程序:

dbus-send --session --dest=org.Cinnamon.LookingGlass -- \
type=method_call /org/Cinnamon/LookingGlass \
org.Cinnamon.LookingGlass.ReloadExtension \
string:'your-applet-uuid' \
string:'APPLET'

其中your-applet-uuidmetadata.jsonuuid键的值。

从另一个控制台运行此命令是最简单的,因此您不必中止日志记录。

使用图形用户界面

Cinnamon 也有 Melange-Cinnamon 调试器。 打开它:

  • 右键单击任务栏并选择疑难解答窥镜
  • 或者,按Win key + L
  • 或者,按Alt + F2 → 输入 "lg" → Enter

日志记录发生在日志选项卡下。

要重新加载小程序的代码,请在扩展选项卡下找到它,右键单击 →重新加载代码

要重新加载 Cinnamon 并执行其他操作,请使用“操作”按钮。

调试器有一个小错误,有时在记录长内容时滚动条会消失。 要取回它,请按ctrl + Homectrl + End

要使调试器窗口位于后台,请右键单击其标题栏。

荣誉

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM