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

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

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

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.