Skip to content

Instantly share code, notes, and snippets.

@shadowmint
Created April 22, 2015 00:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shadowmint/89cedb4f7ae10d292f04 to your computer and use it in GitHub Desktop.
Save shadowmint/89cedb4f7ae10d292f04 to your computer and use it in GitHub Desktop.
#pragma once
#include <memory>
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
/// Cast an FString to char * for debugging purposes
#define AS_CSTR(value) api::Debug::AsStr(value).c_str()
namespace api {
/// Debugging helper
/// see Debug.cpp for usage
class Debug {
public:
/// Internal unsafe buffer cache
static string _debug;
/// Convert an FString into a const char *
/// Nb. Don't call this directly. Use AS_CSTR(value)
static string AsStr(FString value) {
string rtn("");
for (auto i = 0; i < value.Len(); ++i) {
rtn += (char) value[i];
}
return rtn;
}
/// Convert a FVector into a const char *
/// Nb. Don't call this directly. Use AS_CSTR(value)
static string AsStr(FVector value) {
return npp::Format("<FVector: %f %f %f>", value.X, value.Y, value.Z);
}
/// Convert a FVector2D into a const char *
/// Nb. Don't call this directly. Use AS_CSTR(value)
static string AsStr(FVector2D value) {
return npp::Format("<FVector2D: %f %f>", value.X, value.Y);
}
/// Trace a debug message
static void Trace(UObject *context, FString value);
/// Trace a debug message dynamically
template<typename ... Args>
static void Trace(UObject *context, const string& format, Args ... args) {
size_t size = 1 + snprintf(nullptr, 0, format.c_str(), args ...);
unique_ptr<char[]> buf(new char[size]);
snprintf(buf.get(), size, format.c_str(), args ...);
auto compiled = string(buf.get(), buf.get() + size);
Debug::Trace(context, FString(&compiled[0]));
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment