Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 2 | /* Copyright (c) FIRST 2008-2016. All Rights Reserved. */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 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() { |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 69 | std::stringstream locStream; |
| 70 | locStream << m_function << " ["; |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 71 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 72 | #if defined(_WIN32) |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 73 | const int MAX_DIR = 100; |
| 74 | char basename[MAX_DIR]; |
| 75 | _splitpath_s(m_filename.c_str(), NULL, 0, basename, MAX_DIR, NULL, 0, NULL, 0); |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 76 | locStream << basename; |
| 77 | #else |
| 78 | locStream << basename(m_filename.c_str()); |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 79 | #endif |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 80 | locStream << ":" << m_lineNumber << "]"; |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 81 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 82 | DriverStation::ReportError(true, m_code, m_message, locStream.str(), |
| 83 | GetStackTrace(4)); |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void Error::Clear() { |
| 87 | m_code = 0; |
| 88 | m_message = ""; |
| 89 | m_filename = ""; |
| 90 | m_function = ""; |
| 91 | m_lineNumber = 0; |
| 92 | m_originatingObject = nullptr; |
| 93 | m_timestamp = 0.0; |
| 94 | } |