开发者

What kind of good approaches do C++ programmers use for storing error messages?

Say I have a huge amount of code and have different kinds of error messages. For these, I want to have a separate place where I store error codes and error messages. For example, for an error that occured because the program could not open a file I store:

F001    "Can not open a file." "The same error message in another language" "The same error message in a third language"

What is the best way to store different kinds of error messages and codes in a file for C++ programmers in order to use them quickly and easily in a programme?

FYI I am working w开发者_JS百科ith the Qt lib.


I would say that the strategy is extremely different between:

  • an error message for the logs
  • an error message to be presented to the user

Since you speak of internationalization, I would suppose that you need to present it to the user. In this case it's not different of any other string.

The gettext library should come in handy :)


You can put them in your resource bundle, where all other strings are. Then use their resource code and you will have the benefit, that all error messages are translated in the different languages


I would use XML like this:

<languageList>
    <language short="de" long="Deutsch" default="true" />
    <language short="en" long="English" />
</languageList>
<string alias="couldNotOpenFileError"
    de="Konnte Datei nicht öffnen"
    en="Could not open file"
    />
<string alias="couldNotWriteFileError"
    de="Konnte Datei nicht schreiben"
    en="Could not write file"
    />

In code you could use it like this where you set the actual language in the message pool.

String errorMsg = ErrorMessagePool.get("couldNotOpenFileError") + additionalInformationString;

Edit: The idea of the message pool is just to wrap an existing xml parser+some additional logic.


Which O/S? On Windows, one standard but relatively complicated way is to use Message Files.


In actually prefer to generate error messages in the body of the code rather than keep them in a separate file - for example (picked at random from my own code):

if ( ev[0] != SUB_CMD ) {
    CSVTHROW( "Invalid edit sub-command '" << ev[0] << "' in '" << ev << "' " );
}

This is for three reasons:

  • It allows for more inteligible error messages, with the invalid values interspersed with the message text

  • Associating the error test directly with the message makes it clear what is going on

  • I have seen too many projects which used the WRONG error code for an error, which is hard to test for and causes immense confusion. For example if F001 is your file open error, then I've seen lots of code like this:

-

if ( login_invalid() ) {
   ERROR( "F001" );
}

Of course, doing this makes internationalisation hard, but that's not a concern for the kind of projects I do.


This is not specific to error messages, but in terms of Qt, if you are think about internationalisation and possibly having to translate all your user facing text the Linguist Manual (Programmers Section) is a must read. I personally also favor the approach where errormessages are literal at the point where the error occurs, by using QObject::tr() whenever a free, free as in plain embedded in the code, user facing string occurs you are well on your way to keep things working and enable a later translation. tr() supports a string qualifier to mark up the text to be translated

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜