writing logfile
WARNING: Newbie ALERT!!!
I am trying to write three functions to plugin to my app. They are defined in log.h and are used for opening, writing to, and closing a logfile. I have attached log.h.
My questions are: How do I tell fopen to create if not there or append if it is? I seem to get an error either way.
And: Why does the fputs cause a segfault? Is there a better way of doing it? I know I do not understand types and casting very well so I may have it totally wrong.
Thanks
Signature · 2 lines
Mat Harris OpenGPG Public Key ID: C37D57D9 [email protected] www.genestate.com
Quoted 2 lines · Mat Harris
in
log.hand are used for opening, writing to, and closing a logfile. I have attachedlog.h.
You don't.
Quoted 2 lines
My questions are: How do I tell fopen to create if not there or append if it is? I seem to get an error either way.
This is explained in details in fopen(3).
Quoted 3 lines
And: Why does the fputs cause a segfault? Is there a better way of doing it? I know I do not understand types and casting very well so I may have it totally wrong.
fputs() is not designed to segfault; you most probably use it wrong. You have to show parts of your code to get at least a hint.
Elias
Signature · 2 lines
University of Athens I bet the human brain Physics Department is a kludge --Marvin Minsky
sorry, did my special trick of not attaching the file. here it is
Quoted 25 lines · Elias Athanasopoulos
Quoted 2 lines · Mat Harris
in
log.hand are used for opening, writing to, and closing a logfile. I have attachedlog.h.You don't.
Quoted 2 lines
My questions are: How do I tell fopen to create if not there or append if it is? I seem to get an error either way.
This is explained in details in fopen(3).
Quoted 3 lines
And: Why does the fputs cause a segfault? Is there a better way of doing it? I know I do not understand types and casting very well so I may have it totally wrong.
fputs()is not designed to segfault; you most probably use it wrong. You have to show parts of your code to get at least a hint.Elias
Signature · 2 lines
University of Athens I bet the human brain Physics Department is a kludge --Marvin Minsky
Signature · 2 lines
Mat Harris OpenGPG Public Key ID: C37D57D9 [email protected] www.genestate.com
int open_log()
{
char logfile[4096];
int loghandle;
strcpy(logfile, WEBROOT); strcat(logfile, "/logs/access.log"); loghandle = open(logfile, O_CREAT);
if (loghandle < 0)
{
fprintf(stderr, "Couldn't open logfile: %s\n", logfile);
exit(1);
}
else
{
return loghandle;
}
}void close_log(int loghandle)
{
close(loghandle);
}
void write_log(int loghandle, char string[1024])
{
fputs(string, loghandle);
}
Wow it is a mess! Time to get the old book, sit down and read some
more...
You are opening a file through open() which returns an integer
representation of the file descriptor and trying to write on it through fputs() which writes to a file descriptor represented by a FILE pointer.
That's your answer. You are using the wrong functions.
You may either change your code to work with integers, open(), read()
write() and close() or make it use FILE *, fopen(), fread() (...),fputs() (...) and fclose(). It is up to you and will depends on the kind of application you're writing.
Each of the ways of file manipulation has advantages and/or
disadvantages. You should seek for something about that over the net.
"> loghandle = open(logfile, O_CREAT);"
Maybe I'm wrong, but I think this previous call just creates a file
and doesn't make it available for reading/writing in any way. Is that right brothers???
william
Signature · 2 lines
Perl combines all of the worst aspects of BASIC, C and line noise.
-- Keith Packardi thought it was something along those lines. so i need to do dot-to-dot with the functions. i will read up on that in a mo
Quoted 29 lines · William N. Zanatta
Wow it is a mess! Time to get the old book, sit down and read some
more...You are opening a file through
open()which returns an integer
representation of the file descriptor and trying to write on it throughfputs()which writes to a file descriptor represented by a FILE pointer.That's your answer. You are using the wrong functions.
You may either change your code to work with integers,
open(),read()
write()andclose()or make it use FILE *,fopen(),fread()(...),fputs()(...) andfclose(). It is up to you and will depends on the kind of application you're writing.Each of the ways of file manipulation has advantages and/or
disadvantages. You should seek for something about that over the net."> loghandle =
open(logfile, O_CREAT);"Maybe I'm wrong, but I think this previous call just creates a file
and doesn't make it available for reading/writing in any way. Is that right brothers???william
Signature · 2 lines
Perl combines all of the worst aspects of BASIC, C and line noise. -- Keith Packard
Signature · 2 lines
Mat Harris OpenGPG Public Key ID: C37D57D9 [email protected] www.genestate.com
Quoted 5 lines · William N. Zanatta
"> loghandle =
open(logfile, O_CREAT);"Maybe I'm wrong, but I think this previous call just creates a file
and doesn't make it available for reading/writing in any way. Is that right brothers???
I think it will create it and open it in read-only mode, since O_RDONLY is defined as 0 and thus O_RDONLY | O_CREAT equals to O_CREAT.
It is quite missleading, though, so feeding open() explicitly with the flags it asks is the right thing to do, IMHO.
Elias
Signature · 2 lines
University of Athens I bet the human brain Physics Department is a kludge --Marvin Minsky
Quoted 4 lines · Mat Harris
void
write_log(int loghandle, char string[1024])
{
fputs(string, loghandle);
}
You should compiling with -Wall. This will save you a lot and help you find out your errors.
Remember: a warning is a virtual error, which can break the balance conservation of the application for a small period of time, allowed by the uncertainty principle, enough to make the application segfault. :-)
Elias
Signature · 2 lines
University of Athens I bet the human brain Physics Department is a kludge --Marvin Minsky
Quoted 1 line · Mat Harris
loghandle = open(logfile, O_CREAT);
This should probably be:
loghandle = open(logfile, O_WRONLY | O_CREAT | O_APPEND, 0600);
You may also wish to use the O_SYNC flag, to force data to be written to disk immediately (so that it isn't lost if the system crashes or the power fails).
As William pointed out, if you open the file with open(), you need to use write() to write data to the file. Alternatively, you could use fopen() and fputs(), but fopen() doesn't give you as much control (e.g. you can't set the permissions or the O_SYNC flag on creation).
Also, you can create an ANSI stream (a "FILE *") from a descriptor using fdopen(), and you can get the descriptor for an ANSI stream using fileno(). But you need to be careful about accessing a file using both methods (which basically means that you need to understand how the two relate to each other).
Signature · 1 line
Glynn Clements <[email protected]>
ok i have modified it to use open, write, and close> Now tho I am getting garbage written to the logfile. Evidently i have made an error with types or there is confusion between writing a text string and writing raw bits.
i have attached the modified code.
Thanks for all your help so far people :)
Quoted 26 lines · Glynn Clements
Quoted 1 line · Mat Harris
loghandle = open(logfile, O_CREAT);This should probably be:
loghandle = open(logfile, O_WRONLY | O_CREAT | O_APPEND, 0600);You may also wish to use the
O_SYNCflag, to force data to be written to disk immediately (so that it isn't lost if the system crashes or the power fails).As William pointed out, if you open the file with
open(), you need to usewrite()to write data to the file. Alternatively, you could usefopen()andfputs(), butfopen()doesn't give you as much control (e.g. you can't set the permissions or theO_SYNCflag on creation).Also, you can create an ANSI stream (a "FILE *") from a descriptor using
fdopen(), and you can get the descriptor for an ANSI stream usingfileno(). But you need to be careful about accessing a file using both methods (which basically means that you need to understand how the two relate to each other).Signature · 1 line
Glynn Clements <[email protected]>
Signature · 2 lines
Mat Harris OpenGPG Public Key ID: C37D57D9 [email protected] www.genestate.com
int open_log()
{
char logfile[4096];
int loghandle;
strcpy(logfile, WEBROOT); strcat(logfile, "/logs/access.log"); loghandle = open(logfile, O_WRONLY | O_CREAT | O_APPEND | O_SYNC, 0600);
if (loghandle < 0)
{
fprintf(stderr, "Couldn't open logfile: %s\n", logfile);
exit(1);
}
else
{
return loghandle;
}
}void close_log(int loghandle)
{
close(loghandle);
}
void write_log(int loghandle, char string[1024])
{
printf("requested file: %s\n", string);
write(loghandle, string, sizeof(string));
}
On Wed, 2003-02-26 09:25:02 +0000, Mat Harris <[email protected]> wrote in message <[email protected]>:
Quoted 3 lines
ok i have modified it to use open, write, and close> Now tho I am getting garbage written to the logfile. Evidently i have made an error with types or there is confusion between writing a text string and writing raw bits.
Despite some other shortages, try it this way:
Quoted 8 lines · Glynn Clements
int
open_log()
{
char logfile[4096];
int loghandle;strcpy(logfile, WEBROOT); strcat(logfile, "/logs/access.log");
logfile *may* be too short. This is asking for trouble!
Quoted 13 lines
loghandle = open(logfile, O_WRONLY | O_CREAT | O_APPEND | O_SYNC, 0600);if (loghandle < 0)
{ fprintf(stderr, "Couldn't open logfile: %s\n", logfile); exit(1); } else { return loghandle; } }
Quoted 6 lines
void
close_log(int loghandle)
{
close(loghandle);
}void
write_log(int loghandle, char string[1024])
voidwrite_log(int loghandle, char *string)
Quoted 1 line
{
if(!string)
return;
Quoted 2 lines
printf("requested file: %s\n", string);
write(loghandle, string, sizeof(string)); write(loghandle, string, strlen(string));
write(loghandle, "\n", strlen("\n"));Quoted 1 line
}
MfG, JBG
Signature · 4 lines
Jan-Benedict Glaw [email protected] . +49-172-7608481 "Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! ret = do_actions((curr | FREE_SPEECH) && ~(IRAQ_WAR_2 | DRM | TCPA);
ok and now i am getting something weird. when I try to log a successful request, and then make the request again, it ands to the previous printf message and prints it again so it repeats all the previous messages again and again. Also, the garbage in the logfile is the beginning of my webroot "/home....". I do not know what is going on here.
I have attached all my work so far. PS Please don't tell me about re-inventing the wheel, this is just for experimentation :)
Quoted 60 lines · Jan-Benedict Glaw
On Wed, 2003-02-26 09:25:02 +0000, Mat Harris <[email protected]> wrote in message <[email protected]>:
Quoted 3 lines
ok i have modified it to use open, write, and close> Now tho I am getting garbage written to the logfile. Evidently i have made an error with types or there is confusion between writing a text string and writing raw bits.
Despite some other shortages, try it this way:
Quoted 8 lines · Glynn Clements
int
open_log()
{
char logfile[4096];
int loghandle;strcpy(logfile, WEBROOT); strcat(logfile, "/logs/access.log");logfile *may* be too short. This is asking for trouble!
Quoted 13 lines
loghandle = open(logfile, O_WRONLY | O_CREAT | O_APPEND | O_SYNC, 0600);if (loghandle < 0)
{ fprintf(stderr, "Couldn't open logfile: %s\n", logfile); exit(1); } else { return loghandle; } }Quoted 6 lines
void
close_log(int loghandle)
{
close(loghandle);
}void
write_log(int loghandle, char string[1024])void
write_log(int loghandle, char *string)Quoted 1 line
{
if(!string)
return;Quoted 2 lines
printf("requested file: %s\n", string); write(loghandle, string, sizeof(string));write(loghandle, string, strlen(string)); write(loghandle, "\n", strlen("\n"));Quoted 1 line
}
MfG, JBG
Signature · 4 lines
Jan-Benedict Glaw [email protected] . +49-172-7608481 "Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! ret = do_actions((curr | FREE_SPEECH) && ~(IRAQ_WAR_2 | DRM | TCPA);
Signature · 2 lines
Mat Harris OpenGPG Public Key ID: C37D57D9 [email protected] www.genestate.com
Quoted 5 lines · Mat Harris
ok and now i am getting something weird. when I try to log a successful request, and then make the request again, it ands to the previous printf message and prints it again so it repeats all the previous messages again and again. Also, the garbage in the logfile is the beginning of my webroot "/home....". I do not know what is going on here.
Line 52 of webserver.c:
Quoted 1 line
strcat(logmsg, filename);
should be
strcpy(logmsg, filename);
Also:
Quoted 1 line
#include "
log.h" /* logging functions */
Don't use #include for actual code. Put it in a separate .c file and compile it separately.
Quoted 4 lines
void
write_log(int loghandle, char *string)
{
write(loghandle, string, sizeof(string));
}
write(loghandle, string, strlen(string));
Applying sizeof() to a "char *" will return the size of the pointer (typically 4 on a 32-bit system, 8 on a 64-bit system), not the length of the string to which it points.
Signature · 1 line
Glynn Clements <[email protected]>
ok, thanks guys for the help with my webserver, the loggin works fine and now I have remembered about extern void etc I have put the functions to compile seperateley as suggested.
Just one more thing tho, the logmessage is getting printed to the client's browser after any html content has been transferred and I can't see where it is happening. I have put a tarball of the whole thing at
http://www.genestate.com/~matthewh/debug/
thanks again.
Quoted 37 lines · Glynn Clements
Quoted 5 lines · Mat Harris
ok and now i am getting something weird. when I try to log a successful request, and then make the request again, it ands to the previous printf message and prints it again so it repeats all the previous messages again and again. Also, the garbage in the logfile is the beginning of my webroot "/home....". I do not know what is going on here.
Line 52 of
webserver.c:Quoted 1 line
strcat(logmsg, filename);should be
strcpy(logmsg, filename);Also:
Quoted 1 line
#include "
log.h" /* logging functions */Don't use #include for actual code. Put it in a separate .c file and compile it separately.
Quoted 4 lines
void
write_log(int loghandle, char *string)
{
write(loghandle, string, sizeof(string));
}write(loghandle, string, strlen(string));Applying
sizeof()to a "char *" will return the size of the pointer (typically 4 on a 32-bit system, 8 on a 64-bit system), not the length of the string to which it points.Signature · 1 line
Glynn Clements <[email protected]>
Signature · 2 lines
Mat Harris OpenGPG Public Key ID: C37D57D9 [email protected] www.genestate.com
SOLVED: Re: writing logfile
it's alright, i was closing the logfile inside the connection loop so it was printing to a null filehandle and got confused.
Quoted 54 lines · Mat Harris
ok, thanks guys for the help with my webserver, the loggin works fine and now I have remembered about extern void etc I have put the functions to compile seperateley as suggested.
Just one more thing tho, the logmessage is getting printed to the client's browser after any html content has been transferred and I can't see where it is happening. I have put a tarball of the whole thing at
http://www.genestate.com/~matthewh/debug/
thanks again.
Quoted 37 lines · Glynn Clements
Quoted 5 lines · Mat Harris
ok and now i am getting something weird. when I try to log a successful request, and then make the request again, it ands to the previous printf message and prints it again so it repeats all the previous messages again and again. Also, the garbage in the logfile is the beginning of my webroot "/home....". I do not know what is going on here.
Line 52 of
webserver.c:Quoted 1 line
strcat(logmsg, filename);should be
strcpy(logmsg, filename);Also:
Quoted 1 line
#include "
log.h" /* logging functions */Don't use #include for actual code. Put it in a separate .c file and compile it separately.
Quoted 4 lines
void
write_log(int loghandle, char *string)
{
write(loghandle, string, sizeof(string));
}write(loghandle, string, strlen(string));Applying
sizeof()to a "char *" will return the size of the pointer (typically 4 on a 32-bit system, 8 on a 64-bit system), not the length of the string to which it points.Signature · 1 line
Glynn Clements <[email protected]>Signature · 2 lines
Mat Harris OpenGPG Public Key ID: C37D57D9 [email protected] www.genestate.com
Signature · 2 lines
Mat Harris OpenGPG Public Key ID: C37D57D9 [email protected] www.genestate.com