otsdaq  v2_05_02_indev
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")) + "/ProgressBarData/")
19  , cProgressBarFileExtension_(".txt")
20  , totalStepsFileName_("")
21  , stepCount_(0)
22  , stepsToComplete_(0)
23  , started_(false)
24 {
25  std::string path = cProgressBarFilePath_;
26  DIR* dir = opendir(path.c_str());
27  if(dir)
28  closedir(dir);
29  else if(-1 == mkdir(path.c_str(), 0755))
30  {
31  // lets create the service folder (for first time)
32  __SS__ << "Service directory creation failed: " << path << std::endl;
33  __SS_THROW__;
34  }
35 }
36 
37 //==============================================================================
38 // reset() ~~
39 // Resets progress bar to 0% complete
40 void ProgressBar::reset(std::string file, std::string lineNumber, int id)
41 {
42  std::lock_guard<std::mutex> lock(theMutex_); // lock out for remainder of scope
43 
44  stepCount_ = 0;
45  stepsToComplete_ = 0;
46 
47  // try to load stepsToComplete based on file, lineNumber and id
48  char fn[1000];
49  sprintf(fn, "%s_%s_%d", file.c_str(), lineNumber.c_str(), id);
50 
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')))
53  fn[c] = '_';
54  totalStepsFileName_ = cProgressBarFilePath_ + fn + cProgressBarFileExtension_;
55  // std::cout << __COUT_HDR_FL__ << totalStepsFileName_ << std::endl;
56 
57  FILE* fp = fopen(totalStepsFileName_.c_str(), "r");
58  if(fp)
59  {
60  fscanf(fp, "%d", &stepsToComplete_);
61  fclose(fp);
62  // std::cout << __COUT_HDR_FL__ << "File Found - stepsToComplete = " <<
63  // stepsToComplete_ << std::endl;
64  }
65  else
66  std::cout << __COUT_HDR_FL__ << "File Not there" << std::endl;
67 
68  started_ = true;
69 }
70 
71 //==============================================================================
72 void ProgressBar::step()
73 {
74  std::lock_guard<std::mutex> lock(theMutex_); // lock out for remainder of scope
75  ++stepCount_;
76  // std::cout << __COUT_HDR_FL__ << totalStepsFileName_ << " " <<
77  // readPercentageString() << "% complete" << std::endl;
78 }
79 
80 //==============================================================================
81 bool ProgressBar::isComplete()
82 {
83  std::lock_guard<std::mutex> lock(theMutex_); // lock out for remainder of scope
84  return !started_;
85 }
86 
87 //==============================================================================
88 void ProgressBar::complete()
89 {
90  step(); // consider complete as a step
91 
92  std::lock_guard<std::mutex> lock(theMutex_); // lock out for remainder of scope
93 
94  stepsToComplete_ = stepCount_;
95  started_ = false;
96 
97  // done, save steps to file
98 
99  // std::cout << __COUT_HDR_FL__ << totalStepsFileName_ << std::endl;
100 
101  FILE* fp = fopen(totalStepsFileName_.c_str(), "w");
102  if(fp)
103  {
104  fprintf(fp, "%d", stepsToComplete_);
105  fclose(fp);
106  }
107  else
108  std::cout << __COUT_HDR_FL__ << "Critical ERROR!" << std::endl;
109 }
110 
111 //==============================================================================
112 // return percentage complete as integer
113 int ProgressBar::read()
114 {
115  std::lock_guard<std::mutex> lock(theMutex_); // lock out for remainder of scope
116 
117  if(!started_)
118  return 100; // if no progress started, always return 100% complete
119 
120  if(stepsToComplete_)
121  return stepCount_ * 100.0 / stepsToComplete_;
122 
123  return stepCount_ ? 50 : 0;
124 }
125 
126 //==============================================================================
127 // return percentage complete as std::string
128 std::string ProgressBar::readPercentageString()
129 {
130  char pct[5];
131  sprintf(pct, "%d", read());
132  return pct;
133 }