OUTFILE - like creating a log?

R

rhitz1218

All:

I'm trying to create a log file. I now I can be able to do that by
doing the following:

FILE *outfile;
outfile = fopen("log.txt", "w");

fprintf("This is the text");

Now questions is, I have a bunch of functions, so whenever I got
to a function I would like to write something in my log file, Is is
possible to basically just put the fprintf inside every function, and
the lines will go after the other?

Like for example

func1()
printf("This is the first message");

func2()
prinf("this is the second message");

In my log file.
I will have

This is the first message
this is the second message
 
I

Ian Malone

All:

I'm trying to create a log file. I now I can be able to do that by
doing the following:

FILE *outfile;
outfile = fopen("log.txt", "w");

fprintf("This is the text");

int fprintf(FILE *stream, const char *format, ...);

i.e. you must do:
fprintf(outfile, "This is the text\n");
or
fprintf(outfile, "%s\n", "This is the text");

(and #include said:
Now questions is, I have a bunch of functions, so whenever I got
to a function I would like to write something in my log file, Is is
possible to basically just put the fprintf inside every function, and
the lines will go after the other?

Like for example

func1()
printf("This is the first message");

func2()
prinf("this is the second message");

In my log file.
I will have

This is the first message
this is the second message

You need to pass the value of outfile (pointer to FILE)
to the functions. (Or make it global, but that is not
very extensible, and means your functions won't be
easily separable from the rest of the code.)
 
P

pemo

Ian said:
int fprintf(FILE *stream, const char *format, ...);

i.e. you must do:
fprintf(outfile, "This is the text\n");
or
fprintf(outfile, "%s\n", "This is the text");

You need to pass the value of outfile (pointer to FILE)
to the functions. (Or make it global, but that is not
very extensible, and means your functions won't be
easily separable from the rest of the code.)

Or you could simply call a Logit function from each function, i.e.,

// Untested.

void Logit(const char * msg)
{
FILE * outfile;

if((outfile = fopen("log.txt", "wa")) != NULL)
{
fprintf(outfile, msg);

fclose(outfile);
}
}

You could break the opening/closing out into other functions - so that it
doesn't get open/closed on each call, but then you'd need to make outfile's
scope wider.
 
M

Mark McIntyre

Now questions is, I have a bunch of functions, so whenever I got
to a function I would like to write something in my log file, Is is
possible to basically just put the fprintf inside every function, and
the lines will go after the other?

Simplest way is to fprintf() to stderr in each function, then redirect
stderr to a file with your normal command shell rules.

Otherwise, fopen/fclose the file in main(), make the file handle a
global variable, and off you go.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
S

Spiros Bousbouras

Mark said:
Simplest way is to fprintf() to stderr in each function, then redirect
stderr to a file with your normal command shell rules.

How do you know that he has access to a shell ?
 
M

Mark McIntyre

How do you know that he has access to a shell ?

I don't, and nor does it matter. If he does, the first answer I gave
is good. If he doesn't the second one works.

What was your point?
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
B

Bill Pursell

All:

I'm trying to create a log file. I now I can be able to do that by
doing the following:

FILE *outfile;
outfile = fopen("log.txt", "w");

fprintf("This is the text");

No, you can't. For one, fprintf takes 2 arguments,
and for another, well, consider the following...

[tmp]$ cat a.c

#include <stdio.h>

int main(void)
{
FILE *outfile;
outfile = fopen("log.txt", "w");

fprintf(outfile, "This is the text");
return 0;
}
[tmp]$ gcc -Wall -pedantic a.c
[tmp]$ ./a.out
Segmentation fault
 
G

goose

pemo said:
Or you could simply call a Logit function from each function, i.e.,

// Untested.

void Logit(const char * msg)
{
FILE * outfile;

if((outfile = fopen("log.txt", "wa")) != NULL)
{
fprintf(outfile, msg);

fclose(outfile);
}
}

You could break the opening/closing out into other functions - so that it
doesn't get open/closed on each call, but then you'd need to make outfile's
scope wider.

I'd rather extend the function to close the file when called with
NULL and to open the file if it is not already opened - easier for the
caller to use that way (can be closed and reopened whenever the
program deems necessary).

void Logit(const char * msg)
{
#define LOGFILE "log.txt"

static FILE * outfile;

if (!msg) {
if (outfile) {
fclose (outfile);
outfile = NULL;
}
return;
}
if (!outfile) {
outfile = fopen (LOGFILE, "w");
if (!outfile) {
printf ("cannot open %s for logging\n", LOGFILE);
return;
}
}
fprintf(outfile, msg);
}

I also consider the above a good example for the poster, as
the poster seems to be in the learning phase and there's no
better time to introduce static variables, #define's and
resource-handling :)

For better effect, I'd use a variable argument list and
vfprintf to give the caller much more flexibility (what if
you want to log the number of attempts to connect somewhere?
Surely you don't want the caller to have to manually convert
all integers into strings before logging, do you?)

Using a variable argument list is left as an exercise to
the reader :)

goose,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top