Skip to content

Instantly share code, notes, and snippets.

@CarlLee
Last active September 23, 2019 08:01
Show Gist options
  • Save CarlLee/56b8bd2261f4b3d8b8480e4c46ea61b9 to your computer and use it in GitHub Desktop.
Save CarlLee/56b8bd2261f4b3d8b8480e4c46ea61b9 to your computer and use it in GitHub Desktop.
Unity C++ Debug
using AOT;
using System;
using System.Runtime.InteropServices;
using UnityEngine;
public class DebugCPP : MonoBehaviour
{
// Use this for initialization
void OnEnable()
{
RegisterDebugCallback(OnDebugCallback);
}
//------------------------------------------------------------------------------------------------
[DllImport("fftwater", CallingConvention = CallingConvention.Cdecl)]
static extern void RegisterDebugCallback(debugCallback cb);
//Create string param callback delegate
delegate void debugCallback(IntPtr request, int color, int size);
enum Color { red, green, blue, black, white, yellow, orange };
[MonoPInvokeCallback(typeof(debugCallback))]
static void OnDebugCallback(IntPtr request, int color, int size)
{
//Ptr to string
string debug_string = Marshal.PtrToStringAnsi(request, size);
//Add Specified Color
debug_string =
String.Format("{0}{1}{2}{3}{4}",
"<color=",
((Color)color).ToString(),
">",
debug_string,
"</color>"
);
UnityEngine.Debug.Log(debug_string);
}
}
#ifndef DEBUG_CPP_H
#define DEBUG_CPP_H
#include <stdio.h>
#include <string>
#include <stdio.h>
#include <sstream>
#include "PlatformBase.h"
extern "C"
{
//Create a callback delegate
typedef void(*FuncCallBack)(const char* message, int color, int size);
static FuncCallBack callbackInstance = nullptr;
DLLEXPORT void RegisterDebugCallback(FuncCallBack cb);
}
//Color Enum
enum class Color { Red, Green, Blue, Black, White, Yellow, Orange };
class Debug
{
public:
static void Log(const char* message, Color color = Color::Black);
static void Log(const std::string message, Color color = Color::Black);
static void Log(const int message, Color color = Color::Black);
static void Log(const char message, Color color = Color::Black);
static void Log(const float message, Color color = Color::Black);
static void Log(const double message, Color color = Color::Black);
static void Log(const bool message, Color color = Color::Black);
private:
static void send_log(const std::stringstream &ss, const Color &color);
};
#endif
#ifndef LOG_H
#define LOG_H
#ifdef __ANDROID__
#include <android/log.h>
#define LOG(fmt, ...) __android_log_print(ANDROID_LOG_INFO, "fftwater", fmt, __VA_ARGS__);
#else
#include <stdio.h>
#include <Windows.h>
static char debug_log_[1024];
#define LOG(fmt, ...) sprintf(debug_log_, fmt"\n", __VA_ARGS__); OutputDebugStringA(debug_log_); /*fflush(stdout);*/
#endif
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment