mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-09-11 13:33:33 +08:00
Logger: null safety migration
This commit is contained in:
@ -1,5 +1,3 @@
|
|||||||
// @dart=2.9
|
|
||||||
|
|
||||||
import 'dart:core';
|
import 'dart:core';
|
||||||
|
|
||||||
import 'package:intl/intl.dart';
|
import 'package:intl/intl.dart';
|
||||||
@ -27,7 +25,7 @@ String toZettleDateTime(DateTime dt) {
|
|||||||
return _zettleDateFormat.format(dt);
|
return _zettleDateFormat.format(dt);
|
||||||
}
|
}
|
||||||
|
|
||||||
String toIso8601WithTimezone(DateTime dt, [Duration offset]) {
|
String toIso8601WithTimezone(DateTime dt, [Duration? offset]) {
|
||||||
var result = _iso8601DateFormat.format(dt);
|
var result = _iso8601DateFormat.format(dt);
|
||||||
|
|
||||||
offset = offset ?? dt.timeZoneOffset;
|
offset = offset ?? dt.timeZoneOffset;
|
||||||
@ -58,10 +56,8 @@ String toIso8601WithTimezone(DateTime dt, [Duration offset]) {
|
|||||||
return result + sign + hourStr + ':' + minutesStr;
|
return result + sign + hourStr + ':' + minutesStr;
|
||||||
}
|
}
|
||||||
|
|
||||||
DateTime parseDateTime(String str) {
|
DateTime? parseDateTime(String str) {
|
||||||
if (str == null) return null;
|
DateTime? dt;
|
||||||
|
|
||||||
DateTime dt;
|
|
||||||
try {
|
try {
|
||||||
dt = DateTime.parse(str).toLocal();
|
dt = DateTime.parse(str).toLocal();
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
|
@ -1,20 +1,17 @@
|
|||||||
// @dart=2.9
|
|
||||||
|
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:flutter/foundation.dart' as foundation;
|
import 'package:flutter/foundation.dart' as foundation;
|
||||||
|
|
||||||
import 'package:fimber/fimber.dart';
|
import 'package:fimber/fimber.dart';
|
||||||
import 'package:meta/meta.dart';
|
|
||||||
import 'package:path/path.dart' as p;
|
import 'package:path/path.dart' as p;
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'package:stack_trace/stack_trace.dart';
|
import 'package:stack_trace/stack_trace.dart';
|
||||||
import 'package:time/time.dart';
|
import 'package:time/time.dart';
|
||||||
|
|
||||||
class Log {
|
class Log {
|
||||||
static String logFolderPath;
|
static late String logFolderPath;
|
||||||
static RandomAccessFile logFile;
|
static RandomAccessFile? logFile;
|
||||||
|
|
||||||
static Future<void> init() async {
|
static Future<void> init() async {
|
||||||
if (foundation.kDebugMode) {
|
if (foundation.kDebugMode) {
|
||||||
@ -32,9 +29,19 @@ class Log {
|
|||||||
await setLogCapture(true);
|
await setLogCapture(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void v(String msg,
|
static void v(
|
||||||
{dynamic ex, StackTrace stacktrace, Map<String, dynamic> props}) {
|
String msg, {
|
||||||
stacktrace = Trace.from(stacktrace).terse;
|
dynamic ex,
|
||||||
|
StackTrace? stacktrace,
|
||||||
|
Map<String, dynamic>? props,
|
||||||
|
}) {
|
||||||
|
assert(
|
||||||
|
ex == null || ex.runtimeType == Error || ex.runtimeType == Exception,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (stacktrace != null) {
|
||||||
|
stacktrace = Trace.from(stacktrace).terse;
|
||||||
|
}
|
||||||
|
|
||||||
if (foundation.kDebugMode) {
|
if (foundation.kDebugMode) {
|
||||||
Fimber.log("V", msg,
|
Fimber.log("V", msg,
|
||||||
@ -43,9 +50,19 @@ class Log {
|
|||||||
_write('v', msg, ex, stacktrace, props);
|
_write('v', msg, ex, stacktrace, props);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void d(String msg,
|
static void d(
|
||||||
{dynamic ex, StackTrace stacktrace, Map<String, dynamic> props}) {
|
String msg, {
|
||||||
stacktrace = Trace.from(stacktrace).terse;
|
dynamic ex,
|
||||||
|
StackTrace? stacktrace,
|
||||||
|
Map<String, dynamic>? props,
|
||||||
|
}) {
|
||||||
|
assert(
|
||||||
|
ex == null || ex.runtimeType == Error || ex.runtimeType == Exception,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (stacktrace != null) {
|
||||||
|
stacktrace = Trace.from(stacktrace).terse;
|
||||||
|
}
|
||||||
|
|
||||||
if (foundation.kDebugMode) {
|
if (foundation.kDebugMode) {
|
||||||
Fimber.log("D", msg,
|
Fimber.log("D", msg,
|
||||||
@ -54,9 +71,19 @@ class Log {
|
|||||||
_write('d', msg, ex, stacktrace, props);
|
_write('d', msg, ex, stacktrace, props);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void i(String msg,
|
static void i(
|
||||||
{dynamic ex, StackTrace stacktrace, Map<String, dynamic> props}) {
|
String msg, {
|
||||||
stacktrace = Trace.from(stacktrace).terse;
|
dynamic ex,
|
||||||
|
StackTrace? stacktrace,
|
||||||
|
Map<String, dynamic>? props,
|
||||||
|
}) {
|
||||||
|
assert(
|
||||||
|
ex == null || ex.runtimeType == Error || ex.runtimeType == Exception,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (stacktrace != null) {
|
||||||
|
stacktrace = Trace.from(stacktrace).terse;
|
||||||
|
}
|
||||||
|
|
||||||
if (foundation.kDebugMode) {
|
if (foundation.kDebugMode) {
|
||||||
if (props != null && props.isNotEmpty) {
|
if (props != null && props.isNotEmpty) {
|
||||||
@ -68,9 +95,19 @@ class Log {
|
|||||||
_write('i', msg, ex, stacktrace, props);
|
_write('i', msg, ex, stacktrace, props);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void e(String msg,
|
static void e(
|
||||||
{dynamic ex, StackTrace stacktrace, Map<String, dynamic> props}) {
|
String msg, {
|
||||||
stacktrace = Trace.from(stacktrace).terse;
|
dynamic ex,
|
||||||
|
StackTrace? stacktrace,
|
||||||
|
Map<String, dynamic>? props,
|
||||||
|
}) {
|
||||||
|
assert(
|
||||||
|
ex == null || ex.runtimeType == Error || ex.runtimeType == Exception,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (stacktrace != null) {
|
||||||
|
stacktrace = Trace.from(stacktrace).terse;
|
||||||
|
}
|
||||||
|
|
||||||
if (foundation.kDebugMode) {
|
if (foundation.kDebugMode) {
|
||||||
Fimber.log("E", msg,
|
Fimber.log("E", msg,
|
||||||
@ -79,9 +116,19 @@ class Log {
|
|||||||
_write('e', msg, ex, stacktrace, props);
|
_write('e', msg, ex, stacktrace, props);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void w(String msg,
|
static void w(
|
||||||
{dynamic ex, StackTrace stacktrace, Map<String, dynamic> props}) {
|
String msg, {
|
||||||
stacktrace = Trace.from(stacktrace).terse;
|
dynamic ex,
|
||||||
|
StackTrace? stacktrace,
|
||||||
|
Map<String, dynamic>? props,
|
||||||
|
}) {
|
||||||
|
assert(
|
||||||
|
ex == null || ex.runtimeType == Error || ex.runtimeType == Exception,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (stacktrace != null) {
|
||||||
|
stacktrace = Trace.from(stacktrace).terse;
|
||||||
|
}
|
||||||
|
|
||||||
if (foundation.kDebugMode) {
|
if (foundation.kDebugMode) {
|
||||||
Fimber.log("W", msg,
|
Fimber.log("W", msg,
|
||||||
@ -94,9 +141,13 @@ class Log {
|
|||||||
String level,
|
String level,
|
||||||
String msg,
|
String msg,
|
||||||
dynamic ex,
|
dynamic ex,
|
||||||
StackTrace stackTrace,
|
StackTrace? stackTrace,
|
||||||
Map<String, dynamic> props,
|
Map<String, dynamic>? props,
|
||||||
) {
|
) {
|
||||||
|
assert(
|
||||||
|
ex == null || ex.runtimeType == Error || ex.runtimeType == Exception,
|
||||||
|
);
|
||||||
|
|
||||||
if (logFile == null) {
|
if (logFile == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -106,12 +157,14 @@ class Log {
|
|||||||
l: level,
|
l: level,
|
||||||
msg: msg.replaceAll('\n', ' '),
|
msg: msg.replaceAll('\n', ' '),
|
||||||
ex: ex != null ? ex.toString().replaceAll('\n', ' ') : null,
|
ex: ex != null ? ex.toString().replaceAll('\n', ' ') : null,
|
||||||
stack: stackTrace.toListOfMap(),
|
|
||||||
props: props,
|
props: props,
|
||||||
);
|
);
|
||||||
|
if (stackTrace != null) {
|
||||||
|
logMsg.stack = stackTrace.toListOfMap();
|
||||||
|
}
|
||||||
|
|
||||||
var str = json.encode(logMsg.toMap());
|
var str = json.encode(logMsg.toMap());
|
||||||
logFile.writeStringSync(str + '\n');
|
logFile!.writeStringSync(str + '\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<void> setLogCapture(bool state) async {
|
static Future<void> setLogCapture(bool state) async {
|
||||||
@ -122,7 +175,7 @@ class Log {
|
|||||||
print("Writing logs to file $logFilePath");
|
print("Writing logs to file $logFilePath");
|
||||||
} else {
|
} else {
|
||||||
if (logFile != null) {
|
if (logFile != null) {
|
||||||
await logFile.close();
|
await logFile!.close();
|
||||||
}
|
}
|
||||||
logFile = null;
|
logFile = null;
|
||||||
}
|
}
|
||||||
@ -180,17 +233,17 @@ class Log {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class LogMessage {
|
class LogMessage {
|
||||||
int t;
|
int? t;
|
||||||
String l;
|
String? l;
|
||||||
String msg;
|
String? msg;
|
||||||
String ex;
|
String? ex;
|
||||||
List<Map<String, dynamic>> stack;
|
List<Map<String, dynamic>>? stack;
|
||||||
Map<String, dynamic> props;
|
Map<String, dynamic>? props;
|
||||||
|
|
||||||
LogMessage({
|
LogMessage({
|
||||||
@required this.t,
|
required this.t,
|
||||||
@required this.l,
|
required this.l,
|
||||||
@required this.msg,
|
required this.msg,
|
||||||
this.ex,
|
this.ex,
|
||||||
this.stack,
|
this.stack,
|
||||||
this.props,
|
this.props,
|
||||||
@ -201,9 +254,9 @@ class LogMessage {
|
|||||||
't': t,
|
't': t,
|
||||||
'l': l,
|
'l': l,
|
||||||
'msg': msg,
|
'msg': msg,
|
||||||
if (ex != null && ex.isNotEmpty) 'ex': ex,
|
if (ex != null && ex!.isNotEmpty) 'ex': ex,
|
||||||
if (stack != null) 'stack': stack,
|
if (stack != null) 'stack': stack,
|
||||||
if (props != null && props.isNotEmpty) 'p': props,
|
if (props != null && props!.isNotEmpty) 'p': props,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user