LogSvc Class Reference

#include <LogSvc.hh>

List of all members.

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 LogSvcGetLogSvc ()

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 LogSvcfLogSvc = 0

Detailed Description

Definition at line 19 of file LogSvc.hh.


Constructor & Destructor Documentation

LogSvc::LogSvc (  ) 

Definition at line 21 of file LogSvc.cc.

References fLogSvc.

00022 {
00023         if (fLogSvc){
00024                 G4Exception("LogSvc::LogSvc()","Run0031",
00025                                 FatalException, "LogSvc constructed twice.");
00026         }
00027         fLogSvc = this;
00028 
00029 }

LogSvc::~LogSvc (  ) 

Definition at line 31 of file LogSvc.cc.

00032 {
00033         printf("~LogSvc\n");
00034 }


Member Function Documentation

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 (  ) 

Definition at line 54 of file LogSvc.cc.

References fFileName.

00054                                {
00055         bool isOK = ( access( fFileName.c_str(), 6 ) == 0 );
00056         return isOK;
00057 }

bool LogSvc::CheckFileExist (  ) 

Definition at line 49 of file LogSvc.cc.

References fFileName.

Referenced by AddLog().

00049                            {
00050         bool isOK = ( access( fFileName.c_str(), 0 ) == 0 );
00051         return isOK;
00052 }

void LogSvc::CloseFile (  )  [private]

Definition at line 71 of file LogSvc.cc.

References fd.

Referenced by AddLog().

00071                       {
00072         close(fd);
00073 }

bool LogSvc::CreateFile (  ) 

Definition at line 43 of file LogSvc.cc.

References fFileName.

Referenced by AddLog().

00043                        {
00044         std::ofstream outfile(fFileName.c_str());
00045         outfile<<"0 TEST";
00046         outfile.close();
00047 }

LogSvc * LogSvc::GetLogSvc (  )  [static]

Definition at line 36 of file LogSvc.cc.

References fLogSvc.

Referenced by MyAnalysisSvc::BeginOfRunAction().

00036                          {
00037         if ( !fLogSvc ){
00038                 fLogSvc = new LogSvc;
00039         }
00040         return fLogSvc;
00041 }

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]

Definition at line 63 of file LogSvc.cc.

References fd, and fFileName.

Referenced by AddLog().

00063                     {
00064         fd = open(fFileName.c_str(), O_RDWR|O_CREAT);
00065         if ( fd<0 ){
00066                 return -1;
00067         }
00068         return 0;
00069 }

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 }


Member Data Documentation

int LogSvc::fd [private]

Definition at line 51 of file LogSvc.hh.

Referenced by CloseFile(), LockFile(), OpenFile(), and UnLockFile().

std::string LogSvc::fFileName [private]
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().


The documentation for this class was generated from the following files:

Generated on 15 Jun 2016 for g4sim by  doxygen 1.6.1