Logging into a file

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
FleshCrawler
Posts: 108
Joined: Fri Aug 22, 2003 1:04 pm
Location: Kerkrade, Netherlands
Contact:

Logging into a file

Post by FleshCrawler »

Any Idea how i can log into a file?
like that what gets written in the dosbox, i get logged into a file. like a program.log ot logfile.txt , i have no idea how to fix something like that.

tnx in advance :)
WhytWulf
Posts: 51
Joined: Mon Sep 08, 2003 11:14 am
Location: Australia, Tamworth, NSW
Contact:

Post by WhytWulf »

here is the code I use for my log file writer..in might be of use to someone. functions like printf();

Code: Select all


#include <stdio.h>
#include <stdarg.h>
#include <time.h>

void InitLog(void)
{
	//creates logfile "sprawl.log" if it doesn't exist

	FILE *LogFile;

	time_t current = time(NULL);
	if((LogFile=fopen("Runner.log","w+"))!=NULL)
	{
		fprintf(LogFile,"Log File started %s",ctime(&current));
		fclose(LogFile);
	}

}

void LogWrite(char *string, ...)
{

	char LogString[1000];
	va_list ap;
	va_start(ap, string);
	vsprintf(LogString,string,ap);
	va_end(ap);
	FILE *filelog;
	time_t current = time(NULL);
	if ((filelog=fopen("Runner.log","a"))!=NULL)
	{
		fprintf(filelog,"-> %s: %s",ctime(&current),LogString);
		fprintf(filelog,"\n");
		fclose(filelog);
	}

}
To use call InitLog(); this deletes the file blah blah and creates a new file.
and when you want to output something to the logfile, use

Code: Select all

LogWrite("This is a test output variable %d\n",output);
It is quite versitile..It could easily be added to and made into a monster debug/log file..
Project Admin -
http://shadowrunonline.sourceforge.net

------------------------------------
There are 10 kinds of people, those that understand binary and those that don't.
FleshCrawler
Posts: 108
Joined: Fri Aug 22, 2003 1:04 pm
Location: Kerkrade, Netherlands
Contact:

Post by FleshCrawler »

thanks very much :)
FleshCrawler
Posts: 108
Joined: Fri Aug 22, 2003 1:04 pm
Location: Kerkrade, Netherlands
Contact:

Post by FleshCrawler »

Digging this old topic up again, but i felt like it was useless to create a complete new topic on the same subject :)

My Logwriter works fine so far, but i got the following problem, i want to let it write wich kind of renderer and Resolution the app. is running.

but when i use the LogWrite function like

LogWrite("Render: %d on Resolution: %d", DriverType, ResolutionType);

so if someone has a solution for that, thanks in advance :)
I've been absent for really long, but i'm ready to reign my terror on you once again, mwuahahahahaha
Gonosz
Posts: 24
Joined: Wed Oct 29, 2003 4:21 pm
Location: Hungary (one joke and you're dead)

Post by Gonosz »

I am not quite sure what you are talking about :], but if you are saying you are looing for a printf-like function, then here it goes:

Code: Select all

inline void logwrite(char* logline, ...)
{
        va_list pfmt;
    FILE * fp = fopen("log.txt", "a");
    if (fp == NULL)
       return;
    if (logline != NULL)
        {
            va_start(pfmt, logline);
            vfprintf(fp, logline, pfmt);
            fprintf(fp,"\n");
            fclose(fp);
            va_end(pfmt);  
        }
}
BTW if you don't close the file and your app crashes, chances are that the logfile will be gone.

Hope I helped,

Gonosz
FleshCrawler
Posts: 108
Joined: Fri Aug 22, 2003 1:04 pm
Location: Kerkrade, Netherlands
Contact:

Post by FleshCrawler »

well, i got this:

Code: Select all

void InitLog(void) 
{ 
   //creates logfile "Astornia.log" if it doesn't exist 

   FILE *LogFile; 

   time_t current = time(NULL); 
   if((LogFile=fopen("Astornia.log","w+"))!=NULL) 
   { 
      fprintf(LogFile,"Log File started %s \n",ctime(&current)); 
      fclose(LogFile); 
   } 

} 

void LogWrite(char *logstr) 
{ 

   char LogString[1000]; 
   va_list ap; 
   va_start(ap, logstr); 
   vsprintf(LogString,logstr,ap); 
   va_end(ap); 
   FILE *filelog; 
   time_t current = time(NULL); 
   if ((filelog=fopen("Astornia.log","a"))!=NULL) 
   { 
      fprintf(filelog,"%s",LogString); 
      fclose(filelog); 
   } 

}
and i cant add more parameters to it, because i only wanna have it for 1 function. to show the driver, and resolution in the logfile
I've been absent for really long, but i'm ready to reign my terror on you once again, mwuahahahahaha
Gonosz
Posts: 24
Joined: Wed Oct 29, 2003 4:21 pm
Location: Hungary (one joke and you're dead)

Post by Gonosz »

Okay. You can use vsprintf, which is like printf, but the output goes to a char[]. Then you can pass this string to your logwrite.
FleshCrawler
Posts: 108
Joined: Fri Aug 22, 2003 1:04 pm
Location: Kerkrade, Netherlands
Contact:

Post by FleshCrawler »

when i change it VC6 gives me the following error.

c:\program files\microsoft visual studio\myfiles\projects\astornia\logwriter.h(43) : error C2664: 'vsprintf' : cannot convert parameter 1 from 'struct _iobuf *' to 'char *'

i dont know so much about C/C++ yet. still learning :)
I've been absent for really long, but i'm ready to reign my terror on you once again, mwuahahahahaha
Gonosz
Posts: 24
Joined: Wed Oct 29, 2003 4:21 pm
Location: Hungary (one joke and you're dead)

Post by Gonosz »

I don't think I can help without seeing you're source, so I copied a working function here for you (it's from the Half-Life SDK). BTW this is C, not C++ :)

Code: Select all

void UTIL_LogPrintf( char *fmt, ... )
{
        va_list        argptr;
        char    string[1024];
        
        va_start ( argptr, fmt );
        vsprintf ( string, fmt, argptr );
        va_end   ( argptr );
        logwrite( string );  
}
EDIT:
Damn I am stupid, it is in the code you posted above :D You actually use the vsprintf in the logwrite! Anyway, this just proves that I didn't give you a bad advice :P Another idea: maybe you passed the file pointer as the first argument instead of the char[]... maybe :)

Okay, I'm off to learn reading ;)

Gonosz<helpdesk>
FleshCrawler
Posts: 108
Joined: Fri Aug 22, 2003 1:04 pm
Location: Kerkrade, Netherlands
Contact:

Post by FleshCrawler »

Gonosz wrote:Another idea: maybe you passed the file pointer as the first argument instead of the char[]... maybe :)
could you explain plz :)

and everyone can make reading mistakes, no big problem :D
I've been absent for really long, but i'm ready to reign my terror on you once again, mwuahahahahaha
Gonosz
Posts: 24
Joined: Wed Oct 29, 2003 4:21 pm
Location: Hungary (one joke and you're dead)

Post by Gonosz »

Code: Select all

char string[200];
vsprintf ( string, "Render: %d on Resolution: %d\n", DriverType, ResolutionType);
LogWrite(string);
This should work.
FleshCrawler
Posts: 108
Joined: Fri Aug 22, 2003 1:04 pm
Location: Kerkrade, Netherlands
Contact:

Post by FleshCrawler »

it still says it cant take 4 parameters, vsprintf that is,

but if i remove em one by one, it says cant take 3 ,2 ,1...somehow i dont understand :P

but as you wrote it there, it actually has to work :S
I've been absent for really long, but i'm ready to reign my terror on you once again, mwuahahahahaha
FleshCrawler
Posts: 108
Joined: Fri Aug 22, 2003 1:04 pm
Location: Kerkrade, Netherlands
Contact:

Post by FleshCrawler »

I guess i will never get that fixed. been thinking how to solve, asked a friend, he didnt know the solution because he uses CBuilder. and only knew the CBuilder solution that doesnt work in VC6. so if someone is out there that does know the solution to this, i'm like begging to help me with a code to solve it, i'm really desperate...
I've been absent for really long, but i'm ready to reign my terror on you once again, mwuahahahahaha
Halan
Posts: 447
Joined: Tue Oct 04, 2005 8:17 pm
Location: Germany, Freak City
Contact:

Post by Halan »

okay this is an old topic i know but i used the code for logging

when compiling it says "`va_start' used in function with fixed args+"
any ideas?

greets,
Halan
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Learn C, read your compiler manual, try google. va_start is a function which is required for variable argument lists. This is plain ANSI C.
Post Reply