views:

34

answers:

4

I have a cgi web program (in C) that prints out different error messages to a log file. If the program is run again and runs into he same error, I do not want the same error message to log again. I'm looking at different options and any advice is appreciated. Thanks.

-Cookie: unable to set cookie after the html <head> section has been printed out.
(After the head section is where any errors would occur.)

-Database: Do not have one. Too much overhead for this issue to install one.

-Parse log file: Many other processes are writing to this log file.

-Hidden form inputs on html file: Seems messy. Have 3 different forms on the same html page. How do am I sure the hidden fields are always submitted regardless of which form is submitted? But one of the errors is when the html can not be produced, so can not depend on this.

Thanks.

+1  A: 

Storing the built page in a variable and outputting it at the end will allow you to emit a header any time before then.

Ignacio Vazquez-Abrams
Unfortunately, I'm using a template html file and only replacing the $KEYWORDS. www.algonet.se/~thunberg
Tommy
Wouldn't it be possible to gather all the keywords and *then* do all the replacements in one go?
Ignacio Vazquez-Abrams
+1  A: 

The other option is to create some form of temporary file wherever you are able (not sure about permissions) and read that pre doing any work. Simply list the error types and optionally times in there, perhaps? This is assuming you want to persist this behaviour across runs of your program. This is the database solution without the database, really, so I'm not sure how helpful that is.

Whenever I mention database solutions without databases I always have to mention SQLite which is a file based serverless SQL "server".

Ninefingers
Yes, SQLite is great but have to leave this out for now. As far as temp files, is xml a good idea or just parse a text file?
Tommy
I guess it depends on your system - I would argue that if you don't need a database XML is probably overkill too... after all, only your program is going to need to read it so it can be in whatever format you like (if you need to extend the functionality maybe use XML?).
Ninefingers
I'm not entirely sure what permissions you have as a CGI program but I assume you can just write to /tmp/yourcgiprogramstatename or c:\windows\temp\yourcfgprogram.state, whichever suits you.
Ninefingers
@Ninefingers: What is the .state? How can I read it into the cgi on the next run of the program? Is this a cookie?
Tommy
No it was just an arbitrary extension - you could have used .yellow. Same way you open any file I suppose, with fopen but a hard-coded path or path looked up from a config file. After all, this is sort of a config file you are writing.
Ninefingers
o yeah, got ya, thanks.
Tommy
+1  A: 

I think you should refactor your program to create all its output previously to sending any HTML to the client, that way you'll be able to know beforehand all existing errors and set a cookie.

Now, if this is not viable for any reason you should have a temporary file identifying IP address and user agent and errors already shown. A simple text file should be quick enough to parse.

Vinko Vrsalovic
Would have to change too much to refactor the program. Sounds like the .txt might be the way....
Tommy
+1  A: 

Using memcached might be a way to keep states throughout different sessions.

speakman