blob: 499f9b4a700bb7194fc6085b8addd4a858893130 [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -08002/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
Brian Silverman41cdd3e2019-01-19 19:48:58 -08003/* 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 "frc/Utility.h"
9
10#ifndef _WIN32
11#include <cxxabi.h>
12#include <execinfo.h>
13#endif
14
15#include <cstdio>
16#include <cstdlib>
17#include <cstring>
18
19#include <hal/DriverStation.h>
20#include <hal/HAL.h>
21#include <wpi/Path.h>
22#include <wpi/SmallString.h>
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080023#include <wpi/StackTrace.h>
Brian Silverman41cdd3e2019-01-19 19:48:58 -080024#include <wpi/raw_ostream.h>
25
26#include "frc/ErrorBase.h"
27
28using namespace frc;
29
30bool wpi_assert_impl(bool conditionValue, const wpi::Twine& conditionText,
31 const wpi::Twine& message, wpi::StringRef fileName,
32 int lineNumber, wpi::StringRef funcName) {
33 if (!conditionValue) {
34 wpi::SmallString<128> locBuf;
35 wpi::raw_svector_ostream locStream(locBuf);
36 locStream << funcName << " [" << wpi::sys::path::filename(fileName) << ":"
37 << lineNumber << "]";
38
39 wpi::SmallString<128> errorBuf;
40 wpi::raw_svector_ostream errorStream(errorBuf);
41
42 errorStream << "Assertion \"" << conditionText << "\" ";
43
44 if (message.isTriviallyEmpty() ||
45 (message.isSingleStringRef() && message.getSingleStringRef().empty())) {
46 errorStream << "failed.\n";
47 } else {
48 errorStream << "failed: " << message << "\n";
49 }
50
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080051 std::string stack = wpi::GetStackTrace(2);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080052
53 // Print the error and send it to the DriverStation
54 HAL_SendError(1, 1, 0, errorBuf.c_str(), locBuf.c_str(), stack.c_str(), 1);
55 }
56
57 return conditionValue;
58}
59
60/**
61 * Common error routines for wpi_assertEqual_impl and wpi_assertNotEqual_impl.
62 *
63 * This should not be called directly; it should only be used by
64 * wpi_assertEqual_impl and wpi_assertNotEqual_impl.
65 */
66void wpi_assertEqual_common_impl(const wpi::Twine& valueA,
67 const wpi::Twine& valueB,
68 const wpi::Twine& equalityType,
69 const wpi::Twine& message,
70 wpi::StringRef fileName, int lineNumber,
71 wpi::StringRef funcName) {
72 wpi::SmallString<128> locBuf;
73 wpi::raw_svector_ostream locStream(locBuf);
74 locStream << funcName << " [" << wpi::sys::path::filename(fileName) << ":"
75 << lineNumber << "]";
76
77 wpi::SmallString<128> errorBuf;
78 wpi::raw_svector_ostream errorStream(errorBuf);
79
80 errorStream << "Assertion \"" << valueA << " " << equalityType << " "
81 << valueB << "\" ";
82
83 if (message.isTriviallyEmpty() ||
84 (message.isSingleStringRef() && message.getSingleStringRef().empty())) {
85 errorStream << "failed.\n";
86 } else {
87 errorStream << "failed: " << message << "\n";
88 }
89
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080090 std::string trace = wpi::GetStackTrace(3);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080091
92 // Print the error and send it to the DriverStation
93 HAL_SendError(1, 1, 0, errorBuf.c_str(), locBuf.c_str(), trace.c_str(), 1);
94}
95
96bool wpi_assertEqual_impl(int valueA, int valueB,
97 const wpi::Twine& valueAString,
98 const wpi::Twine& valueBString,
99 const wpi::Twine& message, wpi::StringRef fileName,
100 int lineNumber, wpi::StringRef funcName) {
101 if (!(valueA == valueB)) {
102 wpi_assertEqual_common_impl(valueAString, valueBString, "==", message,
103 fileName, lineNumber, funcName);
104 }
105 return valueA == valueB;
106}
107
108bool wpi_assertNotEqual_impl(int valueA, int valueB,
109 const wpi::Twine& valueAString,
110 const wpi::Twine& valueBString,
111 const wpi::Twine& message, wpi::StringRef fileName,
112 int lineNumber, wpi::StringRef funcName) {
113 if (!(valueA != valueB)) {
114 wpi_assertEqual_common_impl(valueAString, valueBString, "!=", message,
115 fileName, lineNumber, funcName);
116 }
117 return valueA != valueB;
118}