|
| 1 | +import 'package:logger/logger.dart'; |
| 2 | + |
| 3 | +import 'platform_standard.dart' |
| 4 | + if (dart.library.html) 'platform_web.dart' |
| 5 | + if (dart.library.io) 'platform_vm.dart'; |
| 6 | + |
| 7 | +typedef ConsolePrinter = void Function(String arg); |
| 8 | + |
| 9 | +/// Runtime platform capabilities |
| 10 | +class LogPlatform { |
| 11 | + /// Returns [true] if running in Web platform (Browser). |
| 12 | + static bool get isWeb => isPlatformWebImpl(); |
| 13 | + |
| 14 | + /// Returns [true] if running in VM platform. |
| 15 | + static bool get isVM => isPlatformVMImpl(); |
| 16 | + |
| 17 | + /// Returns [true] if the platform console supports colors. |
| 18 | + static bool get consoleSupportsColors => |
| 19 | + getPlatformConsoleAcceptsColorsImpl(); |
| 20 | + |
| 21 | + /// Returns console columns size. |
| 22 | + static int get consoleColumns => getPlatformConsoleColumnsImpl(); |
| 23 | + |
| 24 | + /// Returns [true] if the console supports emoji. |
| 25 | + static bool get consoleSupportsEmoji => isPlatformWebImpl(); |
| 26 | + |
| 27 | + static bool _enableSTDERR = false; |
| 28 | + |
| 29 | + /// If set to [true], allows write of [Level.error] and |
| 30 | + /// [Level.wtf] to `stderr` (if present in the runtime platform). |
| 31 | + /// |
| 32 | + /// Default to [false], to be backward compatible with previous versions. |
| 33 | + /// of this package. |
| 34 | + static bool get enableSTDERR => _enableSTDERR; |
| 35 | + |
| 36 | + static set enableSTDERR(bool value) { |
| 37 | + _enableSTDERR = value ?? false; |
| 38 | + } |
| 39 | + |
| 40 | + /// Returns a [ConsolePrinter] that is muted (won't print anything). |
| 41 | + static ConsolePrinter get consoleMutedPrinter => (arg) {}; |
| 42 | + |
| 43 | + /// Returns a [ConsolePrinter] for log events. |
| 44 | + static ConsolePrinter get consoleLogPrinter => getPlatformConsoleLogPrinter(); |
| 45 | + |
| 46 | + /// Returns a [ConsolePrinter] for info events. |
| 47 | + static ConsolePrinter get consoleInfoPrinter => |
| 48 | + getPlatformConsoleInfoPrinter(); |
| 49 | + |
| 50 | + /// Returns a [ConsolePrinter] for warn events. |
| 51 | + static ConsolePrinter get consoleWarnPrinter => |
| 52 | + getPlatformConsoleWarnPrinter(); |
| 53 | + |
| 54 | + /// Returns a [ConsolePrinter] for error events. |
| 55 | + static ConsolePrinter get consoleErrorPrinter => |
| 56 | + getPlatformConsoleErrorPrinter(); |
| 57 | + |
| 58 | + /// Returns the [ConsolePrinter] for the [level]. |
| 59 | + static ConsolePrinter getConsolePrinter(Level level) { |
| 60 | + switch (level) { |
| 61 | + case Level.verbose: |
| 62 | + case Level.debug: |
| 63 | + return consoleLogPrinter; |
| 64 | + case Level.info: |
| 65 | + return consoleInfoPrinter; |
| 66 | + case Level.warning: |
| 67 | + return consoleWarnPrinter; |
| 68 | + case Level.error: |
| 69 | + case Level.wtf: |
| 70 | + return consoleErrorPrinter; |
| 71 | + case Level.nothing: |
| 72 | + return consoleMutedPrinter; |
| 73 | + default: |
| 74 | + return consoleLogPrinter; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /// In some browsers, depending of the [ConsolePrinter] for some [level], |
| 79 | + /// the 1st line printed to the console is not vertically |
| 80 | + /// aligned (head indented) with the other lines. |
| 81 | + static bool isConsolePrinterHeadIndented(Level level) => |
| 82 | + isPlatformConsolePrinterHeadIndented(level); |
| 83 | + |
| 84 | + /// Join lines, to avoid multiple calls to printer. |
| 85 | + /// |
| 86 | + /// In Web platform (browser), each call to printer, in case of `warnings` or |
| 87 | + /// `erros`, will associate a stack trace tree (visible and expandable |
| 88 | + /// in Browser console). |
| 89 | + /// |
| 90 | + /// In VM platform, multiple calls to printer is less efficient, since will |
| 91 | + /// generate multiple IO calls. |
| 92 | + static String joinLines(List<String> lines, Level level) { |
| 93 | + if (lines == null) return ''; |
| 94 | + final linesLength = lines.length; |
| 95 | + if (linesLength == 0) return ''; |
| 96 | + |
| 97 | + // Tries to avoid new [String] allocations: |
| 98 | + if (linesLength == 1) { |
| 99 | + return lines[0]; |
| 100 | + } |
| 101 | + // Ensure that head line is vertical aligned with other lines |
| 102 | + // in the current console platform (if not, add a blank head line): |
| 103 | + else if (LogPlatform.isConsolePrinterHeadIndented(level)) { |
| 104 | + final buffer = StringBuffer(); |
| 105 | + for (final line in lines) { |
| 106 | + buffer.write('\n'); |
| 107 | + buffer.write(line); |
| 108 | + } |
| 109 | + return buffer.toString(); |
| 110 | + } else { |
| 111 | + return lines.join('\n'); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + static final int DEFAULT_METHOD_COUNT = isWeb ? 2 : 3; |
| 116 | + |
| 117 | + static final int DEFAULT_ERROR_METHOD_COUNT = isWeb ? 8 : 12; |
| 118 | + |
| 119 | + static final int DEFAULT_LINE_LENGTH = consoleColumns; |
| 120 | + |
| 121 | + static final bool DEFAULT_USE_COLORS = consoleSupportsColors; |
| 122 | + |
| 123 | + static final bool DEFAULT_USE_EMOJI = consoleSupportsEmoji; |
| 124 | +} |
0 commit comments