$treeview $search $mathjax $extrastylesheet
otsdaq
v2_03_00
$projectbrief
|
$projectbrief
|
$searchbox |
00001 #include "otsdaq-core/ProgressBar/ProgressBar.h" 00002 #include "otsdaq-core/Macros/CoutMacros.h" 00003 #include "otsdaq-core/MessageFacility/MessageFacility.h" 00004 00005 #include <dirent.h> //for DIR 00006 #include <sys/stat.h> 00007 #include <cassert> 00008 #include <cstdio> 00009 #include <cstdlib> 00010 #include <cstring> 00011 #include <iostream> 00012 00013 using namespace ots; 00014 00015 //======================================================================================================================== 00016 ProgressBar::ProgressBar() 00017 : cProgressBarFilePath_(std::string(getenv("SERVICE_DATA_PATH")) + 00018 "/ProgressBarData/") 00019 , cProgressBarFileExtension_(".txt") 00020 , totalStepsFileName_("") 00021 , stepCount_(0) 00022 , stepsToComplete_(0) 00023 , started_(false) 00024 { 00025 std::string path = cProgressBarFilePath_; 00026 DIR* dir = opendir(path.c_str()); 00027 if(dir) 00028 closedir(dir); 00029 else if(-1 == mkdir(path.c_str(), 0755)) 00030 { 00031 // lets create the service folder (for first time) 00032 std::cout << __COUT_HDR_FL__ << "Service directory creation failed: " << path 00033 << std::endl; 00034 assert(false); 00035 } 00036 } 00037 00038 //======================================================================================================================== 00039 // reset() ~~ 00040 // Resets progress bar to 0% complete 00041 void ProgressBar::reset(std::string file, std::string lineNumber, int id) 00042 { 00043 stepCount_ = 0; 00044 stepsToComplete_ = 0; 00045 00046 // try to load stepsToComplete based on file, lineNumber and id 00047 char fn[1000]; 00048 sprintf(fn, "%s_%s_%d", file.c_str(), lineNumber.c_str(), id); 00049 00050 for(unsigned int c = 0; c < strlen(fn); ++c) 00051 if(!((fn[c] >= '0' && fn[c] <= '9') || (fn[c] >= 'a' && fn[c] <= 'z') || 00052 (fn[c] >= 'A' && fn[c] <= 'Z'))) 00053 fn[c] = '_'; 00054 totalStepsFileName_ = cProgressBarFilePath_ + fn + cProgressBarFileExtension_; 00055 // std::cout << __COUT_HDR_FL__ << totalStepsFileName_ << std::endl; 00056 00057 FILE* fp = fopen(totalStepsFileName_.c_str(), "r"); 00058 if(fp) 00059 { 00060 fscanf(fp, "%d", &stepsToComplete_); 00061 fclose(fp); 00062 // std::cout << __COUT_HDR_FL__ << "File Found - stepsToComplete = " << 00063 // stepsToComplete_ << std::endl; 00064 } 00065 else 00066 std::cout << __COUT_HDR_FL__ << "File Not there" << std::endl; 00067 00068 started_ = true; 00069 } 00070 00071 //======================================================================================================================== 00072 void ProgressBar::step() 00073 { 00074 std::lock_guard<std::mutex> lock(theMutex_); // lock out for remainder of scope 00075 ++stepCount_; 00076 // std::cout << __COUT_HDR_FL__ << totalStepsFileName_ << " " << 00077 // readPercentageString() << "% complete" << std::endl; 00078 } 00079 00080 //======================================================================================================================== 00081 void ProgressBar::complete() 00082 { 00083 step(); // consider complete as a step 00084 stepsToComplete_ = stepCount_; 00085 started_ = false; 00086 00087 // done, save steps to file 00088 00089 // std::cout << __COUT_HDR_FL__ << totalStepsFileName_ << std::endl; 00090 00091 FILE* fp = fopen(totalStepsFileName_.c_str(), "w"); 00092 if(fp) 00093 { 00094 fprintf(fp, "%d", stepsToComplete_); 00095 fclose(fp); 00096 } 00097 else 00098 std::cout << __COUT_HDR_FL__ << "Critical ERROR!" << std::endl; 00099 } 00100 00101 //======================================================================================================================== 00102 // return percentage complete as integer 00103 int ProgressBar::read() 00104 { 00105 if(!started_) 00106 return 100; // if no progress started, always return 100% complete 00107 00108 std::lock_guard<std::mutex> lock(theMutex_); // lock out for remainder of scope 00109 if(stepsToComplete_) 00110 return stepCount_ * 100.0 / stepsToComplete_; 00111 00112 return stepCount_ ? 50 : 0; 00113 } 00114 00115 //======================================================================================================================== 00116 // return percentage complete as std::string 00117 std::string ProgressBar::readPercentageString() 00118 { 00119 char pct[5]; 00120 sprintf(pct, "%d", read()); 00121 return pct; 00122 }