• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 358
  • Last Modified:

send via email only new logs entries

Hi.
On a server I have an application which writes an entry in a log file (txt file) every time an event occours. The log written are all of the same type. This is the entry:

time and data/ field1/field2


Now . I want to create another small application (as win service)
 which can read the file one time per day (i.e during the night at 2 am when I know for sure that the first application won't write into the log file) and sends all the new entry to a specific mail recepient as one shot. So the second application should know from where start by keeping track of time and data field.

Can you help me with this small project?


0
jaja2005
Asked:
jaja2005
  • 6
  • 6
1 Solution
 
Geert GOracle dbaCommented:
just use the equivalent of tail on windows:
http://tailforwin32.sourceforge.net/
0
 
jaja2005Author Commented:
Unfortunatly is C++
0
 
Geert GOracle dbaCommented:
all the groups you assigned to this question are "delphi"
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
jaja2005Author Commented:
How to achive that using pascal.?. :-(
0
 
Geert GOracle dbaCommented:
see snippet to get changed log lines


procedure TForm1.Tail(FileName: string; Lines: TStrings; LimitLines: Integer = 0);

  function OpenReg: TRegistry;
  const
    CompanyName = 'jaja2005';
    AppName     = 'Tail';
  begin
    Result := TRegistry.Create(KEY_ALL_ACCESS);
    Result.RootKey := HKEY_CURRENT_USER; // you may want to use Local machine on a server
    Result.OpenKey(Format('Software\%s\%s', [CompanyName, AppName]), True);
  end;

  function ReadRegInt(ParamName: string; Default: integer = 0): integer;
  begin
    Result := Default;
    with OpenReg do
    try
      if ValueExists(ParamName) then
        Result := ReadInteger(ParamName);
    finally
      Free;
    end;
  end;

  procedure WriteRegInt(ParamName: string; Value: integer);
  begin
    with OpenReg do
    try
      WriteInteger(ParamName, Value);
    finally
      Free;
    end;
  end;

var
  Start: Integer;
  F: TFileStream;
begin
  if FileExists(FileName) then
  begin
    Lines.BeginUpdate;
    try
      Start := ReadRegInt(Format('filesize_%s', [FileName]));
      F := TFileStream.Create(FileName, fmOpenRead);
      try
        F.Seek(Start, soFromBeginning);
        Lines.LoadFromStream(F);
        WriteRegInt(Format('filesize_%s', [FileName]), F.Size);
      finally
        F.Free;
      end;
    finally
      Lines.EndUpdate;
    end;
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  Tail('c:\temp\test.txt', Memo1.Lines);
end;

Open in new window

0
 
jaja2005Author Commented:
how to use it? Can you send the project file? :-(
0
 
Geert GOracle dbaCommented:
eh ?

just create a file c:\temp\test.txt
add some lines and save
then run the above code

then add some more lines to the file
and run the above code again

the memo will contain the newly added lines

only appending works
0
 
jaja2005Author Commented:
Thx.
Why it uses Tregistry Class?
0
 
jaja2005Author Commented:
it works. Great. can you explain me in a few word how it uses the registry?
0
 
jaja2005Author Commented:
Shouldn't be a big task to covert this small application in a windows service right?
0
 
Geert GOracle dbaCommented:
it uses the registry to save the last size of the file it read
the next time it first checks this size and reads on from that point

hmmm come to think of it, i haven't put a check in, if this goes past the file size
emptying the file would not show anything anymore

change this code:
F := TFileStream.Create(FileName, fmOpenRead);
try
  F.Seek(Start, soFromBeginning);

to
F := TFileStream.Create(FileName, fmOpenRead);
try
  if (Start > 0) and (Start > F.Size) then
    Start := 0;
  F.Seek(Start, soFromBeginning);
0
 
Geert GOracle dbaCommented:
to a windows service ?

well not really
i never actually do that, i have some apps like this running on database servers

what i do, is add a windows schedular task,
and set properties to run when user is logged off and max priviliges

then i don't need to bother with converting it to a service
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now