Skip to content

Commit

Permalink
Avoid cerr stuttering
Browse files Browse the repository at this point in the history
This also considerably simplifies the code. You may be concerned that I
no longer print the reportType. That is because the message already
contains it.
  • Loading branch information
EduardGomezEscandell committed Sep 26, 2023
1 parent c0d5d6a commit 63c6867
Showing 1 changed file with 6 additions and 23 deletions.
29 changes: 6 additions & 23 deletions msix/storeapi/dllmain.cpp
Original file line number Diff line number Diff line change
@@ -1,36 +1,19 @@
// dllmain.cpp : Defines the entry point for the DLL application.
#include <stdexcept>

#include "framework.hpp"

#ifdef _MSC_VER
#include <Windows.h>

#include <iostream>
#include <string_view>

int DebugReportHook(int reportType, char* message, int* returnValue) {
const auto type = [=]() -> std::string_view {
switch (reportType) {
case _CRT_WARN:
return "[WARNING]";
case _CRT_ERROR:
return "[ERROR]";
case _CRT_ASSERT:
return "[ASSERT]";
default:
return "[UNKNOWN]";
}
}();

std::cerr << type << ' ' << message << std::endl;
throw std::runtime_error(message);
}
#else
#define _CrtSetReportHook(x)
constexpr void _CrtSetReportHook(auto) {}
#endif // _MSC_VER

BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call,
LPVOID lpReserved) {
_CrtSetReportHook(DebugReportHook);
_CrtSetReportHook([](int reportType, char* message, int* returnValue) -> int {
throw std::runtime_error(message);
});

switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
Expand Down

0 comments on commit 63c6867

Please sign in to comment.