I am trying to search for a string in a large text file and return an exit code accordingly. For context, the text file is a ModelSim log file and I need to pass an exit code of 1 (bad) or 0 (good) to the Windows batch script that is calling the following TCL script (snippet):
set sim_pass {# ** Failure: NONE. End of Test: PASS}
set sim_fail {# ** Failure: ERRORS. End of Test: FAIL}
# set each_line "# ** Failure: ERRORS. End of Test: FAIL\r";
set failed_sim 0
# Set top-level paths and testbench.
global app_sim_path reg_tb_path parent_path log_directory msim_ini
set TOP_LEVEL_NAME project_io_tb
cd $app_sim_path
# Create logfile.
set sim_log_file ${parent_path}/${log_directory}/${TOP_LEVEL_NAME}_sim.log
if {[file exists $sim_log_file]} {
file delete -force $sim_log_file
}
set fs [open $sim_log_file {RDWR CREAT EXCL}]
close $fs
# Initialize ModelSim.
puts "\[INFO\] Now testing ... $TOP_LEVEL_NAME"
vsim -modelsimini $msim_ini -c -quiet -l $sim_log_file
# Compile all files required for testbench.
source compile.tcl
# Log all signals for debug.
log -r /*
# Run simulation.
run -all
quit -sim
# Read log file.
set fptr [open ${sim_log_file} r]
set log_data [read $fptr]
close $fptr
# Split file contents on new line.
set line_data [split $log_data "\n"];
# Parse each line.
foreach each_line $line_data {
# Check if simulation test failed.
if {[string match $sim_fail [string trim $each_line]]} {
set failed_sim 1
break
}
}
# Return correct exit code.
if {$failed_sim} {
puts "Simulation Failed, returning $failed_sim"
} else {
puts "Simulation Passed, returning $failed_sim"
}
exit -code $failed_sim
quit -f
At least 2 things go wrong when I execute the above script:
- The log file is echoed to my Windows command shell w/o line breaks. This does not occur if I comment out everything after "# Read log file".
- The exit code is 0 even when the string appears in the log file.
What am I doing wrong?
[EDIT]: Thanks all for the responses. Unfortunately, I still can't get my TCL script to work. It may have to do with the fact that my log file contains the following text string that I'm searching for:
# ** Failure: ERRORS. End of Test: FAIL
I cannot change the first part of the text, "# ** Failure: ", as this is the syntax ModelSim uses for the log file. So the "**" may screw up the string match since I don't know how to escape it.
What I really want to do is search for the substring "ERRORS. End of Test: FAIL", which is the text I can change.
Note that I am using the string commands in a foreach loop.
I'm still getting the crazy long echo of the entire log file to my Windows command window, which is also written to the log file w/o line breaks. I'm afraid I'm not opening/closing the log file properly, so have added more to my original code snippet.