From ea7a7729ec58a124ad87c94c0c1a0cfafdba5178 Mon Sep 17 00:00:00 2001
From: Daniele Kruse <dkruse@cern.ch>
Date: Thu, 16 Jun 2016 17:12:09 +0200
Subject: [PATCH] Plugged in the logging system into the cta frontend

---
 common/log/SyslogLogger.cpp        | 56 ++++++++++++++++--------------
 xroot_plugins/XrdCtaFile.cpp       |  3 ++
 xroot_plugins/XrdCtaFile.hpp       |  8 ++++-
 xroot_plugins/XrdCtaFilesystem.cpp | 10 +++++-
 xroot_plugins/XrdCtaFilesystem.hpp |  6 ++++
 5 files changed, 54 insertions(+), 29 deletions(-)

diff --git a/common/log/SyslogLogger.cpp b/common/log/SyslogLogger.cpp
index 24d7f7509f..a5cdac13ce 100644
--- a/common/log/SyslogLogger.cpp
+++ b/common/log/SyslogLogger.cpp
@@ -46,15 +46,15 @@
 cta::log::SyslogLogger::SyslogLogger(const std::string &socketName,
   const std::string &programName, const int logMask):
   Logger(programName, logMask),
-  m_socketName(socketName),
+  m_socketName(_PATH_LOG),
   m_logFile(-1) {
   struct stat fileStatus;
   bzero(&fileStatus, sizeof(fileStatus));
-  if(stat(socketName.c_str(), &fileStatus)) {
+  if(stat(m_socketName.c_str(), &fileStatus)) {
     const std::string errMsg = cta::utils::errnoToString(errno);
     cta::exception::Exception ex;
     ex.getMessage() << "Failed to instantiate syslog logger: Failed to stat"
-      " socket \"" << socketName << "\"" ": " << errMsg;
+      " socket \"" << m_socketName << "\"" ": " << errMsg;
     throw ex;
   }
 }
@@ -147,28 +147,30 @@ void cta::log::SyslogLogger::reducedSyslog(const std::string& msg) {
   // Truncate the log message if it exceeds the permitted maximum
   std::string truncatedMsg = msg.substr(0, m_maxMsgLen);
 
-  int send_flags = MSG_NOSIGNAL;
-
-  // enter critical section
-  threading::MutexLocker lock(m_mutex);
-
-  // Try to connect if not already connected
-  openLog();
-
-  // If connected
-  if(-1 != m_logFile) {
-    // If sending the log message fails then try to reopen the syslog
-    // connection and try again
-    if(0 > send(m_logFile, truncatedMsg.c_str(), truncatedMsg.length(), send_flags)) {
-      closeLog();
-      openLog();
-      if (-1 != m_logFile) {
-        // If the second attempt to send the log message fails then give up and
-        // attempt re-open next time
-        if(0 > send(m_logFile, truncatedMsg.c_str(), truncatedMsg.length(), send_flags)) {
-          closeLog();
-        }
-      }
-    }
-  }
+//  int send_flags = MSG_NOSIGNAL;
+//
+//  // enter critical section
+//  threading::MutexLocker lock(m_mutex);
+//
+//  // Try to connect if not already connected
+//  openLog();
+//
+//  // If connected
+//  if(-1 != m_logFile) {
+//    // If sending the log message fails then try to reopen the syslog
+//    // connection and try again
+//    if(0 > send(m_logFile, truncatedMsg.c_str(), truncatedMsg.length(), send_flags)) {
+//      closeLog();
+//      openLog();
+//      if (-1 != m_logFile) {
+//        // If the second attempt to send the log message fails then give up and
+//        // attempt re-open next time
+//        if(0 > send(m_logFile, truncatedMsg.c_str(), truncatedMsg.length(), send_flags)) {
+//          closeLog();
+//        }
+//      }
+//    }
+//  }
+  
+  syslog(LOG_LOCAL3|LOG_INFO, truncatedMsg.c_str());
 }
diff --git a/xroot_plugins/XrdCtaFile.cpp b/xroot_plugins/XrdCtaFile.cpp
index 0cf9eabeee..03792bdc52 100644
--- a/xroot_plugins/XrdCtaFile.cpp
+++ b/xroot_plugins/XrdCtaFile.cpp
@@ -315,11 +315,13 @@ int XrdCtaFile::getCXinfo(char cxtype[4], int &cxrsz) {
 XrdCtaFile::XrdCtaFile(
   cta::catalogue::Catalogue *catalogue,
   cta::Scheduler *scheduler,
+  cta::log::Logger *log,
   const char *user,
   int MonID):
   error(user, MonID),
   m_catalogue(catalogue),
   m_scheduler(scheduler),
+  m_log(log),
   m_data(""),
   m_rc(cta::common::dataStructures::FrontendReturnCode::ok) {  
 }
@@ -2384,6 +2386,7 @@ int XrdCtaFile::xCom_liststorageclass(const std::vector<std::string> &tokens, co
 // getGenericHelp
 //------------------------------------------------------------------------------
 std::string XrdCtaFile::getGenericHelp(const std::string &programName) const {
+  
   std::stringstream help;
   help << "CTA ADMIN commands:" << std::endl;
   help << "" << std::endl;
diff --git a/xroot_plugins/XrdCtaFile.hpp b/xroot_plugins/XrdCtaFile.hpp
index bcc1bfd4ca..b6902b5103 100644
--- a/xroot_plugins/XrdCtaFile.hpp
+++ b/xroot_plugins/XrdCtaFile.hpp
@@ -20,6 +20,7 @@
 
 #include "catalogue/Catalogue.hpp"
 #include "common/dataStructures/FrontendReturnCode.hpp"
+#include "common/log/SyslogLogger.hpp"
 #include "scheduler/Scheduler.hpp"
 
 #include "XrdSfs/XrdSfsInterface.hh"
@@ -55,7 +56,7 @@ public:
   virtual int sync(XrdSfsAio *aiop);
   virtual int truncate(XrdSfsFileOffset fsize);
   virtual int getCXinfo(char cxtype[4], int &cxrsz);
-  XrdCtaFile(cta::catalogue::Catalogue *catalogue, cta::Scheduler *scheduler, const char *user=0, int MonID=0);
+  XrdCtaFile(cta::catalogue::Catalogue *catalogue, cta::Scheduler *scheduler, cta::log::Logger *log, const char *user=0, int MonID=0);
   ~XrdCtaFile();
   
 protected:
@@ -70,6 +71,11 @@ protected:
    */
   cta::Scheduler *m_scheduler;
   
+  /**
+   * The scheduler object pointer
+   */
+  cta::log::Logger *m_log;
+  
   /**
    * This is the string holding the result of the command
    */
diff --git a/xroot_plugins/XrdCtaFilesystem.cpp b/xroot_plugins/XrdCtaFilesystem.cpp
index b9f9899554..482e5b530f 100644
--- a/xroot_plugins/XrdCtaFilesystem.cpp
+++ b/xroot_plugins/XrdCtaFilesystem.cpp
@@ -84,7 +84,7 @@ int XrdCtaFilesystem::FSctl(const int cmd, XrdSfsFSctl &args, XrdOucErrInfo &eIn
 //------------------------------------------------------------------------------
 XrdSfsFile * XrdCtaFilesystem::newFile(char *user, int MonID)
 {  
-  return new cta::xrootPlugins::XrdCtaFile(m_catalogue.get(), m_scheduler.get(), user, MonID);
+  return new cta::xrootPlugins::XrdCtaFile(m_catalogue.get(), m_scheduler.get(), m_log.get(), user, MonID);
 }
 
 //------------------------------------------------------------------------------
@@ -262,6 +262,14 @@ XrdCtaFilesystem::XrdCtaFilesystem():
   m_backendPopulator(*m_backend),
   m_scheddb(*m_backend, m_backendPopulator.getAgent()) {
   using namespace cta;
+  
+  // Try to instantiate the logging system API
+  try {
+    m_log.reset(new log::SyslogLogger(log::SOCKET_NAME, "cta-frontend", log::DEBUG));
+  } catch(exception::Exception &ex) {
+    throw cta::exception::Exception(std::string("Failed to instantiate object representing CTA logging system: ")+ex.getMessage().str());
+  }
+  
   const catalogue::DbLogin catalogueLogin = catalogue::DbLogin::parseFile("/etc/cta/cta_catalogue_db.conf");
   m_catalogue.reset(catalogue::CatalogueFactory::create(catalogueLogin));
 
diff --git a/xroot_plugins/XrdCtaFilesystem.hpp b/xroot_plugins/XrdCtaFilesystem.hpp
index 1a1d462a6b..393d01ea7b 100644
--- a/xroot_plugins/XrdCtaFilesystem.hpp
+++ b/xroot_plugins/XrdCtaFilesystem.hpp
@@ -20,6 +20,7 @@
 
 #include "catalogue/Catalogue.hpp"
 #include "common/Configuration.hpp"
+#include "common/log/SyslogLogger.hpp"
 #include "nameserver/mockNS/MockNameServer.hpp"
 #include "objectstore/BackendPopulator.hpp"
 #include "objectstore/BackendVFS.hpp"
@@ -94,6 +95,11 @@ protected:
    * The scheduler.
    */
   std::unique_ptr<cta::Scheduler> m_scheduler;
+  
+  /**
+   * The logger.
+   */
+  std::unique_ptr<log::Logger> m_log;
 }; // XrdCtaFilesystem
 
 }}
-- 
GitLab