Well, considering that this is an error report, you may want to consider try/catch blocks and exceptions :
try {
//some code
//some code
//ERROR
throw errorType
} catch (const errorType &e) {
cout << "ERROR: " << e.what() << endl;
}
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
Well you could just put a bit more "style" into it:
std::cout << "ERROR " << event
<< ": " << (origin ? origin : "?")
<< ": " << fulltext
<< std::endl;
It takes more lines of code, but it is worth it in terms of clarity.
If you have code like this that gets repetitive, like an error report that is basically always "event" "origin" "message", then just make a function for that:
inline void report_error(int event, const std::string& origin, const std::string& message) {
std::cout << "ERROR " << event
<< ": " << (origin ? origin : "?")
<< ": " << message
<< std::endl;
};
Or use a MACRO like this:
#define ERROR_REPORT(A,B,C) "ERROR " << A << ": " << (B ? B : "?") << ": " << C
//and use it as:
std::cout << ERROR_REPORT(event,origin,fulltext) << std::endl;
Or use exception messages as Fbody said.
Or, just use printf, if you prefer, there's nothing really wrong with that. You should always consider the C++ options first because they are often better, but wherever the C style works fine, it's ok too (as long as it is within a small piece of code that is well encapsulated).
mike_2000_17
Posting Virtuoso
2,134 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
Your eyes will also get used to the C++ version with time. For instance, I find the C++ version you posted more appealing to the eye. And your fingers will also get used to it, believe me.
mike_2000_17
Posting Virtuoso
2,134 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457