Convert logging to log4shib via compile time switch.
[shibboleth/cpp-sp.git] / shibsp / remoting / impl / SocketListener.cpp
index f32fc45..66ebeb8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2005 Internet2
+ *  Copyright 2001-2007 Internet2
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include <shibsp/SPConfig.h>
 #include <xmltooling/util/NDC.h>
 
-#ifdef HAVE_UNISTD_H
-# include <unistd.h>
+#ifndef WIN32
+# include <netinet/in.h>
 #endif
 
 using namespace shibsp;
 using namespace xmltooling;
-using namespace log4cpp;
 using namespace std;
 using xercesc::DOMElement;
 
@@ -54,9 +53,9 @@ namespace shibsp {
   
     private:
         SocketListener::ShibSocket connect();
-        
+       
+        Category& m_log; 
         const SocketListener* m_listener;
-        Category& m_log;
         auto_ptr<Mutex> m_lock;
         stack<SocketListener::ShibSocket> m_pool;
     };
@@ -155,7 +154,7 @@ void SocketPool::put(SocketListener::ShibSocket s)
 }
 
 SocketListener::SocketListener(const DOMElement* e) : log(&Category::getInstance(SHIBSP_LOGCAT".Listener")),
-    m_shutdown(NULL), m_child_lock(NULL), m_child_wait(NULL), m_socketpool(NULL), m_socket((ShibSocket)0)
+    m_socketpool(NULL), m_shutdown(NULL), m_child_lock(NULL), m_child_wait(NULL), m_socket((ShibSocket)0)
 {
     // Are we a client?
     if (SPConfig::getConfig().isEnabled(SPConfig::InProcess)) {
@@ -180,6 +179,7 @@ bool SocketListener::run(bool* shutdown)
 #ifdef _DEBUG
     NDC ndc("run");
 #endif
+    log->info("listener service starting");
 
     // Save flag to monitor for shutdown request.
     m_shutdown=shutdown;
@@ -252,7 +252,7 @@ DDF SocketListener::send(const DDF& in)
     NDC ndc("send");
 #endif
 
-    log->debug("sending message: %s", in.name());
+    log->debug("sending message (%s)", in.name() ? in.name() : "unnamed");
 
     // Serialize data for transmission.
     ostringstream os;
@@ -323,6 +323,7 @@ DDF SocketListener::send(const DDF& in)
         XMLToolingException* except=NULL;
         try { 
             except=XMLToolingException::fromString(out.string());
+            log->error("remoted message returned an error: %s", except->what());
         }
         catch (XMLToolingException& e) {
             log->error("caught XMLToolingException while building the XMLToolingException: %s", e.what());
@@ -446,8 +447,8 @@ bool ServerThread::job()
 {
     Category& log = Category::getInstance("shibd.Listener");
 
-    DDF out;
-    DDFJanitor jout(out);
+    bool incomingError = true;  // set false once incoming message is received
+    ostringstream sink;
 #ifdef WIN32
     u_long len;
 #else
@@ -479,30 +480,41 @@ bool ServerThread::job()
         DDFJanitor jin(in);
         is >> in;
 
+        log.debug("dispatching message (%s)", in.name() ? in.name() : "unnamed");
+
+        incomingError = false;
+
         // Dispatch the message.
-        out=m_listener->receive(in);
+        m_listener->receive(in, sink);
     }
     catch (XMLToolingException& e) {
-        log.error("error processing incoming message: %s", e.what());
-        out=DDF("exception").string(e.toString().c_str());
+        if (incomingError)
+            log.error("error processing incoming message: %s", e.what());
+        DDF out=DDF("exception").string(e.toString().c_str());
+        DDFJanitor jout(out);
+        sink << out;
     }
     catch (exception& e) {
-        log.error("error processing incoming message: %s", e.what());
+        if (incomingError)
+            log.error("error processing incoming message: %s", e.what());
         ListenerException ex(e.what());
-        out=DDF("exception").string(ex.toString().c_str());
+        DDF out=DDF("exception").string(ex.toString().c_str());
+        DDFJanitor jout(out);
+        sink << out;
     }
 #ifndef _DEBUG
     catch (...) {
-        log.error("unexpected error processing incoming message");
+        if (incomingError)
+            log.error("unexpected error processing incoming message");
         ListenerException ex("An unexpected error occurred while processing an incoming message.");
-        out=DDF("exception").string(ex.toString().c_str());
+        DDF out=DDF("exception").string(ex.toString().c_str());
+        DDFJanitor jout(out);
+        sink << out;
     }
 #endif
     
     // Return whatever's available.
-    ostringstream xmlout;
-    xmlout << out;
-    string response(xmlout.str());
+    string response(sink.str());
     int outlen = response.length();
     len = htonl(outlen);
     if (m_listener->send(m_sock,(char*)&len,sizeof(len)) != sizeof(len)) {