diff --git a/include/serial/serial.h b/include/serial/serial.h index 7d9fff9..c1a9269 100644 --- a/include/serial/serial.h +++ b/include/serial/serial.h @@ -43,6 +43,8 @@ #include #include +#define THROW(exceptionClass, message) throw exceptionClass(__FILE__, __LINE__, (message) ) + namespace serial { /*! @@ -447,13 +449,16 @@ public: class IOException : public std::exception { + std::string file_; + int line_; const char* e_what_; int errno_; public: - explicit IOException (int errnum) - : e_what_ (strerror (errnum)), errno_(errnum) {} - explicit IOException (const char * description) - : e_what_ (description), errno_(0) {} + explicit IOException (std::string file, int line, int errnum) + : file_(file), line_(line), e_what_ (strerror (errnum)), errno_(errnum) {} + explicit IOException (std::string file, int line, const char * description) + : file_(file), line_(line), e_what_ (description), errno_(0) {} + virtual ~IOException() throw() {} int getErrorNumber () { return errno_; } @@ -461,9 +466,10 @@ public: { std::stringstream ss; if (errno_ == 0) - ss << "IO Exception " << e_what_ << " failed."; + ss << "IO Exception: " << e_what_; else - ss << "IO Exception (" << errno_ << "): " << e_what_ << " failed."; + ss << "IO Exception (" << errno_ << "): " << e_what_; + ss << ", file " << file_ << ", line " << line_ << "."; return ss.str ().c_str (); } }; @@ -482,6 +488,11 @@ public: } }; +class SerialExceptionBase : public std::exception +{ + +}; + } // namespace serial #endif diff --git a/src/impl/unix.cc b/src/impl/unix.cc index f9785e5..a98128b 100644 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -87,10 +87,10 @@ Serial::SerialImpl::open () return; case ENFILE: case EMFILE: - throw IOException ("Too many file handles open."); + THROW (IOException, "Too many file handles open."); break; default: - throw IOException (errno); + THROW (IOException, errno); } } @@ -104,14 +104,14 @@ Serial::SerialImpl::reconfigurePort () if (fd_ == -1) { // Can only operate on a valid file descriptor - throw IOException ("Invalid file descriptor, is the serial port open?"); + THROW (IOException, "Invalid file descriptor, is the serial port open?"); } struct termios options; // The options for the file descriptor if (tcgetattr(fd_, &options) == -1) { - throw IOException ("::tcgetattr"); + THROW (IOException, "::tcgetattr"); } // set up raw mode / no echo / binary @@ -225,7 +225,7 @@ Serial::SerialImpl::reconfigurePort () int new_baud = static_cast (baudrate_); if (ioctl (fd_, IOSSIOSPEED, &new_baud, 1) < 0) { - throw IOException (errno); + THROW (IOException, errno); } // Linux Support #elif defined(__linux__) @@ -239,7 +239,7 @@ Serial::SerialImpl::reconfigurePort () if (ioctl (fd_, TIOCSSERIAL, ser) < 0) { - throw IOException (errno); + THROW (IOException, errno); } #else throw invalid_argument ("OS does not currently support custom bauds"); @@ -370,7 +370,7 @@ Serial::SerialImpl::available () } else { - throw IOException (errno); + THROW (IOException, errno); } } @@ -442,7 +442,7 @@ Serial::SerialImpl::read (unsigned char* buf, size_t size) continue; } // Otherwise there was some error - throw IOException (errno); + THROW (IOException, errno); } /** Timeout **/ if (r == 0) { @@ -484,8 +484,8 @@ Serial::SerialImpl::read (unsigned char* buf, size_t size) } } // This shouldn't happen, if r > 0 our fd has to be in the list! - throw IOException ("select reports ready to read, but our fd isn't" - " in the list, this shouldn't happen!"); + THROW (IOException, "select reports ready to read, but our fd isn't" + " in the list, this shouldn't happen!"); } } return bytes_read; @@ -733,7 +733,7 @@ void Serial::SerialImpl::readLock() { int result = pthread_mutex_lock(&this->read_mutex); if (result) { - throw (IOException (result)); + THROW (IOException, result); } } @@ -741,7 +741,7 @@ void Serial::SerialImpl::readUnlock() { int result = pthread_mutex_unlock(&this->read_mutex); if (result) { - throw (IOException (result)); + THROW (IOException, result); } } @@ -749,7 +749,7 @@ void Serial::SerialImpl::writeLock() { int result = pthread_mutex_lock(&this->write_mutex); if (result) { - throw (IOException (result)); + THROW (IOException, result); } } @@ -757,6 +757,6 @@ void Serial::SerialImpl::writeUnlock() { int result = pthread_mutex_unlock(&this->write_mutex); if (result) { - throw (IOException (result)); + THROW (IOException, result); } } diff --git a/src/impl/windows.cc b/src/impl/windows.cc index 48030a9..5e82227 100644 --- a/src/impl/windows.cc +++ b/src/impl/windows.cc @@ -346,7 +346,6 @@ Serial::SerialImpl::read (char* buf, size_t size) while (true) { count++; - // printf("Counting: %u\n", count); if (timeout_ != -1) { FD_ZERO (&readfds);