Line data Source code
1 : /* MIT License
2 : *
3 : * Copyright (C) 2019, 2020, 2021 Famedly GmbH
4 : *
5 : * Permission is hereby granted, free of charge, to any person obtaining a copy
6 : * of this software and associated documentation files (the "Software"), to deal
7 : * in the Software without restriction, including without limitation the rights
8 : * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 : * copies of the Software, and to permit persons to whom the Software is
10 : * furnished to do so, subject to the following conditions:
11 : *
12 : * The above copyright notice and this permission notice shall be included in all
13 : * copies or substantial portions of the Software.
14 : *
15 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 : * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 : * SOFTWARE.
22 : */
23 :
24 : import 'package:matrix/matrix_api_lite/utils/print_logs_native.dart'
25 : if (dart.library.html) 'print_logs_web.dart';
26 :
27 : enum Level {
28 : wtf,
29 : error,
30 : warning,
31 : info,
32 : debug,
33 : verbose,
34 : }
35 :
36 : class Logs {
37 192 : static final Logs _singleton = Logs._internal();
38 :
39 : /// Override this function if you want to convert a stacktrace for some reason
40 : /// for example to apply a source map in the browser.
41 114 : static StackTrace? Function(StackTrace?) stackTraceConverter = (s) => s;
42 :
43 64 : factory Logs() {
44 64 : return _singleton;
45 : }
46 :
47 : Level level = Level.info;
48 : bool nativeColors = true;
49 :
50 : final List<LogEvent> outputEvents = [];
51 :
52 64 : Logs._internal();
53 :
54 38 : void addLogEvent(LogEvent logEvent) {
55 76 : outputEvents.add(logEvent);
56 190 : if (logEvent.level.index <= level.index) {
57 37 : logEvent.printOut();
58 : }
59 : }
60 :
61 1 : void wtf(String title, [Object? exception, StackTrace? stackTrace]) =>
62 1 : addLogEvent(
63 1 : LogEvent(
64 : title,
65 : exception: exception,
66 2 : stackTrace: stackTraceConverter(stackTrace),
67 : level: Level.wtf,
68 : ),
69 : );
70 :
71 18 : void e(String title, [Object? exception, StackTrace? stackTrace]) =>
72 18 : addLogEvent(
73 18 : LogEvent(
74 : title,
75 : exception: exception,
76 36 : stackTrace: stackTraceConverter(stackTrace),
77 : level: Level.error,
78 : ),
79 : );
80 :
81 34 : void w(String title, [Object? exception, StackTrace? stackTrace]) =>
82 34 : addLogEvent(
83 34 : LogEvent(
84 : title,
85 : exception: exception,
86 68 : stackTrace: stackTraceConverter(stackTrace),
87 : level: Level.warning,
88 : ),
89 : );
90 :
91 34 : void i(String title, [Object? exception, StackTrace? stackTrace]) =>
92 34 : addLogEvent(
93 34 : LogEvent(
94 : title,
95 : exception: exception,
96 68 : stackTrace: stackTraceConverter(stackTrace),
97 : level: Level.info,
98 : ),
99 : );
100 :
101 31 : void d(String title, [Object? exception, StackTrace? stackTrace]) =>
102 31 : addLogEvent(
103 31 : LogEvent(
104 : title,
105 : exception: exception,
106 62 : stackTrace: stackTraceConverter(stackTrace),
107 : level: Level.debug,
108 : ),
109 : );
110 :
111 33 : void v(String title, [Object? exception, StackTrace? stackTrace]) =>
112 33 : addLogEvent(
113 33 : LogEvent(
114 : title,
115 : exception: exception,
116 66 : stackTrace: stackTraceConverter(stackTrace),
117 : level: Level.verbose,
118 : ),
119 : );
120 : }
121 :
122 : // ignore: avoid_print
123 : class LogEvent {
124 : final String title;
125 : final Object? exception;
126 : final StackTrace? stackTrace;
127 : final Level level;
128 :
129 38 : LogEvent(
130 : this.title, {
131 : this.exception,
132 : this.stackTrace,
133 : this.level = Level.debug,
134 : });
135 : }
|