00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "XrdMonBufferedOutput.hh"
00014 #include "XrdSys/XrdSysHeaders.hh"
00015 #include <fcntl.h>
00016 #include <strings.h>
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);
00062 }
00063 strcat(_buf, s);
00064 }
00065
00066 void
00067 XrdMonBufferedOutput::flush(bool lockIt)
00068 {
00069
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
00088 int f = open(_fName, O_WRONLY|O_CREAT|O_APPEND,m);
00089 write(f, _buf, strlen(_buf));
00090 close(f);
00091
00092 strcpy(_buf, "");
00093 }
00094 cout << s;
00095
00096
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