Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log File Monitor

Is is possible to open a text file and read the contents while another application is updating the file, in such a way that it does not cause a lock conflict?

I need to monitor a log file from one application which is updated by another application each time an event occurs.

I do check if the file is in use before I try to read it, but that does not seem to work in all cases.

Thanks, Pieter

like image 338
Pieter van Wyk Avatar asked Dec 23 '22 12:12

Pieter van Wyk


1 Answers

it depends on how the first app open that file.

i.e when calling CreateFile API to open a file, there is dwShareMode param which tells the api how to open it (if this was given 0, it can't be accessed from other applications IIRC). otherwise there should be no problem with reading from that file. if im not mistaken, to check if that file is being opened read only u can call something like

CreateFile(pchar(fName), GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0) ;
like image 189
avar Avatar answered Jan 04 '23 18:01

avar