Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2008-2017. 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 the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "Error.h" |
| 9 | |
| 10 | #include <sstream> |
| 11 | |
| 12 | #include "DriverStation.h" |
| 13 | #include "Timer.h" |
| 14 | #include "Utility.h" |
| 15 | |
| 16 | using namespace frc; |
| 17 | |
| 18 | void Error::Clone(const Error& error) { |
| 19 | m_code = error.m_code; |
| 20 | m_message = error.m_message; |
| 21 | m_filename = error.m_filename; |
| 22 | m_function = error.m_function; |
| 23 | m_lineNumber = error.m_lineNumber; |
| 24 | m_originatingObject = error.m_originatingObject; |
| 25 | m_timestamp = error.m_timestamp; |
| 26 | } |
| 27 | |
| 28 | Error::Code Error::GetCode() const { return m_code; } |
| 29 | |
| 30 | std::string Error::GetMessage() const { return m_message; } |
| 31 | |
| 32 | std::string Error::GetFilename() const { return m_filename; } |
| 33 | |
| 34 | std::string Error::GetFunction() const { return m_function; } |
| 35 | |
| 36 | int Error::GetLineNumber() const { return m_lineNumber; } |
| 37 | |
| 38 | const ErrorBase* Error::GetOriginatingObject() const { |
| 39 | return m_originatingObject; |
| 40 | } |
| 41 | |
| 42 | double Error::GetTimestamp() const { return m_timestamp; } |
| 43 | |
| 44 | void Error::Set(Code code, llvm::StringRef contextMessage, |
| 45 | llvm::StringRef filename, llvm::StringRef function, |
| 46 | int lineNumber, const ErrorBase* originatingObject) { |
| 47 | bool report = true; |
| 48 | |
| 49 | if (code == m_code && GetTime() - m_timestamp < 1) { |
| 50 | report = false; |
| 51 | } |
| 52 | |
| 53 | m_code = code; |
| 54 | m_message = contextMessage; |
| 55 | m_filename = filename; |
| 56 | m_function = function; |
| 57 | m_lineNumber = lineNumber; |
| 58 | m_originatingObject = originatingObject; |
| 59 | |
| 60 | if (report) { |
| 61 | m_timestamp = GetTime(); |
| 62 | Report(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void Error::Report() { |
| 67 | std::stringstream locStream; |
| 68 | locStream << m_function << " ["; |
| 69 | |
| 70 | #if defined(_WIN32) |
| 71 | const int MAX_DIR = 100; |
| 72 | char basename[MAX_DIR]; |
| 73 | _splitpath_s(m_filename.c_str(), nullptr, 0, basename, MAX_DIR, nullptr, 0, |
| 74 | nullptr, 0); |
| 75 | locStream << basename; |
| 76 | #else |
| 77 | locStream << basename(m_filename.c_str()); |
| 78 | #endif |
| 79 | locStream << ":" << m_lineNumber << "]"; |
| 80 | |
| 81 | DriverStation::ReportError(true, m_code, m_message, locStream.str(), |
| 82 | GetStackTrace(4)); |
| 83 | } |
| 84 | |
| 85 | void Error::Clear() { |
| 86 | m_code = 0; |
| 87 | m_message = ""; |
| 88 | m_filename = ""; |
| 89 | m_function = ""; |
| 90 | m_lineNumber = 0; |
| 91 | m_originatingObject = nullptr; |
| 92 | m_timestamp = 0.0; |
| 93 | } |