00001 #include "otsdaq-core/ProgressBar/ProgressBar.h"
00002 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00003 #include "otsdaq-core/Macros/CoutMacros.h"
00004
00005 #include <iostream>
00006 #include <cstdio>
00007 #include <cstring>
00008 #include <cstdlib>
00009 #include <cassert>
00010 #include <dirent.h>
00011 #include <sys/stat.h>
00012
00013 using namespace ots;
00014
00015
00016
00017 ProgressBar::ProgressBar()
00018 : cProgressBarFilePath_ (std::string(getenv("SERVICE_DATA_PATH")) + "/ProgressBarData/")
00019 , cProgressBarFileExtension_(".txt")
00020 , totalStepsFileName_ ("")
00021 , stepCount_ (0)
00022 , stepsToComplete_ (0)
00023 , started_ (false)
00024 {
00025 std::string path = cProgressBarFilePath_;
00026 DIR *dir = opendir(path.c_str());
00027 if(dir)
00028 closedir(dir);
00029 else if(-1 == mkdir(path.c_str(),0755))
00030 {
00031
00032 std::cout << __COUT_HDR_FL__ << "Service directory creation failed: " <<
00033 path << std::endl;
00034 assert(false);
00035 }
00036 }
00037
00038
00039
00040
00041 void ProgressBar::reset(std::string file, std::string lineNumber, int id)
00042 {
00043 stepCount_ = 0;
00044 stepsToComplete_ = 0;
00045
00046
00047
00048 char fn[1000];
00049 sprintf(fn,"%s_%s_%d",file.c_str(),lineNumber.c_str(),id);
00050
00051 for(unsigned int c=0;c<strlen(fn);++c)
00052 if(!(
00053 (fn[c] >= '0' && fn[c] <= '9') ||
00054 (fn[c] >= 'a' && fn[c] <= 'z') ||
00055 (fn[c] >= 'A' && fn[c] <= 'Z')
00056 ))
00057 fn[c] = '_';
00058 totalStepsFileName_ = cProgressBarFilePath_ + fn + cProgressBarFileExtension_;
00059
00060
00061 FILE *fp = fopen(totalStepsFileName_.c_str(),"r");
00062 if(fp)
00063 {
00064 fscanf(fp,"%d",&stepsToComplete_);
00065 fclose(fp);
00066
00067 }
00068 else
00069 std::cout << __COUT_HDR_FL__ << "File Not there" << std::endl;
00070
00071 started_ = true;
00072 }
00073
00074
00075 void ProgressBar::step()
00076 {
00077 ++stepCount_;
00078
00079 }
00080
00081
00082 void ProgressBar::complete()
00083 {
00084 step();
00085 stepsToComplete_ = stepCount_;
00086 started_ = false;
00087
00088
00089
00090
00091
00092 FILE *fp = fopen(totalStepsFileName_.c_str(),"w");
00093 if(fp)
00094 {
00095 fprintf(fp,"%d",stepsToComplete_);
00096 fclose(fp);
00097 }
00098 else
00099 std::cout << __COUT_HDR_FL__ << "Critical ERROR!" << std::endl;
00100 }
00101
00102
00103
00104 int ProgressBar::read()
00105 {
00106 if(!started_)
00107 return 100;
00108
00109 if(stepsToComplete_)
00110 return stepCount_*100.0/stepsToComplete_;
00111
00112 return stepCount_?50:0;
00113 }
00114
00115
00116
00117 std::string ProgressBar::readPercentageString()
00118 {
00119 char pct[5];
00120 sprintf(pct,"%d",read());
00121 return pct;
00122 }
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132