From 83a6e7727861bfb653ef76b00da7274ec10bac54 Mon Sep 17 00:00:00 2001 From: Ben Thompson Date: Thu, 14 Feb 2019 10:50:22 -0600 Subject: [PATCH] Logging to syslog instead of cout/printf --- src/AppendTask.cpp | 2 +- src/CopyTask.cpp | 3 +- src/IOCoordinator.cpp | 3 +- src/ListDirectoryTask.cpp | 2 +- src/OpenTask.cpp | 2 +- src/PosixTask.cpp | 5 +-- src/ProcessTask.cpp | 3 +- src/ReadTask.cpp | 2 +- src/SessionManager.cpp | 70 +++++++++++++++++---------------------- src/StatTask.cpp | 2 +- src/TruncateTask.cpp | 2 +- src/UnlinkTask.cpp | 2 +- src/WriteTask.cpp | 2 +- src/main.cpp | 12 +++++++ 14 files changed, 59 insertions(+), 53 deletions(-) mode change 100644 => 100755 src/CopyTask.cpp mode change 100644 => 100755 src/SessionManager.cpp mode change 100644 => 100755 src/main.cpp diff --git a/src/AppendTask.cpp b/src/AppendTask.cpp index 2c4d0af9e..5784744d9 100644 --- a/src/AppendTask.cpp +++ b/src/AppendTask.cpp @@ -44,7 +44,7 @@ bool AppendTask::run() check_error("AppendTask read", false); #ifdef SM_TRACE - cout << "append " << cmd->count << " bytes to " << cmd->filename << endl; + syslog(LOG_DEBUG, "append %d bytes to %s.",cmd->count,cmd->filename); #endif size_t readCount = 0, writeCount = 0; diff --git a/src/CopyTask.cpp b/src/CopyTask.cpp old mode 100644 new mode 100755 index d350a7628..9799cc766 --- a/src/CopyTask.cpp +++ b/src/CopyTask.cpp @@ -1,6 +1,7 @@ #include "CopyTask.h" #include +#include "syslog.h" #include "messageFormat.h" using namespace std; @@ -41,7 +42,7 @@ bool CopyTask::run() f_name *filename2 = (f_name *) &buf[sizeof(copy_cmd) + cmd->file1.flen]; #ifdef SM_TRACE - cout << "copy " << filename1 << " to " << filename2 << endl; + syslog(LOG_DEBUG, "copy %s to %s.",filename1,filename2); #endif int err = ioc->copyFile(filename1.c_str(), filename2->filename); diff --git a/src/IOCoordinator.cpp b/src/IOCoordinator.cpp index 48de51116..fec219de4 100644 --- a/src/IOCoordinator.cpp +++ b/src/IOCoordinator.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -199,7 +200,7 @@ int IOCoordinator::copyFile(const char *filename1, const char *filename2) l_errno = e.code().value(); // why not. // eh, not going to translate all of boost's errors into our errors for this. // log the error - cout << "IOCoordinator::copy(): got " << e.what() << endl; + syslog(LOG_ERR, "IOCoordinator::copy(): got %s",e.what()); } catch (...) { err = -1; diff --git a/src/ListDirectoryTask.cpp b/src/ListDirectoryTask.cpp index 4547ad78c..cdc5b22b3 100644 --- a/src/ListDirectoryTask.cpp +++ b/src/ListDirectoryTask.cpp @@ -71,7 +71,7 @@ bool ListDirectoryTask::run() listdir_cmd *cmd = (listdir_cmd *) buf; #ifdef SM_TRACE - cout << "list_directory " << cmd->path << endl; + syslog(LOG_DEBUG, "list_directory %s.",cmd->path); #endif vector listing; diff --git a/src/OpenTask.cpp b/src/OpenTask.cpp index ff943db03..9ba285a53 100644 --- a/src/OpenTask.cpp +++ b/src/OpenTask.cpp @@ -45,7 +45,7 @@ bool OpenTask::run() open_cmd *cmd = (open_cmd *) buf; #ifdef SM_TRACE - cout << "open filename " << cmd->filename << " mode " << oct << cmd->openmode << dec << endl; + syslog(LOG_DEBUG, "open filename %s mode %o.",cmd->filename,cmd->openmode); #endif sm_response *resp = (sm_response *) buf; int err = ioc->open(cmd->filename, cmd->openmode, (struct stat *) &resp->payload); diff --git a/src/PosixTask.cpp b/src/PosixTask.cpp index 6285c8be6..9b4cc9e90 100644 --- a/src/PosixTask.cpp +++ b/src/PosixTask.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #define min(x, y) (x < y ? x : y) @@ -42,7 +43,7 @@ void PosixTask::handleError(const char *name, int errCode) write(*resp, 4); // TODO: construct and log a message - cout << name << " caught an error: " << strerror_r(errCode, buf, 80) << endl; + syslog(LOG_ERR, "%s caught an error: %s.",name,strerror_r(errCode, buf, 80)); } uint PosixTask::getRemainingLength() @@ -186,7 +187,7 @@ void PosixTask::consumeMsg() while (remainingLengthInStream > 0) { - cout << "ERROR: eating data" << endl; + syslog(LOG_ERR, "ERROR: eating data."); err = ::recv(sock, buf, min(remainingLengthInStream, 1024), 0); if (err <= 0) { remainingLengthInStream = 0; diff --git a/src/ProcessTask.cpp b/src/ProcessTask.cpp index d11b145bf..57a6de906 100644 --- a/src/ProcessTask.cpp +++ b/src/ProcessTask.cpp @@ -4,6 +4,7 @@ #include #include "messageFormat.h" #include +#include #include #include "AppendTask.h" @@ -39,7 +40,7 @@ void ProcessTask::handleError(int saved_errno) SessionManager::get()->socketError(sock); returnedSock = true; char buf[80]; - cout << "ProcessTask: got an error during a socket read: " << strerror_r(saved_errno, buf, 80) << endl; + syslog(LOG_ERR, "ProcessTask: got an error during a socket read: %s.",strerror_r(saved_errno, buf, 80)); } void ProcessTask::operator()() diff --git a/src/ReadTask.cpp b/src/ReadTask.cpp index 9336f5447..e1d5b9ffb 100644 --- a/src/ReadTask.cpp +++ b/src/ReadTask.cpp @@ -41,7 +41,7 @@ bool ReadTask::run() read_cmd *cmd = (read_cmd *) buf; #ifdef SM_TRACE - cout << "read " << cmd->filename << " count " << cmd->count << " offset " << cmd->offset << endl; + syslog(LOG_DEBUG, "read %s count %i offset %i.",cmd->filename,cmd->count,cmd->offset); #endif // read from IOC, write to the socket diff --git a/src/SessionManager.cpp b/src/SessionManager.cpp old mode 100644 new mode 100755 index bcbe3fc21..4ee23f88f --- a/src/SessionManager.cpp +++ b/src/SessionManager.cpp @@ -12,6 +12,7 @@ #include #include #include +#include using namespace std; #include @@ -32,7 +33,7 @@ namespace storagemanager SessionManager::SessionManager() { - crp = ClientRequestProcessor::get(); + crp = ClientRequestProcessor::get(); } SessionManager::~SessionManager() @@ -60,25 +61,25 @@ int SessionManager::start() int current_size = 0; bool running = true; - printf("SessionManager starting...\n"); + syslog(LOG_INFO, "SessionManager starting..."); if (pipe(socketCtrl)==-1) { - perror("Pipe Failed" ); + syslog(LOG_CRIT, "Pipe Failed: %s", strerror(errno)); return 1; } listenSockfd = ::socket(AF_UNIX, SOCK_STREAM, 0); if (listenSockfd < 0) { - perror("socket() failed"); + syslog(LOG_CRIT, "socket() failed: %s", strerror(errno)); return -1; } rc = ::setsockopt(listenSockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)); if (rc < 0) { - perror("setsockopt() failed"); + syslog(LOG_CRIT, "setsockopt() failed: %s", strerror(errno)); close(listenSockfd); return -1; } @@ -86,7 +87,7 @@ int SessionManager::start() rc = ::ioctl(listenSockfd, FIONBIO, (char *)&on); if (rc < 0) { - perror("ioctl() failed"); + syslog(LOG_CRIT, "ioctl() failed: %s", strerror(errno)); close(listenSockfd); return -1; } @@ -97,7 +98,7 @@ int SessionManager::start() rc = ::bind(listenSockfd,(struct sockaddr *)&addr, sizeof(addr)); if (rc < 0) { - perror("bind() failed"); + syslog(LOG_CRIT, "bind() failed: %s", strerror(errno)); close(listenSockfd); return -1; } @@ -105,7 +106,7 @@ int SessionManager::start() rc = ::listen(listenSockfd, 32); if (rc < 0) { - perror("listen() failed"); + syslog(LOG_CRIT, "listen() failed: %s", strerror(errno)); close(listenSockfd); return -1; } @@ -117,17 +118,18 @@ int SessionManager::start() fds[1].events = POLLIN; nfds = 2; - printf("SessionManager waiting for sockets....\n"); + syslog(LOG_INFO, "SessionManager waiting for sockets."); while (running) { try { //if (current_size != nfds) + //syslog(LOG_DEBUG, "polling %i fds %i", nfds,fds); //cout << "polling " << nfds << " fds" << endl; rc = ::poll(fds, nfds, pollTimeout); if (rc < 0) { - perror("poll() failed"); + syslog(LOG_CRIT, "poll() failed: %s", strerror(errno)); break; } current_size = nfds; @@ -137,16 +139,16 @@ int SessionManager::start() if(fds[socketIncr].revents == 0) continue; //if (socketIncr >= 2) - //cout << "got event on fd " << fds[socketIncr].fd << " index is " << socketIncr << endl; + //syslog(LOG_DEBUG, "got event on fd %i index is %i", fds[socketIncr].fd,socketIncr); if(fds[socketIncr].revents != POLLIN) { - //printf("Error! revents = %d\n", fds[socketIncr].revents); + //syslog(LOG_DEBUG, "Error! revents = %d", fds[socketIncr].revents,); close(fds[socketIncr].fd); fds[socketIncr].fd = -1; } if (fds[socketIncr].fd == listenSockfd) { - //printf(" Listening socket is readable\n"); + //syslog(LOG_DEBUG, "Listening socket is readable"); incomingSockfd = 0; while (incomingSockfd != -1) { @@ -158,12 +160,12 @@ int SessionManager::start() { if (errno != EWOULDBLOCK) { - perror("accept() failed"); + syslog(LOG_CRIT, "accept() failed: %s", strerror(errno)); running = false; } break; } - //printf(" New incoming connection - %d\n", incomingSockfd); + //syslog(LOG_DEBUG, "New incoming connection - %d",incomingSockfd); fds[nfds].fd = incomingSockfd; fds[nfds].events = POLLIN; nfds++; @@ -171,7 +173,7 @@ int SessionManager::start() } else if (fds[socketIncr].fd == socketCtrl[0]) { - //printf(" SocketControl is readable\n"); + //syslog(LOG_DEBUG, "SocketControl is readable"); uint8_t ctrlCode; int len,socket; @@ -192,7 +194,7 @@ int SessionManager::start() { if(fds[i].fd == socket) { - //cout << "returned socket " << fds[i].fd << " at index " << i << endl; + //syslog(LOG_DEBUG, "returned socket %i at index %i", fds[i].fd,i); fds[i].events = POLLIN; break; } @@ -220,8 +222,7 @@ int SessionManager::start() } else { - //printf(" socketIncr %d -- Descriptor %d is readable\n", socketIncr,fds[socketIncr].fd); - + //syslog(LOG_DEBUG, "socketIncr %d -- Descriptor %d is readable",socketIncr,fds[socketIncr].fd); bool closeConn = false; char recv_buffer[8192]; uint recvMsgLength = 0; @@ -232,7 +233,7 @@ int SessionManager::start() struct timespec ts; ts.tv_sec = 0; ts.tv_nsec = 100000000; // .1 sec - //cout << "reading from fd " << fds[socketIncr].fd << " index is " << socketIncr << endl; + //syslog(LOG_DEBUG, "reading from fd %i index is %i", fds[socketIncr].fd,socketIncr); if (sockState.find(fds[socketIncr].fd) != sockState.end()) { SockState &state = sockState[fds[socketIncr].fd]; @@ -247,7 +248,6 @@ int SessionManager::start() { if (errno != EWOULDBLOCK) { - //perror("recv() failed"); closeConn = true; break; } @@ -260,7 +260,7 @@ int SessionManager::start() sockState[fds[socketIncr].fd] = state; break; } - //cout << "recv got " << peakLength << " bytes" << endl; + //syslog(LOG_DEBUG, "recv got %i bytes", peakLength); endOfData = remainingBytes + peakLength; if (endOfData < SM_HEADER_LEN) { @@ -276,12 +276,12 @@ int SessionManager::start() { if (*((uint *) &recv_buffer[i]) == SM_MSG_START) { - //printf("Received SM_MSG_START\n"); + //syslog(LOG_DEBUG, "Received SM_MSG_START"); //found it set msgLength and recvMsgStart offset of SM_MSG_START recvMsgLength = *((uint *) &recv_buffer[i+4]); - //cout << "got length = " << recvMsgLength << endl; + //syslog(LOG_DEBUG, "got length = %i", recvMsgLength); recvMsgStart = i + SM_HEADER_LEN; - //printf(" recvMsgLength %d recvMsgStart %d endofData %d\n", recvMsgLength,recvMsgStart,endOfData); + //syslog(LOG_DEBUG, "recvMsgLength %d recvMsgStart %d endofData %d", recvMsgLength,recvMsgStart,endOfData); // if >= endOfData then the start of the message data is the beginning of next message if (recvMsgStart >= endOfData) recvMsgStart = 0; @@ -292,7 +292,7 @@ int SessionManager::start() // didn't find SM_MSG_START in this message consume the data and loop back through on next message if (recvMsgLength == 0) { - printf("No SM_MSG_START\n"); + //syslog(LOG_DEBUG, "No SM_MSG_START"); len = ::read(fds[socketIncr].fd, &recv_buffer[remainingBytes], peakLength); assert(len == peakLength); // we know the msg header isn't in position [0, endOfData - i), so throw that out @@ -307,7 +307,7 @@ int SessionManager::start() //remove the junk in front of the message if (recvMsgStart > 0) { - //printf("SM_MSG_START data is here\n"); + //syslog(LOG_DEBUG, "SM_MSG_START data is here"); // how many to consume here... // recvMsgStart is the position in the buffer // peakLength is the amount peeked this time @@ -316,7 +316,7 @@ int SessionManager::start() } else { - //printf("SM_MSG_START data is next message\n"); + //syslog(LOG_DEBUG, "SM_MSG_START data is next message"); len = ::read(fds[socketIncr].fd, &recv_buffer[remainingBytes], peakLength); } //Disable polling on this socket @@ -327,7 +327,7 @@ int SessionManager::start() /* //Doing this to work with cloudio_component_test len = ::read(fds[socketIncr].fd, out, recvMsgLength); - printf("Read %d bytes.\n",len); + syslog(LOG_DEBUG, "Read %d bytes.",len); //Debug test lets send a reponse back uint32_t response[4] = { storagemanager::SM_MSG_START, 8, (uint32_t ) -1, EINVAL }; len = ::send(fds[socketIncr].fd, response, 16, 0); @@ -356,18 +356,8 @@ int SessionManager::start() { if (fds[i].fd == -1) { - // this should be the same if (i < nfds - 1) memmove(&fds[i], &fds[i+1], sizeof(struct pollfd) * nfds); - /* - for (int j = i; j < nfds-1; j++) - { - - fds[j].fd = fds[j + 1].fd; - fds[j].events = fds[j + 1].events; - fds[j].revents = fds[j + 1].revents; - } - */ i--; nfds--; } @@ -397,7 +387,7 @@ void SessionManager::returnSocket(int socket) void SessionManager::socketError(int socket) { boost::mutex::scoped_lock s(ctrlMutex); - cout << " ****** socket error!" << endl; + syslog(LOG_CRIT, " ****** socket error!"); int err; uint8_t ctrlCode = REMOVEFD; err = ::write(socketCtrl[1], &ctrlCode, 1); diff --git a/src/StatTask.cpp b/src/StatTask.cpp index 6cb818d47..04cc6c185 100644 --- a/src/StatTask.cpp +++ b/src/StatTask.cpp @@ -43,7 +43,7 @@ bool StatTask::run() sm_response *resp = (sm_response *) buf; #ifdef SM_TRACE - cout << "stat " << cmd->filename << endl; + syslog(LOG_DEBUG, "stat %s.",cmd->filename); #endif int err = ioc->stat(cmd->filename, (struct stat *) resp->payload); diff --git a/src/TruncateTask.cpp b/src/TruncateTask.cpp index 48a5a1bb0..f57738307 100644 --- a/src/TruncateTask.cpp +++ b/src/TruncateTask.cpp @@ -38,7 +38,7 @@ bool TruncateTask::run() truncate_cmd *cmd = (truncate_cmd *) buf; #ifdef SM_TRACE - cout << "truncate " << cmd->filename << " newlength " << cmd->length << endl; + syslog(LOG_DEBUG, "truncate %s newlength %i.",cmd->filename,cmd->length); #endif int err = ioc->truncate(cmd->filename, cmd->length); diff --git a/src/UnlinkTask.cpp b/src/UnlinkTask.cpp index 259cc15e4..c994a2f2e 100644 --- a/src/UnlinkTask.cpp +++ b/src/UnlinkTask.cpp @@ -39,7 +39,7 @@ bool UnlinkTask::run() unlink_cmd *cmd = (unlink_cmd *) buf; #ifdef SM_TRACE - cout << "unlink " << cmd->filename << endl; + syslog(LOG_DEBUG, "unlink %s.",cmd->filename); #endif int err = ioc->unlink(cmd->filename); diff --git a/src/WriteTask.cpp b/src/WriteTask.cpp index dc09d8147..47a4246e4 100644 --- a/src/WriteTask.cpp +++ b/src/WriteTask.cpp @@ -44,7 +44,7 @@ bool WriteTask::run() check_error("WriteTask read", false); #ifdef SM_TRACE - cout << "write filename " << cmd->filename << " offset " << cmd->offset << " count " << cmd->count << endl; + syslog(LOG_DEBUG, "write filename %s offset %i count %i.",cmd->filename,cmd->offset,cmd->count); #endif size_t readCount = 0, writeCount = 0; diff --git a/src/main.cpp b/src/main.cpp old mode 100644 new mode 100755 index eae58342f..6f614fbd4 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,8 @@ #include #include #include +#include + using namespace std; #include "SessionManager.h" @@ -22,10 +24,20 @@ int main(int argc, char** argv) sigaction(SIGPIPE, &sa, NULL); int ret = 0; + + //TODO: make this configurable + setlogmask (LOG_UPTO (LOG_DEBUG)); + + openlog ("StorageManager", LOG_PID | LOG_NDELAY, LOG_LOCAL2); + + syslog(LOG_NOTICE, "StorageManager started."); + SessionManager* sm = SessionManager::get(); ret = sm->start(); + closelog (); + return ret; }