Autor Beitrag
G-man
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 258

Win 2000, Win ME, SuSE 8.2
D5 Standard, D6 Professional
BeitragVerfasst: Di 16.09.03 16:15 
Moin,
kann mir jemand sagen, wie ich eine *.log File schreiben kann, (aus einem Delphiprogramm heraus)?

_________________
...To err is human, but to really foul things up requires a computer.


Zuletzt bearbeitet von G-man am Fr 19.09.03 13:11, insgesamt 1-mal bearbeitet
barfuesser
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 324



BeitragVerfasst: Di 16.09.03 16:42 
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Di 16.09.03 18:06 
Und noch ein brandheißer Tipp: Suche in: Delphi-Forum, Delphi-Library TSTRINGLIST.
mimi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3458

Ubuntu, Win XP
Lazarus
BeitragVerfasst: Mi 17.09.03 17:34 
NOCH besser: Suche in: Delphi-Forum, Delphi-Library TFILESTREAM

_________________
MFG
Michael Springwald, "kann kein englisch...."
Licki
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 132

Knoppix, Win 95, Win 2000, Win NT, Win 98, Win XP
Delphi 3
BeitragVerfasst: Do 18.09.03 09:06 
Guter Tipp, ähnliche Frage hatte ich auch.
Tino
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Veteran
Beiträge: 9839
Erhaltene Danke: 45

Windows 8.1
Delphi XE4
BeitragVerfasst: Do 18.09.03 10:24 
Hallo,

ich benutze für kleine Projekte oft die folgende Unit. Damit kann man recht einfach ein Logfile schreiben. Einfach im Programm eine Instance von tLogFile erzeugen und mit den Methoden WriteHint, WriteError, etc Einträge in das Logfile schreiben. Mit Hilfe der Methoden BeginSection und EndSection kann man dem Logfile ein wenig Übersichtlichkeit spendieren:
ausblenden volle Höhe Delphi-Quelltext
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:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
Unit LogFile;

Interface

Type
  tLogFileLineTypeId = (
      idLogFileLineTypeDebug,
      idLogFileLineTypeHint,
      idLogFileLineTypeWarning,
      idLogFileLineTypeError
    );

  tLogFile = Class
      Private
        fFile: TextFile;
        fSectionLevel: Integer;
        fWithDebug: Boolean;

        Procedure WriteLine (aType: tLogFileLineTypeId; aText: String);
      Public
        Constructor Create (aFilename: String);
        Destructor Destroy;  override;

        Procedure BeginSection;
        Procedure EndSection;

        Procedure WriteDebug (aText: String);
        Procedure WriteHint (aText: String);
        Procedure WriteWarning (aText: String);
        Procedure WriteError (aText: String);
        Procedure WriteEmptyLine;

        Property  WithDebug: Boolean read fWithDebug write fWithDebug;
    End;

Implementation

Uses
  SysUtils;

Constructor tLogFile.Create (aFilename: String);
Begin
  AssignFile (fFile, aFileName);

  If FileExists (aFilename) Then
    Append (fFile)
  Else
    Rewrite (fFile);

  fSectionLevel := 0;
  fWithDebug := False;
End;

Destructor tLogFile.Destroy;
Begin
  CloseFile (fFile);

  Inherited;
End;

Procedure tLogFile.WriteLine (aType: tLogFileLineTypeId; aText: String);
Const
  cTypeChar : Array [tLogFileLineTypeId] Of Char = ('D'' ''*''!');

    Function GetSpace (aLength: Integer): String;
    Var
      Str: String;
    Begin
      Str := '';

      While Length (Str) < aLength Do
        Str := Str + #32;

      GetSpace := Str;
    End;

Var
  Str: String;
Begin
  If (aType = idLogFileLineTypeDebug) and Not fWithDebug Then
    Exit;

  Str := FormatDateTime ('yy/mm/dd hh:nn:ss', Now);
  Str := Str + #32 + cTypeChar [aType] + #32 + GetSpace (fSectionLevel * 2) + aText;

  WriteLN (fFile, Str);
End;

Procedure tLogFile.WriteDebug (aText: String);
Begin
  WriteLine (idLogFileLineTypeDebug, aText);
End;

Procedure tLogFile.WriteHint (aText: String);
Begin
  WriteLine (idLogFileLineTypeHint, aText);
End;

Procedure tLogFile.WriteWarning (aText: String);
Begin
  WriteLine (idLogFileLineTypeWarning, aText);
End;

Procedure tLogFile.WriteError (aText: String);
Begin
  WriteLine (idLogFileLineTypeError, aText);
End;

Procedure tLogFile.WriteEmptyLine;
Begin
  WriteLN (fFile, '');
End;

Procedure tLogFile.BeginSection;
Begin
  Inc (fSectionLevel);
End;

Procedure tLogFile.EndSection;
Begin
  Dec (fSectionLevel);
  If fSectionLevel < 0 Then
    fSectionLevel := 0;
End;

End.

Gruß
Tino
G-man Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 258

Win 2000, Win ME, SuSE 8.2
D5 Standard, D6 Professional
BeitragVerfasst: Fr 19.09.03 13:10 
Vielen Dank, sowas hatte ich gesucht...

_________________
...To err is human, but to really foul things up requires a computer.
Udontknow
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2596

Win7
D2006 WIN32, .NET (C#)
BeitragVerfasst: Fr 19.09.03 14:26 
Sehr nützlich, die Unit! :)

BTW: Wir wäre es mit einer Sparte "Units", in der man häufig verwendete Units / Komponenten ablegt?

Cu,
Udontknow
maximus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 896

Win XP, Suse 8.1
Delphi 4/7/8 alles prof
BeitragVerfasst: Fr 19.09.03 15:14 
Udontknow hat folgendes geschrieben:

BTW: Wir wäre es mit einer Sparte "Units", in der man häufig verwendete Units / Komponenten ablegt?
...


ich würde sogar noch eine schritt weiter gehen und eine sektion einrichten, in der man units zur diskussion stellen kann und die community kann sie dann weiterentwickeln und ergänzen. Die fertigen units werden dann gesammelt und alle AUQler dürfen sie dann frei benutzen!

denke so könnte man eine schöne code-base schaffen, die uns das leben einfacher machen würde..?

_________________
mfg.
mâximôv
Crowbar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 180

WinXP, SP2
D6 Enterprise
BeitragVerfasst: Sa 17.01.04 19:08 
Hi,
ich möchte dieses Thema noch einmal kurz mit einer Frage "öffnen".
In meinem Programm möchte ich ebenfalls eine "error.log" - Datei erstellen lassen, um darin die aufgetretenen Fehlermeldungen zu speichern und anschliessend auszuwerten bzw. zu beheben. ;-)
Ist es zusätzlich irgendwie möglich, auch die Quellcode-Zeile mitzuloggen, an der der Fehler auftrat?
Zum Beispiel:
EConvertError in Zeile 199 aufgetreten

Natürlich könnte man in jeder "gefährlichen" Routine einen "try...except..end" - Block mit der entsprechenden Quellcode-Zeile schreiben, wie z.B.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
197:  ...
198:  try
199:   a:=StrToInt(123);
200:  except 
201:   showmessage('Fehler in Zeile 199 aufgetreten');
202:  end;
203:  ...

aber dies wäre irgendwie zu aufwendig. Da ich zumal meine Exceptions gesondert bzw. zusätzlich mit:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
Procedure TForm1.AppException(Sender: TObject; E: Exception);
begin
  MessageDlg('Es ist folgender Fehler aufgetreten:'+#10#13#10#13+E.Message+#10#13+
      'Fehlertyp: '+E.ClassName+#10#13+'Sender: '+Sender.ClassName,mtError,[mbOk],0);
end;

Procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnException :=AppException;
  ...
end;

abfange.

Cu,
Crowbar

Moderiert von user profile iconPeter Lustig: Code- durch Delphi-Tags ersetzt
LarsMiddendorf
Hält's aus hier
Beiträge: 11



BeitragVerfasst: Sa 17.01.04 23:21 
Eine andere Methode ist im initialization Block eine Datei zu öffnen und der Variablen Output diese Datei zuzuweisen. Dann kann man überall im Programm einfach mit writeln in die Log Datei schreiben und braucht die log unit nicht überall einzubinden. Im finalization Block der log Unit schließt man die Datei dann wieder.