Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File I/O

Status
Not open for further replies.

meghaAgrawal

Programmer
May 20, 2004
14
US
I am creating a log system where I will have say 5 log files named like <whatever>.1.log, <whatever>.2.log, ..., <whatever>.5.log

I will start writing into first log and subsequent info gets appended into the log file till it is full to the capacity. Then I will open up second log and so on till the last log is full. Afterwards I plan to cycle back and start rewriting into first log.

Now is the time to ask for valuable suggestions and feedbacks:

1. How do I determine that the log file is full and I should jump to the next log? What size/capacity would be good for a txt file on windows platform?

2. How will I know which log file should be used at a particular time? Say I have used up first 3 logs and now I run my program which needs a log - how would the program know that it should use 4th log?

I was thinking to use the DateLastModified property of file object. But I was not able to make it work. Ideas?


 
What text viewer will you be using for viewing the logs - each viewer has its limits on the file sizes that it can handle

%, 2004
 
Any generic text viewer should be able to open these log files. At least Notepad I would say.

I had no idea that text viewer will put a limit on the file size.
 
1. You will set your logfile maximum size - maybe a constant in your program or a registry entry that your program reads at runtime.

For a reasonable logfile size, I'd suggest either 100k or 1MB tops.

For example:

Const MAX_LOG_SIZE = 100000

In your code, to check the size of the most recent log file use the FileLen({filepath}) function.

if (FileLen(your_log_file) > MAX_LOG_SIZE) then
...
endif

2. Better than using the DateLastModified, write a simple loop to determine the last file:

i = 1
while (Dir$("whatever." & Cstr(i) & ".log") <> "")
i = i + 1
wend

so, after the loop, whatever.i.log will be your next logfile (or i-1 being your most recent).

If you used DateLastModified, you could possibly not get the correct logfile - say if someone manually edited and changed it for some reason.
 
>
say if someone manually edited and changed it for some reason.
>

what my brain said was "say if someone manually edited and changed A PRIOR VERSION for some reason", but the hands typed something else.
 
2. When all logs will be written for the first time - the above mentioned loop will work. But what about finding next log candidate when all logs are created and they are full. Program wants to write more. It will loop back and make another cycle starting with the first log. In this case all log files are existing and full to the capacity so the program should select first log.

Can we write something like this in VB...?

i = indexOfLastModifiedFile("<whatever>.?.log")
fileName = <whatever>.i.log
if fileSize(fileName) = MAX_CAPACITY then
nextIndex = i mod 9 + 1
else
nextIndex = i
end

Function indexOfLastModifiedFile() would somehow give the log file index which was last written/modified among all log files. I can rely on last modified value, no one is going to edit any of these log files.
 
yes, you can do what you ask using the mod function to let you cycle through them
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top