Refactor the code that counts the last log version to keep
When silencing the Coverity warning in remove_old_tsversions(), the code was refactored to reduce the indentation levels and break down the long code into individual functions. This improve fix for [GL #1989 (closed)].
(cherry picked from commit aca18b8b)
Closes #2033 (closed)