1 // This file is part of VSTGUI. It is subject to the license terms
2 // in the LICENSE file found in the top-level directory of this
3 // distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
4 
5 #include "vstguidebug.h"
6 #include <cstdarg>
7 
8 #if DEBUG
9 
10 #include "cstring.h"
11 
12 #if WINDOWS
13 	#include "platform/win32/win32support.h"
14 #endif
15 
16 #include <cstdio>
17 
18 namespace VSTGUI {
19 
20 //-----------------------------------------------------------------------------
TimeWatch(UTF8StringPtr name,bool startNow)21 TimeWatch::TimeWatch (UTF8StringPtr name, bool startNow)
22 : startTime (0)
23 {
24 	this->name = name ? name : "";
25 	if (startNow)
26 		start ();
27 }
28 
29 //-----------------------------------------------------------------------------
~TimeWatch()30 TimeWatch::~TimeWatch () noexcept
31 {
32 	stop ();
33 }
34 
35 //-----------------------------------------------------------------------------
start()36 void TimeWatch::start ()
37 {
38 	startTime = std::clock ();
39 }
40 
41 //-----------------------------------------------------------------------------
stop()42 void TimeWatch::stop ()
43 {
44 	if (startTime > 0)
45 	{
46 		clock_t stopTime = std::clock ();
47 		DebugPrint ("%s took %d\n", name.data (), stopTime - startTime);
48 		startTime = 0;
49 	}
50 }
51 
52 //-----------------------------------------------------------------------------
DebugPrint(const char * format,...)53 void DebugPrint (const char *format, ...)
54 {
55 	char string[300];
56 	std::va_list marker;
57 	va_start (marker, format);
58 	std::vsprintf (string, format, marker);
59 	if (string[0] == 0)
60 		std::strcpy (string, "Empty string\n");
61 	#if WINDOWS
62 	UTF8StringHelper debugString (string);
63 	OutputDebugString (debugString);
64 	#else
65 	std::fprintf (stderr, "%s", string);
66 	#endif
67 }
68 
69 } // namespace
70 
71 #endif // DEBUG
72 
73 namespace VSTGUI {
74 
75 static AssertionHandler assertionHandler {};
76 
77 //------------------------------------------------------------------------
setAssertionHandler(const AssertionHandler & handler)78 void setAssertionHandler (const AssertionHandler& handler)
79 {
80 	assertionHandler = handler;
81 }
82 
83 //------------------------------------------------------------------------
hasAssertionHandler()84 bool hasAssertionHandler ()
85 {
86 	return assertionHandler ? true : false;
87 }
88 
89 //------------------------------------------------------------------------
doAssert(const char * filename,const char * line,const char * desc)90 void doAssert (const char* filename, const char* line, const char* desc) noexcept (false)
91 {
92 #if NDEBUG
93 	if (!hasAssertionHandler ())
94 		return;
95 #endif
96 	if (hasAssertionHandler ())
97 	{
98 		try {
99 			assertionHandler (filename, line, desc);
100 		} catch (...)
101 		{
102 		 std::rethrow_exception (std::current_exception());
103 		}
104 	}
105 #if DEBUG
106 	else
107 	{
108 		DebugPrint ("\nassert at %s:%s: %s\n", filename, line, desc ? desc : "unknown");
109 		assert (false);
110 	}
111 #endif // DEBUG
112 }
113 
114 } // namespace
115