首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Execl错误调试

Execl错误调试
EN

Stack Overflow用户
提问于 2016-06-19 08:25:05
回答 1查看 49关注 0票数 0

目标

我的目标是创建一个简单的潘多克前端。我了解到,execl是在系统中调用可执行文件的一种好方法。

注意,在下面的代码中,函数btn_pressed使用上述方法调用pandoc。

代码语言:javascript
复制
[indent=4]
uses
    Posix
    Gtk


class TestWindow:Window
    _file_chooser:FileChooserButton
    _entry:Gtk.Entry
    _button:Gtk.Button
    _file:File

    construct()
        title = "Pandoc GUI"
        window_position = WindowPosition.CENTER
        destroy.connect( Gtk.main_quit )
        var folder_chooser = new FileChooserButton("Choose a Folder",FileChooserAction.SELECT_FOLDER)
        folder_chooser.set_current_folder( Environment.get_home_dir() )

        //I used selection_changed directly as per the question in stack_exchange
        //http://stackoverflow.com/questions/34689763/the-signal-connect-syntax
        folder_chooser.selection_changed.connect( folder_changed )

        _file_chooser = new FileChooserButton("Choose a File",FileChooserAction.OPEN)
        _file_chooser.set_current_folder( Environment.get_home_dir() )

        _file_chooser.file_set.connect( file_changed )
        _entry = new Gtk.Entry()
        _entry.set_text("Here the file name")

        _button = new Button.with_label("Convert to pdf")
        _button.set_sensitive(false)
        _button.clicked.connect(btn_pressed)

        var box = new Box( Orientation.VERTICAL, 0 )
        box.pack_start( folder_chooser, true, true, 0 )
        box.pack_start( _file_chooser, true, true, 0 )
        box.pack_start( _entry, true, true, 0 )
        box.pack_start( _button, true, true, 0 )
        add( box )

    def folder_changed( folder_chooser_widget:FileChooser )
        folder:string = folder_chooser_widget.get_uri()
        _file_chooser.set_current_folder_uri( folder )

    def file_changed ( file_chooser_widget: FileChooser )
        _file = File.new_for_uri(file_chooser_widget.get_uri())

        try
            info:FileInfo = _file.query_info (FileAttribute.ACCESS_CAN_WRITE, FileQueryInfoFlags.NONE, null)
            writable: bool = info.get_attribute_boolean (FileAttribute.ACCESS_CAN_WRITE)
            if !writable
                _entry.set_sensitive (false)
            else
                _button.set_sensitive (true)
        except e: Error
            print e.message

        _entry.set_text(_file.get_basename())

    def btn_pressed ()
        var md_name=_entry.get_text()+".md -s -o "+_entry.get_text()+".pdf"
        execl("/usr/bin/pandoc", md_name)
        _button.set_sensitive (false)

init
    Gtk.init( ref args )
    var test = new TestWindow()
    test.show_all()
    Gtk.main()

错误

在执行过程中,我完全没有从我的代码中得到任何响应,也没有任何pdf正在呈现。

问题

  • 如何用execl调试二进制文件的执行?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-24 19:16:45

我将使用GLib.Subprocess调用外部命令,因为它能够更好地控制外部命令的输入和输出。不过,将下面的示例更改为execl应该很容易。

第一件事是将外部命令从窗口对象中分离出来。这使得它更易于测试。为此,使用了一个单独的对象-- Subprocess调用的包装器。将此代码保存为ToPDF.gs

代码语言:javascript
复制
namespace FileConverters

    class ToPDF

        const _command:string = "pandoc"

        def async convert( source:string, output:string )
            try
                var flags = SubprocessFlags.STDOUT_PIPE \
                            | SubprocessFlags.STDERR_PIPE
                var subprocess = new Subprocess( flags, 
                                                 _command, 
                                                 source, 
                                                 output 
                                                )
                output_buffer:Bytes
                yield subprocess.communicate_async( null, 
                                                    null,
                                                    out output_buffer, 
                                                    null 
                                                   )
                if ( subprocess.get_exit_status() == 0 )
                    debug( "command successful: \n %s",
                           (string)output_buffer.get_data() 
                          )
                else
                    debug( "command failed" )
            except err:Error
                debug( err.message )

现在,ToPDF类已与应用程序的其余部分解除耦合。这意味着它可以被重复使用。为了说明这一点,下面显示了一个使用该类的集成测试。

ToPDF还使用异步代码。所以我先解释一下。创建异步方法意味着它将与应用程序的主线程并发运行。通过同时运行对外部程序的调用,这意味着主线程在等待外部程序完成时不会锁定。使用async意味着函数被分成两部分。第一部分使用convert.begin( source, output )调用,并将运行到yield命令。在这一点上,程序的执行被分成两部分。主线程将返回到convert.begin的调用方,但是在后台启动的是Subprocess。当Subprocess完成时,它返回到convert并完成方法调用。

将集成测试保存为ToPDFTest.gs

代码语言:javascript
复制
uses FileConverters

init
    var a = new ToPDF()
    a.convert.begin( "source_file.md", "output_file.pdf" )

    var loop = new MainLoop()
    var loop_quitter = new LoopQuitter( loop )
    Timeout.add_seconds( 2, loop_quitter.quit )
    loop.run()

class LoopQuitter
    _loop:MainLoop

    construct( loop:MainLoop )
        _loop = loop

    def quit():bool
        _loop.quit()
        return false

使用valac --pkg gio-2.0 ToPDF.gs ToPDFTest.gs编译,然后使用G_MESSAGES_DEBUG=all ./ToPDFTest运行测试

测试使用MainLoop,它是Gtk.Main的基类。为了模拟一个长时间运行的程序,设置了两个秒的超时,然后调用MainLoop.quit()来结束测试。不幸的是,MainLoop.quit()Timeout回调没有正确的函数签名,因此使用了包装类LoopQuitter

像这样的集成测试通常在软件发布之前保存和运行,以确保应用程序与其他软件模块一起工作。

要将ToPDF集成到窗口中,您需要更改

execl("/usr/bin/pandoc", md_name)

到某种程度上

代码语言:javascript
复制
var to_pdf = new Fileconverts.ToPDF()
to_pdf.convert.begin( md_name, pdf_name )

您还可能希望将其包装成类似于避免Genie中的全局变量的命令模式。您还可能希望修改它以向用户提供更好的反馈。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37905309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档