2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2015-06-25 15:07:55 -04:00
|
|
|
/* Copyright (c) FIRST 2008. All Rights Reserved.
|
|
|
|
|
*/
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "ErrorBase.h"
|
2014-05-02 17:54:01 -04:00
|
|
|
#include "HAL/cpp/Synchronized.hpp"
|
2013-12-15 18:30:16 -05:00
|
|
|
#define WPI_ERRORS_DEFINE_STRINGS
|
|
|
|
|
#include "WPIErrors.h"
|
|
|
|
|
|
|
|
|
|
#include <errno.h>
|
2014-07-25 16:52:00 -04:00
|
|
|
#include <cstring>
|
2013-12-15 18:30:16 -05:00
|
|
|
#include <cstdio>
|
|
|
|
|
|
2014-01-06 10:12:21 -05:00
|
|
|
MUTEX_ID ErrorBase::_globalErrorMutex = initializeMutexNormal();
|
2013-12-15 18:30:16 -05:00
|
|
|
Error ErrorBase::_globalError;
|
|
|
|
|
/**
|
|
|
|
|
* @brief Initialize the instance status to 0 for now.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
ErrorBase::ErrorBase() {}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Retrieve the current error.
|
|
|
|
|
* Get the current error information associated with this sensor.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
Error& ErrorBase::GetError() { return m_error; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
const Error& ErrorBase::GetError() const { return m_error; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Clear the current error information associated with this sensor.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void ErrorBase::ClearError() const { m_error.Clear(); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* @brief Set error information associated with a C library call that set an
|
|
|
|
|
* error to the "errno" global variable.
|
2014-07-25 16:52:00 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param contextMessage A custom message from the code that set the error.
|
|
|
|
|
* @param filename Filename of the error source
|
|
|
|
|
* @param function Function of the error source
|
|
|
|
|
* @param lineNumber Line number of the error source
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void ErrorBase::SetErrnoError(const char* contextMessage, const char* filename,
|
|
|
|
|
const char* function, uint32_t lineNumber) const {
|
|
|
|
|
char err[256];
|
|
|
|
|
int errNo = errno;
|
|
|
|
|
if (errNo == 0) {
|
|
|
|
|
sprintf(err, "OK: %s", contextMessage);
|
|
|
|
|
} else {
|
|
|
|
|
snprintf(err, 256, "%s (0x%08X): %s", strerror(errNo), errNo,
|
|
|
|
|
contextMessage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set the current error information for this object.
|
|
|
|
|
m_error.Set(-1, err, filename, function, lineNumber, this);
|
|
|
|
|
|
|
|
|
|
// Update the global error if there is not one already set.
|
|
|
|
|
Synchronized mutex(_globalErrorMutex);
|
|
|
|
|
if (_globalError.GetCode() == 0) {
|
|
|
|
|
_globalError.Clone(m_error);
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* @brief Set the current error information associated from the nivision Imaq
|
|
|
|
|
* API.
|
2014-07-25 16:52:00 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param success The return from the function
|
|
|
|
|
* @param contextMessage A custom message from the code that set the error.
|
|
|
|
|
* @param filename Filename of the error source
|
|
|
|
|
* @param function Function of the error source
|
|
|
|
|
* @param lineNumber Line number of the error source
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void ErrorBase::SetImaqError(int success, const char* contextMessage,
|
|
|
|
|
const char* filename, const char* function,
|
|
|
|
|
uint32_t lineNumber) const {
|
|
|
|
|
// If there was an error
|
|
|
|
|
if (success <= 0) {
|
|
|
|
|
char err[256];
|
|
|
|
|
sprintf(err, "%i: %s", success, contextMessage);
|
|
|
|
|
|
|
|
|
|
// Set the current error information for this object.
|
|
|
|
|
m_error.Set(success, err, filename, function, lineNumber, this);
|
|
|
|
|
|
|
|
|
|
// Update the global error if there is not one already set.
|
|
|
|
|
Synchronized mutex(_globalErrorMutex);
|
|
|
|
|
if (_globalError.GetCode() == 0) {
|
|
|
|
|
_globalError.Clone(m_error);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Set the current error information associated with this sensor.
|
2014-07-25 16:52:00 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param code The error code
|
|
|
|
|
* @param contextMessage A custom message from the code that set the error.
|
|
|
|
|
* @param filename Filename of the error source
|
|
|
|
|
* @param function Function of the error source
|
|
|
|
|
* @param lineNumber Line number of the error source
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void ErrorBase::SetError(Error::Code code, const char* contextMessage,
|
|
|
|
|
const char* filename, const char* function,
|
|
|
|
|
uint32_t lineNumber) const {
|
|
|
|
|
// If there was an error
|
|
|
|
|
if (code != 0) {
|
|
|
|
|
// Set the current error information for this object.
|
|
|
|
|
m_error.Set(code, contextMessage, filename, function, lineNumber, this);
|
|
|
|
|
|
|
|
|
|
// Update the global error if there is not one already set.
|
|
|
|
|
Synchronized mutex(_globalErrorMutex);
|
|
|
|
|
if (_globalError.GetCode() == 0) {
|
|
|
|
|
_globalError.Clone(m_error);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Set the current error information associated with this sensor.
|
2014-07-25 16:52:00 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param errorMessage The error message from WPIErrors.h
|
|
|
|
|
* @param contextMessage A custom message from the code that set the error.
|
|
|
|
|
* @param filename Filename of the error source
|
|
|
|
|
* @param function Function of the error source
|
|
|
|
|
* @param lineNumber Line number of the error source
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void ErrorBase::SetWPIError(const char* errorMessage, Error::Code code,
|
|
|
|
|
const char* contextMessage, const char* filename,
|
|
|
|
|
const char* function, uint32_t lineNumber) const {
|
|
|
|
|
char err[256];
|
|
|
|
|
sprintf(err, "%s: %s", errorMessage, contextMessage);
|
|
|
|
|
|
|
|
|
|
// Set the current error information for this object.
|
|
|
|
|
m_error.Set(code, err, filename, function, lineNumber, this);
|
|
|
|
|
|
|
|
|
|
// Update the global error if there is not one already set.
|
|
|
|
|
Synchronized mutex(_globalErrorMutex);
|
|
|
|
|
if (_globalError.GetCode() == 0) {
|
|
|
|
|
_globalError.Clone(m_error);
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void ErrorBase::CloneError(ErrorBase* rhs) const {
|
|
|
|
|
m_error.Clone(rhs->GetError());
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@brief Check if the current error code represents a fatal error.
|
2014-07-25 16:52:00 -04:00
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
@return true if the current error is fatal.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool ErrorBase::StatusIsFatal() const { return m_error.GetCode() < 0; }
|
|
|
|
|
|
|
|
|
|
void ErrorBase::SetGlobalError(Error::Code code, const char* contextMessage,
|
|
|
|
|
const char* filename, const char* function,
|
|
|
|
|
uint32_t lineNumber) {
|
|
|
|
|
// If there was an error
|
|
|
|
|
if (code != 0) {
|
|
|
|
|
Synchronized mutex(_globalErrorMutex);
|
|
|
|
|
|
|
|
|
|
// Set the current error information for this object.
|
|
|
|
|
_globalError.Set(code, contextMessage, filename, function, lineNumber,
|
2015-06-23 04:49:51 -07:00
|
|
|
nullptr);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void ErrorBase::SetGlobalWPIError(const char* errorMessage,
|
|
|
|
|
const char* contextMessage,
|
|
|
|
|
const char* filename, const char* function,
|
|
|
|
|
uint32_t lineNumber) {
|
|
|
|
|
char err[256];
|
|
|
|
|
sprintf(err, "%s: %s", errorMessage, contextMessage);
|
|
|
|
|
|
|
|
|
|
Synchronized mutex(_globalErrorMutex);
|
|
|
|
|
if (_globalError.GetCode() != 0) {
|
|
|
|
|
_globalError.Clear();
|
|
|
|
|
}
|
2015-06-23 04:49:51 -07:00
|
|
|
_globalError.Set(-1, err, filename, function, lineNumber, nullptr);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-07-25 16:52:00 -04:00
|
|
|
* Retrieve the current global error.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
Error& ErrorBase::GetGlobalError() {
|
|
|
|
|
Synchronized mutex(_globalErrorMutex);
|
|
|
|
|
return _globalError;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|