AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 505594
Accepted
FUZxxl
FUZxxl
Asked: 2014-08-02 11:55:31 +0800 CST2014-08-02 11:55:31 +0800 CST 2014-08-02 11:55:31 +0800 CST

如何阻止 gedit(和其他程序)在我的终端中输出 GTK 警告等?

  • 772

从 raring 升级后,我在 trusty 上运行了很棒的窗口管理器。我的桌面环境故意没有运行所有的 Gnome / Freedesktop 守护进程——我不想要它们。

当我gedit从这样的终端执行时:

gedit file

每当我按下 enter 或 save 或在其他各种情况下,它都会在我的终端上输出这样的消息:

(gedit:5700): Gtk-WARNING **: Calling Inhibit failed: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.SessionManager was not provided by any .service files

我明白这个警告的意思,我决定这对我来说无关紧要。

我怎样才能关闭这种警告?通过“关闭”,我并不是指任何这些或类似的解决方法:

  • 将 gedit 的输出通过管道传输到/dev/null
  • 编写一个包装器脚本,将 gedit 的输出通过管道传输到/dev/null
  • 创建一个别名,将 gedit 的输出传送到/dev/null

这些变通办法是不可接受的,因为它们必须单独应用于每个 Gnome 应用程序——gedit 并不是唯一喜欢弄乱终端的应用程序。

gtk
  • 5 5 个回答
  • 28059 Views

5 个回答

  • Voted
  1. Best Answer
    avih
    2015-01-13T04:50:00+08:002015-01-13T04:50:00+08:00

    首先,我还发现这些警告出现在开箱即用的 Ubuntu 上很烦人,没有我能找到的禁用它们的“正确”方法(似乎最常见的“解决方案”是安装gir1.2-gtksource-3.0这似乎不起作用,因为它已经安装,或者忽略它们 - 但我想完全抑制它们,因为它们只会让我的终端嘈杂)。

    我想出了以下代码,到目前为止它的行为似乎完全符合我的预期,并且基于 TuKsn 的回答,但对其进行了一些增强:

    • 默认情况下工作 ( gedit ...),无需使用 F12 或其他快捷方式(调用未过滤的使用/usr/bin/gedit ...)。
    • 当它作为后台任务终止时显示输入的命令名称。

    仍然可以概括一点,但就目前而言,如果您需要对其他命令进行相同的处理,请gedit()为每个需要相同过滤器的命令名称复制该函数。

    # solution adapted from: http://askubuntu.com/questions/505594
    # TODO: use a list of warnings instead of cramming all of them to a single grep.
    # TODO: generalize gedit() to allow the same treatment for several commands
    #       without duplicating the function with only a different name
    # output filter. takes: name_for_history some_command [arguments]
    # the first argument is required both for history, but also when invoking to bg
    # such that it shows Done <name> ... instead of e.g. Done /usr/bin/gedit ...
    suppress-gnome-warnings() {
        # $1 is the name which should appear on history but is otherwise unused.
        historyName=$1
        shift
    
        if [ -n "$*" ]; then
            # write the real command to history without the prefix
            # syntax adapted from http://stackoverflow.com/questions/4827690
            history -s "$historyName ${@:2}"
    
            # catch the command output
            errorMsg=$( $* 2>&1 )
    
            # check if the command output contains not a (one of two) GTK-Warnings
            if ! $(echo $errorMsg | grep -q 'Gtk-WARNING\|connect to accessibility bus'); then
                echo $errorMsg
            fi
        fi
    }
    gedit() {
      suppress-gnome-warnings $FUNCNAME $(which $FUNCNAME) $@
    }
    

    还有一个更好的版本(更小,完全通用,不需要重写历史,因为按原样调用,并且更好地过滤每行而不是整个输出):

    # generates a function named $1 which:
    # - executes $(which $1) [with args]
    # - suppresses output lines which match $2
    # e.g. adding: _supress echo "hello\|world"
    # will generate this function:
    # echo() { $(which echo) "$@" 2>&1 | tr -d '\r' | grep -v "hello\|world"; }
    # and from now on, using echo will work normally except that lines with
    # hello or world will not show at the output
    # to see the generated functions, replace eval with echo below
    # the 'tr' filter makes sure no spurious empty lines pass from some commands
    _supress() {
      eval "$1() { \$(which $1) \"\$@\" 2>&1 | tr -d '\r' | grep -v \"$2\"; }"
    }
    
    _supress gedit          "Gtk-WARNING\|connect to accessibility bus"
    _supress gnome-terminal "accessibility bus\|stop working with a future version"
    _supress firefox        "g_slice_set_config"
    
    • 18
  2. TuKsn
    2014-08-03T01:59:34+08:002014-08-03T01:59:34+08:00

    这也是一种解决方法,但您不必为每个应用程序都应用它。

    将此写给您.bashrc,您可以将此包装器与 F12(或选择另一个键)一起使用来抑制警告:

    # output filter
    of() { 
        if [ -n "$*" ]; then   
            # write the real command to history without the prefix "of" 
            history -s "$*"
    
            # catch the command output
            errorMsg=$( $* 2>&1 )
    
            # check if the command output contains not a GTK-Warning
            if ! $(echo $errorMsg | grep -q 'Gtk-WARNING'); then
                echo $errorMsg 
            fi
        fi
    }
    
    # write the function "of" before every command if the user presses F12
    bind '"\e[24~": "\e[1~ of \e[4~\n"'
    
    • 2
  3. Alexis Wilke
    2016-12-17T02:42:20+08:002016-12-17T02:42:20+08:00

    我实际上用 C 编写了hide-warnings 工具,我发现它比上面显示的脚本更容易使用。此外,它将写入所有stdout默认写入的输出(因为 Gtk 和其他警告被发送到,stderr所以默认情况下它stderr不解析stdout)。

    上面脚本的一个大问题是它不会向您的控制台写入任何内容,即使它与正则表达式不匹配,直到完成。这是因为它将所有数据保存在一个变量中,然后在完成后 grep 该变量。这也意味着它会将输出保存在该变量中,可能会使用大量内存(至少您应该将其保存在临时文件中。)最后,据我所知,如果任何一行匹配,grep 将阻止任何显示. 也许不完全是你想要的。

    该工具可以像这样在一个简单的别名中使用:

    alias gvim="hide-warnings gvim"
    

    (我用gvim……我相信它也能用gedit。)

    该文件是自包含的,除了 C 库之外没有依赖项,因此您可以获得一个副本并轻松编译和安装它:

    gcc hide-warnings.c -o hide-warnings
    sudo cp hide-warnings /usr/bin/.
    

    该文件中有一些进一步的文档,您可以使用--help编译后的快速文档。

    较新的版本,在某些时候将使用 advgetopt 库,是在 C++ 中。

    • 2
  4. Shane
    2018-07-20T02:50:48+08:002018-07-20T02:50:48+08:00

    我一直在寻找一种实用程序来解决这类问题,我自己。

    我对所提供答案的问题如下:

    • stdout 和 stderr 不要聚合到一个流中,这一点很重要
    • 我无法调用命令,过滤所有输出直到它终止,并在最后打印它(即解决方案必须正确地流式传输输出)
    • 我想尽可能保留 stdout 和 stderr 消息的顺序

    我很欣赏我所看到的使用 Bash 进行此操作的尝试,但是,我未能找到一种解决方案来满足上述所有 3 个条件。

    我的最终解决方案是用 NodeJS 编写的,我知道它不会安装在许多 linux 机器上。在选择编写 JS 版本之前,我尝试用 python 编写代码,发现异步 IO 库非常丑陋且损坏,直到非常新的 python 版本(~3.5 在一些较新的发行版上开箱即用)。

    最小化依赖性是选择 python 的唯一原因,所以我放弃了它而选择了 NodeJS,它有一组非常好的库,用于一些低级的、面向异步的 IO。

    这里是:

    #!/usr/bin/env nodejs
    
    const spawn = require('child_process').spawn
    
    function usage() {
        console.warn('Usage: filter-err <error regex> <cmd> [<cmd arg> ...]')
        process.exit(1)
    }
    
    function main(err_regex, cmd_arr) {
        let filter = new RegExp(err_regex)
    
        let proc = spawn(cmd_arr[0], cmd_arr.slice(1), {
            shell: true,
            stdio: ['inherit', 'inherit', 'pipe']
        })
    
        proc.stderr.on('data', (err) => {
            err = err.toString('utf8')
    
            if (! err.match(filter))
                process.stderr.write(err)
        })
    
        proc.on('close', (code) => process.exit(code))
    }
    
    const argv = process.argv
    
    if (argv.length < 4)
        usage()
    else
        main(argv[2], argv.slice(3))
    

    要使用此脚本,您可以将这些行添加到您的 .bashrc 中:

    alias gimp='filter-err "GLib-[^ ]*-WARNING" gimp'
    

    您选择运行的子进程将继承标准输入,因此您可以自由使用 BASH 管道或重定向。

    • 0
  5. l3x
    2021-04-13T14:25:49+08:002021-04-13T14:25:49+08:00

    一种解决方案是创建一个函数来抑制 Gtk 错误消息。

    有时,错误的来源并不明显。

    例如,如果您的 .gitconfig 中有以下内容,它告诉 git 使用meld 作为 git difftool:

    [diff]
        tool = meld
    [difftool]
        prompt = false
    [difftool "meld"]
        cmd = meld "$LOCAL" "$REMOTE"
    

    然后您尝试比较您的.bashrc文件:

    $ git difftool .bashrc
    

    你可能会收到很多恼人的 Gtk 错误消息:

    2021-04-12 18:01:59,905 CRITICAL Gtk: gtk_widget_get_scale_factor: assertion 'GTK_IS_WIDGET (widget)' failed
    2021-04-12 18:01:59,905 CRITICAL Gtk: gtk_widget_get_scale_factor: assertion 'GTK_IS_WIDGET (widget)' failed
    2021-04-12 18:01:59,905 CRITICAL Gtk: gtk_widget_get_scale_factor: assertion 'GTK_IS_WIDGET (widget)' failed
    2021-04-12 18:01:59,905 CRITICAL Gtk: gtk_widget_get_scale_factor: assertion 'GTK_IS_WIDGET (widget)' failed
    2021-04-12 18:01:59,905 CRITICAL Gtk: gtk_widget_get_scale_factor: assertion 'GTK_IS_WIDGET (widget)' failed
    . . .
    

    创建一个函数,将您的输出和 stderr 重定向到 /dev/null

    $ function git-difftool(){ git difftool ${@} & &>/dev/null; }
    

    现在,当您运行 时git-difftool .bashrc,您将不会再看到 Gtk 错误消息。

    一个附带的好处是,通过在git-difftool函数的定义中添加 & 控制运算符,您可以在后台运行此命令,这将释放您的终端 st,您可以在 meld difftool 窗口中运行其他命令.

    • 0

相关问题

  • Ubuntu:“可编辑菜单加速器”(基于每个应用程序)。这个选项在哪里?

  • 我在哪里可以获取桌面主题和吸引眼球的东西?

  • 如何解决 keryx 'UnicodeEncodeError' 错误?

  • 如何在 Ubuntu 上安装依赖于 libgtk1.2 的 Wolfenstein Enemy Teritory?

  • GTK 改进何时会在 Maverick 上落地?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve