#include <LogSvc.hh>
Public Member Functions | |
LogSvc () | |
~LogSvc () | |
bool | CreateFile () |
bool | CheckFileExist () |
bool | CheckFileAvailable () |
void | SetLogFile (const char *file_name) |
int | AddLog (const char *run_name) |
Static Public Member Functions | |
static LogSvc * | GetLogSvc () |
Private Member Functions | |
int | OpenFile () |
void | CloseFile () |
int | LockFile () |
int | UnLockFile () |
Private Attributes | |
std::string | fFileName |
int | fd |
struct flock | lock |
Static Private Attributes | |
static LogSvc * | fLogSvc = 0 |
Definition at line 19 of file LogSvc.hh.
LogSvc::LogSvc | ( | ) |
LogSvc::~LogSvc | ( | ) |
int LogSvc::AddLog | ( | const char * | run_name | ) |
Definition at line 127 of file LogSvc.cc.
References CheckFileExist(), CloseFile(), CreateFile(), fFileName, LockFile(), OpenFile(), and UnLockFile().
Referenced by MyAnalysisSvc::BeginOfRunAction().
00127 { 00128 if ( !CheckFileExist() ){ 00129 std::cout<<"File "<<fFileName<<" dose not exist! Will generate a new one."<<std::endl; 00130 CreateFile(); 00131 } 00132 int status; 00133 status = OpenFile(); 00134 if ( status != 0 ){ 00135 std::cout<<"In LogSvs::AddLog, cannnot open file "<<fFileName<<", will set run_num to -1"<<std::endl; 00136 return -1; 00137 } 00138 status = LockFile(); 00139 if (status != 0){ 00140 std::cout<<"In LogSvs::AddLog, cannnot lock file "<<fFileName<<", will set run_num to -1"<<std::endl; 00141 return -1; 00142 } 00143 std::ifstream fin_log(fFileName.c_str()); 00144 if(!fin_log){ 00145 std::cout<<"In LogSvs::AddLog, cannot open "<<fFileName<<" in ifstream format, will set run_num to -1"<<std::endl; 00146 return -1; 00147 } 00148 std::stringstream buf_log; 00149 buf_log.clear(); 00150 buf_log.str(""); 00151 std::string s_log; 00152 while(getline(fin_log,s_log)){ 00153 buf_log<<s_log<<"\n"; 00154 } 00155 //std::cout<<"before modification, logfile is like:"<<std::endl; 00156 //std::cout<<buf_log.str()<<std::endl; 00157 int run_num; 00158 buf_log >> run_num; 00159 run_num++; 00160 std::ofstream fout; 00161 fout.open(fFileName.c_str()); 00162 if(!fout){ 00163 std::cout<<"In LogSvs::AddLog, cannot open "<<fFileName<<" in ofstream format, will set run_num to -1"<<std::endl; 00164 return -1; 00165 } 00166 time_t t = time( 0 ); 00167 char tmp[64]; 00168 strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A",localtime(&t) ); 00169 fout<<std::setiosflags(std::ios::left)<<std::setw(5)<<run_num<<" "<<std::setiosflags(std::ios::left)<<std::setw(30)<<run_name<<" "<<tmp<<std::endl; 00170 fout<<buf_log.str(); 00171 //std::cout<<"after modification, logfile is like:"<<std::endl; 00172 //std::cout<<std::setiosflags(std::ios::left)<<std::setw(5)<<run_num<<" "<<std::setiosflags(std::ios::left)<<std::setw(30)<<run_name<<" "<<tmp<<std::endl; 00173 //std::cout<<buf_log.str()<<std::endl; 00174 buf_log.str(""); 00175 buf_log.clear(); 00176 // sleep(3); 00177 status = UnLockFile(); 00178 if ( status != 0 ){ 00179 std::cout<<"Cannot unlock"<<fFileName<<", will set run number to -1."<<std::endl; 00180 CloseFile(); 00181 return -1; 00182 } 00183 fout.close(); 00184 //CloseFile(); 00185 return run_num; 00186 }
bool LogSvc::CheckFileAvailable | ( | ) |
bool LogSvc::CheckFileExist | ( | ) |
void LogSvc::CloseFile | ( | ) | [private] |
bool LogSvc::CreateFile | ( | ) |
LogSvc * LogSvc::GetLogSvc | ( | ) | [static] |
int LogSvc::LockFile | ( | ) | [private] |
Definition at line 75 of file LogSvc.cc.
References fd, fFileName, and lock.
Referenced by AddLog().
00075 { 00076 lock.l_type = F_WRLCK; 00077 lock.l_whence = 0; 00078 lock.l_start = 0; 00079 lock.l_len = 0; 00080 int count = 0; 00081 while(fcntl(fd,F_SETLK,&lock)<0){ 00082 if(errno==EAGAIN||errno==EACCES){//locked by other process 00083 if(++count<100){ 00084 fcntl(fd,F_GETLK,&lock); //give up and output error message 00085 std::cout<<"Pid: "<<getpid()<<" process finds "<<lock.l_pid<<" locked the file "<<fFileName<<", "<<count<<"s."<<std::endl; 00086 sleep(1);//wait for 100 sec at most 00087 } 00088 else{ 00089 fcntl(fd,F_GETLK,&lock); //give up and output error message 00090 std::cout<<"Pid: "<<getpid()<<" process finds "<<lock.l_pid<<" locked the file "<<fFileName<<std::endl; 00091 return 1; 00092 } 00093 } 00094 else{ 00095 std::cout<<"Error: cannnot lock file "<<fFileName<<", errno = "<<errno<<std::endl; 00096 return 2; 00097 } 00098 } 00099 time_t t = time( 0 ); 00100 char tmp[64]; 00101 strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A",localtime(&t) ); 00102 std::cout<<tmp<<std::endl; 00103 std::cout<<"Pid: "<<getpid()<<" process locked file "<<fFileName<<std::endl; 00104 return 0; 00105 }
int LogSvc::OpenFile | ( | ) | [private] |
void LogSvc::SetLogFile | ( | const char * | file_name | ) |
Definition at line 59 of file LogSvc.cc.
References fFileName.
Referenced by MyAnalysisSvc::BeginOfRunAction().
00059 { 00060 fFileName = file_name; 00061 }
int LogSvc::UnLockFile | ( | ) | [private] |
Definition at line 107 of file LogSvc.cc.
References fd, fFileName, and lock.
Referenced by AddLog().
00107 { 00108 lock.l_type = F_UNLCK; 00109 lock.l_whence = 0; 00110 lock.l_start = 0; 00111 lock.l_len = 0; 00112 int val = fcntl(fd,F_SETLK,&lock); 00113 if(val<0){ 00114 std::cout<<"Pid: "<<getpid()<<" process cannnot unlock file "<<fFileName<<std::endl; 00115 return 1; 00116 } 00117 else{ 00118 time_t t = time( 0 ); 00119 char tmp[64]; 00120 strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A",localtime(&t) ); 00121 std::cout<<tmp<<std::endl; 00122 std::cout<<"Pid: "<<getpid()<<" process unlocked file "<<fFileName<<std::endl; 00123 } 00124 return 0; 00125 }
int LogSvc::fd [private] |
Definition at line 51 of file LogSvc.hh.
Referenced by CloseFile(), LockFile(), OpenFile(), and UnLockFile().
std::string LogSvc::fFileName [private] |
Definition at line 49 of file LogSvc.hh.
Referenced by AddLog(), CheckFileAvailable(), CheckFileExist(), CreateFile(), LockFile(), OpenFile(), SetLogFile(), and UnLockFile().
LogSvc * LogSvc::fLogSvc = 0 [static, private] |
Definition at line 47 of file LogSvc.hh.
Referenced by GetLogSvc(), and LogSvc().
struct flock LogSvc::lock [read, private] |
Definition at line 53 of file LogSvc.hh.
Referenced by LockFile(), and UnLockFile().