Logging, how do I do that?

Started by
13 comments, last by MindWipe 23 years, 2 months ago
I just need a funktion that saves some text like "DirectDraw init - OK" and/or "Shutdown complete" But the only save to file method I know saves it like many weird ACHII. Can anyone post his/her log funktion? I need a easy to use funktion like AddToLog("bla bla"); I thankful for any help /MindWipe "If it doesn''t fit, force it; if it breaks, it needed replacement anyway."
"To some its a six-pack, to me it's a support group."
Advertisement
the easiest way would be file streams
you create the objekt
check if its ok
and store data by using just the << operator

example:
ofstream save(strFileName);

if (save.good())
{
save << somthing << something_else;
}
save.close();

you can save any basic type from int to char

der_meista
Have you tried following:

At init:
FILE* log=fopen("mylog.txt","wt");

when at any place
fprintf(log,"DirectDraw init OK\n");

and at end:
fclose(log);

This is more convenient than streams cause you may use expression like:
fprint(log,"%s %d %f",string,intval,floatval);
to print values to log (I found it very useful, and not only I)

Or you programming in Delphi of Visual Basic?
Hope you find it useful

Edited by - nullguid on January 29, 2001 2:48:36 PM
something like this?

    bool State_Protokollfileopened = false;void Log(char* st, ...){ if(st == NULL)  return;  FILE* protokollfile = fopen("protokoll.log", "a+t"); if(State_Protokollfileopened) {   fseek(protokollfile, 0, SEEK_SET);   State_Protokollfileopened = true; } else   fseek(protokollfile, 0, SEEK_END); char output[800]; va_list ap; va_start(ap, st);     // Parses The String For Variables   vsprintf(output, st, ap);    // And Converts Symbols To  va_end(ap); fprintf(protokollfile, "%s\n", output); fclose(protokollfile);}    


I just took this from my leveleditor. had to take some things out, so if somethings wrong, just e-mail me....
cya,
Phil

Uh, btw.. you can use this function just as any format function (just like sscanf, fprintf, printf, an so on)...

Visit Rarebyte!
and no!, there are NO kangaroos in Austria (I got this questions a few times over in the states

Edited by - phueppl1 on January 29, 2001 2:48:17 PM
Visit Rarebyte! and no!, there are NO kangaroos in Austria (I got this question a few times over in the states ;) )
nullguid> Yup I''m using C++, I think I''ll use your method. Looks easiest. Thankx.

/MindWipe

"If it doesn''t fit, force it; if it breaks, it needed replacement anyway."
"To some its a six-pack, to me it's a support group."
MindWipe, if you use C++ you might want to consider creating a LogFile class and make it a little more robust (support for multiple log files, debug levels for logging, displaying message to output window and/or printer, etc).

BTW, if you are going to log successful function calls (ie, DirectDraw init - OK) I''d recommend against opening/closing your file after every single log. It''ll kill your performance.


- Houdini
- Houdini
I have a logfile class that has everything you talk about Houdini except printer support. How would I go implementing that ?


WHO DO THEY
THINK THEY''RE
FOOLING : YOU ?



GARAL website
WHO DO THEYTHINK THEY'REFOOLING : YOU ?
BTW im revamping a general logfile and im a little stumped for ideas. Do anyone have suggestions or wishlists for things to be included in a logfile ?

WHO DO THEY
THINK THEY''RE
FOOLING : YOU ?



GARAL website
WHO DO THEYTHINK THEY'REFOOLING : YOU ?
I would love to see multiple log files supported as well as auto indenting. So if you are like loading a level, you could indent it and show all the portions of the level that loaded... that would be nice

Timestamping would be a nice and easy feature as well.

Spanky
You can also keep a log in RAM, which you''d want to do if you had a drop-down console for users to read as it is updated. You''d also need something like this for a message queue for RPGs or strategy games (conversations for RPGs, commands issued for strategy games). It would be this:


  struct MessageLog{    char* Message;    MessageLog* NextEntry;};//MessageLog g_Log; // I''ll use a global for this example//void InitLog ( ){    g_Log.Message = NULL;    g_Log.NextEntry = NULL;}//void AddEntry ( char* NewMessage ){    MessageLog* Temp;    if( !g_Log.Message ) g_Log.Message = NewMessage;    else    {        Temp = &g_Log;//        while( Temp->NextEntry )            Temp = Temp->NextEntry;        Temp->NextEntry = new MessageLog;        Temp->NextEntry->Message = NewMessage;        Temp->NextEntry->NextEntry = NULL;    }}//void DeleteLog ( ){    MessageLog* Temp;    MessageLog* Next;//    if( g_Log.Message ) delete [] g_Log.Message;    g_Log.Message = NULL;    Temp = g_Log.NextEntry;    g_Log.NextEntry = NULL;//    while( Temp )    {        if( Temp->Message ) delete [] Temp->Message;        Next = Temp->NextEntry;        delete Temp;        Temp = Next;    }}  


~CGameProgrammer( );

~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.

This topic is closed to new replies.

Advertisement