【问题标题】:vim editor, navigate at the source code using a log filevim 编辑器,使用日志文件导航到源代码
【发布时间】:2011-07-07 09:17:55
【问题描述】:

这个问题可能很抽象,请提前道歉。

我有一个日志文件,其中包含遵循特定模式的行(文件名、行、函数、跟踪语句)。例如

file1.cpp, 12, function1, "we are in function 1"
file2.cpp, 104, add, "add function"
another_file.cpp, 300, function2, "This is a trace"

我想要的是 vim 编辑器分成两个窗口。一个窗口有日志文件,每次我将光标移动到跟踪行时,另一个窗口将在正确的代码行中打开真实文件。

例如在顶部窗口中,我的光标位于行

file2.cpp, 104, add, "add function"

第二个(vim 分为两个窗口)窗口在第 104 行(在第二个窗口的中心)打开 file2.cpp。

是否有机会使用结构化文件(日志文件)作为源代码的“导航器”? 如果是,我们如何在 vim 中做到这一点?如果没有,让我们成功吧! (但我不想重新发明轮子:-))

【问题讨论】:

    标签: logging vim


    【解决方案1】:

    您所描述的在 Vim 中称为快速修复窗口。您可能从:make 命令的结果中熟悉它。您可以使用:cfile 打开一个快速修复窗口。格式由errorformat 变量确定。有关更多详细信息,请查看 Vim 帮助。

    对于您的示例(文件名、行、函数、跟踪语句),您可以这样做:

    :set errorformat=%f\\\,\\\ %l\\\,\\\ %m
    :cfile log.txt
    

    :set 命令中,无缘无故的三重反斜杠用于绕过转义序列。格式转换为%f\,\ %l\,\ %m

    或者,您可以以 gcc 格式输出您的日志。在这种情况下,默认的errorformat 将能够解析它,您所要做的就是使用:cfile 命令打开它。

    加载后,您可以使用:clist:copen查看日志。

    【讨论】:

    • 这正是正确答案;让它完整我会参考'errorfile'设置以及你可以使用vim [arguments] -q [errorfile]启动vim以使用错误文件的事实。 (当然后者假设在vimrc文件中设置了合适的错误格式)
    • 好的,所以基本上正确的答案是谁来创建与日志文件格式匹配的错误格式?
    • @Xavier T.:要么这样,要么 OP 可以修改日志代码以使用更容易识别的错误格式my/path/to/file.txt:123:INFO no worries, just logging,例如
    • 我使用 printf("%s:%d, 这是之前的调用6\n", _ FILE , _ _LINE _); .我的错误格式应该是什么?
    • 有一个默认的errorformat 可以处理类似于sehe 示例的消息。如果你使用它,那么你只需要使用:cfile 来加载你的日志。
    【解决方案2】:

    这是一个不平凡的练习,但肯定可以做到。

    您需要一个 autocmd 来调用某些操作的函数(特别是当光标移动时),如下所示:

    autocmd CursorMoved mylogfile.txt call LogFileUpdate()
    

    您可能还想使用 CursorMovedI 和其他工具,但我会将其作为练习留给您...请参阅:

    :help autocommand-events
    

    在函数中,您执行“魔术”。这是未经测试的,没有错误检查;它旨在为您提供一些可玩的东西并从中构建您的脚本。

    function! LogFileUpdate()
        " Make sure the cursor stays put
        let saved_view = winsaveview()
    
        " This is a slightly lazy way of making a consistent split: you could do something
        " clever here, working out whether there is a split at present and re-using it:
    
        " Close all other windows
        only
    
        " Get the current line into a variable
        let current_line = getline('.')
        " Split on commas:
        let parts = split(current_line, ',')
    
        " Get the filename and line number (removing leading and trailing spaces)
        let filename = substitute(parts[0],'^\s*\(.*\)\s*$','\1','')
        let number_str = substitute(parts[1],'^\s*\(.*\)\s*$','\1','')
    
        " Open the file at the required line number
        exe 'sp +'.number_str filename
        " Set the file type (doesn't seem to happen automatically in a CursorMoved autocmd)
        filetype detect
    
        " Switch back to the log file window
        wincmd w
    
        " Restore the cursor to its original position
        call winrestview(saved_view)
    endfunction
    

    【讨论】:

      【解决方案3】:

      使用 Vim 在源代码中导航的常用方法是使用 ctags

      见:

      ctags 是一个外部程序,它将为变量、函数等生成标识符列表,然后 Vim 将使用该列表跳转到函数的定义。

      要生成tag 文件,您只需在项目目录中运行ctags -R *.cpp,*.hpp。它将递归地索引每个 cpp 和 hpp 文件。

      一旦你的标签文件被正确生成并在 Vim 的标签路径中,当你的光标在 "函数名”。 ctags 考虑了参数的数量,但我认为只使用它应该有效的名称。

      然后您可以使用 CTRL+TCTRL+o 返回您的日志文件。

      它不会完全按照您的意愿工作,但您将能够在日志文件和代码之间快速来回跳转。

      您也可以在 StackOverflow 上搜索 了解更多信息。

      【讨论】:

        猜你喜欢
        • 2015-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-09
        • 2010-11-29
        相关资源
        最近更新 更多