00001 /*****************************************************************************/ 00002 /* */ 00003 /* XrdMonCtrWriter.hh */ 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: XrdMonCtrWriter.hh 22437 2008-03-04 14:35:16Z rdm $ 00012 00013 #ifndef XRDMONCTRWRITER_HH 00014 #define XRDMONCTRWRITER_HH 00015 00016 #include "XrdMon/XrdMonTypes.hh" 00017 #include <fstream> 00018 #include <string> 00019 using std::fstream; 00020 using std::ostream; 00021 using std::string; 00022 00023 class XrdMonHeader; 00024 00025 // Class writes data to a log file. 00026 // One instance per one xrootd instance. 00027 // It buffers data in memory to avoid 00028 // overloading disks 00029 00030 class XrdMonCtrWriter { 00031 public: 00032 XrdMonCtrWriter(senderid_t senderId, kXR_int32 stod); 00033 ~XrdMonCtrWriter(); 00034 void operator()(const char* packet, 00035 const XrdMonHeader& header, 00036 long currentTime); 00037 00038 kXR_int32 prevStod() const { return _prevStod; } 00039 00040 void forceClose(); 00041 long lastActivity() const { return _lastActivity; } 00042 00043 static void setBaseDir(const char* dir) { _baseDir = dir; } 00044 static void setMaxLogSize(kXR_int64 size) { _maxLogSize = size;} 00045 static void setBufferSize(int size) { _bufferSize = size;} 00046 00047 private: 00048 enum LogType { ACTIVE, PERMANENT }; 00049 00050 bool logIsOpen() { return _file.is_open(); } 00051 bool logIsFull() { return (kXR_int64) _file.tellp() >= _maxLogSize; } 00052 bool bufferIsFull(packetlen_t x) { return _bPos + x > _bufferSize; } 00053 00054 string logName(LogType t) const; 00055 void mkActiveLogNameDirs() const; 00056 00057 void flushBuffer(); 00058 void openLog(); 00059 void closeLog(); 00060 void publish(); 00061 00062 private: 00063 static string _baseDir; 00064 static kXR_int64 _maxLogSize; 00065 static int _bufferSize; 00066 static long _totalArchived; 00067 00068 kXR_int32 _prevStod; // for checking if xrootd restarted 00069 00070 string _timestamp; 00071 hp_t _sender; // { hostName, portNr } 00072 char* _buffer; 00073 kXR_int32 _bPos; // position where to write to buffer 00074 fstream _file; // non-published log file 00075 00076 long _lastActivity; // approx time of last activity 00077 00078 friend ostream& operator<<(ostream& o, const XrdMonCtrWriter& w); 00079 }; 00080 00081 #endif /* XRDMONCTRWRITER_HH */