1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-23 04:04:54 +08:00

Changing the throw to add file and line number

This commit is contained in:
William Woodall 2012-02-07 14:11:17 -06:00
parent c3fb62a7d0
commit a8259fcda2
3 changed files with 31 additions and 21 deletions

View File

@ -43,6 +43,8 @@
#include <exception>
#include <stdexcept>
#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

View File

@ -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<int> (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,7 +484,7 @@ 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"
THROW (IOException, "select reports ready to read, but our fd isn't"
" in the list, this shouldn't happen!");
}
}
@ -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);
}
}

View File

@ -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);