简体   繁体   English

跨平台错误消息

[英]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. 当我的C ++跨平台(Windows,Linux,Mac)OpenGL应用程序无法初始化以让用户知道出错时,我想显示错误消息。

My ideas so far: 我的想法到目前为止:

  1. Write errors to std::cerr 将错误写入std :: cerr
    • Problem: Shell not displayed on Linux when not running from it 问题:未在Linux上运行时,Shell不会显示在Linux上
    • Problem: Console always displayed on Windows, even if not needed (if compiled as console application) 问题:控制台始终显示在Windows上,即使不需要(如果编译为控制台应用程序)
  2. Use GUI library 使用GUI库
    • Problem: Overkill since everything else will be displayed using OpenGL. 问题:过度杀伤,因为其他一切都将使用OpenGL显示。 I get the window/context using GLFW . 我使用GLFW获取窗口/上下文。
  3. Platform-specific implementations and #ifdef 特定于平台的实现和#ifdef
    • Problem: I would rather not want to mess with native APIs and potentially have to expand support later 问题:我宁愿不想使用本机API,也可能不得不在以后扩展支持

How would you solve this? 你怎么解决这个问题?

wxWidgets (or similar cross platform framework). wxWidgets(或类似的跨平台框架)。 Or, manually what you suggested under 3. 或者,手动根据您的建议3。

The GUI solution is actually a good one but the problem is indeed that you would need another dependency, unless... GUI解决方案实际上是一个很好的解决方案,但问题确实是你需要另一个依赖,除非......

...you just code one function for each platform, which would report the error using the GUI code for each platform. ...您只需为每个平台编写一个函数,它将使用每个平台的GUI代码报告错误。

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 KISS principle. 只需遵循KISS原则。

(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). 在这种情况下,可以有另一个依赖,比如Qt(尽管它有点重量级......)或wxWidgets(也有点)或GTK(我对它的体验不是积极的,但它对于简单的事情是可以的)。 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. 一个设计为轻量级和简单的GUI框架是FLTK,因此它可能非常简单轻便,可以放在应用程序旁边。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM