diff --git a/src/sse-pkg/util/sonataInfoDisplay/logfile.cpp b/src/sse-pkg/util/sonataInfoDisplay/logfile.cpp index aea6bd2..00a5b7c 100644 --- a/src/sse-pkg/util/sonataInfoDisplay/logfile.cpp +++ b/src/sse-pkg/util/sonataInfoDisplay/logfile.cpp @@ -36,6 +36,7 @@ int Logfile::m_maxFd = 0; fd_set Logfile::m_rfds; +time_t Logfile::m_lastStatusTime = -1; /* * Constructor @@ -43,18 +44,11 @@ fd_set Logfile::m_rfds; * * @param filename name of file */ -Logfile::Logfile(string filename) +Logfile::Logfile(string filename, Components *details) { m_logfile = filename; openLogfile(m_logfile); -} - -/* - * Destructor - * Does nothing. - */ -Logfile::~Logfile() -{ + m_details = details; } /* @@ -69,7 +63,7 @@ void Logfile::openLogfile(string filename) m_fp = fopen(filename.c_str(), "a+"); if(m_fp == NULL) { - // Change to stderr? + // FIXME: actually exit, then send this to stderr. fprintf(stdout," Could not open %s, EXITING.\n", filename.c_str()); } @@ -78,12 +72,10 @@ void Logfile::openLogfile(string filename) { Logfile::m_maxFd = m_fd; } - struct stat stbuf; - if(fstat(m_fd, &stbuf) == 0) { - m_inode = stbuf.st_ino; + m_inode = stbuf.st_ino; } else { @@ -100,13 +92,12 @@ void Logfile::checkRefresh() if (stat(m_logfile.c_str(), &stbuf) == 0) { - // The most likely reason the inode would be changed is if a new // logfile has been created. // If so, switch to reading the new file. if (m_inode != stbuf.st_ino) { - //FIXME: Need exception handling. + // FIXME: Needs exception handling. if(fclose(m_fp) == 0) { openLogfile(m_logfile); @@ -152,8 +143,14 @@ void Logfile::getLine(char *buf, unsigned long bufsize) * @return the number of file descriptors ready for reading */ -int Logfile::readLogfiles(list logfiles) +int Logfile::readLogfiles(list& logfiles, Screen *screen) { + int linesSinceLastStatus = 0; + + if(Logfile::m_lastStatusTime == -1) + { + Logfile::m_lastStatusTime = time(NULL); + } FD_ZERO(&m_rfds); FD_SET(0, &m_rfds); @@ -166,7 +163,7 @@ int Logfile::readLogfiles(list logfiles) struct timeval tv; tv.tv_sec = 0; - tv.tv_usec = 200000; //1/5 second + tv.tv_usec = 200000; // 1/5 second int retVal = -1; @@ -176,15 +173,31 @@ int Logfile::readLogfiles(list logfiles) // Consider a delay by e.g. opting out of &m_rfds for n passes when at EOF? retVal = select(Logfile::m_maxFd + 1, &m_rfds, NULL, NULL, &tv); - return retVal; -} + for(it=logfiles.begin(); it != logfiles.end(); it++) + { + if(it->m_details != NULL) + { + char line[2048]; + + if(FD_ISSET(it->getFd(), &m_rfds)) + { + it->getLine(line, sizeof(line) - 1); + if(line[0] != 0 && it->m_details->addWithFilter(line)) + screen->paint(it->m_details); + linesSinceLastStatus++; + memset(line, 0, sizeof(line)); + } + //FIXME: Hack--move to filter in some way? + if(linesSinceLastStatus > 0 && + (int)(time(NULL) - Logfile::m_lastStatusTime) > 1) + { + linesSinceLastStatus = 0; + Logfile::m_lastStatusTime = time(NULL); + it->m_details->addWithFilter("===================================="); + screen->paint(it->m_details); + } + } + } -/* - * Returns file descriptor set for reads. - * - * @return the file descriptor set for reads - */ -fd_set *Logfile::getDescriptors() -{ - return &Logfile::m_rfds; + return retVal; } diff --git a/src/sse-pkg/util/sonataInfoDisplay/logfile.h b/src/sse-pkg/util/sonataInfoDisplay/logfile.h index 0ac4fae..1946e1c 100644 --- a/src/sse-pkg/util/sonataInfoDisplay/logfile.h +++ b/src/sse-pkg/util/sonataInfoDisplay/logfile.h @@ -40,6 +40,8 @@ #include #include #include +#include "components.h" +#include "screen.h" using namespace std; @@ -58,13 +60,13 @@ using namespace std; * * @param filename name of file */ - Logfile(string filename); + Logfile(string filename, Components *details); /** * Destructor * Does nothing. */ - ~Logfile(); + ~Logfile() {}; /** * Returns the logfile's file descriptor. @@ -88,18 +90,7 @@ using namespace std; * * @return the number of file descriptors ready for reading */ - static int readLogfiles(list logfiles); - - /** - * Returns file descriptor set for reads. - * - * @return the file descriptor set for reads - */ - static fd_set *getDescriptors(); // FIXME: Will be - // private once all file - // handling is removed - // from main(). - + static int readLogfiles(list& logfiles, Screen *screen); private: @@ -116,6 +107,11 @@ using namespace std; static int m_maxFd; /** File-descriptor set used for select() reads */ static fd_set m_rfds; + /** Timestamp of most recently read status. */ + static time_t m_lastStatusTime; + + /** Input-line filter */ + Components *m_details; /** * Open logfile. diff --git a/src/sse-pkg/util/sonataInfoDisplay/main.cpp b/src/sse-pkg/util/sonataInfoDisplay/main.cpp index 884b98d..e4c79d2 100644 --- a/src/sse-pkg/util/sonataInfoDisplay/main.cpp +++ b/src/sse-pkg/util/sonataInfoDisplay/main.cpp @@ -44,8 +44,6 @@ #include "details.h" #include "utils.h" -#include "screen.h" -#include "components.h" #include "logfile.h" #include @@ -70,7 +68,6 @@ int main(int argc, char **argv) string systemLogFileName = ""; string systemErrorFileName = ""; Screen screen; - char line[2048]; list logfiles; Components componentDetails; @@ -90,64 +87,23 @@ int main(int argc, char **argv) systemLogFileName = argv[2]; systemErrorFileName = argv[3]; - Logfile systemStatusFile = Logfile(systemStatusFileName); - Logfile systemLogFile = Logfile(systemLogFileName); - Logfile systemErrorFile = Logfile(systemErrorFileName); + Logfile systemStatusFile = Logfile(systemStatusFileName, &componentDetails); + Logfile systemLogFile = Logfile(systemLogFileName, NULL); + Logfile systemErrorFile = Logfile(systemErrorFileName, NULL); logfiles.push_back(systemStatusFile); logfiles.push_back(systemLogFile); logfiles.push_back(systemErrorFile); - //Initialize the curses screen. + // Initialize the curses screen. screen.init(); screen.screenResize(0); - time_t lastStatusTime = time(NULL);; - int linesSinceLastStatus = 0; - - //Loop foever + // Loop foever while(1) { - - Logfile::readLogfiles(logfiles); - - // FIXME: Move all the stuff below out of main! - // (Note the current clumsy use of Logfile::m_rfds.) - - // Process any data read from the status file. - if(FD_ISSET(systemStatusFile.getFd(), Logfile::getDescriptors())) - { - systemStatusFile.getLine(line, sizeof(line) - 1); - if(line[0] != 0 && componentDetails.addWithFilter(line)) - screen.paint(&componentDetails); - linesSinceLastStatus++; - memset(line, 0, sizeof(line)); - } - - //The trigger for the end of a status screen paint is "===..." but sometimes - //this does not arrive, so we have to force it. - if(linesSinceLastStatus > 0 && (int)(time(NULL) - lastStatusTime) > 1) - { - linesSinceLastStatus = 0; - lastStatusTime = time(NULL); - componentDetails.addWithFilter("===================================="); - screen.paint(&componentDetails); - } - - //Process the log file - if(FD_ISSET(systemLogFile.getFd(), Logfile::getDescriptors())) - { - } - - //Process the error file - if(FD_ISSET(systemErrorFile.getFd(), Logfile::getDescriptors())) - { - } - + Logfile::readLogfiles(logfiles, &screen); screen.processKey(&componentDetails); } - - return 0; - }