···11+/*
22+This file is part of Darling.
33+44+Copyright (C) 2020 Lubos Dolezel
55+66+Darling is free software: you can redistribute it and/or modify
77+it under the terms of the GNU General Public License as published by
88+the Free Software Foundation, either version 3 of the License, or
99+(at your option) any later version.
1010+1111+Darling is distributed in the hope that it will be useful,
1212+but WITHOUT ANY WARRANTY; without even the implied warranty of
1313+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1414+GNU General Public License for more details.
1515+1616+You should have received a copy of the GNU General Public License
1717+along with Darling. If not, see <http://www.gnu.org/licenses/>.
1818+*/
1919+#include <CoreServices/Debugging.h>
2020+#include <iostream>
2121+#include <sstream>
2222+#include <algorithm>
2323+#include "UserBreak.h"
2424+2525+static void defaultHandler(OSType componentSignature, UInt32 options, const char *assertionString,
2626+ const char *exceptionLabelString, const char *errorString, const char *fileName,
2727+ long lineNumber, ConstStr255Param outputMsg)
2828+{
2929+ DebugStr(outputMsg);
3030+}
3131+3232+static DebugAssertOutputHandlerUPP debugAssertHandler = defaultHandler;
3333+3434+void DebugAssert(OSType componentSignature, UInt32 options, const char *assertionString,
3535+ const char *exceptionLabelString, const char *errorString, const char *fileName,
3636+ long lineNumber, void *value)
3737+{
3838+ Str255 out;
3939+4040+ std::stringstream ss;
4141+ ss << "DebugAssert: " << assertionString << " " << exceptionLabelString
4242+ << " " << errorString << " [" << fileName << ":" << lineNumber << "]\n";
4343+4444+ std::string str = ss.str();
4545+ out[0] = std::min<uint8_t>(str.length(), 255);
4646+ memcpy(&out[1], str.c_str(), out[0]);
4747+4848+ debugAssertHandler(componentSignature, options, assertionString, exceptionLabelString, errorString, fileName, lineNumber, out);
4949+}
5050+5151+void InstallDebugAssertOutputHandler(DebugAssertOutputHandlerUPP handler)
5252+{
5353+ if (handler == nullptr)
5454+ debugAssertHandler = defaultHandler;
5555+ else
5656+ debugAssertHandler = handler;
5757+}
5858+5959+UInt32 TaskLevel(void)
6060+{
6161+ return 0;
6262+}
6363+