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
0
Question by NoseKills · Oct 25, 2016 at 09:50 PM · custom editorcompilerconsoledebug.logwarnings

How to access compiler warnings to i.e. output them to custom console?

If you have used for example an obsolete method like Application.LoadLevel(), you will see a compiler warning about it in Unity's console window every time the compiler finishes compiling/importing scripts. I'm making a custom console implementation and would like to get the compiler warnings to show in the custom window as well.

Using a custom ILogHandler for example doesn't seem to work in this case, which kinda makes sense, since how could a piece of code output warnings that happen when it's only just under compilation :)

Is there an easier way to get access to the compiler output than parsing Editor.log? I did notice the compiler outputs the warnings there.

Here's a part of what I tried with ILogHandler, not that it matters since I don't think this is the way to go anyway, It adds a "my log" prefix to all console output just to demonstrate which pieces of logging i can access from code to do something with them.

editor/SomeEditorWindow.cs

 using UnityEditor;
 using UnityEditor.Callbacks;
 using UnityEngine;
 
 public class SomeEditorWindow : EditorWindow {
     [MenuItem("Window/SomeEditorWindow")]
     static void Init() {
         ((SomeEditorWindow)GetWindow(typeof(SomeEditorWindow))).Show();
     }
 
     private void OnEnable() {
         new SomeLogHandler();
         Debug.Log(" OnEnable ");
     }
     
     [DidReloadScripts]
     private static void OnScriptsReloaded() {
         new SomeLogHandler();
         Debug.Log(" OnScriptsReloaded ");
     }
 }
 
 class MyAllPostprocessor : AssetPostprocessor {
     static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) {
         Debug.Log(" OnPostprocessAllAssets 1 ");
         new SomeLogHandler();
         Debug.Log(" OnPostprocessAllAssets 2 ");
     }
 }

SomeLogHandler.cs

 using UnityEngine;
 using System;
 
 public class SomeLogHandler : ILogHandler {
     private readonly ILogHandler defaultLogHandler = Debug.logger.logHandler;
 
     public SomeLogHandler() {
         if (!(Debug.logger.logHandler is SomeLogHandler)) {
             Debug.logger.logHandler = this;
         }
     }
 
     public void LogFormat(LogType logType, UnityEngine.Object context, string format, params object[] args) {
 // was hoping to capture compiler warnings here
         defaultLogHandler.LogFormat(logType, context, "my log " + format, args);
     }
 
     public void LogException(Exception exception, UnityEngine.Object context) {
         defaultLogHandler.LogException(exception, context);
     }
 }
 
 public class Test {
     public Test() {
         Application.LoadLevel("intentional compiler warning bcos obsolete");
     }
 }

If you click window-> SomeEditorWindow and then right-click and reimport SomeLogHandler.cs, the console will output console output

So everything except the compiler warning gets the desired "my log" prefix.

console.png (24.3 kB)
Comment
Add comment
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

1 Reply

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

Answer by Cynikal · Oct 26, 2016 at 01:46 AM

  var output : String = "";
     var stack : String = "";
     function OnEnable () {
         Application.RegisterLogCallback(HandleLog);
     }
     function OnDisable () {
         // Remove callback when object goes out of scope
         Application.RegisterLogCallback(null);
     }
     function HandleLog (logString : String, stackTrace : String, type : LogType) {
         output = logString;
         stack = stackTrace;
     }


You'd have to do some wizardry, but that should get you going in the right direction.

Comment
Add comment · Show 1 · 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 NoseKills · Oct 26, 2016 at 05:05 PM 0
Share

Thanks. RegisterLogCallback seems to be obsolete but Application.log$$anonymous$$essageReceived is there to replace it. Perhaps add that to your answer.

The callback catches compiler warnings and errors too. I can save the logs in EditorPrefs to get a hold of them even after the codebase gets recompiled.

Now i just gotta figure out how to juggle the listeners not to miss anything.

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

58 People are following this question.

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

Related Questions

How to re-trigger all compiler warnings in the Editor?? 1 Answer

Unity Console not showing result 1 Answer

Is it possible to make a console entry (debug.log/print statement) with more than 2 lines of consecutive content? 1 Answer

Is there a logging method, similar to C#'s Console.WriteLine? (what I want is the {N}) 3 Answers

The current score variable not updating when an enemy is hit (Using OnTriggerEnter2D function),Counter for current score not working with OnTriggerEnter2D function. 1 Answer


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