blob: 5237a923b391932fc117fe66a3f8ae1fc677e02d [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
Brian Silverman1a675112016-02-20 20:42:49 -05002/* Copyright (c) FIRST 2008-2016. All Rights Reserved. */
Brian Silverman26e4e522015-12-17 01:56:40 -05003/* Open Source Software - may be modified and shared by FRC teams. The code */
Brian Silverman1a675112016-02-20 20:42:49 -05004/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
Brian Silverman26e4e522015-12-17 01:56:40 -05006/*----------------------------------------------------------------------------*/
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
20void 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
30Error::Code Error::GetCode() const { return m_code; }
31
32std::string Error::GetMessage() const { return m_message; }
33
34std::string Error::GetFilename() const { return m_filename; }
35
36std::string Error::GetFunction() const { return m_function; }
37
38uint32_t Error::GetLineNumber() const { return m_lineNumber; }
39
40const ErrorBase* Error::GetOriginatingObject() const {
41 return m_originatingObject;
42}
43
44double Error::GetTimestamp() const { return m_timestamp; }
45
46void 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
68void Error::Report() {
Brian Silverman1a675112016-02-20 20:42:49 -050069 std::stringstream locStream;
70 locStream << m_function << " [";
Brian Silverman26e4e522015-12-17 01:56:40 -050071
Brian Silverman1a675112016-02-20 20:42:49 -050072#if defined(_WIN32)
Brian Silverman26e4e522015-12-17 01:56:40 -050073 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 Silverman1a675112016-02-20 20:42:49 -050076 locStream << basename;
77#else
78 locStream << basename(m_filename.c_str());
Brian Silverman26e4e522015-12-17 01:56:40 -050079#endif
Brian Silverman1a675112016-02-20 20:42:49 -050080 locStream << ":" << m_lineNumber << "]";
Brian Silverman26e4e522015-12-17 01:56:40 -050081
Brian Silverman1a675112016-02-20 20:42:49 -050082 DriverStation::ReportError(true, m_code, m_message, locStream.str(),
83 GetStackTrace(4));
Brian Silverman26e4e522015-12-17 01:56:40 -050084}
85
86void 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}