otsdaq  v1_01_03
 All Classes Namespaces Functions
ProgressBar.cc
1 #include "otsdaq-core/ProgressBar/ProgressBar.h"
2 #include "otsdaq-core/MessageFacility/MessageFacility.h"
3 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
4 
5 #include <iostream>
6 #include <cstdio>
7 #include <cstring>
8 #include <cstdlib>
9 #include <cassert>
10 #include <dirent.h> //for DIR
11 #include <sys/stat.h>
12 
13 using namespace ots;
14 
15 
16 //========================================================================================================================
17 ProgressBar::ProgressBar()
18 : cProgressBarFilePath_ (std::string(getenv("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  std::cout << __COUT_HDR_FL__ << "Service directory creation failed: " <<
33  path << 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 
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(!(
53  (fn[c] >= '0' && fn[c] <= '9') ||
54  (fn[c] >= 'a' && fn[c] <= 'z') ||
55  (fn[c] >= 'A' && fn[c] <= 'Z')
56  ))
57  fn[c] = '_';
58  totalStepsFileName_ = cProgressBarFilePath_ + fn + cProgressBarFileExtension_;
59 // std::cout << __COUT_HDR_FL__ << totalStepsFileName_ << std::endl;
60 
61  FILE *fp = fopen(totalStepsFileName_.c_str(),"r");
62  if(fp)
63  {
64  fscanf(fp,"%d",&stepsToComplete_);
65  fclose(fp);
66 // std::cout << __COUT_HDR_FL__ << "File Found - stepsToComplete = " << stepsToComplete_ << std::endl;
67  }
68  else
69  std::cout << __COUT_HDR_FL__ << "File Not there" << std::endl;
70 
71  started_ = true;
72 }
73 
74 //========================================================================================================================
75 void ProgressBar::step()
76 {
77  ++stepCount_;
78  //std::cout << __COUT_HDR_FL__ << totalStepsFileName_ << " " << 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  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 }
123 
124 
125 
126 
127 
128 
129 
130 
131 
132