XrdMonBufferedOutput.cc

Go to the documentation of this file.
00001 /*****************************************************************************/
00002 /*                                                                           */
00003 /*                          XrdMonBufferedOutput.cc                          */
00004 /*                                                                           */
00005 /* (c) 2005 by the Board of Trustees of the Leland Stanford, Jr., University */
00006 /*                            All Rights Reserved                            */
00007 /*       Produced by Jacek Becla for Stanford University under contract      */
00008 /*              DE-AC02-76SF00515 with the Department of Energy              */
00009 /*****************************************************************************/
00010 
00011 // $Id: XrdMonBufferedOutput.cc 24468 2008-06-22 16:47:03Z ganis $
00012 
00013 #include "XrdMonBufferedOutput.hh"
00014 #include "XrdSys/XrdSysHeaders.hh"
00015 #include <fcntl.h>
00016 #include <strings.h> /* bcopy */
00017 #include <sys/stat.h>
00018 #include <fstream>
00019 #include <stdio.h>
00020 using std::cout;
00021 using std::endl;
00022 using std::fstream;
00023 using std::ios;
00024 
00025 
00026 XrdMonBufferedOutput::XrdMonBufferedOutput(const char* outFileName,
00027                                            const char* lockFileName,
00028                                            int bufSize)
00029                                        
00030     : _buf(0), 
00031       _bufSize(bufSize)
00032 {
00033     _fName = new char[strlen(outFileName)+1];
00034     strcpy(_fName, outFileName);
00035 
00036     if ( lockFileName == 0 ) {
00037         _fNameLock = new char [strlen(outFileName)+8];
00038         sprintf(_fNameLock, "%s.lock", outFileName);
00039     } else {
00040         _fNameLock = new char [strlen(lockFileName)+1];
00041         sprintf(_fNameLock, lockFileName);
00042     }
00043     
00044     _buf = new char [_bufSize];
00045     strcpy(_buf, "");
00046 }
00047 
00048 XrdMonBufferedOutput::~XrdMonBufferedOutput()
00049 {
00050     delete [] _fName;
00051     delete [] _fNameLock;
00052     delete [] _buf;
00053 }
00054 
00055 void
00056 XrdMonBufferedOutput::add(const char* s)
00057 {
00058     XrdSysMutexHelper mh; mh.Lock(&_mutex);
00059 
00060     if ( static_cast<int>(strlen(_buf) + strlen(s)) >= _bufSize ) {
00061         flush(false); // false -> don't lock mutex, already locked
00062     }
00063     strcat(_buf, s);
00064 }
00065 
00066 void
00067 XrdMonBufferedOutput::flush(bool lockIt)
00068 {
00069     // get the lock, wait if necessary
00070     struct flock lock_args;
00071     bzero(&lock_args, sizeof(lock_args));
00072 
00073     mode_t m = S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH;
00074 
00075     cout << "RT locking." << std::flush;
00076     int fLock = open(_fNameLock, O_WRONLY|O_CREAT, m);
00077     lock_args.l_type = F_WRLCK;
00078     fcntl(fLock, F_SETLKW, &lock_args);    
00079     cout << "ok." << std::flush;
00080 
00081     int s = strlen(_buf);
00082     if ( s > 0 ) {        
00083         XrdSysMutexHelper mh;
00084         if ( lockIt ) {
00085             mh.Lock(&_mutex);
00086         }
00087         // open rt log, write to it, and close it
00088         int f = open(_fName, O_WRONLY|O_CREAT|O_APPEND,m);
00089         write(f, _buf, strlen(_buf));
00090         close(f);
00091         // clear buffer
00092         strcpy(_buf, "");
00093     }
00094     cout << s;
00095 
00096     // unlock
00097     bzero(&lock_args, sizeof(lock_args));
00098     lock_args.l_type = F_UNLCK;
00099     fcntl(fLock, F_SETLKW, &lock_args);
00100     close (fLock);
00101     
00102     cout << ".unlocked" << endl;
00103 }
00104 

Generated on Tue Jul 5 14:46:36 2011 for ROOT_528-00b_version by  doxygen 1.5.1