Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2008. All Rights Reserved. |
| 3 | */ |
| 4 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 5 | /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "Error.h" |
| 9 | |
| 10 | #include <iostream> |
| 11 | #include <sstream> |
| 12 | #include <cstring> |
| 13 | #include <cstdlib> |
| 14 | #include <stdint.h> |
| 15 | |
| 16 | #include "DriverStation.h" |
| 17 | #include "Timer.h" |
| 18 | #include "Utility.h" |
| 19 | |
| 20 | void Error::Clone(const Error& error) { |
| 21 | m_code = error.m_code; |
| 22 | m_message = error.m_message; |
| 23 | m_filename = error.m_filename; |
| 24 | m_function = error.m_function; |
| 25 | m_lineNumber = error.m_lineNumber; |
| 26 | m_originatingObject = error.m_originatingObject; |
| 27 | m_timestamp = error.m_timestamp; |
| 28 | } |
| 29 | |
| 30 | Error::Code Error::GetCode() const { return m_code; } |
| 31 | |
| 32 | std::string Error::GetMessage() const { return m_message; } |
| 33 | |
| 34 | std::string Error::GetFilename() const { return m_filename; } |
| 35 | |
| 36 | std::string Error::GetFunction() const { return m_function; } |
| 37 | |
| 38 | uint32_t Error::GetLineNumber() const { return m_lineNumber; } |
| 39 | |
| 40 | const ErrorBase* Error::GetOriginatingObject() const { |
| 41 | return m_originatingObject; |
| 42 | } |
| 43 | |
| 44 | double Error::GetTimestamp() const { return m_timestamp; } |
| 45 | |
| 46 | void Error::Set(Code code, llvm::StringRef contextMessage, |
| 47 | llvm::StringRef filename, llvm::StringRef function, |
| 48 | uint32_t lineNumber, const ErrorBase* originatingObject) { |
| 49 | bool report = true; |
| 50 | |
| 51 | if (code == m_code && GetTime() - m_timestamp < 1) { |
| 52 | report = false; |
| 53 | } |
| 54 | |
| 55 | m_code = code; |
| 56 | m_message = contextMessage; |
| 57 | m_filename = filename; |
| 58 | m_function = function; |
| 59 | m_lineNumber = lineNumber; |
| 60 | m_originatingObject = originatingObject; |
| 61 | |
| 62 | if (report) { |
| 63 | m_timestamp = GetTime(); |
| 64 | Report(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void Error::Report() { |
| 69 | std::stringstream errorStream; |
| 70 | |
| 71 | errorStream << "Error on line " << m_lineNumber << " "; |
| 72 | #if defined(_UNIX) |
| 73 | errorStream << "of " << basename(m_filename.c_str()) << ": "; |
| 74 | #elif defined(_WIN32) |
| 75 | const int MAX_DIR = 100; |
| 76 | char basename[MAX_DIR]; |
| 77 | _splitpath_s(m_filename.c_str(), NULL, 0, basename, MAX_DIR, NULL, 0, NULL, 0); |
| 78 | errorStream << "of " << basename << ": "; |
| 79 | #endif |
| 80 | |
| 81 | errorStream << m_message << std::endl; |
| 82 | errorStream << GetStackTrace(4); |
| 83 | |
| 84 | std::string error = errorStream.str(); |
| 85 | |
| 86 | DriverStation::ReportError(error); |
| 87 | } |
| 88 | |
| 89 | void Error::Clear() { |
| 90 | m_code = 0; |
| 91 | m_message = ""; |
| 92 | m_filename = ""; |
| 93 | m_function = ""; |
| 94 | m_lineNumber = 0; |
| 95 | m_originatingObject = nullptr; |
| 96 | m_timestamp = 0.0; |
| 97 | } |