- Home /
How to print/debug from a c plugin
The title says it all, I have try'd a couple(std::out,printf), but none of them go through to any where.. I miss Debug.Log :(
$$anonymous$$y approach has been to pop up a new console with AllocConsole and freopen stdout. From there you can printf to your heart's content. I usually tuck it in the UnityPluginLoad function that is exposed by the Native Rendering Plugin interface (see their example project). You can just as easily stick it in your own "init" function as well.
#include "windows.h"
#include "stdio.h"
#include "Unity\IUnityGraphics.h"
static HWND s_hConsole = NULL;
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces* unityInterfaces)
{
s_hConsole = GetConsoleWindow();
if (s_hConsole == NULL)
{
AllocConsole();
s_hConsole = GetConsoleWindow();
}
if (s_hConsole != NULL)
{
freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
}
}
Answer by PaulUsul · Jun 19, 2011 at 03:06 PM
Redirecting stdout to a file works when you can create a file(standalone)
freopen("debug.txt", "a", stdout);
printf("SetTexture id:%d\n");
Answer by bleater · Jun 14, 2012 at 06:45 AM
fprintf(stderr, "something"); writes to ~/Library/Logs/Unity/Editor.log and/or ~/Library/Logs/Unity/Player.log just fine for me.
Another alternative is to use syslog. E.g. somewhere near the start of your app: #include openlog("My identifier", LOG_CONS, LOG_USER);
and on shutdown: closelog();
to write to the log: const char *error = "fubar"; syslog(LOG_ERR, "Error: %s", error);
Answer by elenzil · Sep 05, 2018 at 10:10 PM
writing 6 years after the previous answers, and using unity 2017, i also see fprintf(stdout, ...) or fprintf(stderr, ...) working as expected, except that it seems like it's (now) also necessary to do a fflush() as well. this seems like a change in behavior on unity's part.
Answer by Fattie · Jan 15, 2019 at 05:54 PM
From c++ you have to do this:
1) in your plugin code (ie, in Xcode)
2) use ::fprintf(stderr, "some text \n");
3) for example
static void UNITY_INTERFACE_API OnRenderEvent(int eventID)
{
if (s_CurrentAPI == NULL) return;
::fprintf(stderr, "howdy log \n"); // don't forget the newline
ModifyTexturePixels();
}
4) on mac, open terminal
5) cd ~/Library/Logs/Unity
6) tail -f Editor.log
7) In Unity, click "Play"
You will now see the log (from the fprintf statement) in real time as the project Plays.
In an iOS project / C plugin, it's easier:
Just
#include <stdio.h>
and then normal printf statements do appear on console
void set_texture_from_unity(void* textureHandle, int w, int h) {
printf("set texture called \n");
° it will appear on the console in Xcode
° when you run the final built app on tethered iPhone
(Recall it's generally impossible to run iPhone apps with native plugins in the Editor; you have to go through to a device anyway.)
Your answer
Follow this Question
Related Questions
Unity plugin folder 0 Answers
How to access USB on Linux 1 Answer
Android Multi-Plugins 0 Answers
Using 3rd Party shared library on Android with NDK 0 Answers