It doesn’t work. PlayerColor has the operator<<, but chars are interpreted just as they are(\0 is \0 not 0), which makes sense. But in log text files we want to have characters and no binary values, so we have to convert a char to a number if it represents a number.
Are you sure? I look carefully on MSDN and GCC pages if I can use a particular c++11 feature. Look at that page: msdn.microsoft.com/de-de/library … 00%29.aspx
There are also stoi, … available for VS10. Can you confirm this please? I have no VS.
No, no, I don’t want you to wrap everything with try,catch. If the method was left because of exception, you’ll simply see that the “Leaving XYZ” message is missing. I don’t think that it requires that much effort to write TRACE_BEGIN and TRACE_END. You don’t have to output param/return values, if you don’t want to. It is optional. So if you’re fine with knowing that a function entered/left, than all you have to do is to write a TRACE_BEGIN at the begin of a method and a TRACE_END at every return statement. As said above, it’s almost always possible to refactor the code, so that it has only one return stmt. Two examples:
Example 1:
int CBattleInfoEssentials::battleGetMoatDmg() const
{
auto town = getBattle()->town;
if(!town)
return 0;
return town->town->moatDamage;
}
Can be written as:
int CBattleInfoEssentials::battleGetMoatDmg() const
{
TRACE_BEGIN(logGlobal);
int moatDmg = 0;
auto town = getBattle()->town;
if(town) moatDmg = town->town->moatDamage;
TRACE_END_PARAMS(logGlobal, "moatDmg '%d'", moatDmg);
return moatDmg;
}
Example 2:
bool CBattleInfoEssentials::battleHasNativeStack(ui8 side) const
{
BOOST_FOREACH(const CStack *s, battleGetAllStacks())
{
if(s->attackerOwned == !side && s->getCreature()->isItNativeTerrain(getBattle()->terrainType))
return true;
}
return false;
}
Transformed to:
bool CBattleInfoEssentials::battleHasNativeStack(ui8 side) const
{
TRACE_BEGIN_PARAMS(logGlobal, "side '%s'", std::to_string(side));
bool hasNativeStack = false;
BOOST_FOREACH(const CStack *s, battleGetAllStacks())
{
if(s->attackerOwned == !side && s->getCreature()->isItNativeTerrain(getBattle()->terrainType))
{
hasNativeStack = true;
break;
}
}
TRACE_END_PARAMS(logGlobal, "hasNativeStack '%d'", hasNativeStack);
return hasNativeStack;
}
I don’t want to change your style of programming nor impose any coding guidelines towards this and I think too that the first solutions above are nicer to read. But TRACE without printing return values or not knowing that the function has left by exception is not the ideal solution either. However if it’s enough, then the RAII solution seems to be nice just to print “Leaving XYZ” at every possible exit point.
That’s right. I would have to further investigate the source code to know where printing an error is not enough. Sometimes I got the feeling that we cannot continue proper execution, as it could lead to NPE, Buffer overflow, wrong index access,… But we better leave that for now…
I’m snowed under with work! BTW, what problem do you mean?