Repository navigation
Expand file tree
/
Copy pathsettings.dart
More file actions
212 lines (186 loc) · 6.56 KB
/
Copy pathsettings.dart
File metadata and controls
212 lines (186 loc) · 6.56 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import 'package:talker/talker.dart';
final _defaultTitles = {
/// Base logs section
TalkerKey.critical: 'critical',
TalkerKey.warning: 'warning',
TalkerKey.verbose: 'verbose',
TalkerKey.info: 'info',
TalkerKey.debug: 'debug',
TalkerKey.error: 'error',
TalkerKey.exception: 'exception',
/// Http section
TalkerKey.httpError: 'http-error',
TalkerKey.httpRequest: 'http-request',
TalkerKey.httpResponse: 'http-response',
/// Bloc section
TalkerKey.blocEvent: 'bloc-event',
TalkerKey.blocTransition: 'bloc-transition',
TalkerKey.blocCreate: 'bloc-create',
TalkerKey.blocClose: 'bloc-close',
/// Riverpod section
TalkerKey.riverpodAdd: 'riverpod-add',
TalkerKey.riverpodUpdate: 'riverpod-update',
TalkerKey.riverpodDispose: 'riverpod-dispose',
TalkerKey.riverpodFail: 'riverpod-fail',
/// Flutter section
TalkerKey.route: 'route',
};
final _defaultColors = {
/// Base logs section
TalkerKey.critical: AnsiPen()..red(),
TalkerKey.warning: AnsiPen()..yellow(),
TalkerKey.verbose: AnsiPen()..gray(),
TalkerKey.info: AnsiPen()..blue(),
TalkerKey.debug: AnsiPen()..gray(),
TalkerKey.error: AnsiPen()..red(),
TalkerKey.exception: AnsiPen()..red(),
/// Http section
TalkerKey.httpError: AnsiPen()..red(),
TalkerKey.httpRequest: AnsiPen()..xterm(219),
TalkerKey.httpResponse: AnsiPen()..xterm(46),
/// Bloc section
TalkerKey.blocEvent: AnsiPen()..xterm(51),
TalkerKey.blocTransition: AnsiPen()..xterm(49),
TalkerKey.blocCreate: AnsiPen()..xterm(35),
TalkerKey.blocClose: AnsiPen()..xterm(198),
/// Riverpod section
TalkerKey.riverpodAdd: AnsiPen()..xterm(51),
TalkerKey.riverpodUpdate: AnsiPen()..xterm(49),
TalkerKey.riverpodDispose: AnsiPen()..xterm(198),
TalkerKey.riverpodFail: AnsiPen()..red(),
/// Flutter section
TalkerKey.route: AnsiPen()..xterm(135),
};
final _fallbackPen = AnsiPen()..gray();
/// {@template talker_settings}
/// This class used for setup [Talker] configuration
/// {@endtemplate}
class TalkerSettings {
TalkerSettings({
this.enabled = true,
bool useHistory = true,
bool useConsoleLogs = true,
int maxHistoryItems = 1000,
Map<String, String>? titles,
Map<String, AnsiPen>? colors,
TimeFormat timeFormat = TimeFormat.timeAndSeconds,
}) : _useHistory = useHistory,
_useConsoleLogs = useConsoleLogs,
_maxHistoryItems = maxHistoryItems,
_timeFormat = timeFormat {
if (colors != null) {
_defaultColors.addAll(colors);
}
if (titles != null) {
_defaultTitles.addAll(titles);
}
this.colors.addAll(_defaultColors);
this.titles.addAll(_defaultTitles);
}
// _writeToFile = writeToFile;
/// By default talker write all Errors / Exceptions and logs in history list
/// (base dart [List] field in core)
/// If [true] - writing in history
/// If [false] - not writing
bool get useHistory => _useHistory && enabled;
final bool _useHistory;
/// By default talker print all Errors / Exceptions and logs in console.
/// If [true] - printing in console [false] - not printing.
bool get useConsoleLogs => _useConsoleLogs && enabled;
final bool _useConsoleLogs;
/// Max records count in history list
int get maxHistoryItems => _maxHistoryItems;
final int _maxHistoryItems;
/// The time format of the logs [TimeFormat]
TimeFormat get timeFormat => _timeFormat;
final TimeFormat _timeFormat;
/// Use writing talker records in file
// bool get writeToFile => _writeToFile && enabled;
// final bool _writeToFile;
/// The main rule that is responsible
/// for the operation of the package
/// All log and handle error / exception methods are working when [true] and not working when [false]
bool enabled;
/// Custom Logger Titles.
///
/// The `titles` field is intended for storing custom titles for the logger, associated with various log types.
/// Each title is associated with a specific log type represented as an enum called `TalkerTitle`. This allows you to
/// provide informative and unique titles for each log type, making logging more readable and informative.
///
/// Example usage:
///
/// ```dart
/// final customTitles = {
/// TalkerKey.info: "Information",
/// TalkerKey.error: "Error",
/// TalkerKey.warning: "Warning",
/// };
///
/// final logger = Talker(
/// settings: TalkerSettings(
/// titles: customTitles,
/// )
/// );
/// ```
final Map<String, String> titles = _defaultTitles;
/// Custom Logger Colors.
///
/// The `colors` field is designed for setting custom text colors for the logger, associated with specific log keys.
/// Each color is associated with a specific log key represented as an enum called `TalkerKey`. This allows you to
/// define custom text colors for each log key, enhancing the visual representation of logs in the console.
///
/// Example usage:
///
/// ```dart
/// final customColors = {
/// TalkerKey.info: AnsiPen()..white(bold: true),
/// TalkerKey.error: AnsiPen()..red(bold: true),
/// TalkerKey.warning: AnsiPen()..yellow(bold: true),
/// };
///
/// final logger = Talker(
/// settings: TalkerSettings(
/// colors: customColors,
/// )
/// );
/// ```
///
/// By using the `colors` field, you can customize the text colors for specific log keys in the console.
final Map<String, AnsiPen> colors = _defaultColors;
/// A list of [TalkerKey]s that can be included in filter by [TalkerScreen] settings
/// This allows you to register custom keys that can be used for filtering logs in the Talker UI.
final List<String> registeredKeys = [];
/// Registers multiple custom keys for filtering logs in the Talker UI
void registerKeys(List<String> keys) {
for (final key in keys) {
_registerKey(key);
}
}
/// Registers a custom key for filtering logs in the Talker UI
void _registerKey(String key) {
if (!registeredKeys.contains(key)) {
registeredKeys.add(key);
}
}
String getTitleByKey(String key) => titles[key] ?? key;
AnsiPen getPenByKey(String key, {AnsiPen? fallbackPen}) {
return colors[key] ?? fallbackPen ?? _fallbackPen;
}
TalkerSettings copyWith({
bool? enabled,
bool? useHistory,
bool? useConsoleLogs,
int? maxHistoryItems,
Map<String, String>? titles,
Map<String, AnsiPen>? colors,
}) {
return TalkerSettings(
useHistory: useHistory ?? _useHistory,
useConsoleLogs: useConsoleLogs ?? _useConsoleLogs,
maxHistoryItems: maxHistoryItems ?? _maxHistoryItems,
enabled: enabled ?? this.enabled,
titles: titles ?? this.titles,
colors: colors ?? this.colors,
);
}
}