Hey,
Im currently working on a Program in Delphi and have a "uLogAbstractor" unit tot handle logging across threads, this unit essentially just writes the log message and level to a global variable
and the sends a Windows Message using the Winapi to the Main Form which has the logging components. Now I have noticed that regardless of the thread, the global variable containing the
message seems to reset to its previous value after ive called PostMessage. Weirdly this doesn't occur if i output it using ShowMessage before calling PostMessage. Here is the log abstractor
unit uLogAbstractor;
interface
uses SysUtils, Winapi.Windows, uConsts, Vcl.Dialogs;
procedure Log(const lvl: Integer; const msg: String);
procedure LogF(const lvl: Integer; const msg: String; const _format: array of const);
var
msghandle: Cardinal = 0;
logmsg: String;
loglvl: Integer;
implementation
procedure Log(const lvl: Integer; const msg: String);
begin
if msghandle = 0 then exit;
loglvl := lvl;
logmsg := msg;
// ShowMessage(logmsg);
PostMessage(msghandle, WM_LOG_MESSAGE, 0, 0);
end;
procedure LogF(const lvl: Integer; const msg: String; const _format: array of const);
begin
if msghandle = 0 then exit;
loglvl := lvl;
logmsg := Format(msg, _format);
PostMessage(msghandle, WM_LOG_MESSAGE, 0, 0);
end;
end.
And here is the procedure handling the message in the main form
procedure TfrmMain.WMLogMessage(var msg: string);
begin
FLogger.Log(uLogAbstractor.loglvl, uLogAbstractor.logmsg);
if uLogAbstractor.loglvl = LOG_LVL_DBG then
begin
if FLogger.FLogDebug then
memoLog.Lines.Add(FLogger.FLastMsg);
end else
memoLog.Lines.Add(FLogger.FLastMsg);
end;
Does anyone know the reason for this behaviour? I'd be grateful for any help!