Skip to content

Commit

Permalink
[GL] Improve formatting of error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
hyazinthh committed Dec 9, 2023
1 parent cd91be4 commit c15620c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
31 changes: 24 additions & 7 deletions src/Aardvark.Rendering.GL/Core/DebugOutput.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,37 @@ open Aardvark.Rendering.GL
module Error =

exception OpenGLException of ec : ErrorCode * msg : string with
override x.Message = sprintf "%A: %s" x.ec x.msg
override x.Message = $"{x.msg} (Error: {x.ec})"

type GL with
static member private Check(str, throwOnError) =
let err = GL.GetError()
if err <> ErrorCode.NoError then
let str = $"{str}"
let msg =
if String.IsNullOrEmpty str then "An error occurred"
else string (Char.ToUpper str.[0]) + str.Substring(1)

Report.Error($"[GL] {msg} (Error: {err})")

if throwOnError then
raise <| OpenGLException(err, msg)

/// Gets the value of the GL error flag and logs it
/// with the given message string if it is not equal to GL_NO_ERROR.
/// Throws an exception after logging if DebugConfig.ErrorFlagCheck = ThrowOnError.
/// Does nothing if DebugConfig.ErrorFlagCheck = Disabled.
static member Check str =
let mode = GL.CheckErrors

if mode <> ErrorFlagCheck.Disabled then
let err = GL.GetError()
if err <> ErrorCode.NoError then
let message = $"[GL] {str} ({err})"
Report.Error(message)
let throwOnError = (mode = ErrorFlagCheck.ThrowOnError)
GL.Check(str, throwOnError)

if mode = ErrorFlagCheck.ThrowOnError then
raise <| OpenGLException(err, message)
/// Gets the value of the GL error flag and throws an exception
/// with the given message string if it is not equal to GL_NO_ERROR.
static member Assert str =
GL.Check(str, true)

[<AutoOpen>]
module private IGraphicsContextDebugExtensions =
Expand Down
9 changes: 7 additions & 2 deletions src/Aardvark.Rendering.GL/Core/Utilities/Common.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ module private ErrorUtilities =

let inline failf fmt =
Printf.kprintf (fun str ->
Report.Error $"[GL] {str}"
failwith ("[GL] " + str)
let str =
if String.IsNullOrEmpty str then "An error occurred"
else string (Char.ToUpper str.[0]) + str.Substring(1)

let msg = $"[GL] {str}"
Report.Error msg
failwith msg
) fmt

[<AutoOpen>]
Expand Down

0 comments on commit c15620c

Please sign in to comment.