Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(502)

Unified Diff: tools/gardening/lib/src/logger.dart

Issue 3005443002: Additional tools for gardening. (Closed)
Patch Set: Added changes from johnniwinther Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/gardening/lib/src/cache_new.dart ('k') | tools/gardening/lib/src/luci_api.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/gardening/lib/src/logger.dart
diff --git a/tools/gardening/lib/src/logger.dart b/tools/gardening/lib/src/logger.dart
new file mode 100644
index 0000000000000000000000000000000000000000..3701f2f2c82836f23167e7c1b5ff0a8cb9a71ed0
--- /dev/null
+++ b/tools/gardening/lib/src/logger.dart
@@ -0,0 +1,117 @@
+// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'dart:io';
+import 'package:path/path.dart' as path;
+
+enum Level { debug, info, warning, error }
+
+abstract class Logger {
+ Level level;
+
+ Logger(this.level);
+
+ void info(String msg, [Error, StackTrace]);
+ void warning(String msg, [Error, StackTrace]);
+ void error(String msg, [Error, StackTrace]);
+ void debug(String msg, [Error, StackTrace]);
+
+ void destroy();
+}
+
+String _formatErrorMessage(String msg, Exception error,
+ [StackTrace stackTrace]) {
+ if (error == null) return msg;
+ if (stackTrace == null) return msg + ": $error";
+ return msg + ": $error\n$stackTrace";
+}
+
+class StdOutLogger extends Logger {
+ StdOutLogger(Level level) : super(level);
+
+ @override
+ void info(String msg, [error, stackTrace]) {
+ msg = _formatErrorMessage(msg, error, stackTrace);
+ _print(Level.info, "$_datetime Info: $msg");
+ }
+
+ @override
+ void warning(String msg, [error, stackTrace]) {
+ msg = _formatErrorMessage(msg, error, stackTrace);
+ _print(Level.warning, "$_datetime Warning: $msg");
+ }
+
+ @override
+ void error(String msg, [error, stackTrace]) {
+ msg = _formatErrorMessage(msg, error, stackTrace);
+ _print(Level.error, "$_datetime Error: $msg");
+ }
+
+ @override
+ void debug(String msg, [error, stackTrace]) {
+ msg = _formatErrorMessage(msg, error, stackTrace);
+ _print(Level.debug, "$_datetime Debug: $msg");
+ }
+
+ void _print(Level logLevel, String msg) {
+ if (logLevel.index >= level.index) {
+ print(msg);
+ }
+ }
+
+ @override
+ void destroy() {
+ // nothing to do
+ }
+
+ String get _datetime => "${new DateTime.now()}";
+}
+
+class FileLogger extends Logger {
+ IOSink _sink;
+
+ FileLogger(String fileName, Level level, {bool append: false})
+ : super(level) {
+ var mode = append ? FileMode.APPEND : FileMode.WRITE;
+ _sink = new File(path.absolute(fileName)).openWrite(mode: mode);
+ }
+
+ @override
+ void destroy() {
+ if (_sink != null) {
+ _sink.close();
+ _sink = null;
+ }
+ }
+
+ @override
+ void info(String msg, [error, stackTrace]) {
+ msg = _formatErrorMessage(msg, error, stackTrace);
+ _print(Level.info, "$_datetime Info: $msg");
+ }
+
+ @override
+ void warning(String msg, [error, stackTrace]) {
+ msg = _formatErrorMessage(msg, error, stackTrace);
+ _print(Level.warning, "$_datetime Warning: $msg");
+ }
+
+ @override
+ void error(String msg, [error, stackTrace]) {
+ msg = _formatErrorMessage(msg, error, stackTrace);
+ _print(Level.error, "$_datetime Error: $msg");
+ }
+
+ @override
+ void debug(String msg, [error, stackTrace]) {
+ msg = _formatErrorMessage(msg, error, stackTrace);
+ _print(Level.debug, "$_datetime Debug: $msg");
+ }
+
+ void _print(Level logLevel, String msg) {
+ if (logLevel.index >= level.index && _sink != null) _sink.writeln(msg);
+ }
+
+ static String get _datetime => "${new DateTime.now()}";
+}
« no previous file with comments | « tools/gardening/lib/src/cache_new.dart ('k') | tools/gardening/lib/src/luci_api.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698