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

Write a message to a log and display a message

Dear Experts,

I making a programma that maintains a logging.
But I want certain messages not only written to a logging
but also show a message, how can i do that?

This is the procedure that writes to a logging:

procedure Log(str: string);
var
F: TextFile;
begin
AssignFile(F, 'c:\log.txt');
if not FileExists('c:\log.txt') then
Rewrite(F)
else
Append(F);
Writeln(F, DateTimeToStr(Now) + ': ' + str);
CloseFile(F);
end;

I have a lot of procedures that write to a logging, this is one of them:

procedure MakeConnect;
begin
  TcpStartup();
  if deb then
    Log('TCP Started.');
  soc := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if soc = INVALID_SOCKET then
  begin
    Log('Error socket invalid.');
    socOpen := False;
  end
  else
  begin
    if deb then
      Log('Socket opened OK.');
    socOpen := True;
  end;

The user can check a checkbox if he want to log messages.
What do i have to add or change to make the certain messages not only write to a log but give also a showmessage, even when the checkbox is not checked.

Greetings,

P.
0
peterkiers
Asked:
peterkiers
  • 5
1 Solution
 
Russell LibbySoftware Engineer, Advisory Commented:
One example:

procedure Log(str: string; Display: Boolean = False);
var  F:       TextFile;
begin

  AssignFile(F, 'c:\log.txt');
  try
     if not FileExists('c:\log.txt') then
        Rewrite(F)
     else
        Append(F);
     Writeln(F, DateTimeToStr(Now) + ': ' + str);
  finally
     CloseFile(F);
  end;
  if Display then MessageBox(Application.Handle, PChar(str), 'Log Display', MB_OK or MB_ICONINFORMATION);

end;

which can be called using the same exact layout as you have, or by:

Log('Socket opened OK.', True);

which will display the info as well as log it

Regards,
Russell



0
 
peterkiersAuthor Commented:
error at line:

if Display then MessageBox(Application.Handle, PChar(str), 'Log Display', MB_OK or MB_ICONINFORMATION);
end;

[Error] telnetu.pas(52): Undeclared identifier: 'Application'
[Error] telnetu.pas(52): Not enough actual parameters

P.
0
 
peterkiersAuthor Commented:
I found it i have at Form to uses.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
peterkiersAuthor Commented:
One small thing where in this line i should at the boolean:

  Log('Getservbyname error: ' + Format('%d', [WSAGetLastError()]));

P.
0
 
peterkiersAuthor Commented:
I have got it

      Log('Get local host error:'
        + Format('%d', [WSAGetLastError()]),true);

gr,

Peter Kiers
0
 
peterkiersAuthor Commented:
Thanks RLLIBY.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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