From fb5b75a9f3e5551c358d7f948c74406c3d378589 Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Mon, 2 Sep 2019 15:55:43 +0200 Subject: record: Use exceptions 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(-) (limited to 'trace') 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 #include +#include #include #include +#include +#include +#include +#include + +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(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; } -- cgit v1.2.3