Now you can download a copy of these docs so you can use them offline! Download now
ErrorBase.cpp
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2008. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
5 /*----------------------------------------------------------------------------*/
6 
7 #include "ErrorBase.h"
8 #include "Synchronized.h"
9 #include "nivision.h"
10 #define WPI_ERRORS_DEFINE_STRINGS
11 #include "WPIErrors.h"
12 
13 #include <errnoLib.h>
14 #include <symLib.h>
15 #include <sysSymTbl.h>
16 #include <cstdio>
17 
18 SEM_ID ErrorBase::_globalErrorMutex = semMCreate(SEM_Q_PRIORITY | SEM_DELETE_SAFE | SEM_INVERSION_SAFE);
19 Error ErrorBase::_globalError;
24 {}
25 
26 ErrorBase::~ErrorBase()
27 {}
28 
34 {
35  return m_error;
36 }
37 
38 const Error& ErrorBase::GetError() const
39 {
40  return m_error;
41 }
42 
47 {
48  m_error.Clear();
49 }
50 
59 void ErrorBase::SetErrnoError(const char *contextMessage,
60  const char* filename, const char* function, uint32_t lineNumber) const
61 {
62  char err[256];
63  int errNo = errnoGet();
64  if (errNo == 0)
65  {
66  sprintf(err, "OK: %s", contextMessage);
67  }
68  else
69  {
70  char *statName = new char[MAX_SYS_SYM_LEN + 1];
71  int pval;
72  SYM_TYPE ptype;
73  symFindByValue(statSymTbl, errNo, statName, &pval, &ptype);
74  if (pval != errNo)
75  snprintf(err, 256, "Unknown errno 0x%08X: %s", errNo, contextMessage);
76  else
77  snprintf(err, 256, "%s (0x%08X): %s", statName, errNo, contextMessage);
78  delete [] statName;
79  }
80 
81  // Set the current error information for this object.
82  m_error.Set(-1, err, filename, function, lineNumber, this);
83 
84  // Update the global error if there is not one already set.
85  Synchronized mutex(_globalErrorMutex);
86  if (_globalError.GetCode() == 0) {
87  _globalError.Clone(m_error);
88  }
89 }
90 
100 void ErrorBase::SetImaqError(int success, const char *contextMessage, const char* filename, const char* function, uint32_t lineNumber) const
101 {
102  // If there was an error
103  if (success <= 0) {
104  char err[256];
105  sprintf(err, "%s: %s", contextMessage, imaqGetErrorText(imaqGetLastError()));
106 
107  // Set the current error information for this object.
108  m_error.Set(imaqGetLastError(), err, filename, function, lineNumber, this);
109 
110  // Update the global error if there is not one already set.
111  Synchronized mutex(_globalErrorMutex);
112  if (_globalError.GetCode() == 0) {
113  _globalError.Clone(m_error);
114  }
115  }
116 }
117 
127 void ErrorBase::SetError(Error::Code code, const char *contextMessage,
128  const char* filename, const char* function, uint32_t lineNumber) const
129 {
130  // If there was an error
131  if (code != 0) {
132  // Set the current error information for this object.
133  m_error.Set(code, contextMessage, filename, function, lineNumber, this);
134 
135  // Update the global error if there is not one already set.
136  Synchronized mutex(_globalErrorMutex);
137  if (_globalError.GetCode() == 0) {
138  _globalError.Clone(m_error);
139  }
140  }
141 }
142 
152 void ErrorBase::SetWPIError(const char *errorMessage, const char *contextMessage,
153  const char* filename, const char* function, uint32_t lineNumber) const
154 {
155  char err[256];
156  sprintf(err, "%s: %s", errorMessage, contextMessage);
157 
158  // Set the current error information for this object.
159  m_error.Set(-1, err, filename, function, lineNumber, this);
160 
161  // Update the global error if there is not one already set.
162  Synchronized mutex(_globalErrorMutex);
163  if (_globalError.GetCode() == 0) {
164  _globalError.Clone(m_error);
165  }
166 }
167 
168 void ErrorBase::CloneError(ErrorBase *rhs) const
169 {
170  m_error.Clone(rhs->GetError());
171 }
172 
179 {
180  return m_error.GetCode() < 0;
181 }
182 
183 void ErrorBase::SetGlobalError(Error::Code code, const char *contextMessage,
184  const char* filename, const char* function, uint32_t lineNumber)
185 {
186  // If there was an error
187  if (code != 0) {
188  Synchronized mutex(_globalErrorMutex);
189 
190  // Set the current error information for this object.
191  _globalError.Set(code, contextMessage, filename, function, lineNumber, NULL);
192  }
193 }
194 
195 void ErrorBase::SetGlobalWPIError(const char *errorMessage, const char *contextMessage,
196  const char* filename, const char* function, uint32_t lineNumber)
197 {
198  char err[256];
199  sprintf(err, "%s: %s", errorMessage, contextMessage);
200 
201  Synchronized mutex(_globalErrorMutex);
202  if (_globalError.GetCode() != 0) {
203  _globalError.Clear();
204  }
205  _globalError.Set(-1, err, filename, function, lineNumber, NULL);
206 }
207 
212 {
213  Synchronized mutex(_globalErrorMutex);
214  return _globalError;
215 }
216 
virtual void SetWPIError(const char *errorMessage, const char *contextMessage, const char *filename, const char *function, uint32_t lineNumber) const
Set the current error information associated with this sensor.
Definition: ErrorBase.cpp:152
virtual Error & GetError()
Retrieve the current error. Get the current error information associated with this sensor...
Definition: ErrorBase.cpp:33
Definition: Error.h:21
static Error & GetGlobalError()
Definition: ErrorBase.cpp:211
ErrorBase()
Initialize the instance status to 0 for now.
Definition: ErrorBase.cpp:23
virtual void SetError(Error::Code code, const char *contextMessage, const char *filename, const char *function, uint32_t lineNumber) const
Set the current error information associated with this sensor.
Definition: ErrorBase.cpp:127
virtual void ClearError() const
Clear the current error information associated with this sensor.
Definition: ErrorBase.cpp:46
virtual bool StatusIsFatal() const
Check if the current error code represents a fatal error.
Definition: ErrorBase.cpp:178
virtual void SetErrnoError(const char *contextMessage, const char *filename, const char *function, uint32_t lineNumber) const
Set error information associated with a C library call that set an error to the "errno" global variab...
Definition: ErrorBase.cpp:59
virtual void SetImaqError(int success, const char *contextMessage, const char *filename, const char *function, uint32_t lineNumber) const
Set the current error information associated from the nivision Imaq API.
Definition: ErrorBase.cpp:100

Generated on Sat Apr 26 2014 12:26:45 for WPILibC++ by doxygen 1.8.6