Jump to content
  • Advertisement
Sign in to follow this  
garyfletcher

Predefined strings for message output

This topic is 4868 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all. I'm developing my messaging system and I am providing a log clas sthat provides 2 Write functions: void Write(int target, const char* msg, ...); void Write(int target, unsigned long msgID, ...); The 1st is pretty straight foward, just taking a const char* and a vairable argument list much the same as the C printf() function. Now the 2nd function takes a message ID which relates to a pre-defined message in a message.txt file. Currently the file looks like this:
“Everything ok in function %s”
“Unable to load bitmap, %s”
“Memory violation in function %s”
The messages are read into a std::vector and map to vec[0], vec[1]...vec[n]. Now I have been thinking that there is no clear correlation between the string and the message ID..the 1st one in the list is vec[0], 2nd is vec[1]. So I want my messages.txt to be something like:
APPERR0 “Everything ok in function %s”
APPERR1 “Unable to load bitmap, %s”
APPERR2 “Memory violation in function %s”
This way I can seperate messages into functional areas and have message ID groups...some for memory problems, some for application errors, some for SDL problems etc. The messages would be stored in an std::map<int, const char*> and the message ID's defined in message.h: const int APPERR0=0; const int APPERR1=1; " const int APPERRn=n; My problem is mapping the message ID, APPERn, in the message.txt to the equivalent in the messages.h file. Would I need lots of if statements to test the "value" of the message ID when reading in message.txt or is there a more elegant solution? Is the only way to do this by changing the message.txt to:
0 “Everything ok in function %s”
1 “Unable to load bitmap, %s”
2 “Memory violation in function %s”
Losing the correlation of message ID, in message.h" and actual message. OR
APPERR0 0 “Everything ok in function %s”
APPERR1 1 “Unable to load bitmap, %s”
APPERR2 2 “Memory violation in function %s”
Hmmmmm...a such long post for such a simple question....sorry. I hope I've made myself clear.

Share this post


Link to post
Share on other sites
Advertisement
I would do something like this:

Store the messages in a map, where the key is a string, for example "APPERR0".
I would make some enums(app, error, sdl etc) for example AppMessageType.
Then I would make a generitic function that looks something like this: void Write(int target, const char* classString, int id, ...) where classString + id becomes the search key.
Finally a function that acts stort of like a wrapper for each message type. Like void Write(int target, AppMessageType error, ...) that calls Write(target, "APPMESS", error, blaha); (don't really know how to use "..."). If the compiler complains about functions that looks alike(havent really tried multiple instances of the same function with different enums) then you could rename the functions like WriteAppMessage(...)

hope that helps ;)

Share this post


Link to post
Share on other sites
My approach with this would be (since I assume you only change the messages at compile time) to use a custom generation tool and just:

sed -e 's/\(.*\) "\(.*\)"/const char* \1 = "\2";/' messages.txt > messages.h

Which turns text like:
APPERR0 "Everything is \"cool\" here: %s"
into the source code:
const char* APPERR0 = "Everything is \"cool\" here: %s";

Then, using the Write(int,const char*,...) function, you can simply do:


#include "messages.h"

Write(target, APPERR0, __FUNC__);

Share this post


Link to post
Share on other sites
Like the idea of using the functioanl area (sdl, memory etc) as part of the key...thanks sirGustav.

hmmmmm..sed eh...pity I'm using XP..otherwise that'd be great for generating message headers...does XP have an equivalent?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!