1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-06 04:31:29 +00:00

Simplify and shorten the ScanProgressMeter::printStats code.

This commit is contained in:
david
2009-02-05 00:25:25 +00:00
parent b369fd3c5b
commit 29460cd95d

View File

@@ -557,33 +557,10 @@ bool ScanProgressMeter::printStats(double perc_done,
double time_used_s;
double time_needed_s;
double time_left_s;
time_t timet;
struct tm *ltime;
last_print = *now;
if (perc_done) { /* catch "divide by zero" attempts */
if (!now) {
gettimeofday(&tvtmp, NULL);
now = (const struct timeval *) &tvtmp;
}
/* OK, now lets estimate the time to finish */
time_used_s = difftime(now->tv_sec, begin.tv_sec);
time_needed_s = time_used_s / perc_done;
time_left_s = time_needed_s - time_used_s;
/* Here we go! */
last_print = *now;
/*TIMEVAL_MSEC_ADD(last_est, *now, (unsigned long)time_left_s*1000);*/
last_est = *now;
last_est.tv_sec += time_left_s;
timet = last_est.tv_sec;
ltime = localtime(&timet);
assert(ltime);
}
// If we're less than 1% done we probably don't have enough
// data for decent timing estimates. Also with perc_done == 0
// these elements will be nonsensical.
@@ -591,15 +568,36 @@ bool ScanProgressMeter::printStats(double perc_done,
log_write(LOG_STDOUT, "%s Timing: About %.2f%% done\n",
scantypestr, perc_done * 100);
log_flush(LOG_STDOUT);
} else {
log_write(LOG_STDOUT, "%s Timing: About %.2f%% done; ETC: %02d:%02d (%lu:%02lu:%02lu remaining)\n",
scantypestr, perc_done * 100, ltime->tm_hour, ltime->tm_min, (unsigned long)(time_left_s / 3600),
((unsigned long)time_left_s % 3600) / 60, ((unsigned long)time_left_s % 60));
log_write(LOG_XML, "<taskprogress task=\"%s\" time=\"%lu\" percent=\"%.2f\" remaining=\"%lu\" etc=\"%lu\" />\n",
scantypestr, (unsigned long) now->tv_sec,
perc_done * 100, (unsigned long)time_left_s, (unsigned long) last_est.tv_sec);
log_flush(LOG_STDOUT|LOG_XML);
return true;
}
if (!now) {
gettimeofday(&tvtmp, NULL);
now = (const struct timeval *) &tvtmp;
}
/* OK, now lets estimate the time to finish */
time_used_s = difftime(now->tv_sec, begin.tv_sec);
time_needed_s = time_used_s / perc_done;
/* Add 0.5 to get the effect of rounding when truncating to an integer. */
time_left_s = time_needed_s - time_used_s + 0.5;
last_est = *now;
last_est.tv_sec += time_left_s;
/* Get the estimated time of day at completion */
ltime = localtime(&last_est.tv_sec);
assert(ltime);
log_write(LOG_STDOUT, "%s Timing: About %.2f%% done; ETC: %02d:%02d (%.f:%02.f:%02.f remaining)\n",
scantypestr, perc_done * 100, ltime->tm_hour, ltime->tm_min,
floor(time_left_s / 60.0 / 60.0),
floor(fmod(time_left_s / 60.0, 60.0)),
floor(fmod(time_left_s, 60.0)));
log_write(LOG_XML, "<taskprogress task=\"%s\" time=\"%lu\" percent=\"%.2f\" remaining=\"%.f\" etc=\"%lu\" />\n",
scantypestr, (unsigned long) now->tv_sec,
perc_done * 100, time_left_s, (unsigned long) last_est.tv_sec);
log_flush(LOG_STDOUT|LOG_XML);
return true;
}