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