|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:io'; |
| 3 | + |
| 4 | +import 'package:cli_tools/cli_tools.dart' as cli; |
| 5 | +import 'package:serverpod_logging/serverpod_logging.dart'; |
| 6 | + |
| 7 | +import 'std_out_log_writer.dart'; |
| 8 | + |
| 9 | +/// A [cli.Logger] that delegates to a [Log]. |
| 10 | +/// |
| 11 | +/// This bridges the [cli.Logger] interface with the [LogWriter] architecture, |
| 12 | +/// allowing multi-backend logging (terminal, TUI, file, database, etc.) via |
| 13 | +/// [LogWriter] and [MultiLogWriter]. |
| 14 | +/// |
| 15 | +/// [cli.LogType] is preserved by stashing it in [LogEntry.metadata] under |
| 16 | +/// [logTypeKey], so writers like [StdOutLogWriter] can format accordingly. |
| 17 | +class ServerpodCliLogger extends cli.Logger { |
| 18 | + final Log _log; |
| 19 | + final LogWriter _writer; |
| 20 | + |
| 21 | + ServerpodCliLogger(LogWriter writer, {LogLevel logLevel = LogLevel.info}) |
| 22 | + : _writer = writer, |
| 23 | + _log = Log(writer, logLevel: logLevel), |
| 24 | + super(toCliLogLevel(logLevel)); |
| 25 | + |
| 26 | + /// Releases any resources held by the underlying writer. |
| 27 | + Future<void> close() async { |
| 28 | + await _log.close(); |
| 29 | + await _writer.close(); |
| 30 | + } |
| 31 | + |
| 32 | + @override |
| 33 | + set logLevel(cli.LogLevel level) { |
| 34 | + super.logLevel = level; |
| 35 | + if (level == cli.LogLevel.nothing) return; |
| 36 | + _log.logLevel = _mapLogLevel(level); |
| 37 | + } |
| 38 | + |
| 39 | + @override |
| 40 | + int? get wrapTextColumn => stdout.hasTerminal ? stdout.terminalColumns : null; |
| 41 | + |
| 42 | + @override |
| 43 | + void debug( |
| 44 | + String message, { |
| 45 | + bool newParagraph = false, |
| 46 | + cli.LogType type = cli.TextLogType.normal, |
| 47 | + }) => |
| 48 | + _call(LogLevel.debug, message, type: type); |
| 49 | + |
| 50 | + @override |
| 51 | + void info( |
| 52 | + String message, { |
| 53 | + bool newParagraph = false, |
| 54 | + cli.LogType type = cli.TextLogType.normal, |
| 55 | + }) => |
| 56 | + _call(LogLevel.info, message, type: type); |
| 57 | + |
| 58 | + @override |
| 59 | + void warning( |
| 60 | + String message, { |
| 61 | + bool newParagraph = false, |
| 62 | + cli.LogType type = cli.TextLogType.normal, |
| 63 | + }) => |
| 64 | + _call(LogLevel.warning, message, type: type); |
| 65 | + |
| 66 | + @override |
| 67 | + void error( |
| 68 | + String message, { |
| 69 | + bool newParagraph = false, |
| 70 | + StackTrace? stackTrace, |
| 71 | + cli.LogType type = cli.TextLogType.normal, |
| 72 | + }) => |
| 73 | + _call(LogLevel.error, message, stackTrace: stackTrace, type: type); |
| 74 | + |
| 75 | + @override |
| 76 | + void log( |
| 77 | + String message, |
| 78 | + cli.LogLevel level, { |
| 79 | + bool newParagraph = false, |
| 80 | + cli.LogType type = cli.TextLogType.normal, |
| 81 | + }) { |
| 82 | + // `nothing` is the "off" sentinel, not a message level - suppress rather |
| 83 | + // than map it (mapping would throw on this otherwise-valid enum value). |
| 84 | + if (level == cli.LogLevel.nothing) return; |
| 85 | + _call(_mapLogLevel(level), message, type: type); |
| 86 | + } |
| 87 | + |
| 88 | + @override |
| 89 | + void write( |
| 90 | + String message, |
| 91 | + cli.LogLevel logLevel, { |
| 92 | + bool newParagraph = false, |
| 93 | + bool newLine = true, |
| 94 | + }) { |
| 95 | + if (logLevel == cli.LogLevel.nothing) return; |
| 96 | + _call(_mapLogLevel(logLevel), message); |
| 97 | + } |
| 98 | + |
| 99 | + @override |
| 100 | + Future<bool> progress( |
| 101 | + String message, |
| 102 | + Future<bool> Function() runner, { |
| 103 | + bool newParagraph = false, |
| 104 | + String? successMessage, |
| 105 | + }) { |
| 106 | + if (_silent) return runner(); |
| 107 | + return _log.progress(message, runner); |
| 108 | + } |
| 109 | + |
| 110 | + /// Renders [stream] as a parent operation with one nested sub-operation per |
| 111 | + /// event. Each sub-operation stays open until the next event arrives (or the |
| 112 | + /// stream ends), so its elapsed time is the gap between events. The parent |
| 113 | + /// closes when the stream completes. |
| 114 | + /// |
| 115 | + /// Returns the last event. Rethrows a stream error (failing the open |
| 116 | + /// sub-operation and the parent). Throws [StateError] if the stream is empty. |
| 117 | + @override |
| 118 | + Future<T> progressStream<T>( |
| 119 | + String initialMessage, |
| 120 | + Stream<T> stream, { |
| 121 | + String Function(T)? toMessage, |
| 122 | + bool Function(T)? isSuccess, |
| 123 | + bool newParagraph = false, |
| 124 | + }) async { |
| 125 | + if (_silent) return _drain(stream); |
| 126 | + |
| 127 | + T? last; |
| 128 | + return _log.progress<T>( |
| 129 | + initialMessage, |
| 130 | + () async { |
| 131 | + final events = StreamIterator(stream); |
| 132 | + try { |
| 133 | + if (!await events.moveNext()) { |
| 134 | + throw StateError(_noEventsMessage); |
| 135 | + } |
| 136 | + while (true) { |
| 137 | + final event = events.current; |
| 138 | + last = event; |
| 139 | + final label = toMessage?.call(event) ?? event.toString(); |
| 140 | + // The sub-operation's lifetime is the wait for the next event. |
| 141 | + var hasMore = false; |
| 142 | + await _log.progress<bool>(label, () async { |
| 143 | + hasMore = await events.moveNext(); |
| 144 | + return true; |
| 145 | + }); |
| 146 | + if (!hasMore) break; |
| 147 | + } |
| 148 | + } finally { |
| 149 | + await events.cancel(); |
| 150 | + } |
| 151 | + return last as T; |
| 152 | + }, |
| 153 | + // Let Log own the success verdict |
| 154 | + isSuccess: isSuccess ?? (_) => true, |
| 155 | + ); |
| 156 | + } |
| 157 | + |
| 158 | + @override |
| 159 | + Future<void> flush() => _log.flush(); |
| 160 | + |
| 161 | + /// Consumes [stream] without rendering, honoring the same return/throw |
| 162 | + /// contract as [progressStream] (used when output is silenced). |
| 163 | + static Future<T> _drain<T>(Stream<T> stream) async { |
| 164 | + T? last; |
| 165 | + var hasEvent = false; |
| 166 | + await for (final event in stream) { |
| 167 | + hasEvent = true; |
| 168 | + last = event; |
| 169 | + } |
| 170 | + if (!hasEvent) throw StateError(_noEventsMessage); |
| 171 | + return last as T; |
| 172 | + } |
| 173 | + |
| 174 | + static const _noEventsMessage = 'No events in stream'; |
| 175 | + |
| 176 | + void _call( |
| 177 | + LogLevel level, |
| 178 | + String message, { |
| 179 | + StackTrace? stackTrace, |
| 180 | + cli.LogType? type, |
| 181 | + }) { |
| 182 | + if (_silent) return; |
| 183 | + _log( |
| 184 | + level, |
| 185 | + () => LogEntry( |
| 186 | + time: DateTime.now(), |
| 187 | + level: level, |
| 188 | + message: message, |
| 189 | + scope: _log.currentScope, |
| 190 | + stackTrace: stackTrace, |
| 191 | + metadata: type != null ? {logTypeKey: type} : null, |
| 192 | + ), |
| 193 | + ); |
| 194 | + } |
| 195 | + |
| 196 | + // serverpod_logging LogLevel has no "nothing" sentinel - its lowest-passing |
| 197 | + // level is fatal, and the filter check is strict-`<` so fatal still leaks |
| 198 | + // through. Progress / scope events bypass logLevel entirely. So when the |
| 199 | + // caller sets cli.LogLevel.nothing we gate at the bridge instead of mapping |
| 200 | + // through to _log.logLevel. |
| 201 | + bool get _silent => super.logLevel == cli.LogLevel.nothing; |
| 202 | + |
| 203 | + static LogLevel _mapLogLevel(cli.LogLevel level) => switch (level) { |
| 204 | + cli.LogLevel.debug => LogLevel.debug, |
| 205 | + cli.LogLevel.info => LogLevel.info, |
| 206 | + cli.LogLevel.warning => LogLevel.warning, |
| 207 | + cli.LogLevel.error => LogLevel.error, |
| 208 | + // The setter early-returns before reaching here, so this branch only |
| 209 | + // fires from log() / write() being called with nothing as a message |
| 210 | + // level - which is a programmer error worth surfacing. |
| 211 | + cli.LogLevel.nothing => throw ArgumentError.value( |
| 212 | + level, |
| 213 | + 'level', |
| 214 | + 'cli.LogLevel.nothing is a filter sentinel; it cannot be used as a ' |
| 215 | + 'message level', |
| 216 | + ), |
| 217 | + }; |
| 218 | +} |
0 commit comments