otsdaq  3.03.00
ProgressBar.cc
1 #include "otsdaq/ProgressBar/ProgressBar.h"
2 #include "otsdaq/Macros/CoutMacros.h"
3 #include "otsdaq/Macros/StringMacros.h"
4 #include "otsdaq/MessageFacility/MessageFacility.h"
5 
6 #include <dirent.h> //for DIR
7 #include <sys/stat.h>
8 #include <cassert>
9 #include <cstdio>
10 #include <cstdlib>
11 #include <cstring>
12 #include <iostream>
13 
14 using namespace ots;
15 
16 //==============================================================================
17 ProgressBar::ProgressBar()
18  : cProgressBarFilePath_(std::string(__ENV__("SERVICE_DATA_PATH")) +
19  "/ProgressBarData/")
20  , cProgressBarFileExtension_(".txt")
21  , totalStepsFileName_("")
22  , stepCount_(0)
23  , stepsToComplete_(0)
24  , started_(false)
25 {
26  std::string path = cProgressBarFilePath_;
27  DIR* dir = opendir(path.c_str());
28  if(dir)
29  closedir(dir);
30  else if(-1 == mkdir(path.c_str(), 0755))
31  {
32  // lets create the service folder (for first time)
33  __SS__ << "Service directory creation failed: " << path << std::endl;
34  __SS_THROW__;
35  }
36 } //end constructor()
37 
38 //==============================================================================
41 void ProgressBar::reset(std::string file, std::string lineNumber, int id)
42 {
43  std::lock_guard<std::mutex> lock(theMutex_); // lock out for remainder of scope
44 
45  stepCount_ = 0;
46  stepsToComplete_ = 0;
47 
48  // try to load stepsToComplete based on file, lineNumber and id
49  char fn[1000];
50  sprintf(fn, "%s_%s_%d", file.c_str(), lineNumber.c_str(), id);
51 
52  for(unsigned int c = 0; c < strlen(fn); ++c)
53  if(!((fn[c] >= '0' && fn[c] <= '9') || (fn[c] >= 'a' && fn[c] <= 'z') ||
54  (fn[c] >= 'A' && fn[c] <= 'Z')))
55  fn[c] = '_';
56  totalStepsFileName_ = cProgressBarFilePath_ + fn + cProgressBarFileExtension_;
57  __COUTVS__(10, totalStepsFileName_);
58 
59  FILE* fp = fopen(totalStepsFileName_.c_str(), "r");
60  if(fp)
61  {
62  fscanf(fp, "%d", &stepsToComplete_);
63  fclose(fp);
64  __COUTS__(10) << "File Found - stepsToComplete = " << stepsToComplete_
65  << std::endl;
66  }
67  else
68  __COUTT__ << "File Not there: " << totalStepsFileName_ << __E__;
69 
70  started_ = true;
71 } //end reset()
72 
73 //==============================================================================
75 {
76  std::lock_guard<std::mutex> lock(theMutex_); // lock out for remainder of scope
77  ++stepCount_;
78 
79  // do not allow to get to 100% until complete (in case stepsToComplete is not constant for all time)
80  if(stepsToComplete_ && stepCount_ >= stepsToComplete_)
81  stepsToComplete_ = stepCount_ + 1;
82 
83  __COUTS__(10) << totalStepsFileName_ << " " << readPercentageString() << "% complete"
84  << std::endl;
85 } //end step()
86 
87 //==============================================================================
89 {
90  std::lock_guard<std::mutex> lock(theMutex_); // lock out for remainder of scope
91  return !started_;
92 } //end isComplete()
93 
94 //==============================================================================
96 {
97  step(); // consider complete as a step
98 
99  std::lock_guard<std::mutex> lock(theMutex_); // lock out for remainder of scope
100 
101  stepsToComplete_ = stepCount_;
102  started_ = false;
103 
104  // done, save steps to file
105 
106  __COUTS__(10) << totalStepsFileName_ << std::endl;
107 
108  FILE* fp = fopen(totalStepsFileName_.c_str(), "w");
109  if(fp)
110  {
111  fprintf(fp, "%d", stepsToComplete_);
112  fclose(fp);
113  }
114  else
115  __COUT_ERR__ << "Critical ERROR!" << std::endl;
116 } //end complete()
117 
118 //==============================================================================
121 {
122  std::lock_guard<std::mutex> lock(theMutex_); // lock out for remainder of scope
123 
124  if(!started_)
125  return 100; // if no progress started, always return 100% complete
126 
127  if(stepsToComplete_)
128  return stepCount_ * 100.0 / stepsToComplete_;
129 
130  return stepCount_ ? 50 : 0;
131 } //end read()
132 
133 //==============================================================================
136 {
137  char pct[5];
138  sprintf(pct, "%d", read());
139  return pct;
140 } //end readPercentageString()
std::string readPercentageString()
return percentage complete as std::string
Definition: ProgressBar.cc:135
bool isComplete()
get functions
Definition: ProgressBar.cc:88
void step()
thread safe
Definition: ProgressBar.cc:74
int read()
if stepsToComplete==0, then define any progress as 50%, thread safe
Definition: ProgressBar.cc:120
void complete()
declare complete, thread safe
Definition: ProgressBar.cc:95