pokemoen's picture

glIntercept like logging

Project:The Open Toolkit library
Category:feature request

The addition of the automatic glError checking gave me the idea to try and implement glIntercept like logging.

So far I came up with this added to Bind.Structures.Function.CreateBody()

			f.Body.Add("#if DEBUG");
			List<string> argNames = new List<string>();
			List<string> argFormat = new List<string>();
			int i = 0;
			foreach (Parameter item in f.Parameters) {
				string name = item.Name;
				// check if plain old pointer, which can not be passed to String.Format
				if (item.WrapperType == WrapperTypes.PointerParameter ||
						item.WrapperType == WrapperTypes.ArrayParameter ||
						item.WrapperType == WrapperTypes.ReferenceParameter) {
					name = "new IntPtr(" + name + ").ToString()";
				argFormat.Add("{" + i++ + "}");
			string args = argNames.Count == 0 ? "" : ", " + String.Join(", ", argNames.ToArray());
			// using + here to build up the string because we are inserting "{n}"
			f.Body.Add("System.Diagnostics.Debug.WriteLine(String.Format(\"" + f.Name + "(" +
				String.Join(", ", argFormat.ToArray()) + ")\"" + args + "));");

this adds code like:

            #if DEBUG
            System.Diagnostics.Debug.WriteLine(String.Format("Viewport({0}, {1}, {2}, {3})", x, y, width, height));

To make this usefull I think there at least needs to be:

  1. A way to enable / disable logging
  2. File output

Would love some comments.


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
pokemoen's picture


The reason to have this could be to have logging on platforms that glIntercept does not support, maybe it will be easier to use too. Currently I am renaming the opengl32.dll when I want to enable or disable logging, maybe this feature can be less annoying :)

Also glIntercept does not seem to be actively maintained and there are quite a few new functions of which it can not interpret the parameter values.

If I need dumping of textures, lists and/or shaders I still could use glIntercept as I don't see a way to easily add extra code to dump data to specific generated methods. (tho it can be hacked in one way or another I suppose :) )

pokemoen's picture


I hope writing to file can be alot faster than Debug.Writeline. I am now testing this with a real application and while i'm writing this post the first frame has not even rendered yet.

the Fiddler's picture


Haven't had a chance to try this yet, but there are a few things you could do to speed up Debug.WriteLine:

  • Make sure Debug.AutoFlush is set to false.
  • Increase the buffer size of the FileStream or StreamWriter you are using.
  • Write the log to a MemoryStream and transfer its contents to a file once per frame.
  • Don't direct the output to the console.

I don't think this will ever be fast, however.

the Fiddler's picture


To make this trully useful, we need to modify OpenTK to generate the actual delegate implementations at runtime. This is definitely possible and will *not* result in a speed decrease (since function calls are routed through runtime-allocated delegates anyway), but all my tries to generate the calls with LCG (lightweight code generation) have been met with crashes.

The amazing thing is that this will allow us to turn automatic error checking and logging on/off at runtime, without needing to link with a debug/release OpenTK.dll.

I will revisit this topic in the future, just a heads up that this is still on the table.

the Fiddler's picture


Status:open» postponed

This is material for a post-1.0 release.