Skip to content

Commit

Permalink
feat(neon_framework): use logging framework instead of print
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolas Rimikis <leptopoda@users.noreply.github.com>
  • Loading branch information
Leptopoda committed Feb 23, 2024
1 parent 41e7c61 commit d3f5b05
Show file tree
Hide file tree
Showing 24 changed files with 263 additions and 119 deletions.
5 changes: 4 additions & 1 deletion packages/neon/neon_files/lib/src/blocs/files.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:neon_files/l10n/localizations.dart';
import 'package:neon_files/src/blocs/browser.dart';
Expand All @@ -19,6 +20,8 @@ import 'package:rxdart/rxdart.dart';
import 'package:share_plus/share_plus.dart';
import 'package:universal_io/io.dart';

final _log = Logger('FilesBloc');

sealed class FilesBloc implements InteractiveBloc {
@internal
factory FilesBloc(
Expand Down Expand Up @@ -215,7 +218,7 @@ class _FilesBloc extends InteractiveBloc implements FilesBloc {
final file = File(p.join(cacheDir.path, 'files', etag.replaceAll('"', ''), uri.name));

if (!file.existsSync()) {
debugPrint('Downloading $uri since it does not exist');
_log.info('Downloading $uri since it does not exist');
if (!file.parent.existsSync()) {
await file.parent.create(recursive: true);
}
Expand Down
1 change: 1 addition & 0 deletions packages/neon/neon_files/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies:
go_router: ^13.0.0
image_picker: ^1.0.0
intl: ^0.18.0
logging: ^1.0.0
meta: ^1.0.0
neon_framework:
git:
Expand Down
12 changes: 9 additions & 3 deletions packages/neon/neon_news/lib/src/widgets/articles_view.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:html/dom.dart' as html_dom;
import 'package:html/parser.dart' as html_parser;
import 'package:logging/logging.dart';
import 'package:neon_framework/blocs.dart';
import 'package:neon_framework/platform.dart';
import 'package:neon_framework/sort_box.dart';
Expand All @@ -18,6 +19,8 @@ import 'package:neon_news/src/widgets/feed_icon.dart';
import 'package:nextcloud/news.dart' as news;
import 'package:url_launcher/url_launcher_string.dart';

final _log = Logger('NewsArticlesView');

class NewsArticlesView extends StatefulWidget {
const NewsArticlesView({
required this.bloc,
Expand Down Expand Up @@ -203,9 +206,12 @@ class _NewsArticlesViewState extends State<NewsArticlesView> {
String? bodyData;
try {
bodyData = _fixArticleBody(article.body);
} catch (e, s) {
debugPrint(e.toString());
debugPrint(s.toString());
} on Exception catch (error, stackTrace) {
_log.warning(
'Could not parse the body of ${article.title}',
error,
stackTrace,
);
}

final account = NeonProvider.of<AccountsBloc>(context).activeAccount.value!;
Expand Down
1 change: 1 addition & 0 deletions packages/neon/neon_news/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies:
go_router: ^13.0.0
html: ^0.15.0
intl: ^0.18.0
logging: ^1.0.0
meta: ^1.0.0
neon_framework:
git:
Expand Down
17 changes: 12 additions & 5 deletions packages/neon/neon_notes/lib/src/blocs/note.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:neon_framework/blocs.dart';
import 'package:neon_framework/models.dart';
Expand All @@ -11,6 +12,8 @@ import 'package:nextcloud/notes.dart' as notes;
import 'package:queue/queue.dart';
import 'package:rxdart/rxdart.dart';

final _log = Logger('NewsArticlesView');

sealed class NotesNoteBloc implements InteractiveBloc {
@internal
factory NotesNoteBloc(
Expand Down Expand Up @@ -63,10 +66,14 @@ class _NotesNoteBloc extends InteractiveBloc implements NotesNoteBloc {
final response = await call(etag);
emitNote(response.body);
await notesBloc.refresh();
} catch (e, s) {
debugPrint(e.toString());
debugPrint(s.toString());
addError(e);
} on http.ClientException catch (error, stackTrace) {
_log.warning(
'Error executing note action.',
error,
stackTrace,
);

addError(error);
}
});
}
Expand Down
2 changes: 2 additions & 0 deletions packages/neon/neon_notes/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ dependencies:
flutter_markdown: ^0.6.0
flutter_material_design_icons: ^1.0.0
go_router: ^13.0.0
http: ^1.2.1
intl: ^0.18.0
logging: ^1.0.0
meta: ^1.0.0
neon_framework:
git:
Expand Down
13 changes: 10 additions & 3 deletions packages/neon_framework/lib/src/bloc/bloc.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
import 'package:neon_framework/src/models/disposable.dart';
import 'package:neon_framework/src/utils/request_manager.dart';

final _log = Logger('Bloc');

/// A Bloc for implementing the Business Logic Component pattern.
///
/// This design pattern helps to separate presentation from business logic.
Expand Down Expand Up @@ -63,9 +66,13 @@ abstract class InteractiveBloc extends Bloc {
}

await (refresh ?? this.refresh)();
} catch (error, stacktrace) {
debugPrint(error.toString());
debugPrint(stacktrace.toString());
} on Exception catch (error, stacktrace) {
_log.info(
'An Exception has occurred executing an action.',
error,
stacktrace,
);

addError(error);
}
}
Expand Down
14 changes: 10 additions & 4 deletions packages/neon_framework/lib/src/blocs/accounts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import 'dart:convert';

import 'package:built_collection/built_collection.dart';
import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:neon_framework/src/bloc/bloc.dart';
import 'package:neon_framework/src/blocs/apps.dart';
Expand All @@ -24,6 +25,8 @@ import 'package:neon_framework/storage.dart';
import 'package:nextcloud/core.dart' as core;
import 'package:rxdart/rxdart.dart';

final _log = Logger('AccountsBloc');

/// The Bloc responsible for managing the [Account]s
@sealed
abstract interface class AccountsBloc implements Disposable {
Expand Down Expand Up @@ -258,9 +261,12 @@ class _AccountsBloc extends Bloc implements AccountsBloc {
unawaited(() async {
try {
await account.client.core.appPassword.deleteAppPassword();
} catch (e, s) {
debugPrint(e.toString());
debugPrint(s.toString());
} on http.ClientException catch (error, stackTrace) {
_log.info(
'Error deleting the app password.',
error,
stackTrace,
);
}
}());
}
Expand Down
24 changes: 12 additions & 12 deletions packages/neon_framework/lib/src/blocs/apps.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:async';

import 'package:built_collection/built_collection.dart';
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:neon_framework/src/bloc/bloc.dart';
import 'package:neon_framework/src/bloc/result.dart';
Expand All @@ -17,6 +17,8 @@ import 'package:nextcloud/nextcloud.dart';
import 'package:provider/provider.dart';
import 'package:rxdart/rxdart.dart';

final _log = Logger('AppsBloc');

/// The Bloc responsible for managing the [AppImplementation]s.
@sealed
abstract class AppsBloc implements InteractiveBloc {
Expand Down Expand Up @@ -171,14 +173,9 @@ class _AppsBloc extends InteractiveBloc implements AppsBloc {

final notSupported = <String, VersionCheck>{};

try {
final coreCheck = account.client.core.getVersionCheck(capabilities.requireData);
if (!coreCheck.isSupported && !capabilities.requireData.version.string.contains('dev')) {
notSupported['core'] = coreCheck;
}
} catch (e, s) {
debugPrint(e.toString());
debugPrint(s.toString());
final coreCheck = account.client.core.getVersionCheck(capabilities.requireData);
if (!coreCheck.isSupported && !capabilities.requireData.version.string.contains('dev')) {
notSupported['core'] = coreCheck;
}

for (final app in apps.requireData) {
Expand All @@ -192,9 +189,12 @@ class _AppsBloc extends InteractiveBloc implements AppsBloc {
if (!check.isSupported) {
notSupported[app.id] = check;
}
} catch (e, s) {
debugPrint(e.toString());
debugPrint(s.toString());
} on Exception catch (error, stackTrace) {
_log.warning(
'An Exception occurred while checking the installed version of $app.',
error,
stackTrace,
);
}
}

Expand Down
17 changes: 12 additions & 5 deletions packages/neon_framework/lib/src/blocs/login_check_account.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:neon_framework/src/bloc/bloc.dart';
import 'package:neon_framework/src/bloc/result.dart';
Expand All @@ -10,6 +11,8 @@ import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud/provisioning_api.dart' as provisioning_api;
import 'package:rxdart/rxdart.dart';

final _log = Logger('LoginCheckAccountBloc');

/// Bloc that checks that the account is ready for logging in.
sealed class LoginCheckAccountBloc implements InteractiveBloc {
@internal
Expand Down Expand Up @@ -73,10 +76,14 @@ class _LoginCheckAccountBloc extends InteractiveBloc implements LoginCheckAccoun
);

state.add(Result.success(account));
} catch (e, s) {
debugPrint(e.toString());
debugPrint(s.toString());
state.add(Result.error(e));
} on http.ClientException catch (error, stackTrace) {
_log.warning(
'Error fetching the user information.',
error,
stackTrace,
);

state.add(Result.error(error));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:neon_framework/src/bloc/bloc.dart';
import 'package:neon_framework/src/bloc/result.dart';
Expand All @@ -9,6 +10,8 @@ import 'package:nextcloud/core.dart' as core;
import 'package:nextcloud/nextcloud.dart';
import 'package:rxdart/rxdart.dart';

final _log = Logger('LoginCheckServerStatusBloc');

/// Bloc that checks that the server is ready for logging in.
sealed class LoginCheckServerStatusBloc implements InteractiveBloc {
@internal
Expand Down Expand Up @@ -47,10 +50,14 @@ class _LoginCheckServerStatusBloc extends InteractiveBloc implements LoginCheckS

final status = await client.core.getStatus();
state.add(Result.success(status.body));
} catch (e, s) {
debugPrint(e.toString());
debugPrint(s.toString());
state.add(Result.error(e));
} on http.ClientException catch (error, stackTrace) {
_log.warning(
'Error getting the server status.',
error,
stackTrace,
);

state.add(Result.error(error));
}
}
}
26 changes: 18 additions & 8 deletions packages/neon_framework/lib/src/blocs/login_flow.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:neon_framework/src/bloc/bloc.dart';
import 'package:neon_framework/src/bloc/result.dart';
Expand All @@ -9,6 +10,8 @@ import 'package:nextcloud/core.dart' as core;
import 'package:nextcloud/nextcloud.dart';
import 'package:rxdart/rxdart.dart';

final _log = Logger('LoginFlowBloc');

/// Bloc for running the Nextcloud Login Flow V2.
sealed class LoginFlowBloc implements InteractiveBloc {
@internal
Expand Down Expand Up @@ -66,15 +69,22 @@ class _LoginFlowBloc extends InteractiveBloc implements LoginFlowBloc {
final resultResponse = await client.core.clientFlowLoginV2.poll(token: initResponse.body.poll.token);
cancelPollTimer();
resultController.add(resultResponse.body);
} catch (e, s) {
debugPrint(e.toString());
debugPrint(s.toString());
} on DynamiteStatusCodeException catch (error) {
if (error.statusCode != 404) {
rethrow;
}

_log.fine('Login flow not found or completed. Polling again in one second.');
}
});
} catch (e, s) {
debugPrint(e.toString());
debugPrint(s.toString());
init.add(Result.error(e));
} on http.ClientException catch (error, stackTrace) {
_log.severe(
'Error while polling the login flow.',
error,
stackTrace,
);

init.add(Result.error(error));
}
}

Expand Down
15 changes: 11 additions & 4 deletions packages/neon_framework/lib/src/blocs/next_push.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:neon_framework/src/bloc/bloc.dart';
import 'package:neon_framework/src/blocs/accounts.dart';
Expand All @@ -10,6 +11,8 @@ import 'package:neon_framework/src/utils/global_options.dart';
import 'package:nextcloud/uppush.dart' as uppush;
import 'package:rxdart/rxdart.dart';

final _log = Logger('NextPushBloc');

/// Bloc for checking NextPush compatibility.
sealed class NextPushBloc implements Disposable {
@internal
Expand Down Expand Up @@ -59,9 +62,13 @@ class _NextPushBloc extends Bloc implements NextPushBloc {
try {
final response = await account.client.uppush.check();
supported[account] = response.body.success;
} catch (e, s) {
debugPrint(e.toString());
debugPrint(s.toString());
} on http.ClientException catch (error, stackTrace) {
_log.warning(
'Error checking unified push availability.',
error,
stackTrace,
);

supported[account] = false;
}
}
Expand Down
Loading

0 comments on commit d3f5b05

Please sign in to comment.