Добавлено логирование
This commit is contained in:
@@ -40,7 +40,9 @@ final class AuthService implements AuthInterface {
|
||||
if (response['success'] == false) return null;
|
||||
|
||||
return response['result']['authUid'];
|
||||
} catch (e) {
|
||||
} catch (e, st) {
|
||||
logger.logError('Ошибка в методе login', e, st);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -66,10 +68,8 @@ final class AuthService implements AuthInterface {
|
||||
}
|
||||
|
||||
return success;
|
||||
} catch (e) {
|
||||
if (e is DioException) {
|
||||
showErrorToast(e.response?.data['message']);
|
||||
}
|
||||
} catch (e, st) {
|
||||
logger.logError('Ошибка в методе send code', e, st);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
87
lib/services/logs/custom_history.dart
Normal file
87
lib/services/logs/custom_history.dart
Normal file
@@ -0,0 +1,87 @@
|
||||
// Dart imports:
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
// Flutter imports:
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
// Package imports:
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:talker_flutter/talker_flutter.dart';
|
||||
|
||||
class CustomHistory implements TalkerHistory {
|
||||
CustomHistory(this.settings, {List<TalkerData>? history}) {
|
||||
if (history != null) {
|
||||
_history.addAll(history);
|
||||
}
|
||||
}
|
||||
|
||||
final TalkerSettings settings;
|
||||
|
||||
final List<TalkerData> _history = <TalkerData>[];
|
||||
|
||||
@override
|
||||
List<TalkerData> get history => _history;
|
||||
|
||||
@override
|
||||
void clean() async {
|
||||
if (settings.useHistory) {
|
||||
_history.clear();
|
||||
|
||||
final Directory docDir = await getApplicationDocumentsDirectory();
|
||||
|
||||
final String path = '${docDir.path}/history.log';
|
||||
|
||||
final File logFile = File(path);
|
||||
|
||||
if (logFile.existsSync()) {
|
||||
logFile.deleteSync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void write(TalkerData data) {
|
||||
if (settings.useHistory && settings.enabled) {
|
||||
if (settings.maxHistoryItems <= _history.length) {
|
||||
_history.removeAt(0);
|
||||
}
|
||||
|
||||
_history.add(data);
|
||||
|
||||
_writeToFile(data);
|
||||
}
|
||||
}
|
||||
|
||||
void _writeToFile(TalkerData data) async {
|
||||
try {
|
||||
final Directory docDir = await getApplicationDocumentsDirectory();
|
||||
|
||||
final String path = '${docDir.path}/history.log';
|
||||
|
||||
final File logFile = File(path);
|
||||
|
||||
if (!logFile.existsSync()) {
|
||||
logFile.createSync();
|
||||
}
|
||||
|
||||
final Map<String, dynamic> jData = <String, dynamic>{
|
||||
'error': data.error,
|
||||
'exception': data.exception,
|
||||
'logLevel': data.logLevel,
|
||||
'key': data.key,
|
||||
'message': data.message,
|
||||
'stackTrace': data.stackTrace,
|
||||
'title': data.title,
|
||||
'time': data.time.toString(),
|
||||
};
|
||||
|
||||
logFile.writeAsStringSync(
|
||||
'${jsonEncode(jData)}\n',
|
||||
mode: FileMode.writeOnlyAppend,
|
||||
);
|
||||
} catch (e) {
|
||||
debugPrint('ALARMA ERROR in _writeToFile customHistory $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
123
lib/services/logs/logs_service.dart
Normal file
123
lib/services/logs/logs_service.dart
Normal file
@@ -0,0 +1,123 @@
|
||||
// Dart imports:
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
// Flutter imports:
|
||||
import 'package:flutter/cupertino.dart';
|
||||
|
||||
// Package imports:
|
||||
import 'package:fluttertoast/fluttertoast.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:remever/common/functions.dart';
|
||||
import 'package:remever/common/typedef.dart';
|
||||
import 'package:remever/models/logs/build_log.dart';
|
||||
import 'package:talker_flutter/talker_flutter.dart';
|
||||
|
||||
@Singleton()
|
||||
class LogsService {
|
||||
void log(String message) {
|
||||
talker.log(message);
|
||||
}
|
||||
|
||||
void logDebug(String message) {
|
||||
talker.debug(message);
|
||||
}
|
||||
|
||||
void logInfo(String message) {
|
||||
talker.info(message);
|
||||
}
|
||||
|
||||
void logError(String message, Object? exception, StackTrace? stackTrace) {
|
||||
talker.error(message, exception, stackTrace);
|
||||
|
||||
showErrorToast('$message ${exception.toString()}');
|
||||
}
|
||||
|
||||
void logCritical(String message, Object? exception, StackTrace? stackTrace) {
|
||||
talker.critical(message, exception, stackTrace);
|
||||
}
|
||||
|
||||
void logBuild(String message) {
|
||||
talker.logCustom(BuildLog(message));
|
||||
}
|
||||
|
||||
// Метод для получения истории
|
||||
Future<List<TalkerData>?> getHistory() async {
|
||||
try {
|
||||
final Directory docDir = await getApplicationDocumentsDirectory();
|
||||
final String path = '${docDir.path}/history.log';
|
||||
final File logFile = File(path);
|
||||
|
||||
if (!logFile.existsSync()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final List<String> lines = await logFile.readAsLines();
|
||||
if (lines.length > 1500) {
|
||||
lines.removeRange(0, lines.length - 1500);
|
||||
|
||||
logFile.deleteSync();
|
||||
logFile.createSync();
|
||||
logFile.writeAsStringSync(lines.join('\n'));
|
||||
}
|
||||
|
||||
final List<Json> entities = await _parseLines(lines, logFile);
|
||||
|
||||
if (entities.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final List<TalkerData> talkerData = _convertEntitiesToTalkerData(
|
||||
entities,
|
||||
);
|
||||
return talkerData;
|
||||
} catch (e) {
|
||||
debugPrint('ALARM ERROR IN GETHISTORY $e');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Парсинг строк из файла
|
||||
Future<List<Json>> _parseLines(List<String> lines, File logFile) async {
|
||||
final List<Json> entities = <Json>[];
|
||||
|
||||
for (String line in lines) {
|
||||
try {
|
||||
entities.add(jsonDecode(line));
|
||||
} catch (e) {
|
||||
debugPrint('Error $e in line $line of ${logFile.path}');
|
||||
|
||||
if (e.toString().contains('Unexpected character')) {
|
||||
unawaited(logFile.delete());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return entities;
|
||||
}
|
||||
|
||||
// Преобразование JSON-сущностей в TalkerData
|
||||
List<TalkerData> _convertEntitiesToTalkerData(List<Json> entities) {
|
||||
final List<TalkerData> talkerData = <TalkerData>[];
|
||||
|
||||
for (Json json in entities) {
|
||||
talkerData.add(
|
||||
TalkerData(
|
||||
json['message'],
|
||||
error: json['error'],
|
||||
exception: json['exception'],
|
||||
key: json['key'],
|
||||
logLevel: json['logLevel'],
|
||||
stackTrace: json['stackTrace'],
|
||||
time: DateTime.tryParse(json['time']),
|
||||
title: 'SAVED ${json['title']}',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return talkerData;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user