00001
00002
00003
00004
00005
00006
00007
00008 #include "LogSvc.hh"
00009 #include "myglobals.hh"
00010
00011 #include <stdlib.h>
00012 #include <time.h>
00013 #include <iomanip>
00014 #include <sstream>
00015 #include <fstream>
00016 #include <string>
00017 #include <iostream>
00018
00019 LogSvc* LogSvc::fLogSvc = 0;
00020
00021 LogSvc::LogSvc()
00022 {
00023 if (fLogSvc){
00024 G4Exception("LogSvc::LogSvc()","Run0031",
00025 FatalException, "LogSvc constructed twice.");
00026 }
00027 fLogSvc = this;
00028
00029 }
00030
00031 LogSvc::~LogSvc()
00032 {
00033 printf("~LogSvc\n");
00034 }
00035
00036 LogSvc* LogSvc::GetLogSvc(){
00037 if ( !fLogSvc ){
00038 fLogSvc = new LogSvc;
00039 }
00040 return fLogSvc;
00041 }
00042
00043 bool LogSvc::CreateFile(){
00044 std::ofstream outfile(fFileName.c_str());
00045 outfile<<"0 TEST";
00046 outfile.close();
00047 }
00048
00049 bool LogSvc::CheckFileExist(){
00050 bool isOK = ( access( fFileName.c_str(), 0 ) == 0 );
00051 return isOK;
00052 }
00053
00054 bool LogSvc::CheckFileAvailable(){
00055 bool isOK = ( access( fFileName.c_str(), 6 ) == 0 );
00056 return isOK;
00057 }
00058
00059 void LogSvc::SetLogFile( const char* file_name ){
00060 fFileName = file_name;
00061 }
00062
00063 int LogSvc::OpenFile(){
00064 fd = open(fFileName.c_str(), O_RDWR|O_CREAT);
00065 if ( fd<0 ){
00066 return -1;
00067 }
00068 return 0;
00069 }
00070
00071 void LogSvc::CloseFile(){
00072 close(fd);
00073 }
00074
00075 int LogSvc::LockFile(){
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){
00083 if(++count<100){
00084 fcntl(fd,F_GETLK,&lock);
00085 std::cout<<"Pid: "<<getpid()<<" process finds "<<lock.l_pid<<" locked the file "<<fFileName<<", "<<count<<"s."<<std::endl;
00086 sleep(1);
00087 }
00088 else{
00089 fcntl(fd,F_GETLK,&lock);
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 }
00106
00107 int LogSvc::UnLockFile(){
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 }
00126
00127 int LogSvc::AddLog( const char* run_name ){
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
00156
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
00172
00173
00174 buf_log.str("");
00175 buf_log.clear();
00176
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
00185 return run_num;
00186 }