uvm_info的打印完整路径简化

摘要

uvm_info会打印完整的文件路径及行号,本人希望将完整路径显示改正仅显示文件名。DS给出的答复如下。

实现方法1

class my_test extends uvm_test;
    `uvm_component_utils(my_test)

    function new(string name, uvm_component parent);
        super.new(name, parent);
    endfunction

    function void build_phase(uvm_phase phase);
        super.build_phase(phase);
        // 设置 report_format,隐藏路径,只显示文件名和行号
        this.set_report_format(UVM_FILELINE | UVM_INFO);
    endfunction
endclass

实现方法2

  • 创建自定义报告处理器:继承uvm_report_handler并重写compose_message方法,截取文件名:
class custom_report_handler extends uvm_report_handler;

    function new(string name = "custom_report_handler");
        super.new(name);
    endfunction

    virtual function string compose_message(uvm_severity severity,
                                           string name,
                                           string id,
                                           string message,
                                           string filename,
                                           int line,
                                           string context_name,
                                           bit report_enabled_checked);
        string filename_short = get_filename(filename);
        return super.compose_message(severity, name, id, message, filename_short, line, context_name, report_enabled_checked);
    endfunction

    protected function string get_filename(string full_path);
        int last_slash_pos = -1;
        for (int i = 0; i < full_path.len(); i++) begin
            if (full_path[i] == "/" || full_path[i] == "\\") begin
                last_slash_pos = i;
            end
        end
        return (last_slash_pos == -1) ? full_path : full_path.substr(last_slash_pos + 1, full_path.len() - 1);
    endfunction

endclass
  • 替换全局报告处理器:在测试的顶层(如build_phase中)设置自定义处理器。
function void my_test::build_phase(uvm_phase phase);
    super.build_phase(phase);
    custom_report_handler h = new();
    uvm_root::get().set_report_handler(h);
endfunction
  • 解释:

get_filename函数:从完整路径中提取文件名,通过查找最后一个/或\确定位置。
compose_message重写:在调用父类方法前替换filename为处理后的短名称。
全局设置:替换UVM根节点的报告处理器,确保所有组件使用自定义处理。
这样,uvm_info将仅显示文件名,不包含路径。例如,原输出UVM_INFO /path/to/file.sv(123)变为UVM_INFO file.sv(123)。

posted @ 2025-03-01 23:09  MKYC  阅读(161)  评论(0)    收藏  举报