Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2010-07

tail on windows

19 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Martin

Is there any tail like program, that works for lazarus log files on windows?

I found tail apps for win, but none of them can open a lazarus --debug-log while lazarus is open.

Martin

Reply by email

dmitry boyarintsev
to Martin
program tail;
{$mode objfpc}{$H+}
uses
  Classes, Math;
var
  names       : TStringList;
  lines       : TStringList;
  linescount  : Integer;
  l, err      : Integer;
  i, j        : integer;
begin
  names:=TStringList.Create;
  linescount:=20;
  i:=1;
  while i<=Paramcount do begin
    if lowercase(ParamStr(i))='-n' then begin
      Val(ParamStr(i+1), l, err);
      if err=0 then linescount:=l;
      inc(i);
    end else
      names.Add(ParamStr(i));
    inc(i);
  end;
  lines:=TStringList.Create;
  for i:=0 to names.Count-1 do begin
    lines.Clear;
    try
      lines.LoadFromFile(names[i]);
      for j:=Max(0, lines.Count-linescount) to lines.Count-1 do
        writeln(lines[j]);
    except
    end;
  end;
  lines.Free;
  names.Free;
end.

Reply by email

dmitry boyarintsev
to dmitry boyarintsev

To make the file sharable at Windows you need to apply this patch to lcl proc (and eventually rebuild LCL and Lazarus): After that any program can read/write to the logging file

?Index: lclproc.pas
=================================================================== --- lclproc.pas (revision 26528)
+++ lclproc.pas (working copy)
@@ -2051,6 +2051,8 @@

       Result := ExpandFileNameUTF8(Result);
   end;
+var
+  fm: Byte;
 begin
   DebugText := nil;
   DebugFileName := GetDebugFileName;
@@ -2058,11 +2060,19 @@
     (DirPathExists(ExtractFileDir(DebugFileName))) then begin
     new(DebugText);
     try
+      fm:=Filemode;
+      FileMode:=fmShareDenyNone;
       Assign(DebugText^, DebugFileName);
       if FileExistsUTF8(DebugFileName) then
         Append(DebugText^)
-      else
+      else begin
         Rewrite(DebugText^);
+        // recreating the file to be sharable!
+        Close(DebugText^);
+        Assign(DebugText^, DebugFileName);
+        Append(DebugText^)
+      end;
+      Filemode:=fm;
     except
       Freemem(DebugText);
       DebugText := nil;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: lclproc.patch
Type: application/octet-stream
Size: 909 bytes
Desc: not available
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20100717/988c84fd/attachment.obj>

Reply by email

Mattias Gaertner
to dmitry boyarintsev: To make the file sharable at Windows you need to apply this patch to lcl proc (and…
To make the file sharable at Windows you need to apply this patch to lcl proc (and eventually rebuild LCL and Lazarus): After that any program can read/write to the logging file

Is it really that complicated under windows to open a file shared readable?
Why is an Assign, Close, Assign, Append needed?

Mattias

Show quoted text 35 lines
?Index: lclproc.pas =================================================================== --- lclproc.pas (revision 26528) +++ lclproc.pas (working copy) @@ -2051,6 +2051,8 @@ Result := ExpandFileNameUTF8(Result); end; +var + fm: Byte; begin DebugText := nil; DebugFileName := GetDebugFileName; @@ -2058,11 +2060,19 @@ (DirPathExists(ExtractFileDir(DebugFileName))) then begin new(DebugText); try + fm:=Filemode; + FileMode:=fmShareDenyNone; Assign(DebugText^, DebugFileName); if FileExistsUTF8(DebugFileName) then Append(DebugText^) - else + else begin Rewrite(DebugText^); + // recreating the file to be sharable! + Close(DebugText^); + Assign(DebugText^, DebugFileName); + Append(DebugText^) + end; + Filemode:=fm; except Freemem(DebugText); DebugText := nil;

Reply by email

dmitry boyarintsev
to Mattias Gaertner
Mattias Gaertner wrote
Is it really that complicated under windows to open a file shared readable? Why is an Assign, Close, Assign, Append needed?

You're right! There's no reason for such complexity. Changing Filemode flag to fmShareDenyNone (or fmShareDenyWrite) is enough to make the file readable, even if file is created via Rewrite() function.

thanks,
dmitry

Reply by email

Marc Weustink
to Mattias Gaertner: On Sat, 17 Jul 2010 14:33:03 +0000 Is it really that complicated under windows to open a…
Mattias Gaertner wrote 10 lines
On Sat, 17 Jul 2010 14:33:03 +0000 dmitry boyarintsev wrote: > To make the file sharable at Windows you need to apply this patch to > lcl proc (and eventually rebuild LCL and Lazarus): > After that any program can read/write to the logging file Is it really that complicated under windows to open a file shared readable? Why is an Assign, Close, Assign, Append needed?

You need to make sure a file exists. IIRC it is not possible to create a file for sharing, you can only open a file sharable

Marc

Reply by email

dmitry boyarintsev
to Marc Weustink
Marc Weustink wrote
You need to make sure a file exists. IIRC it is not possible to create a file for sharing, you can only open a file sharable

It's possible. at least the following example did work on Windows.

begin
  filemode:=fmShareDenyNone;
  assign(f, 'c:\test.txt'); rewrite(f);
  writeln(f,'test');
  writeln('press enter to unlock file');
  readln;
  close(f);
end.

The newly created c:\test.txt was sharable.

AFAIK, it's "impossible" for TFileStream using fmCreate flag. fmCreate flag causes a file to be re-created without sharing (at Windows).

thanks,
dmitry

Reply by email

Hans-Peter Diettrich
to Marc Weustink: You need to make sure a file exists. IIRC it is not possible to create a file for…
Show quoted text 6 lines
> Is it really that complicated under windows to open a file shared > readable? > Why is an Assign, Close, Assign, Append needed? You need to make sure a file exists. IIRC it is not possible to create a file for sharing, you can only open a file sharable

You mean that the Append is that stupid, that it needs an already existing file? Or is that behaviour required for compatibility reasons?

At least on Windows all files are created or opened with the unique CreateFile procedure - do other platforms require multiple different calls for that purpose?

DoDi

Reply by email

Hans-Peter Diettrich
to dmitry boyarintsev: It's possible. at least the following example did work on Windows. begin…
Show quoted text 10 lines
On Mon, Jul 19, 2010 at 4:23 AM, Marc Weustink wrote: > You need to make sure a file exists. IIRC it is not possible to create a > file for sharing, you can only open a file sharable It's possible. at least the following example did work on Windows. begin filemode:=fmShareDenyNone; assign(f, 'c:\test.txt'); rewrite(f); writeln(f,'test');

Did you try to stop at this point, or even before WriteLn, and check whether the file is accessible from other applications?

Show quoted text 9 lines
writeln('press enter to unlock file'); readln; close(f); end. The newly created c:\test.txt was sharable. AFAIK, it's "impossible" for TFileStream using fmCreate flag. fmCreate flag causes a file to be re-created without sharing (at Windows).

I can not remember any file attribute "shareable" on Windows. At least FAT filesystems have no such file attribute.

It might be another issue, though, that new files become *visible* to other processes only after they have been closed at least once. Same considerations apply to the file size, that is updated (on disk) only when a file is closed. I dunno whether it's possible to read added content, before the file has been closed. That would mean that *all* log files have to be closed and reopened frequently (after every write), to give other processes a chance to also read the added text. Kind of a Flush or Sync procedure would be nice to have, for that purpose...

DoDi

Reply by email

Marc Weustink
to dmitry boyarintsev: It's possible. at least the following example did work on Windows. begin…
dmitry boyarintsev wrote 19 lines
On Mon, Jul 19, 2010 at 4:23 AM, Marc Weustink wrote: > You need to make sure a file exists. IIRC it is not possible to create a > file for sharing, you can only open a file sharable It's possible. at least the following example did work on Windows. begin filemode:=fmShareDenyNone; assign(f, 'c:\test.txt'); rewrite(f); writeln(f,'test'); writeln('press enter to unlock file'); readln; close(f); end. The newly created c:\test.txt was sharable. AFAIK, it's "impossible" for TFileStream using fmCreate flag. fmCreate flag causes a file to be re-created without sharing (at Windows).

Yes, i've to admit that I don't use assign etc. but since it is the case for filestreams ans plain winapi, I assumed it would be the case for assign too.

Marc

Reply by email

José Mejuto
to Hans-Peter Diettrich: Did you try to stop at this point, or even before WriteLn, and check whether the file is…

Hello Lazarus-List,

you wrote
> AFAIK, it's "impossible" for TFileStream using fmCreate flag. fmCreate > flag causes a file to be re-created without sharing (at Windows).

HPD> I can not remember any file attribute "shareable" on Windows. At least HPD> FAT filesystems have no such file attribute.

fmCreate is = -1 so no other flags can be added to the TFileStream creation.

HPD> It might be another issue, though, that new files become *visible* to HPD> other processes only after they have been closed at least once. Same

In Windows at least, a file is visible as soon as it has been created. Assign does not create the file.

HPD> considerations apply to the file size, that is updated (on disk) only HPD> when a file is closed. I dunno whether it's possible to read added HPD> content, before the file has been closed.

When reading a file which is being opened by another process too you get realtime length information about the file size, but directory enumeration functions only check the information on disk. So to get real time information about file sizes you must open each file and check its size.

HPD> That would mean that *all* log
HPD> files have to be closed and reopened frequently (after every write), to HPD> give other processes a chance to also read the added text. Kind of a HPD> Flush or Sync procedure would be nice to have, for that purpose...

No need as the file handle is shared between both (or n) processes, but each one have its own file pointers. File size is shared between both processes.

I'm talking about windows with NT kernel. Win98 and others I'm not sure.

Best regards, Jos?

Reply by email

dmitry boyarintsev
to Hans-Peter Diettrich: Did you try to stop at this point, or even before WriteLn, and check whether the file is…
Hans-Peter Diettrich wrote
Did you try to stop at this point, or even before WriteLn, and check whether the file is accessible from other applications?

Yes. I did.

Hans-Peter Diettrich wrote
You mean that the Append is that stupid, that it needs an already existing file? Or is that behaviour required for compatibility reasons?

It's compatibility with Turbo Pascal file operations.

thanks,
dmitry

Reply by email

Mattias Gaertner
to dmitry boyarintsev: It's possible. at least the following example did work on Windows. begin…
Show quoted text 19 lines
On Mon, Jul 19, 2010 at 4:23 AM, Marc Weustink wrote: > You need to make sure a file exists. IIRC it is not possible to create a > file for sharing, you can only open a file sharable It's possible. at least the following example did work on Windows. begin filemode:=fmShareDenyNone; assign(f, 'c:\test.txt'); rewrite(f); writeln(f,'test'); writeln('press enter to unlock file'); readln; close(f); end. The newly created c:\test.txt was sharable. AFAIK, it's "impossible" for TFileStream using fmCreate flag. fmCreate flag causes a file to be re-created without sharing (at Windows).

Can you create a new patch?

Mattias

Reply by email

dmitry boyarintsev
to Mattias Gaertner
Mattias Gaertner wrote
Can you create a new patch?

Here you go. Has just tested it on Vista64.

Index: lclproc.pas
=================================================================== --- lclproc.pas (revision 26738)
+++ lclproc.pas (working copy)
@@ -2051,13 +2051,18 @@

       Result := ExpandFileNameUTF8(Result);
   end;
+var
+  fm : Byte;
+
 begin
   DebugText := nil;
   DebugFileName := GetDebugFileName;
   if (length(DebugFileName)>0) and
     (DirPathExists(ExtractFileDir(DebugFileName))) then begin
+    fm:=Filemode;
     new(DebugText);
     try
+      Filemode:=fmShareDenyNone;
       Assign(DebugText^, DebugFileName);
       if FileExistsUTF8(DebugFileName) then
         Append(DebugText^)
@@ -2069,6 +2074,7 @@
       // Add extra line ending: a dialog will be shown in windows gui
application
       writeln(StdOut, 'Cannot open file: ', DebugFileName+LineEnding);
     end;
+    Filemode:=fm;
   end;
   if DebugText=nil then
   begin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: lclproc.patch
Type: application/octet-stream
Size: 867 bytes
Desc: not available
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20100719/56dae45a/attachment.obj>

Reply by email