131 lines
3.5 KiB
C
Raw Normal View History

2013-08-29 11:45:22 +09:00
/*
Copyright (c) 2013 yvt
2013-08-29 11:45:22 +09:00
This file is part of OpenSpades.
2013-08-29 11:45:22 +09:00
OpenSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2013-08-29 11:45:22 +09:00
OpenSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2013-08-29 11:45:22 +09:00
You should have received a copy of the GNU General Public License
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
2013-08-29 11:45:22 +09:00
*/
2013-08-18 16:18:06 +09:00
#pragma once
#include <vector>
2013-08-18 16:18:06 +09:00
#include "Exception.h"
namespace spades {
namespace reflection {
class Function {
2013-08-18 16:18:06 +09:00
const char *name;
const char *file;
int line;
2013-08-18 16:18:06 +09:00
public:
Function(const char *name, const char *File, int line);
2013-08-18 16:18:06 +09:00
const char *GetName() const { return name; }
const char *GetFileName() const { return file; }
int GetLineNumber() const { return line; }
};
2013-08-18 16:18:06 +09:00
class Backtrace;
2013-08-18 16:18:06 +09:00
class BacktraceEntry {
Function *function;
2013-08-18 16:18:06 +09:00
public:
BacktraceEntry() {}
BacktraceEntry(Function *f) : function(f) {}
const Function &GetFunction() const { return *function; }
2013-08-18 16:18:06 +09:00
};
2013-08-18 16:18:06 +09:00
class BacktraceEntryAdder {
Backtrace *bt;
2013-08-18 16:18:06 +09:00
public:
BacktraceEntryAdder(const BacktraceEntry &);
2013-08-18 16:18:06 +09:00
~BacktraceEntryAdder();
};
2014-03-16 22:52:24 +09:00
typedef std::vector<BacktraceEntry> BacktraceRecord;
2013-08-18 16:18:06 +09:00
class Backtrace {
std::vector<BacktraceEntry> entries;
2013-08-18 16:18:06 +09:00
public:
static Backtrace *GetGlobalBacktrace();
static void ThreadExiting();
static void StartBacktrace();
void Push(const BacktraceEntry &);
2013-08-18 16:18:06 +09:00
void Pop();
2014-03-16 22:52:24 +09:00
BacktraceRecord GetAllEntries();
BacktraceRecord GetRecord() { return GetAllEntries(); }
2013-08-18 16:18:06 +09:00
std::string ToString() const;
};
std::string BacktraceRecordToString(const BacktraceRecord &);
2013-08-18 16:18:06 +09:00
}
2013-08-26 01:27:44 +09:00
void StartLog();
void LogMessage(const char *file, int line, const char *format, ...)
#ifdef __GNUC__
__attribute__((format(printf, 3, 4)))
#endif
;
2013-08-18 16:18:06 +09:00
}
#ifdef _MSC_VER
#define __PRETTY_FUNCTION__ __FUNCDNAME__
#endif
#define SPADES_MARK_FUNCTION() \
static ::spades::reflection::Function thisFunction(__PRETTY_FUNCTION__, __FILE__, __LINE__); \
::spades::reflection::BacktraceEntryAdder backtraceEntryAdder( \
(::spades::reflection::BacktraceEntry(&thisFunction)))
2013-08-18 16:18:06 +09:00
#if NDEBUG
#define SPADES_MARK_FUNCTION_DEBUG() \
do { \
} while (0)
2013-08-18 16:18:06 +09:00
#else
#define SPADES_MARK_FUNCTION_DEBUG() SPADES_MARK_FUNCTION()
#endif
#if NDEBUG
#define SPAssert(cond) \
do { \
} while (0)
2013-08-18 16:18:06 +09:00
#else
#define SPAssert(cond) ((!(cond)) ? SPRaise("SPAssertion failed: %s", #cond) : (void)0)
2013-08-18 16:18:06 +09:00
#endif
#ifdef _MSC_VER
#define SPLog(format, ...) ::spades::LogMessage(__FILE__, __LINE__, format, __VA_ARGS__)
#else
#define SPLog(format, args...) ::spades::LogMessage(__FILE__, __LINE__, format, ##args)
#endif
2013-08-18 16:18:06 +09:00
#ifdef __GNUC__
#define DEPRECATED(func) func __attribute__((deprecated))
2013-08-18 16:18:06 +09:00
#elif defined(_MSC_VER)
#define DEPRECATED(func) __declspec(deprecated) func
#else
#pragma message("WARNING: You need to implement DEPRECATED for this compiler")
#define DEPRECATED(func) func
#endif