LCOV - code coverage report
Current view: top level - lib/matrix_api_lite/utils - logs.dart (source / functions) Hit Total Coverage
Test: merged.info Lines: 34 34 100.0 %
Date: 2024-09-30 15:57:20 Functions: 0 0 -

          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         111 :   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          37 :   void addLogEvent(LogEvent logEvent) {
      55          74 :     outputEvents.add(logEvent);
      56         185 :     if (logEvent.level.index <= level.index) {
      57          36 :       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          17 :   void e(String title, [Object? exception, StackTrace? stackTrace]) =>
      72          17 :       addLogEvent(
      73          17 :         LogEvent(
      74             :           title,
      75             :           exception: exception,
      76          34 :           stackTrace: stackTraceConverter(stackTrace),
      77             :           level: Level.error,
      78             :         ),
      79             :       );
      80             : 
      81          21 :   void w(String title, [Object? exception, StackTrace? stackTrace]) =>
      82          21 :       addLogEvent(
      83          21 :         LogEvent(
      84             :           title,
      85             :           exception: exception,
      86          42 :           stackTrace: stackTraceConverter(stackTrace),
      87             :           level: Level.warning,
      88             :         ),
      89             :       );
      90             : 
      91          33 :   void i(String title, [Object? exception, StackTrace? stackTrace]) =>
      92          33 :       addLogEvent(
      93          33 :         LogEvent(
      94             :           title,
      95             :           exception: exception,
      96          66 :           stackTrace: stackTraceConverter(stackTrace),
      97             :           level: Level.info,
      98             :         ),
      99             :       );
     100             : 
     101          30 :   void d(String title, [Object? exception, StackTrace? stackTrace]) =>
     102          30 :       addLogEvent(
     103          30 :         LogEvent(
     104             :           title,
     105             :           exception: exception,
     106          60 :           stackTrace: stackTraceConverter(stackTrace),
     107             :           level: Level.debug,
     108             :         ),
     109             :       );
     110             : 
     111          32 :   void v(String title, [Object? exception, StackTrace? stackTrace]) =>
     112          32 :       addLogEvent(
     113          32 :         LogEvent(
     114             :           title,
     115             :           exception: exception,
     116          64 :           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          37 :   LogEvent(
     130             :     this.title, {
     131             :     this.exception,
     132             :     this.stackTrace,
     133             :     this.level = Level.debug,
     134             :   });
     135             : }

Generated by: LCOV version 1.14