John
John

Reputation: 1467

Cross-platform error message

I want to display an error message when my C++ cross-platform (Windows, Linux, Mac) OpenGL application fails to initialize to let the user know what went wrong.

My ideas so far:

  1. Write errors to std::cerr
    • Problem: Shell not displayed on Linux when not running from it
    • Problem: Console always displayed on Windows, even if not needed (if compiled as console application)
  2. Use GUI library
    • Problem: Overkill since everything else will be displayed using OpenGL. I get the window/context using GLFW.
  3. Platform-specific implementations and #ifdef
    • Problem: I would rather not want to mess with native APIs and potentially have to expand support later

How would you solve this?

Upvotes: 1

Views: 1331

Answers (2)

Klaim
Klaim

Reputation: 69682

The GUI solution is actually a good one but the problem is indeed that you would need another dependency, unless...

...you just code one function for each platform, which would report the error using the GUI code for each platform.

You will only implement one of these error function report for your primary platform, then through porting, write the other versions. If you already work directly with all the platforms, then do it once for all platforms and go with it.

As it's just to report a message, it shouldn't be hard, long or even problematic.

Just follow the K.I.S.S. principle.

(note: the message can be displayed on the next startup instead of when the problem occurs, which would certainly be more safe)


That being said, in the future you might want more complex error reporting system. In this case, it is worth investing into developing your error report code as a separate project which would have it's own dependencies and would just be triggered by any problem from the main project (whatever the way it communicates). In this case, it's ok to have another dependency like Qt (though it's kind of heavyweight...) or wxWidgets (kind of too) or GTK (my experience with it is not positive but it's ok for simple things). One GUI framework that is designed to be lightweight and simple is FLTK, so it might be simple and lightweight enough to sit beside your application.

Upvotes: 1

user1764961
user1764961

Reputation: 693

wxWidgets (or similar cross platform framework). Or, manually what you suggested under 3.

Upvotes: 1

Related Questions