otsdaq  v2_03_00
ProgressBar.cc
1 #include "otsdaq-core/ProgressBar/ProgressBar.h"
2 #include "otsdaq-core/Macros/CoutMacros.h"
3 #include "otsdaq-core/MessageFacility/MessageFacility.h"
4 
5 #include <dirent.h> //for DIR
6 #include <sys/stat.h>
7 #include <cassert>
8 #include <cstdio>
9 #include <cstdlib>
10 #include <cstring>
11 #include <iostream>
12 
13 using namespace ots;
14 
15 //========================================================================================================================
16 ProgressBar::ProgressBar()
17  : cProgressBarFilePath_(std::string(getenv("SERVICE_DATA_PATH")) +
18  "/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  std::cout << __COUT_HDR_FL__ << "Service directory creation failed: " << path
33  << std::endl;
34  assert(false);
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  stepCount_ = 0;
44  stepsToComplete_ = 0;
45 
46  // try to load stepsToComplete based on file, lineNumber and id
47  char fn[1000];
48  sprintf(fn, "%s_%s_%d", file.c_str(), lineNumber.c_str(), id);
49 
50  for(unsigned int c = 0; c < strlen(fn); ++c)
51  if(!((fn[c] >= '0' && fn[c] <= '9') || (fn[c] >= 'a' && fn[c] <= 'z') ||
52  (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 void ProgressBar::complete()
82 {
83  step(); // consider complete as a step
84  stepsToComplete_ = stepCount_;
85  started_ = false;
86 
87  // done, save steps to file
88 
89  // std::cout << __COUT_HDR_FL__ << totalStepsFileName_ << std::endl;
90 
91  FILE* fp = fopen(totalStepsFileName_.c_str(), "w");
92  if(fp)
93  {
94  fprintf(fp, "%d", stepsToComplete_);
95  fclose(fp);
96  }
97  else
98  std::cout << __COUT_HDR_FL__ << "Critical ERROR!" << std::endl;
99 }
100 
101 //========================================================================================================================
102 // return percentage complete as integer
103 int ProgressBar::read()
104 {
105  if(!started_)
106  return 100; // if no progress started, always return 100% complete
107 
108  std::lock_guard<std::mutex> lock(theMutex_); // lock out for remainder of scope
109  if(stepsToComplete_)
110  return stepCount_ * 100.0 / stepsToComplete_;
111 
112  return stepCount_ ? 50 : 0;
113 }
114 
115 //========================================================================================================================
116 // return percentage complete as std::string
117 std::string ProgressBar::readPercentageString()
118 {
119  char pct[5];
120  sprintf(pct, "%d", read());
121  return pct;
122 }