Skip to content

Commit

Permalink
Move to separate method (and library)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos committed Jul 1, 2024
1 parent 8750c28 commit 9220575
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 45 deletions.
47 changes: 2 additions & 45 deletions bin/pana.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:io/ansi.dart';
import 'package:io/io.dart';
import 'package:logging/logging.dart' as log;
import 'package:pana/pana.dart';
import 'package:pana/src/logging.dart';
import 'package:path/path.dart' as p;

const defaultHostedUrl = 'https://pub.dev';
Expand Down Expand Up @@ -132,22 +133,7 @@ Future<void> main(List<String> args) async {
}

log.Logger.root.level = log.Level.ALL;

if (isJson) {
log.Logger.root.onRecord.listen((log) {
final map = {
if (log.loggerName.isNotEmpty) 'logName': log.loggerName,
'level': log.level.name,
'message': log.message,
if (log.error != null) 'error': log.error.toString(),
if (log.stackTrace != null) 'stackTrace': log.stackTrace.toString(),
};

stderr.writeln(json.encode(map));
});
} else {
log.Logger.root.onRecord.listen(_logWriter);
}
initializePanaLogging(isJson: isJson);

// Docker is WEIRD
// The SIGTERM signal sent to `docker run...` DOES propagate a signal to the
Expand Down Expand Up @@ -275,35 +261,6 @@ Future<void> main(List<String> args) async {
await subscription.cancel();
}

void _logWriter(log.LogRecord record) {
var wroteHeader = false;

final output = <String>[];
final prefix = '${record.time} ${record.level.name}:';
final emptyPrefix = ' ' * prefix.length;
void printLinesWithPrefix(String lines) {
for (final line in lines.split('\n')) {
final currentPrefix = wroteHeader ? emptyPrefix : prefix;
output.add('$currentPrefix $line');
wroteHeader = true;
}
}

printLinesWithPrefix(record.message);
final e = record.error;
if (e != null) {
printLinesWithPrefix(e.toString());
final st = record.stackTrace;
if (st != null) {
printLinesWithPrefix(st.toString());
}
}

overrideAnsiOutput(stderr.supportsAnsiEscapes, () {
stderr.writeln(darkGray.wrap(output.join('\n')));
});
}

/// A merged stream of all signals that tell the test runner to shut down
/// gracefully.
///
Expand Down
50 changes: 50 additions & 0 deletions lib/src/logging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:io/ansi.dart';
import 'package:logging/logging.dart';

final Logger _log = Logger('pana');
Expand All @@ -16,3 +19,50 @@ Future<R> withLogger<R>(Future<R> Function() fn, {Logger? logger}) => runZoned(
);

Logger get log => (Zone.current[_key] as Logger?) ?? _log;

void initializePanaLogging({bool isJson = false}) {
if (isJson) {
Logger.root.onRecord.listen((log) {
final map = {
if (log.loggerName.isNotEmpty) 'logName': log.loggerName,
'level': log.level.name,
'message': log.message,
if (log.error != null) 'error': log.error.toString(),
if (log.stackTrace != null) 'stackTrace': log.stackTrace.toString(),
};

stderr.writeln(json.encode(map));
});
} else {
Logger.root.onRecord.listen(_logWriter);
}
}

void _logWriter(LogRecord record) {
var wroteHeader = false;

final output = <String>[];
final prefix = '${record.time} ${record.level.name}:';
final emptyPrefix = ' ' * prefix.length;
void printLinesWithPrefix(String lines) {
for (final line in lines.split('\n')) {
final currentPrefix = wroteHeader ? emptyPrefix : prefix;
output.add('$currentPrefix $line');
wroteHeader = true;
}
}

printLinesWithPrefix(record.message);
final e = record.error;
if (e != null) {
printLinesWithPrefix(e.toString());
final st = record.stackTrace;
if (st != null) {
printLinesWithPrefix(st.toString());
}
}

overrideAnsiOutput(stderr.supportsAnsiEscapes, () {
stderr.writeln(darkGray.wrap(output.join('\n')));
});
}

0 comments on commit 9220575

Please sign in to comment.