-
Notifications
You must be signed in to change notification settings - Fork 41
/
act_log.dart
147 lines (130 loc) · 3.42 KB
/
act_log.dart
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
// This source code is a part of Project Violet.
// Copyright (C) 2020-2024. violet-team. Licensed under the Apache-2.0 License.
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:crypto/crypto.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:synchronized/synchronized.dart' as sync;
enum ActLogType {
none,
// signal
signal,
// on/off
appStart,
appSuspense,
appResume,
appStop,
// // after loading
// pageSwipe,
// serviceButton,
// databaseSync,
// databaseSwitching,
// // search
// openSearch,
// searchWhat,
// searchTypeChange,
// filterOn,
// filterData,
// // bookmark
// articleBookmark,
// artistBookmark,
// openBookmarkGroup,
// moveBookmarkArticle,
// addBookmarkGroup,
// removeBookmarkGroup,
// // settings
// settingChange,
// // article
// openArticleInfo,
// openArtistInfo,
// downloadArticle,
// readArticle,
// // download
// readDownloadedArticle,
// // viewer
// viewStart,
// viewEnd,
// viewTab,
// viewTabReplace,
// viewBookmark,
// viewOpenArticleInfo,
// viewOpenSettingInfo,
// viewThumbnail,
// viewThumbnailJump,
// viewSlide,
// viewThumbBar,
}
class ActLogEvent {
DateTime? dateTime;
String? detail;
ActLogType type;
ActLogEvent({
this.dateTime,
this.detail,
required this.type,
}) {
dateTime ??= DateTime.now();
}
toJson() {
return jsonEncode({
'dt': dateTime.toString(),
'type': type.toString(),
'detail': detail,
});
}
static ActLogEvent fromJson(String json) {
final obj = jsonDecode(json);
return ActLogEvent(
type: ActLogType.values.byName((obj['type'] as String).split('.')[1]),
dateTime: DateTime.parse(obj['dt']),
detail: obj['detail'],
);
}
}
// this data is stored only on local storage
// for user data statistics
class ActLogger {
// Since isolates handle all asynchronous operations linearly,
// there is no need for mutual exclusion.
static sync.Lock lock = sync.Lock();
static late File logFile;
/// this session is refreshed every app restarting.
static late String session;
static List<ActLogEvent> events = <ActLogEvent>[];
static Timer? signalTimer;
static Future<void> init() async {
if (Platform.environment.containsKey('FLUTTER_TEST')) {
logFile = File('act-log.txt');
} else {
final dir = await getApplicationDocumentsDirectory();
logFile = File(join(dir.path, 'act-log.txt'));
}
if (!await logFile.exists()) {
await logFile.create();
}
session = sha1.convert(utf8.encode(DateTime.now().toString())).toString();
await log(ActLogEvent(type: ActLogType.appStart));
signalTimer = Timer.periodic(const Duration(seconds: 30), (timer) {
log(ActLogEvent(type: ActLogType.signal));
});
}
static Future<void> log(ActLogEvent msg) async {
print('$session ${msg.toJson()}');
if (!Platform.environment.containsKey('FLUTTER_TEST')) {
await lock.synchronized(() async {
events.add(msg);
await logFile.writeAsString('$session ${msg.toJson()}\n',
mode: FileMode.append);
});
}
}
static Future<void> exportLog() async {
final ext = Platform.isIOS
? await getApplicationSupportDirectory()
: await getExternalStorageDirectory();
final extpath = '${ext!.path}/act-log.txt';
await logFile.copy(extpath);
}
}