[PATCH 1/3] record: Use exceptions

Sebastian Huber sebastian.huber at embedded-brains.de
Wed Sep 4 05:18:38 UTC 2019


Update #3665.
---
 trace/record/client.h              | 41 ++++++++++++++++++++++++++++++++++----
 trace/record/record-client-base.cc | 35 +++++++++++++++++++++++---------
 trace/record/record-main-lttng.cc  | 26 ++++++++++++++----------
 3 files changed, 78 insertions(+), 24 deletions(-)

diff --git a/trace/record/client.h b/trace/record/client.h
index 2d55052..1bf8d37 100644
--- a/trace/record/client.h
+++ b/trace/record/client.h
@@ -31,9 +31,43 @@
 #include <rtems/recordclient.h>
 #include <rtems/recorddata.h>
 
+#include <errno.h>
 #include <sys/types.h>
 
 #include <csignal>
+#include <cstring>
+#include <iostream>
+#include <stdexcept>
+#include <string>
+
+class ErrnoException : public std::runtime_error {
+ public:
+  ErrnoException(std::string msg)
+      : std::runtime_error(msg + ": " + strerror(errno)) {
+    // Nothing to do
+  }
+};
+
+class FileDescriptor {
+ public:
+  FileDescriptor() = default;
+
+  FileDescriptor(const FileDescriptor&) = delete;
+
+  FileDescriptor& operator=(const FileDescriptor&) = delete;
+
+  void Open(const char* file);
+
+  void Connect(const char* host, uint16_t port);
+
+  ssize_t Read(void* buf, size_t n) { return (*reader_)(fd_, buf, n); }
+
+  void Destroy();
+
+ private:
+  int fd_ = -1;
+  ssize_t (*reader_)(int fd, void* buf, size_t n) = nullptr;
+};
 
 class Client {
  public:
@@ -43,9 +77,9 @@ class Client {
 
   Client& operator=(const Client&) = delete;
 
-  void Open(const char* file);
+  void Open(const char* file) { input_.Open(file); }
 
-  void Connect(const char* host, uint16_t port);
+  void Connect(const char* host, uint16_t port) { input_.Connect(host, port); }
 
   void Run();
 
@@ -62,8 +96,7 @@ class Client {
 
  private:
   rtems_record_client_context base_;
-  int fd_ = -1;
-  ssize_t (*reader_)(int fd, void* buf, size_t n) = nullptr;
+  FileDescriptor input_;
   sig_atomic_t stop_ = 0;
 };
 
diff --git a/trace/record/record-client-base.cc b/trace/record/record-client-base.cc
index e56608c..d9e1eea 100644
--- a/trace/record/record-client-base.cc
+++ b/trace/record/record-client-base.cc
@@ -45,17 +45,24 @@ static ssize_t ReadSocket(int fd, void* buf, size_t n) {
   return recv(fd, buf, n, 0);
 }
 
-void Client::Open(const char* file) {
+void FileDescriptor::Open(const char* file) {
   assert(fd_ == -1);
+
   fd_ = open(file, O_RDONLY);
-  assert(fd_ >= 0);
+  if (fd_ < 0) {
+    throw ErrnoException(std::string("cannot open file '") + file + "'");
+  }
+
   reader_ = ReadFile;
 }
 
-void Client::Connect(const char* host, uint16_t port) {
+void FileDescriptor::Connect(const char* host, uint16_t port) {
   assert(fd_ == -1);
+
   fd_ = socket(PF_INET, SOCK_STREAM, 0);
-  assert(fd_ >= 0);
+  if (fd_ < 0) {
+    throw ErrnoException("cannot open socket");
+  }
 
   struct sockaddr_in in_addr;
   memset(&in_addr, 0, sizeof(in_addr));
@@ -64,15 +71,27 @@ void Client::Connect(const char* host, uint16_t port) {
   in_addr.sin_addr.s_addr = inet_addr(host);
 
   int rv = connect(fd_, (struct sockaddr*)&in_addr, sizeof(in_addr));
-  assert(rv == 0);
+  if (rv != 0) {
+    throw ErrnoException(std::string("cannot connect to ") + host + " port " +
+                         std::to_string(port));
+  }
 
   reader_ = ReadSocket;
 }
 
+void FileDescriptor::Destroy() {
+  if (fd_ != -1) {
+    int rv = close(fd_);
+    if (rv != 0) {
+      std::cerr << "close failed: " << strerror(errno) << std::endl;
+    }
+  }
+}
+
 void Client::Run() {
   while (stop_ == 0) {
     int buf[8192];
-    ssize_t n = (*reader_)(fd_, buf, sizeof(buf));
+    ssize_t n = input_.Read(buf, sizeof(buf));
 
     if (n > 0) {
       rtems_record_client_run(&base_, buf, static_cast<size_t>(n));
@@ -83,8 +102,6 @@ void Client::Run() {
 }
 
 void Client::Destroy() {
-  int rv = close(fd_);
-  assert(rv == 0);
-
+  input_.Destroy();
   rtems_record_client_destroy(&base_);
 }
diff --git a/trace/record/record-main-lttng.cc b/trace/record/record-main-lttng.cc
index 51d4f0e..be2406d 100644
--- a/trace/record/record-main-lttng.cc
+++ b/trace/record/record-main-lttng.cc
@@ -487,19 +487,23 @@ int main(int argc, char** argv) {
     }
   }
 
-  GenerateMetadata();
-  client.OpenStreamFiles();
+  try {
+    GenerateMetadata();
+    client.OpenStreamFiles();
+
+    if (file != nullptr) {
+      client.Open(file);
+    } else {
+      client.Connect(host, port);
+    }
 
-  if (file != nullptr) {
-    client.Open(file);
-  } else {
-    client.Connect(host, port);
+    signal(SIGINT, SignalHandler);
+    client.Run();
+    client.Destroy();
+    client.CloseStreamFiles();
+  } catch (std::exception& e) {
+    std::cerr << e.what() << std::endl;
   }
 
-  signal(SIGINT, SignalHandler);
-  client.Run();
-  client.Destroy();
-  client.CloseStreamFiles();
-
   return 0;
 }
-- 
2.16.4




More information about the devel mailing list