how do you write to a log file in C++?

EDIT : see post 9

Two examples :

Code:
TCHAR szOut[1024];
CvWString szTempString;
getUnitAIString(szTempString, pHeadUnit->AI_getUnitAIType());
sprintf(szOut, "Unit : %S(%S)[%d, %d] (%S)\n",pHeadUnit->getName().GetCString(), GET_PLAYER(pHeadUnit->getOwnerINLINE()).getName(), pHeadUnit->getX_INLINE(), pHeadUnit->getY_INLINE(), szTempString.GetCString());
gDLL->messageControlLog(szOut);

TCHAR szOut[1024];
sprintf(szOut, " : \n");
gDLL->messageControlLog(szOut);

Tcho !
 
Two examples :

Code:
TCHAR szOut[1024];
CvWString szTempString;
getUnitAIString(szTempString, pHeadUnit->AI_getUnitAIType());
sprintf(szOut, "Unit : %S(%S)[%d, %d] (%S)\n",pHeadUnit->getName().GetCString(), GET_PLAYER(pHeadUnit->getOwnerINLINE()).getName(), pHeadUnit->getX_INLINE(), pHeadUnit->getY_INLINE(), szTempString.GetCString());
gDLL->messageControlLog(szOut);

TCHAR szOut[1024];
sprintf(szOut, " : \n");
gDLL->messageControlLog(szOut);

Tcho !

I never could get those to work. I haven't really "really" needed it till recently though. Could someone post two examples. One with just the syntax and the other with some actualy code.
 
What your problem with the log ? Do you get a CtD , or simply no log ?

Edit :
You must have logging enable . You should have probably .

some other examples :

Code:
TCHAR szOut[1024];
sprintf(szOut, "Hello World\n");
gDLL->messageControlLog(szOut);

TCHAR szOut[1024];
CvUnit* pUnit = <Something>;
sprintf(szOut, " %S has moved %d tiles (max %d)\n", pUnit.getName(), pUnit.getMoves(), pUnit.maxMoves());
gDLL->messageControlLog(szOut);
 
in civilizationIV.ini :

; Enable the logging system
LoggingEnabled = 1

; Overwrite old network and message logs
OverwriteLogs = 1

; Enable message logging
MessageLog = 1

Then you go to the log document folder to check the log ... I don't remember the file name . Something like MPlog.txt or MPDbg.log .

Br careful with logs , sometimes they can cause a crash if the log file isn't already initialized (like if you put some logs in initcore or some other initialization functions) . I've found a workaround for this case adding :

Code:
		if(CvPlayerAI::areStaticsInitialized())
		{
			<write some log>
		}

Tcho !
 
Hmmm... I'm having trouble with this too. It looks like there is an old MPLog.txt in my Civ4 Docs directory, but the date on it is from way back when I only had vanilla Civ4. In my BTS logs folder there is no such file. I even put MessageLog = 1 in my mods ini file. How do you get this to work with BtS?
 
I've took the example in CvSelectionGroupAI::AI_update() :

Spoiler :
Code:
				if (GC.getLogging())
				{
					TCHAR szOut[1024];
					CvWString szTempString;
					getUnitAIString(szTempString, pHeadUnit->AI_getUnitAIType());
					sprintf(szOut, "Unit stuck in loop: %S(%S)[%d, %d] (%S)", pHeadUnit->getName().GetCString(), GET_PLAYER(pHeadUnit->getOwnerINLINE()).getName(),
						pHeadUnit->getX_INLINE(), pHeadUnit->getY_INLINE(), szTempString.GetCString());
					gDLL->messageControlLog(szOut);
				}

... and get the log file every time i use some. I've noticed that the log file seems to be reinitialized every time you start a game (may be load also), but I'm not sure of that since I've always got what i wanted. I can't help you much sorry. Where do you want to put some log (i can check if i get some log at the same point)?

Tcho !

Edit : LoggingEnabled = 1 seems to be enough to get the log file . (After taken a look in globals)
Edit2 : if you have some time you can also try to compile the mini MOD comp I've made for FFH2 removing the // from CvGame::init to get the log . And check if you get it .
 
Actually, I had some success with this:

Code:
	TCHAR szOut[1024];
	sprintf(szOut, "iHap = %d\n", iHap);
	gDLL->logMsg("CDebug.log",szOut);

I found it somewhere on the forum. It's nice because then the filename "CDebug.log" is arbitrary and I can isolate only the stuff I'm interested in into one file.
 
That's great to be able to separate the log into many files indeed. I will take that for my logs . :)

Tcho !
 
Back
Top Bottom