00001 #include <time.h> 00002 00003 #include <iostream> 00004 #include <iomanip> 00005 #include <limits> 00006 #include <cmath> 00007 00008 #include "EventProgress.h" 00009 00010 EventProgress::EventProgress() : 00011 nEvents(0), startTime(time(NULL)), lastUpdate(0), displayWidth(0) 00012 { 00013 } 00014 00015 EventProgress::EventProgress(unsigned long _nEvents) : 00016 nEvents(_nEvents), startTime(time(NULL)), lastUpdate(0), 00017 displayWidth(int(std::log(nEvents + 0.5) / M_LN10 + 1)) 00018 { 00019 } 00020 00021 EventProgress::~EventProgress() 00022 { 00023 std::cout << " \r" << std::flush; 00024 } 00025 00026 void EventProgress::update(unsigned long event) 00027 { 00028 time_t currentTime = time(0); 00029 if (currentTime == lastUpdate) 00030 return; 00031 00032 lastUpdate = currentTime; 00033 00034 if (displayWidth) 00035 std::cout << "Event " << std::setw(displayWidth) << event; 00036 else 00037 std::cout << "Event " << event; 00038 00039 if (nEvents) { 00040 unsigned long eta = (event >= 10) ? ((currentTime - startTime) * (nEvents - event) / event) 00041 : std::numeric_limits<unsigned long>::max(); 00042 00043 std::cout << " (" << std::setw(2) << (event * 100 / nEvents) << "%), ETA "; 00044 if (eta >= 6000) 00045 std::cout << "##:##\r"; 00046 else 00047 std::cout << std::setw(2) << (eta / 60) << ":" 00048 << std::setfill('0') << std::setw(2) << (eta % 60); 00049 } 00050 00051 std::cout << std::setfill(' ') 00052 << std::resetiosflags(std::ios::fixed) 00053 << std::resetiosflags(std::ios::scientific) 00054 << std::resetiosflags(std::ios::floatfield) 00055 << std::setprecision(8) 00056 << "\r" << std::flush; 00057 }