#ifndef __LogFile_h_ #define __LogFile_h_ #include #include #include "MiscSupport.h" #include "SocketInfo.h" #include "Messages.h" #include "ThreadClass.h" class LogFile : private ThreadClass { private: enum { mtSendString, mtSocketFinished, mtShutDownProcess, mtQuit }; std::string _fileNamePrefix; std::string _fileNameSuffix; std::ofstream _output; void ensureOpen(time_t eventTime); RequestQueue _incoming; volatile bool _showClose; time_t _nextLogTime; void updateNextLogTime(); protected: void threadFunction(); public: LogFile(); ~LogFile(); void sendString(std::string toWrite, SocketInfo *socket = NULL, time_t t = 0); void quoteAndSend(std::string toWrite, SocketInfo *socket = NULL, time_t t = 0) { sendString(tclQuote(toWrite), socket, t); } void quoteAndSend(std::string w1, std::string w2, SocketInfo *socket = NULL, time_t t = 0) { sendString(tclQuote(w1) + ' ' + tclQuote(w2), socket, t); } // This requests that we terminate the program. By sending the request to // this thread, we give this thread a chance to finish any output requests // preceeding // the shutdown request. In particular, you can send a message to the log // explaining the shutdown, then shut down after that message was written. void scheduleShutdown(); // We get a special call from the socket handling code when a socket has // been deleted. This occurs after all threads (including this thread) have // seen the standard socket deleted message. This way the log file can // be sure to add the socket finished message after all other messages // related to the socket. We pass in the serial number, rather than the // socket, because the socket might have already been deleted by now. void socketFinished(SocketInfo::SerialNumber serialNumber); // Potentially you could have more than one log file. The primary log file // is used by a lot of low level services, like the socket handler, so you // don't have to specify a log file when you initialize these modules. If // you create a new log file, there is an issue of the file name. Currently // there is no way to specify a different name for each file. This would // have to be fixed. static LogFile &primary(); void setShowClose(bool showClose) { // By default we add a line to the log when a socket is closed, but set // this to false to disable this feature. _showClose = showClose; } }; #endif