otsdaq  v2_04_02
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 }
37 
38 //========================================================================================================================
39 // reset() ~~
40 // Resets progress bar to 0% complete
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  // std::cout << __COUT_HDR_FL__ << totalStepsFileName_ << std::endl;
58 
59  FILE* fp = fopen(totalStepsFileName_.c_str(), "r");
60  if(fp)
61  {
62  fscanf(fp, "%d", &stepsToComplete_);
63  fclose(fp);
64  // std::cout << __COUT_HDR_FL__ << "File Found - stepsToComplete = " <<
65  // stepsToComplete_ << std::endl;
66  }
67  else
68  std::cout << __COUT_HDR_FL__ << "File Not there" << std::endl;
69 
70  started_ = true;
71 }
72 
73 //========================================================================================================================
74 void ProgressBar::step()
75 {
76  std::lock_guard<std::mutex> lock(theMutex_); // lock out for remainder of scope
77  ++stepCount_;
78  // std::cout << __COUT_HDR_FL__ << totalStepsFileName_ << " " <<
79  // readPercentageString() << "% complete" << std::endl;
80 }
81 
82 //========================================================================================================================
83 bool ProgressBar::isComplete()
84 {
85  std::lock_guard<std::mutex> lock(theMutex_); // lock out for remainder of scope
86  return !started_;
87 }
88 
89 //========================================================================================================================
90 void ProgressBar::complete()
91 {
92  step(); // consider complete as a step
93 
94  std::lock_guard<std::mutex> lock(theMutex_); // lock out for remainder of scope
95 
96  stepsToComplete_ = stepCount_;
97  started_ = false;
98 
99  // done, save steps to file
100 
101  // std::cout << __COUT_HDR_FL__ << totalStepsFileName_ << std::endl;
102 
103  FILE* fp = fopen(totalStepsFileName_.c_str(), "w");
104  if(fp)
105  {
106  fprintf(fp, "%d", stepsToComplete_);
107  fclose(fp);
108  }
109  else
110  std::cout << __COUT_HDR_FL__ << "Critical ERROR!" << std::endl;
111 }
112 
113 //========================================================================================================================
114 // return percentage complete as integer
115 int ProgressBar::read()
116 {
117  std::lock_guard<std::mutex> lock(theMutex_); // lock out for remainder of scope
118 
119  if(!started_)
120  return 100; // if no progress started, always return 100% complete
121 
122  if(stepsToComplete_)
123  return stepCount_ * 100.0 / stepsToComplete_;
124 
125  return stepCount_ ? 50 : 0;
126 }
127 
128 //========================================================================================================================
129 // return percentage complete as std::string
130 std::string ProgressBar::readPercentageString()
131 {
132  char pct[5];
133  sprintf(pct, "%d", read());
134  return pct;
135 }