1 #include "otsdaq/ProgressBar/ProgressBar.h"
2 #include "otsdaq/Macros/CoutMacros.h"
3 #include "otsdaq/Macros/StringMacros.h"
4 #include "otsdaq/MessageFacility/MessageFacility.h"
17 ProgressBar::ProgressBar()
18 : cProgressBarFilePath_(std::string(__ENV__(
"SERVICE_DATA_PATH")) +
"/ProgressBarData/")
19 , cProgressBarFileExtension_(
".txt")
20 , totalStepsFileName_(
"")
25 std::string path = cProgressBarFilePath_;
26 DIR* dir = opendir(path.c_str());
29 else if(-1 == mkdir(path.c_str(), 0755))
32 __SS__ <<
"Service directory creation failed: " << path << std::endl;
40 void ProgressBar::reset(std::string file, std::string lineNumber,
int id)
42 std::lock_guard<std::mutex> lock(theMutex_);
49 sprintf(fn,
"%s_%s_%d", file.c_str(), lineNumber.c_str(), id);
51 for(
unsigned int c = 0; c < strlen(fn); ++c)
52 if(!((fn[c] >=
'0' && fn[c] <=
'9') || (fn[c] >=
'a' && fn[c] <=
'z') || (fn[c] >=
'A' && fn[c] <=
'Z')))
54 totalStepsFileName_ = cProgressBarFilePath_ + fn + cProgressBarFileExtension_;
57 FILE* fp = fopen(totalStepsFileName_.c_str(),
"r");
60 fscanf(fp,
"%d", &stepsToComplete_);
66 std::cout << __COUT_HDR_FL__ <<
"File Not there" << std::endl;
72 void ProgressBar::step()
74 std::lock_guard<std::mutex> lock(theMutex_);
81 bool ProgressBar::isComplete()
83 std::lock_guard<std::mutex> lock(theMutex_);
88 void ProgressBar::complete()
92 std::lock_guard<std::mutex> lock(theMutex_);
94 stepsToComplete_ = stepCount_;
101 FILE* fp = fopen(totalStepsFileName_.c_str(),
"w");
104 fprintf(fp,
"%d", stepsToComplete_);
108 std::cout << __COUT_HDR_FL__ <<
"Critical ERROR!" << std::endl;
113 int ProgressBar::read()
115 std::lock_guard<std::mutex> lock(theMutex_);
121 return stepCount_ * 100.0 / stepsToComplete_;
123 return stepCount_ ? 50 : 0;
128 std::string ProgressBar::readPercentageString()
131 sprintf(pct,
"%d", read());