1// LAF Base Library
2// Copyright (c) 2021 Igara Studio S.A.
3// Copyright (c) 2001-2016 David Capello
4//
5// This file is released under the terms of the MIT license.
6// Read LICENSE.txt for more information.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#ifdef _DEBUG
13
14#include "base/debug.h"
15
16#include "base/convert_to.h"
17#include "base/string.h"
18
19#include <cstdarg>
20#include <cstdlib>
21#include <iostream>
22#include <string>
23#include <vector>
24
25#if LAF_WINDOWS
26 #include <windows.h>
27#endif
28
29int base_assert(const char* condition, const char* file, int lineNum)
30{
31#if LAF_WINDOWS
32
33 std::vector<wchar_t> buf(MAX_PATH);
34 GetModuleFileNameW(NULL, &buf[0], MAX_PATH);
35
36 int ret = _CrtDbgReportW(_CRT_ASSERT,
37 base::from_utf8(file).c_str(),
38 lineNum,
39 &buf[0],
40 base::from_utf8(condition).c_str());
41
42 return (ret == 1 ? 1: 0);
43
44#else
45
46 std::string text = file;
47 text += ":";
48 text += base::convert_to<std::string>(lineNum);
49 text += ": Assertion failed: ";
50 text += condition;
51 std::cerr << text << std::endl;
52 std::abort();
53 return 1;
54
55#endif
56}
57
58void base_trace(const char* msg, ...)
59{
60 va_list ap;
61 va_start(ap, msg);
62 char buf[4096];
63 vsprintf(buf, msg, ap);
64 va_end(ap);
65
66#if LAF_WINDOWS
67 _CrtDbgReport(_CRT_WARN, NULL, 0, NULL, buf);
68#endif
69
70 std::cerr << buf << std::flush;
71}
72
73#endif
74