Add base_assert/trace functions

This commit is contained in:
David Capello 2014-09-20 23:51:56 -03:00
parent 6a10b77baf
commit ff49b50047
4 changed files with 118 additions and 1 deletions

View File

@ -20,6 +20,7 @@ set(BASE_SOURCES
chrono.cpp chrono.cpp
connection.cpp connection.cpp
convert_to.cpp convert_to.cpp
debug.cpp
errno_string.cpp errno_string.cpp
exception.cpp exception.cpp
file_handle.cpp file_handle.cpp

75
src/base/debug.cpp Normal file
View File

@ -0,0 +1,75 @@
// Aseprite Base Library
// Copyright (c) 2001-2014 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef _DEBUG
#include "base/debug.h"
#include "base/convert_to.h"
#include "base/string.h"
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#ifdef WIN32
#include <windows.h>
#endif
int base_assert(const char* condition, const char* file, int lineNum)
{
#ifdef WIN32
std::vector<wchar_t> buf(MAX_PATH);
GetModuleFileNameW(NULL, &buf[0], MAX_PATH);
int ret = _CrtDbgReportW(_CRT_ASSERT,
base::from_utf8(file).c_str(),
lineNum,
&buf[0],
base::from_utf8(condition).c_str());
return (ret == 1 ? 1: 0);
#else
std::string text = "Assertion failed: ";
text += condition;
text += ", file ";
text += file;
text += ", line ";
text += base::convert_to<std::string>(lineNum);
std::cerr << text << std::endl;
return 1;
#endif
}
void base_trace(const char* msg, ...)
{
va_list ap;
va_start(ap, msg);
char buf[4096];
vsprintf(buf, msg, ap);
va_end(ap);
#ifdef WIN32
_CrtDbgReport(_CRT_WARN, NULL, 0, NULL, buf);
#else
std::cerr << buf << std::endl;
#endif
}
#endif

37
src/base/debug.h Normal file
View File

@ -0,0 +1,37 @@
// Aseprite Base Library
// Copyright (c) 2001-2014 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifndef BASE_DEBUG_H_INCLUDED
#define BASE_DEBUG_H_INCLUDED
#pragma once
int base_assert(const char* condition, const char* file, int lineNum);
void base_trace(const char* msg, ...);
#ifdef _DEBUG
#ifdef WIN32
#include <crtdbg.h>
#define base_break() _CrtDbgBreak()
#else
#include <signal.h>
#define base_break() raise(SIGTRAP)
#endif
#define ASSERT(condition) { \
if (!(condition)) { \
if (base_assert(#condition, __FILE__, __LINE__)) { \
base_break(); \
} \
} \
}
#define TRACE base_trace
#else
#define ASSERT(condition)
#define TRACE(...)
#endif
#endif

View File

@ -54,7 +54,11 @@ void verbose_printf(const char* format, ...);
#define PI 3.14159265358979323846 #define PI 3.14159265358979323846
#include <allegro/base.h> #include <allegro/base.h>
#include <allegro/debug.h> // ASSERT #include <allegro/debug.h>
#undef ASSERT
#undef TRACE
#include "base/debug.h"
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// Overloaded new/delete operators to detect memory-leaks // Overloaded new/delete operators to detect memory-leaks