Debian Codegraph @

src/base/android/castleandroidinternallog.pas

raw
1 { Android logging facility.
2 @exclude Internal for the engine. }
3 unit CastleAndroidInternalLog;
4
5 {$I castleconf.inc}
6
7 interface
8
9 { Based on Android NDK platforms/android-4/arch-arm/usr/include/android/log.h .
10 See also Lazarus' trunk/lcl/interfaces/customdrawn/android/log.pas . }
11
12 type
13 TAndroidLogPriority = (
14 alUnknown,
15 alDefault,
16 alVerbose,
17 alDebug,
18 alInfo,
19 alWarn,
20 alError,
21 alFatal,
22 alSilent
23 );
24
25 procedure AndroidLog(const Priority: TAndroidLogPriority; const S: string);
26 procedure AndroidLog(const Priority: TAndroidLogPriority; const S: string; const Args: array of const);
27
28 { Like AndroidLog, but works better for log strings (> 4076 characters),
29 otherwise the default AndroidLog seems to cut them off. }
30 procedure AndroidLogRobust(const Priority: TAndroidLogPriority; const S: string);
31
32 implementation
33
34 uses CTypes, SysUtils, CastleUtils;
35
36 const
37 AndroidLogLib = 'liblog.so';
38
39 function __android_log_write(prio: CInt; tag, text: PChar): CInt; cdecl;
40 external AndroidLogLib;
41
42 var
43 LogTag: string;
44
45 procedure AndroidLog(const Priority: TAndroidLogPriority; const S: string);
46 const
47 MaxAndroidTagLength = 23;
48 begin
49 if LogTag = '' then
50 LogTag := Copy(ApplicationName, 1, MaxAndroidTagLength);
51 __android_log_write(Ord(Priority), PChar(LogTag), PChar(S));
52 end;
53
54 procedure AndroidLog(const Priority: TAndroidLogPriority; const S: string; const Args: array of const);
55 begin
56 AndroidLog(Priority, Format(S, Args));
57 end;
58
59 procedure AndroidLogRobust(const Priority: TAndroidLogPriority; const S: string);
60 var
61 I: Integer;
62 const
63 { The limit is actually 4076,
64 see http://jhshi.me/2014/06/30/stop-android-logcat-from-truncating-log-line/index.html .
65 It's safer to cut off earlier. }
66 MaxChunkLength = 4000;
67 begin
68 I := 1;
69 while I <= Length(S) do
70 begin
71 AndroidLog(Priority, Copy(S, I, MaxChunkLength));
72 I += MaxChunkLength;
73 end;
74 end;
75
76 end.