Skip to content

Commit

Permalink
Support dart 3 (#9)
Browse files Browse the repository at this point in the history
* Support dart 3.

* Resolve all lint warning and suggestion.
  • Loading branch information
JimmyTai authored Jun 19, 2023
1 parent e508c6a commit a5d798d
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 58 deletions.
6 changes: 4 additions & 2 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
include: package:effective_dart/analysis_options.yaml
include: package:flutter_lints/flutter.yaml

linter:
rules:
constant_identifier_names: false
avoid_function_literals_in_foreach_calls: false
public_member_api_docs: false
public_member_api_docs: false
always_use_package_imports: false
prefer_relative_imports: true
4 changes: 3 additions & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ description: A new Flutter application.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
publish_to: none

environment:
sdk: ">=2.12.0 <3.0.0"
sdk: ">=2.18.0 <4.0.0"
flutter: '>=3.0.0'

dependencies:
flutter:
Expand Down
5 changes: 4 additions & 1 deletion lib/data/local/app_database.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import 'dart:async';

import 'package:f_logs/f_logs.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sembast/sembast.dart';
import 'package:sembast/sembast_io.dart';
import 'package:sembast/sembast_memory.dart';

import '../../constants/db_constants.dart';
import '../../model/flog/flog.dart';
import '../../utils/encryption/xxtea.dart';

class AppDatabase {
// Singleton instance
static final AppDatabase _singleton = AppDatabase._();
Expand Down
20 changes: 10 additions & 10 deletions lib/data/local/dao.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:f_logs/model/flog/log_cursor.dart';
import 'package:sembast/sembast.dart';

import '../../constants/db_constants.dart';
import '../../model/flog/log.dart';
import '../../model/flog/log_cursor.dart';

abstract class Dao {
// A Store with int keys and Map<String, dynamic> values.
Expand All @@ -11,16 +11,16 @@ abstract class Dao {
final _flogsStore = intMapStoreFactory.store(DBConstants.FLOG_STORE_NAME);
final _fcursorsStore =
intMapStoreFactory.store(DBConstants.FCURSOR_STORE_NAME);
int _JsonStringLength = -1;
int _jsonStringLength = -1;

int get jsonStringLength => _JsonStringLength;
int get jsonStringLength => _jsonStringLength;

// Private getter to shorten the amount of code needed to get the
// singleton instance of an opened database.
Future<Database> get db;

Future<void> _initDBStringSizeIfRequired({bool force = false}) async {
if (force || _JsonStringLength <= 0) {
if (force || _jsonStringLength <= 0) {
await getAllLogs().then((logs) {
_calculateListLog(logs);
});
Expand All @@ -34,13 +34,13 @@ abstract class Dao {
list.forEach((e) {
s += e.toJson().toString().length;
});
_JsonStringLength = s;
_jsonStringLength = s;
}

/// DB functions:--------------------------------------------------------------
Future<int> insert(Log log) async {
await _initDBStringSizeIfRequired();
_JsonStringLength += log.toJson().toString().length;
_jsonStringLength += log.toJson().toString().length;
return await _flogsStore.add(await db, log.toJson());
}

Expand Down Expand Up @@ -84,7 +84,7 @@ abstract class Dao {
await _flogsStore.delete(
await db,
);
_JsonStringLength = 0;
_jsonStringLength = 0;
}

/// Fetch all Logs which match the given `filters` and sorts them by `dataLogType`
Expand Down Expand Up @@ -142,12 +142,12 @@ abstract class Dao {
if (key == null) {
return await _fcursorsStore.add(await db, logCursor.toJson());
}
LogCursor _logCursor = logCursor;
_logCursor.id = key;
LogCursor newLogCursor = logCursor;
newLogCursor.id = key;
final finder = Finder(filter: Filter.byKey(key));
return await _fcursorsStore.update(
await db,
_logCursor.toJson(),
newLogCursor.toJson(),
finder: finder,
);
}
Expand Down
4 changes: 3 additions & 1 deletion lib/data/local/flog_dao.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'dart:async';

import 'package:f_logs/f_logs.dart';
import 'package:sembast/sembast.dart';

import 'app_database.dart';
import 'dao.dart';

class FlogDao extends Dao {
// Singleton instance
static final FlogDao _singleton = FlogDao._();
Expand Down
4 changes: 3 additions & 1 deletion lib/data/local/flog_dao_memory.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'dart:async';

import 'package:f_logs/f_logs.dart';
import 'package:sembast/sembast.dart';

import 'app_database.dart';
import 'dao.dart';

class FlogDaoMemory extends Dao {
// Singleton instance
static final FlogDaoMemory _singleton = FlogDaoMemory._();
Expand Down
54 changes: 30 additions & 24 deletions lib/model/flog/flog.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
// ignore_for_file: avoid_print

import 'dart:io';

import 'package:f_logs/f_logs.dart';
import 'package:sembast/sembast.dart';
import 'package:stack_trace/stack_trace.dart';

import '../../constants/constants.dart';
import '../../data/local/flog_dao.dart';
import '../../utils/datetime/date_time.dart';
import '../../utils/filters/filter_type.dart';
import '../../utils/filters/filters.dart';
import '../../utils/formatter/formatter.dart';
import '../../utils/storage/logs_storage.dart';
import 'flog_config.dart';
import 'log.dart';
import 'log_level.dart';

class FLog {
// flogs data source
static final _flogDao = FlogDao.instance;
Expand Down Expand Up @@ -194,7 +206,7 @@ class FLog {
_getAllLogs().then((logs) {
var buffer = StringBuffer();

if (logs.length > 0) {
if (logs.isNotEmpty) {
logs.forEach((log) {
buffer.write(Formatter.format(log, _config));
});
Expand Down Expand Up @@ -365,7 +377,6 @@ class FLog {
///
/// Returns the default configuration
static LogsConfig getDefaultConfigurations() {
assert(_config != null);
return _config;
}

Expand All @@ -387,15 +398,14 @@ class FLog {
dynamic exception,
String? dataLogType,
StackTrace? stacktrace) {

// This variable can be ClassName.MethodName or only a function name, when it doesn't belong to a class, e.g. main()
var member = Trace.current().frames[2].member!;

//check to see if className is not provided
//then its already been taken from calling class
if (className == null) {
// If there is a . in the member name, it means the method belongs to a class. Thus we can split it.
if(member.contains(".")) {
if (member.contains(".")) {
className = member.split(".")[0];
} else {
className = "";
Expand All @@ -406,7 +416,7 @@ class FLog {
//then its already been taken from calling class
if (methodName == null) {
// If there is a . in the member name, it means the method belongs to a class. Thus we can split it.
if(member.contains(".")) {
if (member.contains(".")) {
methodName = member.split(".")[1];
} else {
methodName = member;
Expand All @@ -415,8 +425,9 @@ class FLog {

// Generate a custom formatted stack trace
String? formattedStackTrace;
if(_config.stackTraceFormatter != null) {
formattedStackTrace = _config.stackTraceFormatter!(stacktrace ?? StackTrace.current);
if (_config.stackTraceFormatter != null) {
formattedStackTrace =
_config.stackTraceFormatter!(stacktrace ?? StackTrace.current);
}

//check to see if user provides a valid configuration and logs are enabled
Expand Down Expand Up @@ -458,7 +469,8 @@ class FLog {
/// _getAllSortedByFilter
///
/// This will return the list of logs sorted by provided filters
static Future<List<Log>> _getAllSortedByFilter({List<Filter>? filters}) async {
static Future<List<Log>> _getAllSortedByFilter(
{List<Filter>? filters}) async {
//check to see if user provides a valid configuration and logs are enabled
//if not then don't do anything
if (_isLogsConfigValid()) {
Expand All @@ -475,21 +487,15 @@ class FLog {
//check to see if user provides a valid configuration and logs are enabled
//if not then don't do anything
if (_isLogsConfigValid()) {
// skip write logs when log level is to low or
// active log level is not in enabled log levels
if (_config.activeLogLevel != null) {
// skip write logs when log level is to low
if (LogLevel.values.indexOf(_config.activeLogLevel) <=
LogLevel.values.indexOf(log.logLevel!) &&
_config.logLevelsEnabled.contains(_config.activeLogLevel)) {
//check to see if logcat debugging is enabled
if (_config.isDebuggable) {
print(Formatter.format(log, _config));
}
await _flogDao.insert(log);
// skip write logs when log level is to low
if (LogLevel.values.indexOf(_config.activeLogLevel) <=
LogLevel.values.indexOf(log.logLevel!) &&
_config.logLevelsEnabled.contains(_config.activeLogLevel)) {
//check to see if logcat debugging is enabled
if (_config.isDebuggable) {
print(Formatter.format(log, _config));
}
} else {
throw Exception(Constants.EXCEPTION_NULL_LOGS_LEVEL);
await _flogDao.insert(log);
}
} else {
throw Exception(Constants.EXCEPTION_NOT_INIT);
Expand All @@ -501,6 +507,6 @@ class FLog {
/// This will check if user provided any configuration and logs are enabled
/// if yes, then it will return true else it will return false
static _isLogsConfigValid() {
return _config != null && _config.isLogsEnabled;
return _config.isLogsEnabled;
}
}
8 changes: 6 additions & 2 deletions lib/model/flog/flog_config.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import 'package:f_logs/f_logs.dart';
import '../../utils/formatter/field_name.dart';
import '../../utils/formatter/formate_type.dart';
import '../../utils/timestamp/timestamp_format.dart';
import '../datalog/data_log_type.dart';
import 'log_level.dart';

typedef String? StackTraceFormatter(StackTrace stackTrace);
typedef StackTraceFormatter = String? Function(StackTrace stackTrace);

class LogsConfig {
/// print logs in Logcat
Expand Down
15 changes: 10 additions & 5 deletions lib/utils/datetime/date_time.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import 'package:f_logs/f_logs.dart';
// ignore_for_file: avoid_print

import 'package:intl/intl.dart';

import '../../model/flog/flog.dart';
import '../../model/flog/flog_config.dart';
import '../filters/filter_type.dart';

class DateTimeUtils {
DateTimeUtils._();

Expand All @@ -17,7 +22,7 @@ class DateTimeUtils {

static String getTimeInMillis(LogsConfig config) {
final now = DateTime.now();
var fiftyDaysFromNow = now.add(Duration(days: -1));
var fiftyDaysFromNow = now.add(const Duration(days: -1));
return DateFormat(config.timestampFormat.toString())
.format(fiftyDaysFromNow);
}
Expand All @@ -38,15 +43,15 @@ class DateTimeUtils {
// data/time now
var now = DateTime.now();
// last hour
var lh = now.subtract(Duration(hours: 1));
var lh = now.subtract(const Duration(hours: 1));
print(lh);
startTimeInMillis = lh.millisecondsSinceEpoch;
break;
case twentyFourHour:
// data/time now
var now = DateTime.now();
// last twenty four hours from now
var tfh = now.subtract(Duration(hours: 24));
var tfh = now.subtract(const Duration(hours: 24));
//print
if (FLog.getDefaultConfigurations().isDevelopmentDebuggingEnabled) {
print(tfh);
Expand All @@ -72,7 +77,7 @@ class DateTimeUtils {
// midnight today
var td = DateTime(now.year, now.month, now.day);
// last week from today
var w = td.subtract(Duration(days: 7));
var w = td.subtract(const Duration(days: 7));
//print
if (FLog.getDefaultConfigurations().isDevelopmentDebuggingEnabled) {
print(w);
Expand Down
9 changes: 6 additions & 3 deletions lib/utils/filters/filters.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'package:f_logs/f_logs.dart';
import 'package:sembast/sembast.dart';

import '../../constants/db_constants.dart';
import '../datetime/date_time.dart';
import 'filter_type.dart';

class Filters {
Filters._();

Expand All @@ -16,14 +19,14 @@ class Filters {
var timestampFilters = <Filter>[];

//check to see if dataLogsType is not null
if (dataLogsType != null && dataLogsType.length > 0) {
if (dataLogsType != null && dataLogsType.isNotEmpty) {
final dataLogTypeFilter =
Filter.inList(DBConstants.FIELD_DATA_LOG_TYPE, dataLogsType);
filters.add(dataLogTypeFilter);
}

//check to see if logLevels is not null
if (logLevels != null && logLevels.length > 0) {
if (logLevels != null && logLevels.isNotEmpty) {
final logLevelsFilter =
Filter.inList(DBConstants.FIELD_LOG_LEVEL, logLevels);
filters.add(logLevelsFilter);
Expand Down
6 changes: 5 additions & 1 deletion lib/utils/formatter/formatter.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import 'package:f_logs/f_logs.dart';
import 'package:flutter/foundation.dart';

import '../../model/flog/flog_config.dart';
import '../../model/flog/log.dart';
import 'field_name.dart';
import 'formate_type.dart';

class Formatter {
static String format(Log log, LogsConfig config) {
String? output;
Expand Down
Loading

0 comments on commit a5d798d

Please sign in to comment.