Refactored logging code. 20/46920/14
authorPawel Kubik <p.kubik@samsung.com>
Tue, 25 Aug 2015 08:30:03 +0000 (10:30 +0200)
committerPawel Kubik <p.kubik@samsung.com>
Fri, 28 Aug 2015 13:31:44 +0000 (06:31 -0700)
[Feature]       Improved logging messages
[Cause]         Inconsistent logging messages on different loggers
[Solution]      Unified logging messages
[Verification]  Build, install, run tests

Change-Id: I7424733e97a4081707f64c3c4256021c560c7cfc

24 files changed:
common/lxc/exception.hpp
common/lxc/zone.cpp
common/netlink/netlink-message.cpp
common/netlink/netlink.cpp
common/utils/eventfd.cpp
common/utils/exception.hpp
common/utils/fd-utils.cpp
common/utils/signal.cpp
common/utils/signalfd.cpp
libs/ipc/epoll/event-poll.cpp
libs/ipc/internals/request-queue.hpp
libs/ipc/internals/socket.cpp
libs/lxcpp/namespace.cpp
server/exception.hpp
server/host-dbus-connection.cpp
server/input-monitor.cpp
server/netdev.cpp
server/server.cpp
server/zone-provision.cpp
server/zones-manager.cpp
tests/unit_tests/lxc/ut-zone.cpp
tests/unit_tests/server/ut-input-monitor.cpp
tests/unit_tests/server/ut-zone-provision.cpp
zone-daemon/daemon-connection.cpp

index 85272b5..379fe43 100644 (file)
@@ -40,6 +40,10 @@ struct LxcException: public VasumException {
     LxcException(const std::string& error) : VasumException(error) {}
 };
 
+struct KeyNotFoundException: public LxcException {
+
+    KeyNotFoundException(const std::string& error) : LxcException(error) {}
+};
 
 } // namespace vasum
 
index 65207bd..611e043 100644 (file)
@@ -101,8 +101,9 @@ LxcZone::LxcZone(const std::string& lxcPath, const std::string& zoneName)
 {
     mLxcContainer = lxc_container_new(zoneName.c_str(), lxcPath.c_str());
     if (!mLxcContainer) {
-        LOGE("Could not initialize lxc zone " << zoneName << " in path " << lxcPath);
-        throw LxcException("Could not initialize lxc zone");
+        const std::string msg = "Could not initialize lxc zone " + zoneName + " in path " + lxcPath;
+        LOGE(msg);
+        throw LxcException(msg);
     }
 }
 
@@ -121,8 +122,9 @@ std::string LxcZone::getConfigItem(const std::string& key)
     char buffer[1024];
     int len = mLxcContainer->get_config_item(mLxcContainer, key.c_str(), buffer, sizeof(buffer));
     if (len < 0) {
-        LOGE("Key '" << key << "' not found in zone " << getName());
-        throw LxcException("Key not found");
+        const std::string msg = "Key '" + key + "' not found in zone " + getName();
+        LOGE(msg);
+        throw KeyNotFoundException(msg);
     }
     return buffer;
 }
index 58aa1a2..cef7828 100644 (file)
@@ -192,8 +192,9 @@ NetlinkResponse& NetlinkResponse::openNested(int ifla)
 {
     const rtattr *rta = asAttr(get(RTA_LENGTH(0)));
     if (rta->rta_type == ifla) {
-        LOGE("Wrong attribute type, expected: " << ifla << ", got: " << rta->rta_type);
-        throw VasumException("Wrong attribute type");
+        const std::string msg = "Wrong attribute type, expected: " + std::to_string(ifla) + ", got: " + std::to_string(rta->rta_type);
+        LOGE(msg);
+        throw VasumException(msg);
     }
     int pos = mPosition;
     seek(RTA_LENGTH(0));
@@ -227,12 +228,14 @@ const char* NetlinkResponse::get(int ifla, int len) const
 {
     const rtattr *rta = asAttr(get(RTA_LENGTH(len < 0 ? 0 : len)));
     if (rta->rta_type != ifla) {
-        LOGE("Wrong attribute type, expected:" << ifla << ", got: " << rta->rta_type);
-        throw VasumException("Wrong attribute type");
+        const std::string msg = "Wrong attribute type, expected: " + std::to_string(ifla) + ", got: " + std::to_string(rta->rta_type);
+        LOGE(msg);
+        throw VasumException(msg);
     }
     if (len >= 0 && rta->rta_len != RTA_LENGTH(len)) {
-        LOGE("Wrong attribute length, expected: " << rta->rta_len << ", got " << len);
-        throw VasumException("Wrong attribute length");
+        const std::string msg = "Wrong attribute length, expected: " + std::to_string(rta->rta_len) + ", got: " + std::to_string(len);
+        LOGE(msg);
+        throw VasumException(msg);
     }
     return reinterpret_cast<const char*>(RTA_DATA(get(rta->rta_len)));
 }
index 1d966cb..3408293 100644 (file)
@@ -75,8 +75,9 @@ void vsm_sendmsg(int fd, const struct msghdr *msg, int flags)
 {
     int ret = sendmsg(fd, msg, flags);
     if (ret < 0) {
-        LOGE("Can't send message: " << getSystemErrorMessage());
-        throw VasumException("Can't send netlink message");
+        const std::string msg = "Can't send netlink message: " + getSystemErrorMessage();
+        LOGE(msg);
+        throw VasumException(msg);
     }
 }
 
@@ -117,8 +118,9 @@ void Netlink::open(int netNsPid)
     if (bind(mFd, (struct sockaddr *)&local, sizeof(local)) < 0) {
         int err = errno;
         close();
-        LOGE("Can't bind to socket: " << getSystemErrorMessage(err));
-        throw VasumException("Can't set up netlink connection");
+        const std::string msg = "Can't bind to socket: " + getSystemErrorMessage(err);
+        LOGE(msg);
+        throw VasumException(msg);
     }
 }
 
@@ -187,8 +189,9 @@ std::unique_ptr<std::vector<char>> Netlink::rcv(unsigned int nlmsgSeq)
             }
         }
         if (lastOk == NULL) {
-            LOGE("Something went terribly wrong. Check vsm_recvmsg function");
-            throw VasumException("Can't receive data from system");
+            const std::string msg = "Can't receive data from the system";
+            LOGE(msg);
+            throw VasumException(msg);
         }
         offset +=  NLMSG_ALIGN(ret);
     } while (lastOk->nlmsg_type != NLMSG_DONE && lastOk->nlmsg_flags & NLM_F_MULTI);
index 9af1114..bc5e64a 100644 (file)
@@ -40,9 +40,9 @@ EventFD::EventFD()
 {
     mFD = ::eventfd(0, EFD_SEMAPHORE | EFD_CLOEXEC);
     if (mFD == -1) {
-        const std::string msg = getSystemErrorMessage();
-        LOGE("Error in eventfd: " << msg);
-        throw UtilsException("Error in eventfd: " + msg);
+        const std::string msg = "Error in eventfd: " + getSystemErrorMessage();
+        LOGE(msg);
+        throw UtilsException(msg);
     }
 }
 
index ca5564a..fda0554 100644 (file)
@@ -39,6 +39,11 @@ struct UtilsException: public std::runtime_error {
     UtilsException(const std::string& error) : std::runtime_error(error) {}
 };
 
+struct ProvisionExistsException: public UtilsException {
+
+    ProvisionExistsException(const std::string& error) : UtilsException(error) {}
+};
+
 /**
  * Return string describing error number
  * it is wrapper for strerror_r
index 030d1fc..3b7b998 100644 (file)
@@ -69,14 +69,15 @@ void waitForEvent(int fd,
             if (errno == EINTR) {
                 continue;
             }
-            const std::string msg = getSystemErrorMessage();
-            LOGE("Error in poll: " + msg);
-            throw UtilsException("Error in poll: " + msg);
+            const std::string msg = "Error in poll: " + getSystemErrorMessage();
+            LOGE(msg);
+            throw UtilsException(msg);
         }
 
         if (ret == 0) {
-            LOGE("Timeout in read");
-            throw UtilsException("Timeout in read");
+            const std::string msg = "Timeout in read";
+            LOGE(msg);
+            throw UtilsException(msg);
         }
 
         if (fds[0].revents & event) {
@@ -85,8 +86,9 @@ void waitForEvent(int fd,
         }
 
         if (fds[0].revents & POLLHUP) {
-            LOGW("Peer disconnected");
-            throw UtilsException("Peer disconnected");
+            const std::string msg = "Peer disconnected";
+            LOGW(msg);
+            throw UtilsException(msg);
         }
     }
 }
@@ -144,9 +146,9 @@ void write(int fd, const void* bufferPtr, const size_t size, int timeoutMS)
             // Neglected errors
             LOGD("Retrying write");
         } else {
-            const std::string msg = getSystemErrorMessage();
-            LOGE("Error during writing: " + msg);
-            throw UtilsException("Error during writing: " + msg);
+            const std::string msg = "Error during writing: " + getSystemErrorMessage();
+            LOGE(msg);
+            throw UtilsException(msg);
         }
 
         waitForEvent(fd, POLLOUT, deadline);
@@ -170,16 +172,17 @@ void read(int fd, void* bufferPtr, const size_t size, int timeoutMS)
                 break;
             }
             if (n == 0) {
-                LOGW("Peer disconnected");
-                throw UtilsException("Peer disconnected");
+                const std::string msg = "Peer disconnected";
+                LOGW(msg);
+                throw UtilsException(msg);
             }
         } else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
             // Neglected errors
             LOGD("Retrying read");
         } else {
-            const std::string msg = getSystemErrorMessage();
-            LOGE("Error during reading: " + msg);
-            throw UtilsException("Error during reading: " + msg);
+            const std::string msg = "Error during reading: " + getSystemErrorMessage();
+            LOGE(msg);
+            throw UtilsException(msg);
         }
 
         waitForEvent(fd, POLLIN, deadline);
@@ -190,9 +193,9 @@ unsigned int getMaxFDNumber()
 {
     struct rlimit rlim;
     if (-1 ==  getrlimit(RLIMIT_NOFILE, &rlim)) {
-        const std::string msg = getSystemErrorMessage();
-        LOGE("Error during getrlimit: " + msg);
-        throw UtilsException("Error during getrlimit: " + msg);
+        const std::string msg = "Error during getrlimit: " + getSystemErrorMessage();
+        LOGE(msg);
+        throw UtilsException(msg);
     }
     return rlim.rlim_cur;
 }
@@ -203,9 +206,9 @@ void setMaxFDNumber(unsigned int limit)
     rlim.rlim_cur = limit;
     rlim.rlim_max = limit;
     if (-1 ==  setrlimit(RLIMIT_NOFILE, &rlim)) {
-        const std::string msg = getSystemErrorMessage();
-        LOGE("Error during setrlimit: " + msg);
-        throw UtilsException("Error during setrlimit: " + msg);
+        const std::string msg = "Error during setrlimit: " + getSystemErrorMessage();
+        LOGE(msg);
+        throw UtilsException(msg);
     }
 }
 
index cdd79e6..bad3b0b 100644 (file)
@@ -39,24 +39,24 @@ void setSignalMask(int how, const ::sigset_t& set)
 {
     int ret = ::pthread_sigmask(how, &set, nullptr /*&oldSet*/);
     if(ret != 0) {
-        const std::string msg = getSystemErrorMessage(ret);
-        LOGE("Error in pthread_sigmask: " << msg);
-        throw UtilsException("Error in pthread_sigmask: " + msg);
+        const std::string msg = "Error in pthread_sigmask: " + getSystemErrorMessage(ret);
+        LOGE(msg);
+        throw UtilsException(msg);
     }
 }
 
 void changeSignal(int how, const int sigNum) {
     ::sigset_t set;
     if(-1 == ::sigemptyset(&set)) {
-        const std::string msg = getSystemErrorMessage();
-        LOGE("Error in sigfillset: " << msg);
-        throw UtilsException("Error in sigfillset: " + msg);
+        const std::string msg = "Error in sigfillset: " + getSystemErrorMessage();
+        LOGE(msg);
+        throw UtilsException(msg);
     }
 
     if(-1 ==::sigaddset(&set, sigNum)) {
-        const std::string msg = getSystemErrorMessage();
-        LOGE("Error in sigdelset: " << msg);
-        throw UtilsException("Error in sigdelset: " + msg);
+        const std::string msg = "Error in sigdelset: " + getSystemErrorMessage();
+        LOGE(msg);
+        throw UtilsException(msg);
     }
 
     setSignalMask(how, set);
@@ -69,9 +69,9 @@ void changeSignal(int how, const int sigNum) {
     ::sigset_t set;
     int ret = ::pthread_sigmask(0 /*ignored*/, nullptr /*get the oldset*/, &set);
     if(ret != 0) {
-        const std::string msg = getSystemErrorMessage(ret);
-        LOGE("Error in pthread_sigmask: " << msg);
-        throw UtilsException("Error in pthread_sigmask: " + msg);
+        const std::string msg = "Error in pthread_sigmask: " + getSystemErrorMessage(ret);
+        LOGE(msg);
+        throw UtilsException(msg);
     }
     return set;
 }
@@ -82,9 +82,9 @@ bool isSignalBlocked(const int sigNum)
 
     int ret = ::sigismember(&set, sigNum);
     if(-1 == ret) {
-        const std::string msg = getSystemErrorMessage();
-        LOGE("Error in sigismember: " << msg);
-        throw UtilsException("Error in sigismember: " + msg);
+        const std::string msg = "Error in sigismember: " + getSystemErrorMessage();
+        LOGE(msg);
+        throw UtilsException(msg);
     }
 
     return ret == 1;
@@ -99,16 +99,16 @@ void signalBlockAllExcept(const std::initializer_list<int>& signals)
 {
     ::sigset_t set;
     if(-1 == ::sigfillset(&set)) {
-        const std::string msg = getSystemErrorMessage();
-        LOGE("Error in sigfillset: " << msg);
-        throw UtilsException("Error in sigfillset: " + msg);
+        const std::string msg = "Error in sigfillset: " + getSystemErrorMessage();
+        LOGE(msg);
+        throw UtilsException(msg);
     }
 
     for(const int s: signals) {
         if(-1 == ::sigaddset(&set, s)) {
-            const std::string msg = getSystemErrorMessage();
-            LOGE("Error in sigaddset: " << msg);
-            throw UtilsException("Error in sigaddset: " + msg);
+            const std::string msg = "Error in sigaddset: " + getSystemErrorMessage();
+            LOGE(msg);
+            throw UtilsException(msg);
         }
     }
     setSignalMask(SIG_BLOCK, set);
@@ -126,9 +126,9 @@ void signalIgnore(const std::initializer_list<int>& signals)
 
     for(const int s: signals) {
         if(-1 == ::sigaction(s, &act, nullptr)) {
-            const std::string msg = getSystemErrorMessage();
-            LOGE("Error in sigaction: " << msg);
-            throw UtilsException("Error in sigaction: " + msg);
+            const std::string msg = "Error in sigaction: " + getSystemErrorMessage();
+            LOGE(msg);
+            throw UtilsException(msg);
         }
     }
 }
index 6ffa5cb..61d04d5 100644 (file)
@@ -39,9 +39,9 @@ SignalFD::SignalFD(ipc::epoll::EventPoll& eventPoll)
 
     mFD = ::signalfd(-1, &set, SFD_CLOEXEC);
     if (mFD == -1) {
-        const std::string msg = getSystemErrorMessage();
-        LOGE("Error in signalfd: " << msg);
-        throw UtilsException("Error in signalfd: " + msg);
+        const std::string msg = "Error in signalfd: " + getSystemErrorMessage();
+        LOGE(msg);
+        throw UtilsException(msg);
     }
 
     mEventPoll.addFD(mFD, EPOLLIN, std::bind(&SignalFD::handleInternal, this));
@@ -66,9 +66,9 @@ void SignalFD::setHandler(const int sigNum, const Callback&& callback)
 
     int error = ::signalfd(mFD, &set, SFD_CLOEXEC);
     if (error != mFD) {
-        const std::string msg = getSystemErrorMessage();
-        LOGE("Error in signalfd: " << msg);
-        throw UtilsException("Error in signalfd: " + msg);
+        const std::string msg = "Error in signalfd: " + getSystemErrorMessage();
+        LOGE(msg);
+        throw UtilsException(msg);
     }
 
     mCallbacks.insert({sigNum, callback});
index 650e121..c4b1f40 100644 (file)
@@ -42,8 +42,9 @@ EventPoll::EventPoll()
     : mPollFD(::epoll_create1(EPOLL_CLOEXEC))
 {
     if (mPollFD == -1) {
-        LOGE("Failed to create epoll: " << getSystemErrorMessage());
-        throw UtilsException("Could not create epoll");
+        const std::string msg = "Failed to create epoll: " + getSystemErrorMessage();
+        LOGE(msg);
+        throw UtilsException(msg);
     }
 }
 
@@ -69,13 +70,15 @@ void EventPoll::addFD(const int fd, const Events events, Callback&& callback)
     std::lock_guard<Mutex> lock(mMutex);
 
     if (mCallbacks.find(fd) != mCallbacks.end()) {
-        LOGE("Already added fd: " << fd);
-        throw UtilsException("FD already added");
+        const std::string msg = "fd " + std::to_string(fd) + " already added";
+        LOGE(msg);
+        throw UtilsException(msg);
     }
 
     if (!addFDInternal(fd, events)) {
-        LOGE("Could not add fd");
-        throw UtilsException("Could not add fd");
+        const std::string msg = "Could not add fd";
+        LOGE(msg);
+        throw UtilsException(msg);
     }
 
     mCallbacks.insert({fd, std::make_shared<Callback>(std::move(callback))});
@@ -86,8 +89,9 @@ void EventPoll::modifyFD(const int fd, const Events events)
 {
     // No need to lock and check mCallbacks map
     if (!modifyFDInternal(fd, events)) {
-        LOGE("Could not modify fd: " << fd);
-        throw UtilsException("Could not modify fd");
+        const std::string msg = "Could not modify fd: " + std::to_string(fd);
+        LOGE(msg);
+        throw UtilsException(msg);
     }
 }
 
@@ -117,8 +121,9 @@ bool EventPoll::dispatchIteration(const int timeoutMs)
             if (errno == EINTR) {
                 continue;
             }
-            LOGE("Failed to wait on epoll: " << getSystemErrorMessage());
-            throw UtilsException("Could not wait for event");
+            const std::string msg = "Failed to wait on epoll: " + getSystemErrorMessage();
+            LOGE(msg);
+            throw UtilsException(msg);
         }
 
         // callback could be removed in the meantime, so be careful, find it inside lock
index 810175f..e0951bd 100644 (file)
@@ -157,8 +157,9 @@ typename RequestQueue<RequestIdType>::Request RequestQueue<RequestIdType>::pop()
     Lock lock(mStateMutex);
     mEventFD.receive();
     if (mRequests.empty()) {
-        LOGE("Request queue is empty");
-        throw IPCException("Request queue is empty");
+        const std::string msg = "Request queue is empty";
+        LOGE(msg);
+        throw IPCException(msg);
     }
     Request request = std::move(mRequests.front());
     mRequests.pop_front();
index c58809d..a88ff0d 100644 (file)
@@ -52,9 +52,9 @@ void setFdOptions(int fd)
 {
     // Prevent from inheriting fd by zones
     if (-1 == ::fcntl(fd, F_SETFD, FD_CLOEXEC)) {
-        const std::string msg = getSystemErrorMessage();
-        LOGE("Error in fcntl: " + msg);
-        throw IPCException("Error in fcntl: " + msg);
+        const std::string msg = "Error in fcntl: " + getSystemErrorMessage();
+        LOGE(msg);
+        throw IPCException(msg);
     }
 }
 
@@ -94,9 +94,9 @@ std::shared_ptr<Socket> Socket::accept()
 {
     int sockfd = ::accept(mFD, nullptr, nullptr);
     if (sockfd == -1) {
-        const std::string msg = getSystemErrorMessage();
-        LOGE("Error in accept: " << msg);
-        throw IPCException("Error in accept: " + msg);
+        const std::string msg = "Error in accept: " + getSystemErrorMessage();
+        LOGE(msg);
+        throw IPCException(msg);
     }
     setFdOptions(sockfd);
     return std::make_shared<Socket>(sockfd);
@@ -119,8 +119,9 @@ int Socket::getSystemdSocketInternal(const std::string& path)
 {
     int n = ::sd_listen_fds(-1 /*Block further calls to sd_listen_fds*/);
     if (n < 0) {
-        LOGE("sd_listen_fds fails with errno: " << n);
-        throw IPCException("sd_listen_fds fails with errno: " + std::to_string(n));
+        const std::string msg = "sd_listen_fds failed: " + getSystemErrorMessage(-n);
+        LOGE(msg);
+        throw IPCException(msg);
     }
 
     for (int fd = SD_LISTEN_FDS_START;
@@ -140,15 +141,16 @@ int Socket::createSocketInternal(const std::string& path)
 {
     // Isn't the path too long?
     if (path.size() >= sizeof(sockaddr_un::sun_path)) {
-        LOGE("Socket's path too long");
-        throw IPCException("Socket's path too long");
+        const std::string msg = "Socket's path too long";
+        LOGE(msg);
+        throw IPCException(msg);
     }
 
     int sockfd = ::socket(AF_UNIX, SOCK_STREAM, 0);
     if (sockfd == -1) {
-        const std::string msg = getSystemErrorMessage();
-        LOGE("Error in socket: " + msg);
-        throw IPCException("Error in socket: " + msg);
+        const std::string msg = "Error in socket: " + getSystemErrorMessage();
+        LOGE(msg);
+        throw IPCException(msg);
     }
     setFdOptions(sockfd);
 
@@ -162,18 +164,18 @@ int Socket::createSocketInternal(const std::string& path)
     if (-1 == ::bind(sockfd,
                      reinterpret_cast<struct sockaddr*>(&serverAddress),
                      sizeof(struct sockaddr_un))) {
-        std::string message = getSystemErrorMessage();
         utils::close(sockfd);
-        LOGE("Error in bind: " << message);
-        throw IPCException("Error in bind: " + message);
+        const std::string msg = "Error in bind: " + getSystemErrorMessage();
+        LOGE(msg);
+        throw IPCException(msg);
     }
 
     if (-1 == ::listen(sockfd,
                        MAX_QUEUE_LENGTH)) {
-        std::string message = getSystemErrorMessage();
         utils::close(sockfd);
-        LOGE("Error in listen: " << message);
-        throw IPCException("Error in listen: " + message);
+        const std::string msg = "Error in listen: " + getSystemErrorMessage();
+        LOGE(msg);
+        throw IPCException(msg);
     }
 
     return sockfd;
@@ -199,15 +201,16 @@ Socket Socket::connectSocket(const std::string& path)
 {
     // Isn't the path too long?
     if (path.size() >= sizeof(sockaddr_un::sun_path)) {
-        LOGE("Socket's path too long");
-        throw IPCException("Socket's path too long");
+        const std::string msg = "Socket's path too long";
+        LOGE(msg);
+        throw IPCException(msg);
     }
 
     int fd = socket(AF_UNIX, SOCK_STREAM, 0);
     if (fd == -1) {
-        const std::string msg = getSystemErrorMessage();
-        LOGE("Error in socket: " + msg);
-        throw IPCException("Error in socket: " + msg);
+        const std::string msg = "Error in socket: " + getSystemErrorMessage();
+        LOGE(msg);
+        throw IPCException(msg);
     }
     setFdOptions(fd);
 
@@ -218,19 +221,19 @@ Socket Socket::connectSocket(const std::string& path)
     if (-1 == connect(fd,
                       reinterpret_cast<struct sockaddr*>(&serverAddress),
                       sizeof(struct sockaddr_un))) {
-        const std::string msg = getSystemErrorMessage();
         utils::close(fd);
-        LOGE("Error in connect: " + msg);
-        throw IPCException("Error in connect: " + msg);
+        const std::string msg = "Error in connect: " + getSystemErrorMessage();
+        LOGE(msg);
+        throw IPCException(msg);
     }
 
     // Nonblock socket
     int flags = fcntl(fd, F_GETFL, 0);
     if (-1 == fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
-        const std::string msg = getSystemErrorMessage();
         utils::close(fd);
-        LOGE("Error in fcntl: " + msg);
-        throw IPCException("Error in fcntl: " + msg);
+        const std::string msg = "Error in fcntl: " + getSystemErrorMessage();
+        LOGE(msg);
+        throw IPCException(msg);
     }
 
     return Socket(fd);
index 35bc140..0ee8c88 100644 (file)
@@ -52,8 +52,9 @@ std::string toString(const Namespace ns)
     case Namespace::NET:
         return "net";
     default:
-        LOGE("Bad namespace passed to the function");
-        throw BadArgument("Bad namespace passed to the function");
+        const std::string msg = "Bad namespace passed to the function";
+        LOGE(msg);
+        throw BadArgument(msg);
     }
 }
 
index bf5a106..e1b3be4 100644 (file)
@@ -82,6 +82,10 @@ struct InputMonitorException: public ServerException {
     InputMonitorException(const std::string& error) : ServerException(error) {}
 };
 
+struct TimeoutException: public InputMonitorException {
+
+    TimeoutException(const std::string& error) : InputMonitorException (error) {}
+};
 
 }
 
index ce415c8..b5a0625 100644 (file)
@@ -62,8 +62,9 @@ HostDbusConnection::HostDbusConnection(ZonesManager* zonesManagerPtr)
                              std::bind(&HostDbusConnection::onNameLost, this));
 
     if (!waitForName(NAME_ACQUIRED_TIMEOUT)) {
-        LOGE("Could not acquire dbus name: " << api::dbus::BUS_NAME);
-        throw HostConnectionException("Could not acquire dbus name: " + api::dbus::BUS_NAME);
+        const std::string msg = "Could not acquire dbus name: " + api::dbus::BUS_NAME;
+        LOGE(msg);
+        throw HostConnectionException(msg);
     }
 
     LOGT("Registering DBUS interface");
index bb65b90..45982af 100644 (file)
@@ -71,13 +71,15 @@ InputMonitor::InputMonitor(ipc::epoll::EventPoll& eventPoll,
     , mEventPoll(eventPoll)
 {
     if (mConfig.timeWindowMs > MAX_TIME_WINDOW_SEC * 1000L) {
-        LOGE("Time window exceeds maximum: " << MAX_TIME_WINDOW_SEC);
-        throw InputMonitorException("Time window exceeds maximum");
+        const std::string msg = "Time window exceeds maximum: " + MAX_TIME_WINDOW_SEC;
+        LOGE(msg);
+        throw TimeoutException(msg);
     }
 
     if (mConfig.numberOfEvents > MAX_NUMBER_OF_EVENTS) {
-        LOGE("Number of events exceeds maximum: " << MAX_NUMBER_OF_EVENTS);
-        throw InputMonitorException("Number of events exceeds maximum");
+        const std::string msg = "Number of events exceeds maximum: " + MAX_NUMBER_OF_EVENTS;
+        LOGE(msg);
+        throw InputMonitorException(msg);
     }
 
     mDevicePath = getDevicePath();
@@ -185,9 +187,10 @@ void InputMonitor::setHandler(const std::string& devicePath)
     // We need NONBLOCK for FIFOs in the tests
     mFd = ::open(devicePath.c_str(), O_RDONLY | O_NONBLOCK | O_CLOEXEC);
     if (mFd < 0) {
-        LOGE("Cannot create input monitor channel. Device file: " <<
-             devicePath << " doesn't exist");
-        throw InputMonitorException("Device does not exist");
+        const std::string msg = "Cannot create input monitor channel. Device file: " +
+                                devicePath + " doesn't exist";
+        LOGE(msg);
+        throw InputMonitorException(msg);
     }
     mEventPoll.addFD(mFd, EPOLLIN, std::bind(&InputMonitor::handleInternal, this, _1, _2));
 }
index 3086e7f..dc8642c 100644 (file)
@@ -98,8 +98,9 @@ string getUniqueVethName()
 uint32_t getInterfaceIndex(const string& name) {
     uint32_t index = if_nametoindex(name.c_str());
     if (!index) {
-        LOGE("Can't get " << name << " interface index (" << getSystemErrorMessage() << ")");
-        throw ZoneOperationException("Can't find interface");
+        const std::string msg = "Can't get " + name + " interface index (" + getSystemErrorMessage() + ")";
+        LOGE(msg);
+        throw ZoneOperationException(msg);
     }
     return index;
 }
@@ -163,8 +164,9 @@ void attachToBridge(const string& bridge, const string& netdev)
     uint32_t index = getInterfaceIndex(netdev);
     int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
     if (fd < 0) {
-        LOGE("Can't open socket (" << getSystemErrorMessage() << ")");
-        throw ZoneOperationException("Can't attach to bridge");
+        const std::string msg = "Can't open socket (" + getSystemErrorMessage() + ")";
+        LOGE(msg);
+        throw ZoneOperationException(msg);
     }
 
     struct ifreq ifr = utils::make_clean<ifreq>();
@@ -175,8 +177,9 @@ void attachToBridge(const string& bridge, const string& netdev)
         int error = errno;
         //TODO: Close can be interrupted. Move util functions from ipc
         ::close(fd);
-        LOGE("Can't attach to bridge (" + getSystemErrorMessage(error) + ")");
-        throw ZoneOperationException("Can't attach to bridge");
+        const std::string msg = "Can't attach to bridge (" + getSystemErrorMessage(error) + ")";
+        LOGE(msg);
+        throw ZoneOperationException(msg);
     }
     close(fd);
 }
@@ -276,8 +279,9 @@ std::vector<Attrs> getIpAddresses(const pid_t nsPid, int family, uint32_t index)
                         }
                         addr = inet_ntop(family, addr, buf, sizeof(buf));
                         if (addr == NULL) {
-                            LOGE("Can't convert ip address: " << getSystemErrorMessage());
-                            throw VasumException("Can't get ip address");
+                            const std::string msg = "Can't convert ip address: " + getSystemErrorMessage();
+                            LOGE(msg);
+                            throw VasumException(msg);
                         }
                         attrs.push_back(make_tuple("ip", buf));
                         break;
@@ -594,8 +598,9 @@ void setAttrs(const pid_t nsPid, const std::string& netdev, const Attrs& attrs)
             for (const auto& addrAttr : split(params, ip, is_any_of(","))) {
                 size_t pos = addrAttr.find(":");
                 if (pos == string::npos || pos == addrAttr.length()) {
-                    LOGE("Wrong input data format: ill formed address attribute: " << addrAttr);
-                    throw VasumException("Wrong input data format: ill formed address attribute");
+                    const std::string msg = "Wrong input data format: ill formed address attribute: " + addrAttr;
+                    LOGE(msg);
+                    throw VasumException(msg);
                 }
                 attrs.push_back(make_tuple(addrAttr.substr(0, pos), addrAttr.substr(pos + 1)));
             }
@@ -614,15 +619,17 @@ void deleteIpAddress(const pid_t nsPid,
     uint32_t index = getInterfaceIndex(netdev, nsPid);
     size_t slash = ip.find('/');
     if (slash == string::npos) {
-        LOGE("Wrong address format: it is not CIDR notation: can't find '/'");
-        throw VasumException("Wrong address format");
+        const std::string msg = "Wrong address format: it is not CIDR notation: can't find '/'";
+        LOGE(msg);
+        throw VasumException(msg);
     }
     int prefixlen = 0;
     try {
         prefixlen = stoi(ip.substr(slash + 1));
     } catch (const std::exception& ex) {
-        LOGE("Wrong address format: invalid prefixlen");
-        throw VasumException("Wrong address format: invalid prefixlen");
+        const std::string msg = "Wrong address format: invalid prefixlen";
+        LOGE(msg);
+        throw VasumException(msg);
     }
     deleteIpAddress(nsPid, index, ip.substr(0, slash), prefixlen, getIpFamily(ip));
 }
index a42d239..2fc8a65 100644 (file)
@@ -131,9 +131,9 @@ void Server::terminate()
     LOGI("Terminating server");
     int ret = ::pthread_kill(mDispatchingThread, SIGINT);
     if(ret != 0) {
-        const std::string msg = utils::getSystemErrorMessage(ret);
-        LOGE("Error during Server termination: " << msg);
-        throw ServerException("Error during Server termination: " + msg);
+        const std::string msg = "Error during Server termination: " + utils::getSystemErrorMessage(ret);
+        LOGE(msg);
+        throw ServerException(msg);
     }
 }
 
index 4e99f83..ed0eeea 100644 (file)
@@ -77,8 +77,9 @@ std::string ZoneProvision::declareProvision(ZoneProvisioningConfig::Provision&&
                                return getId(existingProvision) == id;
                            });
     if (it != mProvisioningConfig.provisions.end()) {
-        LOGE("Can't add provision. It already exists: " << id);
-        throw UtilsException("Provision already exists");
+        const std::string msg = "Can't add provision. It already exists: " + id;
+        LOGE(msg);
+        throw ProvisionExistsException(msg);
     }
     mProvisioningConfig.provisions.push_back(std::move(provision));
     saveProvisioningConfig();
@@ -249,10 +250,10 @@ void ZoneProvision::link(const ZoneProvisioningConfig::Link& config)
             return;
         }
     }
-    LOGE("Failed to create hard link: path=host: "
-         << srcHostPath
-         << ", msg: Path prefix is not valid path");
-    throw UtilsException("Failed to hard link: path prefix is not valid");
+    const std::string msg = "Failed to create hard link: path=host: " +
+                            srcHostPath + ", msg: Path prefix is not valid path";
+    LOGE(msg);
+    throw UtilsException(msg);
 }
 
 std::string ZoneProvision::getId(const ZoneProvisioningConfig::File& file)
index 5280d21..55e63f5 100644 (file)
@@ -357,8 +357,9 @@ void ZonesManager::destroyZone(const std::string& zoneId)
 
     auto iter = findZone(zoneId);
     if (iter == mZones.end()) {
-        LOGE("Failed to destroy zone " << zoneId << ": no such zone");
-        throw InvalidZoneIdException("No such zone");
+        const std::string msg = "Failed to destroy zone " + zoneId + ": no such zone";
+        LOGE(msg);
+        throw InvalidZoneIdException(msg);
     }
 
     get(iter).setDestroyOnExit();
@@ -1186,8 +1187,9 @@ int ZonesManager::getVTForNewZone()
         candidates.erase(zone->getVT());
     }
     if (candidates.empty()) {
-        LOGE("No free VT for zone");
-        throw ZoneOperationException("No free VT for zone");
+        const std::string msg = "No free VT for zone";
+        LOGE(msg);
+        throw ZoneOperationException(msg);
     }
     // return the smallest
     return *candidates.begin();
@@ -1197,13 +1199,15 @@ void ZonesManager::createZone(const std::string& id,
                               const std::string& templateName)
 {
     if (id.empty() || !isalnum(id)) {
-        LOGE("Failed to add zone - invalid name.");
-        throw InvalidZoneIdException("Invalid name");
+        const std::string msg = "Failed to add zone - invalid name.";
+        LOGE(msg);
+        throw InvalidZoneIdException(msg);
     }
 
     if (find(prohibitedZonesNames.begin(), prohibitedZonesNames.end(), id) != prohibitedZonesNames.end()) {
-        LOGE("Cannot create " << id << " zone - name is not allowed!");
-        throw InvalidZoneIdException("Zone name is not allowed");
+        const std::string msg = "Cannot create " + id + " zone - name is not allowed!";
+        LOGE(msg);
+        throw InvalidZoneIdException(msg);
     }
 
     LOGI("Creating zone " << id);
@@ -1216,14 +1220,15 @@ void ZonesManager::createZone(const std::string& id,
 
     // check if zone does not exist
     if (findZone(id) != mZones.end()) {
-        LOGE("Cannot create " << id << " zone - already exists!");
-        throw InvalidZoneIdException("Already exists");
+        const std::string msg = "Cannot create " + id + " zone - already exists!";
+        LOGE(msg);
+        throw InvalidZoneIdException(msg);
     }
 
     if (fs::exists(fs::path(mConfig.zonesPath) / id)) {
-        LOGE("Cannot create " << id << " zone - file system already exists!");
-        throw InvalidZoneIdException("Zone file system already exists but there is no configuration for it. "
-                                     "Check cleanUpZonesPath in daemon.conf");
+        const std::string msg = "Cannot create " + id + " zone - file system already exists!";
+        LOGE(msg);
+        throw InvalidZoneIdException(msg);
     }
 
     const std::string zonePathStr = utils::createFilePath(mConfig.zonesPath, id, "/");
@@ -1236,8 +1241,9 @@ void ZonesManager::createZone(const std::string& id,
                                                   zonePathStr);
 
         if (!utils::launchAsRoot(copyImageContentsWrapper)) {
-            LOGE("Failed to copy zone image.");
-            throw ZoneOperationException("Failed to copy zone image.");
+            const std::string msg = "Failed to copy zone image.";
+            LOGE(msg);
+            throw ZoneOperationException(msg);
         }
     }
 
index 80c003f..7dae96e 100644 (file)
@@ -96,7 +96,7 @@ BOOST_AUTO_TEST_CASE(CreateDestroy)
 
     BOOST_CHECK(lxc.isDefined());
     BOOST_CHECK_EQUAL(lxc.getConfigItem("lxc.rootfs"), ZONE_PATH + ZONE_NAME + "/rootfs");
-    BOOST_CHECK_EXCEPTION(lxc.getConfigItem("xxx"), LxcException, WhatEquals("Key not found"));
+    BOOST_CHECK_THROW(lxc.getConfigItem("xxx"), KeyNotFoundException);
 
     BOOST_CHECK(lxc.destroy());
 
index 75079a2..c889bb9 100644 (file)
@@ -127,9 +127,8 @@ BOOST_AUTO_TEST_CASE(ConfigTimeWindowMsTooHigh)
     ZonesManager cm(getPoll(), TEST_CONFIG_PATH);
     inputConfig.timeWindowMs = 50000;
 
-    BOOST_REQUIRE_EXCEPTION(InputMonitor inputMonitor(getPoll(), inputConfig, &cm),
-                            InputMonitorException,
-                            WhatEquals("Time window exceeds maximum"));
+    BOOST_REQUIRE_THROW(InputMonitor inputMonitor(getPoll(), inputConfig, &cm),
+                        TimeoutException);
 }
 
 BOOST_AUTO_TEST_CASE(ConfigDeviceFilePathNotExisting)
index 034552e..360ffda 100644 (file)
@@ -257,9 +257,8 @@ BOOST_AUTO_TEST_CASE(DeclareMount)
     ZoneProvision zoneProvision = create({});
     zoneProvision.declareMount("/fake/path1", "/fake/path2", "tmpfs", 077, "fake");
     zoneProvision.declareMount("/fake/path2", "/fake/path2", "tmpfs", 077, "fake");
-    BOOST_CHECK_EXCEPTION(zoneProvision.declareMount("/fake/path2", "/fake/path2", "tmpfs", 077, "fake"),
-                          UtilsException,
-                          WhatEquals("Provision already exists"));
+    BOOST_CHECK_THROW(zoneProvision.declareMount("/fake/path2", "/fake/path2", "tmpfs", 077, "fake"),
+                      ProvisionExistsException);
 
     ZoneProvisioningConfig config;
     load(config);
index 3fb40ac..e1cb734 100644 (file)
@@ -58,8 +58,9 @@ DaemonConnection::DaemonConnection(const NameLostCallback& nameLostCallback,
                              std::bind(&DaemonConnection::onNameLost, this));
 
     if (!waitForNameAndSetCallback(NAME_ACQUIRED_TIMEOUT, nameLostCallback)) {
-        LOGE("Could not acquire dbus name: " << zone_daemon::api::BUS_NAME);
-        throw ZoneDaemonException("Could not acquire dbus name: " + zone_daemon::api::BUS_NAME);
+        const std::string msg = "Could not acquire dbus name: " + zone_daemon::api::BUS_NAME;
+        LOGE(msg);
+        throw ZoneDaemonException(msg);
     }
 
     LOGD("Setting callbacks");