Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by PaulUsul · Jun 07, 2011 at 09:17 PM · pluginpluginsprint

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 :(

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VillageID_10t · Nov 24, 2016 at 08:57 PM 0
Share

$$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);
     }
 }





4 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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");
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
3

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);

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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

EntryPointNotFoundException - .so plugin (Linux) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges