2.2 代码块--delphi 写日志模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//2.2 代码块--写日志
 
//调用例句如:LogMsg('FTP上传线程终止',False,true);
procedure LogMsg(AMsg: string; const blnIsErrorMsg: boolean = False; const BoolSaveToFile: Boolean = True);
var
  strMsg: string;
begin
  try
    //FLogMemoCallFlag.Enter;
    strMsg := AMsg;
 
    if blnIsErrorMsg and (Pub_ProgramLog.IndexOfName(AMsg) = -1) then
      Pub_ProgramLog.Add(AMsg +'=' + FormatDateTime('yyyy-mm-dd hh:nn:ss',Now) );
 
    if blnIsErrorMsg then strMsg := '******' + strMsg + '******';
    if Assigned(Pub_Memo) and (Pub_BlnShowLog) then
    begin
      if Pub_Memo.Lines.Count >= 500 then Pub_Memo.Lines.Clear;
      Pub_Memo.Lines.Add(#13#10 + FormatDateTime('yyyy-mm-dd hh:nn:ss',Now) + ' -> ' + strMsg);
    end;
  finally
    //FLogMemoCallFlag.Leave;
  end;
  if BoolSaveToFile then
  begin
    try
      WriteLog(SlashSep(ExtractFilePath(Application.ExeName), 'PostContentRunLog\' + FormatDateTime('yyyy-mm-dd',Now) + '.Txt'),
        FormatDateTime('hh:nn:ss',Now) + ':' + strMsg);
    except
      ;
    end;
  end;
end;
 
 
function SlashSep(const Path, S: string): string;
begin
  if (Trim(Path) = '') or (Trim(S) = '') then
    Result := path + s
  else begin
        //检查"Path"字符串是否是以"\"结尾
    if AnsiLastChar(Path)^ <> '\' then
      Result := Path + '\' + s
    else
      Result := Path + s;
  end
end;
 
function WriteLog(AFileName: string; ALogCont: string; const BoolOverride: Boolean = false): integer;
var
  //TmpTextFile: TextFile;
  FileFullName: string;
  TmpStr: string;
  TmpFullDir: string;
begin
  result := -1;
  try
    TmpStr := ALogCont;
    FileFullName := AFileName;
    TmpFullDir := ExtractFilePath(FileFullName);
    if not DirectoryExists(TmpFullDir) then
      CreateDir(TmpFullDir);
    try
      AssignFile(Pub_LogTextFile, FileFullName);
      if (not BoolOverride) and FileExists(FileFullName) then
        Append(Pub_LogTextFile)
      else begin
        Rewrite(Pub_LogTextFile);
      end;
      try
        Writeln(Pub_LogTextFile, trim(TmpStr));
      finally
        CloseFile(Pub_LogTextFile);
      end;
      result := 1;
    except
      result := -1;
    end;
  except
    ;
  end;
end;

  

posted @   海蓝7  阅读(396)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示