From 98e7c63e7d43453b5188d7fffbebf382258204c7 Mon Sep 17 00:00:00 2001
From: jkaeber <jkaeber@wiback.org>
Date: Thu, 6 Mar 2008 14:25:02 +0000
Subject: [PATCH] Streamlined SystemException definition and usage

---
 Socket/CommunicationPolicy.cc                 |  2 +-
 Socket/NetdeviceController.cc                 |  8 ++++----
 Socket/Protocols/DVB/DVBDemuxHandles.cc       | 10 +++++-----
 .../Protocols/DVB/DVBDemuxSocketProtocol.cc   |  9 +++++----
 Socket/Protocols/DVB/DVBFrontendHandle.cc     |  4 ++--
 .../INet/ConnectedUDPSocketHandle.cc          |  4 ++--
 .../Protocols/INet/MulticastSocketProtocol.cc |  2 +-
 Socket/Protocols/Raw/PacketSocketHandle.cc    |  4 ++--
 Socket/Protocols/Raw/TunTapSocketHandle.cc    |  4 ++--
 .../UN/ConnectedUNDatagramSocketHandle.cc     |  2 +-
 Socket/Protocols/UN/UNDatagramSocketHandle.cc |  2 +-
 Socket/Protocols/UN/UNSocketProtocol.cc       |  4 ++--
 Socket/SocketProtocol.cc                      |  4 ++--
 Utils/Daemon/Daemon.cc                        | 20 ++++++++++++-------
 Utils/Exception.cc                            |  2 +-
 Utils/Exception.test.cc                       |  3 +--
 16 files changed, 45 insertions(+), 39 deletions(-)

diff --git a/Socket/CommunicationPolicy.cc b/Socket/CommunicationPolicy.cc
index aff4c6693..d175b58ec 100644
--- a/Socket/CommunicationPolicy.cc
+++ b/Socket/CommunicationPolicy.cc
@@ -58,7 +58,7 @@ prefix_ int senf::ConnectedCommunicationPolicy::do_accept(FileHandle handle,
             case EINTR:
                 break;
             default:
-                throw SystemException();
+                throw SystemException("ConnectedCommunicationPolicy::do_accept failed.");
             }
     } while (rv<0);
     return rv;
diff --git a/Socket/NetdeviceController.cc b/Socket/NetdeviceController.cc
index fc43cc1dd..7f40c074c 100644
--- a/Socket/NetdeviceController.cc
+++ b/Socket/NetdeviceController.cc
@@ -101,7 +101,7 @@ prefix_ void senf::NetdeviceController::openSocket()
 {
     sockfd_ = ::socket( PF_INET, SOCK_DGRAM, 0);
     if ( sockfd_ < 0)
-        throw SystemException( "Could not open socket for NetdeviceController");
+        throw SystemException("Could not open socket for NetdeviceController.");
 }
 
 prefix_ void senf::NetdeviceController::ifrName(ifreq& ifr)
@@ -110,15 +110,15 @@ prefix_ void senf::NetdeviceController::ifrName(ifreq& ifr)
     ::memset( &ifr, 0, sizeof(ifr));
     ifr.ifr_ifindex = ifindex_;
     if ( ::ioctl( sockfd_, SIOCGIFNAME, &ifr ) < 0 )
-        throw SystemException( 
-        		"NetdeviceController: Could not discover the name of the interface with index ") << ifindex_;
+        throw SystemException("NetdeviceController")
+        << " could not discover the name of the interface with index " << ifindex_ << ".";
 }
 
 prefix_ void senf::NetdeviceController::doIoctl(ifreq& ifr, int request)
     const
 {
     if ( ::ioctl( sockfd_, request, &ifr ) < 0 )
-        throw SystemException();
+        throw SystemException("NetdeviceController::doIoctl failed.");
 }
 
 ///////////////////////////////cc.e////////////////////////////////////////
diff --git a/Socket/Protocols/DVB/DVBDemuxHandles.cc b/Socket/Protocols/DVB/DVBDemuxHandles.cc
index a8f33a726..788069fde 100644
--- a/Socket/Protocols/DVB/DVBDemuxHandles.cc
+++ b/Socket/Protocols/DVB/DVBDemuxHandles.cc
@@ -47,7 +47,7 @@ prefix_ void senf::DVBDemuxSectionSocketProtocol::init_client(unsigned short ada
             "/dev/dvb/adapter%d/demux%d") % adapter % device);
     int f = open(devDemux.c_str(), O_RDONLY | O_NONBLOCK);
     if (f < 0){
-        throw SystemException( "Could not open demux device of DVB adapter ") << devDemux;
+        throw SystemException("Could not open demux device of DVB adapter ") << devDemux << ".";
     }
     fd(f);
 }
@@ -62,7 +62,7 @@ prefix_ void senf::DVBDemuxSectionSocketProtocol::setSectionFilter(struct dmx_sc
     const
 {
     if (::ioctl(fd(), DMX_SET_FILTER, filter) < 0)
-        throw SystemException("Could not set section filter of DVB adapter");
+        throw SystemException("Could not set section filter of DVB adapter.");
 }
 
 // ----------------------------------------------------------------
@@ -74,7 +74,7 @@ prefix_ void senf::DVBDemuxPESSocketProtocol::init_client(unsigned short adapter
             "/dev/dvb/adapter%d/demux%d") % adapter % device);
     int f = open(devDemux.c_str(), O_RDONLY | O_NONBLOCK);
     if (f < 0)
-        throw SystemException( "Could not open demux device of DVB adapter ") << devDemux;
+        throw SystemException( "Could not open demux device of DVB adapter ") << devDemux << ".";
     fd(f);
 }
 
@@ -88,7 +88,7 @@ prefix_ void senf::DVBDemuxPESSocketProtocol::setPESFilter(struct dmx_pes_filter
     const
 {
     if (::ioctl(fd(), DMX_SET_PES_FILTER, filter) < 0)
-        throw SystemException("Could not set PES filter of DVB adapter");
+        throw SystemException("Could not set PES filter of DVB adapter.");
 }
 
 // ----------------------------------------------------------------
@@ -100,7 +100,7 @@ prefix_ void senf::DVBDvrSocketProtocol::init_client(unsigned short adapter, uns
             "/dev/dvb/adapter%d/dvr%d") % adapter % device);
     int f = open(devDvr.c_str(), O_RDONLY | O_NONBLOCK);
     if (f < 0)
-        throw SystemException( "Could not open dvr device of DVB adapter ") << devDvr;
+        throw SystemException( "Could not open dvr device of DVB adapter ") << devDvr << ".";
     fd(f);
 }
 
diff --git a/Socket/Protocols/DVB/DVBDemuxSocketProtocol.cc b/Socket/Protocols/DVB/DVBDemuxSocketProtocol.cc
index beb974e2e..0059e3908 100644
--- a/Socket/Protocols/DVB/DVBDemuxSocketProtocol.cc
+++ b/Socket/Protocols/DVB/DVBDemuxSocketProtocol.cc
@@ -42,22 +42,23 @@ prefix_ void senf::DVBDemuxSocketProtocol::setBufferSize(unsigned long size)
     const
 {
     if (::ioctl(fd(), DMX_SET_BUFFER_SIZE, size) < 0)
-        throw SystemException(
-                "Could not set the size of the buffer on DVB adapter. requested size: ") << size;
+        throw SystemException()
+              << "Could not set the size of the buffer on DVB adapter. requested size: "
+              << size << ".";
 }
 
 prefix_ void senf::DVBDemuxSocketProtocol::startFiltering()
     const
 {
     if (::ioctl(fd(), DMX_START) < 0)
-        throw SystemException("Could not start filtering operation on DVB adapter");
+        throw SystemException("Could not start filtering operation on DVB adapter.");
 }
 
 prefix_ void senf::DVBDemuxSocketProtocol::stopFiltering()
     const
 {
     if (::ioctl(fd(), DMX_STOP) < 0)
-        throw SystemException("Could not stop filtering operation on DVB adapter");
+        throw SystemException("Could not stop filtering operation on DVB adapter.");
 }
 
 prefix_ bool senf::DVBDemuxSocketProtocol::eof()
diff --git a/Socket/Protocols/DVB/DVBFrontendHandle.cc b/Socket/Protocols/DVB/DVBFrontendHandle.cc
index 1e47c7a11..7933b717c 100644
--- a/Socket/Protocols/DVB/DVBFrontendHandle.cc
+++ b/Socket/Protocols/DVB/DVBFrontendHandle.cc
@@ -47,7 +47,7 @@ prefix_ void senf::DVBFrontendSocketProtocol::init_client(uint8_t adapter, boost
             "/dev/dvb/adapter%d/frontend%d") % adapter % device);
     int f = open(devFrontend.c_str(), O_RDONLY | O_NONBLOCK);
     if (f < 0)
-        throw SystemException("Could not open frontend device of DVB adapter ")<< devFrontend;
+        throw SystemException() << "Could not open frontend device of DVB adapter " << devFrontend << ".";
     
     fd(f);
 }
@@ -68,7 +68,7 @@ prefix_ void senf::DVBFrontendSocketProtocol::signalStrength(int16_t *strength)
     const
 {
     if (::ioctl(fd(), FE_READ_SIGNAL_STRENGTH, strength) < 0)
-        throw SystemException( "Could not get signal strength of DVB adapter");
+        throw SystemException( "Could not get signal strength of DVB adapter.");
 }
 
 ///////////////////////////////cc.e////////////////////////////////////////
diff --git a/Socket/Protocols/INet/ConnectedUDPSocketHandle.cc b/Socket/Protocols/INet/ConnectedUDPSocketHandle.cc
index 5e89b45f5..bc5432aeb 100644
--- a/Socket/Protocols/INet/ConnectedUDPSocketHandle.cc
+++ b/Socket/Protocols/INet/ConnectedUDPSocketHandle.cc
@@ -46,7 +46,7 @@ prefix_ void senf::ConnectedUDPv4SocketProtocol::init_client()
 {
     int sock = ::socket(PF_INET,SOCK_DGRAM,0);
     if (sock < 0)
-        throw SystemException();
+        throw SystemException("::socket(PF_INET,SOCK_DGRAM,0) failed.");
     fd(sock);
 }
 
@@ -66,7 +66,7 @@ prefix_ void senf::ConnectedUDPv6SocketProtocol::init_client()
 {
     int sock = ::socket(PF_INET6,SOCK_DGRAM,0);
     if (sock < 0)
-        throw SystemException();
+        throw SystemException("::socket(PF_INET6,SOCK_DGRAM,0) failed.");
     fd(sock);
 }
 
diff --git a/Socket/Protocols/INet/MulticastSocketProtocol.cc b/Socket/Protocols/INet/MulticastSocketProtocol.cc
index ba30fcee9..1712748c2 100644
--- a/Socket/Protocols/INet/MulticastSocketProtocol.cc
+++ b/Socket/Protocols/INet/MulticastSocketProtocol.cc
@@ -116,7 +116,7 @@ prefix_ void senf::INet4MulticastSocketProtocol::mcAddMembership(INet4Address co
     mreqn.imr_address.s_addr = htons(INADDR_ANY);
     mreqn.imr_ifindex = 0;
     if (::setsockopt(fd(),SOL_IP,IP_ADD_MEMBERSHIP,&mreqn,sizeof(mreqn)) < 0)
-        throw SystemException("::setsockopt(IP_ADD_MEMBERSHIP");
+        throw SystemException("::setsockopt(IP_ADD_MEMBERSHIP)");
 }
 
 prefix_ void senf::INet4MulticastSocketProtocol::mcAddMembership(INet4Address const & mcAddr,
diff --git a/Socket/Protocols/Raw/PacketSocketHandle.cc b/Socket/Protocols/Raw/PacketSocketHandle.cc
index 54345f123..3a7eaade7 100644
--- a/Socket/Protocols/Raw/PacketSocketHandle.cc
+++ b/Socket/Protocols/Raw/PacketSocketHandle.cc
@@ -50,7 +50,7 @@ prefix_ void senf::PacketSocketProtocol::init_client(SocketType type, int protoc
         protocol = ETH_P_ALL;
     int sock = ::socket(PF_PACKET, socktype, htons(protocol));
     if (sock < 0)
-        throw SystemException();
+        throw SystemException("::socket(...) failed.");
     fd(sock);
 }
 
@@ -61,7 +61,7 @@ prefix_ unsigned senf::PacketSocketProtocol::available()
         return 0;
     ssize_t l = ::recv(fd(),0,0,MSG_PEEK | MSG_TRUNC);
     if (l < 0)
-        throw SystemException();
+        throw SystemException("::recv(socket_fd) failed.");
     return l;
 }
 
diff --git a/Socket/Protocols/Raw/TunTapSocketHandle.cc b/Socket/Protocols/Raw/TunTapSocketHandle.cc
index 6734b8a53..cd68e0630 100644
--- a/Socket/Protocols/Raw/TunTapSocketHandle.cc
+++ b/Socket/Protocols/Raw/TunTapSocketHandle.cc
@@ -50,7 +50,7 @@ prefix_ void senf::TapSocketProtocol::init_client(std::string const & interface_
 {
     int f;
     if ( (f = ::open("/dev/net/tun", O_RDWR)) < 0 )
-        throw SystemException( "Could not open tap control device: /dev/net/tun");
+        throw SystemException( "Could not open tap control device: /dev/net/tun.");
     struct ifreq ifr;
     ::memset( &ifr, 0, sizeof(ifr));
     ifr.ifr_flags = IFF_TAP;
@@ -58,7 +58,7 @@ prefix_ void senf::TapSocketProtocol::init_client(std::string const & interface_
         ifr.ifr_flags |= IFF_NO_PI;
     interface_name.copy( ifr.ifr_name, IFNAMSIZ);
     if (::ioctl(f, TUNSETIFF, (void *) &ifr) < 0 )
-        throw SystemException( "Could not create tap device: ") << ifr.ifr_name;
+        throw SystemException( "Could not create tap device: ") << ifr.ifr_name << ".";
     fd(f);
 }
 
diff --git a/Socket/Protocols/UN/ConnectedUNDatagramSocketHandle.cc b/Socket/Protocols/UN/ConnectedUNDatagramSocketHandle.cc
index 9e47881ce..2a9a4db50 100644
--- a/Socket/Protocols/UN/ConnectedUNDatagramSocketHandle.cc
+++ b/Socket/Protocols/UN/ConnectedUNDatagramSocketHandle.cc
@@ -40,7 +40,7 @@ prefix_ void senf::ConnectedUNDatagramSocketProtocol::init_client() const
 {
     int sock = ::socket(PF_UNIX,SOCK_DGRAM,0);
     if (sock < 0)
-        throw SystemException( "Could not create socket(PF_UNIX,SOCK_DGRAM,0)" );
+        throw SystemException( "Could not create socket(PF_UNIX,SOCK_DGRAM,0)." );
    fd(sock);
 }
 
diff --git a/Socket/Protocols/UN/UNDatagramSocketHandle.cc b/Socket/Protocols/UN/UNDatagramSocketHandle.cc
index d1b66bd4f..a84d8ec62 100644
--- a/Socket/Protocols/UN/UNDatagramSocketHandle.cc
+++ b/Socket/Protocols/UN/UNDatagramSocketHandle.cc
@@ -41,7 +41,7 @@ prefix_ void senf::UNDatagramSocketProtocol::init_client() const
 {
     int sock = ::socket(PF_UNIX,SOCK_DGRAM,0);
     if (sock < 0)
-        throw SystemException( "Could not create socket(PF_UNIX,SOCK_DGRAM,0)" );
+        throw SystemException( "Could not create socket(PF_UNIX,SOCK_DGRAM,0)." );
     fd(sock);
 }
 
diff --git a/Socket/Protocols/UN/UNSocketProtocol.cc b/Socket/Protocols/UN/UNSocketProtocol.cc
index 0c58441f1..2e9404dc6 100644
--- a/Socket/Protocols/UN/UNSocketProtocol.cc
+++ b/Socket/Protocols/UN/UNSocketProtocol.cc
@@ -42,7 +42,7 @@ prefix_ unsigned senf::UNSocketProtocol::available()
 {
     int n;
     if (::ioctl(fd(),SIOCINQ,&n) < 0)
-        throw SystemException("Could not call available() on UNSocket");
+        throw SystemException("Could not call available() on UNSocket.");
     return n;
 }
 
@@ -77,7 +77,7 @@ prefix_ void senf::UNSocketProtocol::check_and_unlink()
         ::unlink(una.path().c_str());
     }
     catch (SystemException & e) {
-        SENF_LOG(("UNSocketProtocol::check_and_unlink() failed; " << e.errorString() ));
+        SENF_LOG(("UNSocketProtocol::check_and_unlink() failed; [" << e.errorString() << "]."));
     }
 }
     
diff --git a/Socket/SocketProtocol.cc b/Socket/SocketProtocol.cc
index 672f21fd8..f2fe71237 100644
--- a/Socket/SocketProtocol.cc
+++ b/Socket/SocketProtocol.cc
@@ -38,9 +38,9 @@ prefix_ void senf::SocketProtocol::close()
     const
 {
     if (::shutdown(body().fd(),SHUT_RDWR) < 0)
-        throw SystemException();
+        throw SystemException("::shutdown(socket_fd)");
     if (::close(body().fd()) < 0)
-        throw SystemException();
+        throw SystemException("::close(socket_fd)");
 }
 
 prefix_ void senf::SocketProtocol::terminate()
diff --git a/Utils/Daemon/Daemon.cc b/Utils/Daemon/Daemon.cc
index 152186768..f0be324b5 100644
--- a/Utils/Daemon/Daemon.cc
+++ b/Utils/Daemon/Daemon.cc
@@ -98,7 +98,8 @@ prefix_ void senf::Daemon::openLog()
     if (! stdoutLog_.empty()) {
         fd = ::open(stdoutLog_.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0666);
         if (fd < 0)
-            throw SystemException("::open()");
+            throw SystemException()
+                  << " Could not open \"" << stdoutLog_ << "\" for redirecting stdout.";
         stdout_ = fd;
     }
     if (stderrLog_ == stdoutLog_)
@@ -106,7 +107,8 @@ prefix_ void senf::Daemon::openLog()
     else if (! stderrLog_.empty()) {
         fd = ::open(stdoutLog_.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0666);
         if (fd < 0)
-            throw SystemException("::open()");
+            throw SystemException()
+                  << " Could not open \"" << stderrLog_ << "\" for redirecting stderr.";
         stderr_ = fd;
     }
 }
@@ -323,7 +325,7 @@ prefix_ bool senf::Daemon::pidfileCreate()
     // was some race condition, probably over NFS.
 
     std::string tempname;
-    boost::format linkErrorFormat("; could not link \"%1%\" to \"%2%\".");
+    boost::format linkErrorFormat(" Could not link \"%1%\" to \"%2%\".");
 
     {
         char hostname[HOST_NAME_MAX+1];
@@ -337,13 +339,18 @@ prefix_ bool senf::Daemon::pidfileCreate()
     while (1) {
         {
             std::ofstream pidf (tempname.c_str());
+            if (! pidf)
+                throw SystemException()
+                      << " Could not open pidfile \"" << tempname << "\" for output.";
             pidf << ::getpid() << std::endl;
+            if (! pidf)
+                throw SystemException()
+                      << " Could not write to pidfile \"" << tempname << "\".";
         }
 
         if (::link(tempname.c_str(), pidfile_.c_str()) < 0) {
             if (errno != EEXIST) 
-                throw SystemException("::link()")
-                    << linkErrorFormat % pidfile_.c_str() % tempname.c_str();
+                throw SystemException() << linkErrorFormat % pidfile_ % tempname;
         }
         else {
             struct ::stat s;
@@ -374,8 +381,7 @@ prefix_ bool senf::Daemon::pidfileCreate()
         LIBC_CALL( ::unlink, (tempname.c_str() ));
         if (::link(pidfile_.c_str(), tempname.c_str()) < 0) {
             if (errno != ENOENT)
-                throw SystemException("::link()")
-                    << linkErrorFormat % tempname.c_str() % pidfile_.c_str();
+                throw SystemException() << linkErrorFormat % tempname % pidfile_;
             // Hmm ... the pidfile mysteriously disappeared ... try again.
             continue;
         }
diff --git a/Utils/Exception.cc b/Utils/Exception.cc
index 61d5860d8..e23357f23 100644
--- a/Utils/Exception.cc
+++ b/Utils/Exception.cc
@@ -51,7 +51,7 @@ prefix_ void senf::SystemException::init(std::string const & descr, int code)
 {
     code_ = code;
     (*this) << "[" << errorString() << "]";
-    if (! descr.empty()) (*this) << "; " << descr;
+    if (! descr.empty()) (*this) << " " << descr;
 }
 
 ///////////////////////////////cc.e////////////////////////////////////////
diff --git a/Utils/Exception.test.cc b/Utils/Exception.test.cc
index a65aa5fd3..3e5142878 100644
--- a/Utils/Exception.test.cc
+++ b/Utils/Exception.test.cc
@@ -50,8 +50,7 @@ BOOST_AUTO_UNIT_TEST(errnoException)
     }
     catch (senf::SystemException & e) {
         BOOST_CHECK_EQUAL( e.errorNumber(), ENOENT );
-        //BOOST_CHECK_EQUAL( e.what(), "::open(): (2) No such file or directory\nx=1\ny=2" );
-        BOOST_CHECK_EQUAL( e.what(), "[No such file or directory]; ::open()\nx=1\ny=2" );
+        BOOST_CHECK_EQUAL( e.errorString(), "No such file or directory");
     }
 }
 
-- 
GitLab