mirror of
https://github.com/wjwwood/serial.git
synced 2026-01-22 19:54:57 +08:00
Changing the throw to add file and line number
This commit is contained in:
parent
c3fb62a7d0
commit
a8259fcda2
@ -43,6 +43,8 @@
|
|||||||
#include <exception>
|
#include <exception>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
#define THROW(exceptionClass, message) throw exceptionClass(__FILE__, __LINE__, (message) )
|
||||||
|
|
||||||
namespace serial {
|
namespace serial {
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -447,13 +449,16 @@ public:
|
|||||||
|
|
||||||
class IOException : public std::exception
|
class IOException : public std::exception
|
||||||
{
|
{
|
||||||
|
std::string file_;
|
||||||
|
int line_;
|
||||||
const char* e_what_;
|
const char* e_what_;
|
||||||
int errno_;
|
int errno_;
|
||||||
public:
|
public:
|
||||||
explicit IOException (int errnum)
|
explicit IOException (std::string file, int line, int errnum)
|
||||||
: e_what_ (strerror (errnum)), errno_(errnum) {}
|
: file_(file), line_(line), e_what_ (strerror (errnum)), errno_(errnum) {}
|
||||||
explicit IOException (const char * description)
|
explicit IOException (std::string file, int line, const char * description)
|
||||||
: e_what_ (description), errno_(0) {}
|
: file_(file), line_(line), e_what_ (description), errno_(0) {}
|
||||||
|
virtual ~IOException() throw() {}
|
||||||
|
|
||||||
int getErrorNumber () { return errno_; }
|
int getErrorNumber () { return errno_; }
|
||||||
|
|
||||||
@ -461,9 +466,10 @@ public:
|
|||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
if (errno_ == 0)
|
if (errno_ == 0)
|
||||||
ss << "IO Exception " << e_what_ << " failed.";
|
ss << "IO Exception: " << e_what_;
|
||||||
else
|
else
|
||||||
ss << "IO Exception (" << errno_ << "): " << e_what_ << " failed.";
|
ss << "IO Exception (" << errno_ << "): " << e_what_;
|
||||||
|
ss << ", file " << file_ << ", line " << line_ << ".";
|
||||||
return ss.str ().c_str ();
|
return ss.str ().c_str ();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -482,6 +488,11 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class SerialExceptionBase : public std::exception
|
||||||
|
{
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace serial
|
} // namespace serial
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -87,10 +87,10 @@ Serial::SerialImpl::open ()
|
|||||||
return;
|
return;
|
||||||
case ENFILE:
|
case ENFILE:
|
||||||
case EMFILE:
|
case EMFILE:
|
||||||
throw IOException ("Too many file handles open.");
|
THROW (IOException, "Too many file handles open.");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw IOException (errno);
|
THROW (IOException, errno);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,14 +104,14 @@ Serial::SerialImpl::reconfigurePort ()
|
|||||||
if (fd_ == -1)
|
if (fd_ == -1)
|
||||||
{
|
{
|
||||||
// Can only operate on a valid file descriptor
|
// 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
|
struct termios options; // The options for the file descriptor
|
||||||
|
|
||||||
if (tcgetattr(fd_, &options) == -1)
|
if (tcgetattr(fd_, &options) == -1)
|
||||||
{
|
{
|
||||||
throw IOException ("::tcgetattr");
|
THROW (IOException, "::tcgetattr");
|
||||||
}
|
}
|
||||||
|
|
||||||
// set up raw mode / no echo / binary
|
// set up raw mode / no echo / binary
|
||||||
@ -225,7 +225,7 @@ Serial::SerialImpl::reconfigurePort ()
|
|||||||
int new_baud = static_cast<int> (baudrate_);
|
int new_baud = static_cast<int> (baudrate_);
|
||||||
if (ioctl (fd_, IOSSIOSPEED, &new_baud, 1) < 0)
|
if (ioctl (fd_, IOSSIOSPEED, &new_baud, 1) < 0)
|
||||||
{
|
{
|
||||||
throw IOException (errno);
|
THROW (IOException, errno);
|
||||||
}
|
}
|
||||||
// Linux Support
|
// Linux Support
|
||||||
#elif defined(__linux__)
|
#elif defined(__linux__)
|
||||||
@ -239,7 +239,7 @@ Serial::SerialImpl::reconfigurePort ()
|
|||||||
|
|
||||||
if (ioctl (fd_, TIOCSSERIAL, ser) < 0)
|
if (ioctl (fd_, TIOCSSERIAL, ser) < 0)
|
||||||
{
|
{
|
||||||
throw IOException (errno);
|
THROW (IOException, errno);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
throw invalid_argument ("OS does not currently support custom bauds");
|
throw invalid_argument ("OS does not currently support custom bauds");
|
||||||
@ -370,7 +370,7 @@ Serial::SerialImpl::available ()
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw IOException (errno);
|
THROW (IOException, errno);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -442,7 +442,7 @@ Serial::SerialImpl::read (unsigned char* buf, size_t size)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// Otherwise there was some error
|
// Otherwise there was some error
|
||||||
throw IOException (errno);
|
THROW (IOException, errno);
|
||||||
}
|
}
|
||||||
/** Timeout **/
|
/** Timeout **/
|
||||||
if (r == 0) {
|
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!
|
// 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!");
|
" in the list, this shouldn't happen!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return bytes_read;
|
return bytes_read;
|
||||||
@ -733,7 +733,7 @@ void
|
|||||||
Serial::SerialImpl::readLock() {
|
Serial::SerialImpl::readLock() {
|
||||||
int result = pthread_mutex_lock(&this->read_mutex);
|
int result = pthread_mutex_lock(&this->read_mutex);
|
||||||
if (result) {
|
if (result) {
|
||||||
throw (IOException (result));
|
THROW (IOException, result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -741,7 +741,7 @@ void
|
|||||||
Serial::SerialImpl::readUnlock() {
|
Serial::SerialImpl::readUnlock() {
|
||||||
int result = pthread_mutex_unlock(&this->read_mutex);
|
int result = pthread_mutex_unlock(&this->read_mutex);
|
||||||
if (result) {
|
if (result) {
|
||||||
throw (IOException (result));
|
THROW (IOException, result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -749,7 +749,7 @@ void
|
|||||||
Serial::SerialImpl::writeLock() {
|
Serial::SerialImpl::writeLock() {
|
||||||
int result = pthread_mutex_lock(&this->write_mutex);
|
int result = pthread_mutex_lock(&this->write_mutex);
|
||||||
if (result) {
|
if (result) {
|
||||||
throw (IOException (result));
|
THROW (IOException, result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -757,6 +757,6 @@ void
|
|||||||
Serial::SerialImpl::writeUnlock() {
|
Serial::SerialImpl::writeUnlock() {
|
||||||
int result = pthread_mutex_unlock(&this->write_mutex);
|
int result = pthread_mutex_unlock(&this->write_mutex);
|
||||||
if (result) {
|
if (result) {
|
||||||
throw (IOException (result));
|
THROW (IOException, result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -346,7 +346,6 @@ Serial::SerialImpl::read (char* buf, size_t size)
|
|||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
count++;
|
count++;
|
||||||
// printf("Counting: %u\n", count);
|
|
||||||
if (timeout_ != -1)
|
if (timeout_ != -1)
|
||||||
{
|
{
|
||||||
FD_ZERO (&readfds);
|
FD_ZERO (&readfds);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user