otsdaq  v2_04_01
ProgressBar.cc
1 #include "otsdaq-core/ProgressBar/ProgressBar.h"
2 #include "otsdaq-core/Macros/CoutMacros.h"
3 #include "otsdaq-core/Macros/StringMacros.h"
4 #include "otsdaq-core/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  std::cout << __COUT_HDR_FL__ << "Service directory creation failed: " << path
34  << std::endl;
35  assert(false);
36  }
37 }
38 
39 //========================================================================================================================
40 // reset() ~~
41 // Resets progress bar to 0% complete
42 void ProgressBar::reset(std::string file, std::string lineNumber, int id)
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') ||
53  (fn[c] >= 'A' && fn[c] <= 'Z')))
54  fn[c] = '_';
55  totalStepsFileName_ = cProgressBarFilePath_ + fn + cProgressBarFileExtension_;
56  // std::cout << __COUT_HDR_FL__ << totalStepsFileName_ << std::endl;
57 
58  FILE* fp = fopen(totalStepsFileName_.c_str(), "r");
59  if(fp)
60  {
61  fscanf(fp, "%d", &stepsToComplete_);
62  fclose(fp);
63  // std::cout << __COUT_HDR_FL__ << "File Found - stepsToComplete = " <<
64  // stepsToComplete_ << std::endl;
65  }
66  else
67  std::cout << __COUT_HDR_FL__ << "File Not there" << std::endl;
68 
69  started_ = true;
70 }
71 
72 //========================================================================================================================
73 void ProgressBar::step()
74 {
75  std::lock_guard<std::mutex> lock(theMutex_); // lock out for remainder of scope
76  ++stepCount_;
77  // std::cout << __COUT_HDR_FL__ << totalStepsFileName_ << " " <<
78  // readPercentageString() << "% complete" << std::endl;
79 }
80 
81 //========================================================================================================================
82 void ProgressBar::complete()
83 {
84  step(); // consider complete as a step
85  stepsToComplete_ = stepCount_;
86  started_ = false;
87 
88  // done, save steps to file
89 
90  // std::cout << __COUT_HDR_FL__ << totalStepsFileName_ << std::endl;
91 
92  FILE* fp = fopen(totalStepsFileName_.c_str(), "w");
93  if(fp)
94  {
95  fprintf(fp, "%d", stepsToComplete_);
96  fclose(fp);
97  }
98  else
99  std::cout << __COUT_HDR_FL__ << "Critical ERROR!" << std::endl;
100 }
101 
102 //========================================================================================================================
103 // return percentage complete as integer
104 int ProgressBar::read()
105 {
106  if(!started_)
107  return 100; // if no progress started, always return 100% complete
108 
109  std::lock_guard<std::mutex> lock(theMutex_); // lock out for remainder of scope
110  if(stepsToComplete_)
111  return stepCount_ * 100.0 / stepsToComplete_;
112 
113  return stepCount_ ? 50 : 0;
114 }
115 
116 //========================================================================================================================
117 // return percentage complete as std::string
118 std::string ProgressBar::readPercentageString()
119 {
120  char pct[5];
121  sprintf(pct, "%d", read());
122  return pct;
123 }