forked from adrianosantostreina/MultiLog4D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADRAndroidLogcat.pas
165 lines (137 loc) · 5.22 KB
/
ADRAndroidLogcat.pas
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
unit ADRAndroidLogcat;
interface
uses
System.SysUtils,
System.StrUtils,
System.Classes
{$IFDEF ANDROID}
,Androidapi.Log
,Androidapi.JNI.OS
,Androidapi.JNI.JavaTypes
,Androidapi.JNIBridge
,Androidapi.JNI.GraphicsContentViewText
,Androidapi.Helpers
{$ENDIF}
;
type
{$IFDEF ANDROID}
Jutil_LogClass = interface(JObjectClass)
['{62108FE8-1DBB-4C4F-A0C7-35D12BD116DC}']
{class} function _GetASSERT: Integer; cdecl;
{class} function _GetDEBUG: Integer; cdecl;
{class} function _GetERROR: Integer; cdecl;
{class} function _GetINFO: Integer; cdecl;
{class} function _GetVERBOSE: Integer; cdecl;
{class} function _GetWARN: Integer; cdecl;
{class} function d(tag: JString; msg: JString): Integer; cdecl; overload;
{class} function d(tag: JString; msg: JString; tr: JThrowable): Integer; cdecl; overload;
{class} function e(tag: JString; msg: JString): Integer; cdecl; overload;
{class} function e(tag: JString; msg: JString; tr: JThrowable): Integer; cdecl; overload;
{class} function getStackTraceString(tr: JThrowable): JString; cdecl;
{class} function i(tag: JString; msg: JString): Integer; cdecl; overload;
{class} function i(tag: JString; msg: JString; tr: JThrowable): Integer; cdecl; overload;
{class} function isLoggable(tag: JString; level: Integer): Boolean; cdecl;
{class} function println(priority: Integer; tag: JString; msg: JString): Integer; cdecl;
{class} function v(tag: JString; msg: JString): Integer; cdecl; overload;
{class} function v(tag: JString; msg: JString; tr: JThrowable): Integer; cdecl; overload;
{class} function w(tag: JString; msg: JString): Integer; cdecl; overload;
{class} function w(tag: JString; msg: JString; tr: JThrowable): Integer; cdecl; overload;
{class} function w(tag: JString; tr: JThrowable): Integer; cdecl; overload;
{class} function wtf(tag: JString; msg: JString): Integer; cdecl; overload;
{class} function wtf(tag: JString; tr: JThrowable): Integer; cdecl; overload;
{class} function wtf(tag: JString; msg: JString; tr: JThrowable): Integer; cdecl; overload;
{class} property ASSERT: Integer read _GetASSERT;
{class} property DEBUG: Integer read _GetDEBUG;
{class} property ERROR: Integer read _GetERROR;
{class} property INFO: Integer read _GetINFO;
{class} property VERBOSE: Integer read _GetVERBOSE;
{class} property WARN: Integer read _GetWARN;
end;
[JavaSignature('android/util/Log')]
Jutil_Log = interface(JObject)
['{6A5EC34E-CB76-4AB0-A11D-7CCB3B40C571}']
end;
TJutil_Log = class(TJavaGenericImport<Jutil_LogClass, Jutil_Log>) end;
{$ENDIF}
//I - Info
//W - Warning
//E - Error
//F - Fatal
TLogType = (ltInformation, ltWarning, ltError, ltFatalError);
TADRAndroidLog = class
private
class var FTag : string;
class function GetPackageName: string;
class procedure EventLog(AMsg: string; AIsForceBraodCast: Boolean = False);
class procedure EventLogConsole(AMsg: string);
public
class procedure LogWrite(const AMsg: string; const ALogType: TLogType);
class procedure LogWriteInformation(const AMsg: string);
class procedure LogWriteWarning(const AMsg: string);
class procedure LogWriteError(const AMsg: string);
class procedure LogWriteFatalError(const AMsg: string);
class procedure Tag(const ATag: string);
end;
implementation
class function TADRAndroidLog.GetPackageName: string;
begin
// Result := JStringToString(TAndroidHelper.Context.getPackageName);
end;
class procedure TADRAndroidLog.EventLog(AMsg: string; AIsForceBraodCast: Boolean);
begin
EventLogConsole(AMsg);
end;
class procedure TADRAndroidLog.EventLogConsole(AMsg: string);
begin
{$IFDEF ANDROID}
TJutil_Log.JavaClass.d(StringToJString(FTag), StringToJString(AMsg));
{$ENDIF}
end;
class procedure TADRAndroidLog.LogWrite(const AMsg: string; const ALogType: TLogType);
var
LMsg: string;
LPathFile: string;
LFileName: string;
begin
LMsg := EmptyStr;
case ALogType of
ltWarning:
LMsg := 'WARNING: ';
ltError:
LMsg := 'ERROR: ';
ltFatalError:
LMsg:= 'FATAL ERROR: ';
end;
LMsg := LMsg + FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', Now) + ' ' + AMsg;
// LFileName := FormatDateTime('yyyymmdd', Now) + '.log';
// LPathFile := TUtils.GetApplicationPath;
// LPathFile := TUtils.Combine(LPathFile, ['Log']);
// if not(DirectoryExists(LPathFile)) then
// ForceDirectories(LPathFile);
// LPathFile := TUtils.Combine(LPathFile, [LFileName]);
EventLog(LMsg);
// TFile.AppendAllText(LPathFile, LMsg, TEncoding.UTF8);
end;
class procedure TADRAndroidLog.LogWriteError(const AMsg: string);
begin
LogWrite(AMsg, TLogType.ltError);
end;
class procedure TADRAndroidLog.LogWriteInformation(const AMsg: string);
begin
LogWrite(AMsg, TLogType.ltInformation)
end;
class procedure TADRAndroidLog.LogWriteWarning(const AMsg: string);
begin
LogWrite(AMsg, TLogType.ltWarning)
end;
class procedure TADRAndroidLog.Tag(const ATag: string);
begin
if not ATag.IsEmpty
then FTag := ATag
else FTag := 'TADRAndroidLog';
end;
class procedure TADRAndroidLog.LogWriteFatalError(const AMsg: string);
begin
LogWrite(AMsg, TLogType.ltFatalError);
end;
end.