Now you can download a copy of these docs so you can use them offline! Download now
Error.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 "Error.h"
8 
9 #include <taskLib.h>
10 #include <cstdio>
11 #include <cstring>
12 
13 #include "NetworkCommunication/FRCComm.h"
14 #include "Timer.h"
15 #include "Utility.h"
16 bool Error::m_stackTraceEnabled = false;
17 bool Error::m_suspendOnErrorEnabled = false;
18 
19 Error::Error()
20  : m_code(0)
21  , m_lineNumber(0)
22  , m_originatingObject(NULL)
23  , m_timestamp (0.0)
24 {}
25 
26 Error::~Error()
27 {}
28 
29 void Error::Clone(Error &error)
30 {
31  m_code = error.m_code;
32  m_message = error.m_message;
33  m_filename = error.m_filename;
34  m_function = error.m_function;
35  m_lineNumber = error.m_lineNumber;
36  m_originatingObject = error.m_originatingObject;
37  m_timestamp = error.m_timestamp;
38 }
39 
40 Error::Code Error::GetCode() const
41 { return m_code; }
42 
43 const char * Error::GetMessage() const
44 { return m_message.c_str(); }
45 
46 const char * Error::GetFilename() const
47 { return m_filename.c_str(); }
48 
49 const char * Error::GetFunction() const
50 { return m_function.c_str(); }
51 
52 uint32_t Error::GetLineNumber() const
53 { return m_lineNumber; }
54 
55 const ErrorBase* Error::GetOriginatingObject() const
56 { return m_originatingObject; }
57 
58 double Error::GetTime() const
59 { return m_timestamp; }
60 
61 void Error::Set(Code code, const char* contextMessage, const char* filename, const char* function, uint32_t lineNumber, const ErrorBase* originatingObject)
62 {
63  m_code = code;
64  m_message = contextMessage;
65  m_filename = filename;
66  m_function = function;
67  m_lineNumber = lineNumber;
68  m_originatingObject = originatingObject;
69  m_timestamp = GetTime();
70 
71  Report();
72 
73  if (m_suspendOnErrorEnabled) taskSuspend(0);
74 }
75 
76 void Error::Report()
77 {
78  // Error string buffers
79  char *error = new char[256];
80  char *error_with_code = new char[256];
81 
82  // Build error strings
83  if (m_code != -1)
84  {
85  snprintf(error, 256, "%s: status = %d (0x%08X) %s ...in %s() in %s at line %d\n",
86  m_code < 0 ? "ERROR" : "WARNING", (int32_t)m_code, (uint32_t)m_code, m_message.c_str(),
87  m_function.c_str(), m_filename.c_str(), m_lineNumber);
88  sprintf(error_with_code,"<Code>%ld %s", (int32_t)m_code, error);
89  } else {
90  snprintf(error, 256, "ERROR: %s ...in %s() in %s at line %d\n", m_message.c_str(),
91  m_function.c_str(), m_filename.c_str(), m_lineNumber);
92  strcpy(error_with_code, error);
93  }
94  // TODO: Add logging to disk
95 
96  // Send to the DriverStation
97  setErrorData(error_with_code, strlen(error_with_code), 100);
98 
99  delete [] error_with_code;
100 
101  // Print to console
102  printf("\n\n>>>>%s", error);
103 
104  delete [] error;
105 
106  if (m_stackTraceEnabled)
107  {
108  printf("-----------<Stack Trace>----------------\n");
109  wpi_selfTrace();
110  }
111 }
112 
113 void Error::Clear()
114 {
115  m_code = 0;
116  m_message = "";
117  m_filename = "";
118  m_function = "";
119  m_lineNumber = 0;
120  m_originatingObject = NULL;
121  m_timestamp = 0.0;
122 }
123 
Definition: Error.h:21

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