mirror of
https://github.com/hanskokx/arcane_framework.git
synced 2026-05-14 10:29:06 +02:00
Added logStream to logger. Updated example code.
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
+92
-2
@@ -1,3 +1,5 @@
|
|||||||
|
import "dart:async";
|
||||||
|
|
||||||
import "package:arcane_framework/arcane_framework.dart";
|
import "package:arcane_framework/arcane_framework.dart";
|
||||||
import "package:example/config.dart";
|
import "package:example/config.dart";
|
||||||
import "package:example/interfaces/debug_auth_interface.dart";
|
import "package:example/interfaces/debug_auth_interface.dart";
|
||||||
@@ -78,10 +80,15 @@ class HomeScreen extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _HomeScreenState extends State<HomeScreen> {
|
class _HomeScreenState extends State<HomeScreen> {
|
||||||
|
late final StreamSubscription<String> _subscription;
|
||||||
|
final List<String> latestLogs = [];
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final bool isSignedIn = Arcane.auth.isSignedIn.value;
|
final bool isSignedIn = Arcane.auth.isSignedIn.value;
|
||||||
return GridView.extent(
|
return Column(
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: GridView.extent(
|
||||||
maxCrossAxisExtent: 300,
|
maxCrossAxisExtent: 300,
|
||||||
padding: const EdgeInsets.all(16),
|
padding: const EdgeInsets.all(16),
|
||||||
children: [
|
children: [
|
||||||
@@ -100,14 +107,26 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
children: [
|
children: [
|
||||||
Switch(
|
Switch(
|
||||||
value: Arcane.theme.currentTheme == ThemeMode.dark,
|
value: Arcane.theme.currentTheme == ThemeMode.dark,
|
||||||
thumbIcon: WidgetStateProperty.resolveWith((states) {
|
thumbIcon:
|
||||||
|
WidgetStateProperty.resolveWith((states) {
|
||||||
if (states.contains(WidgetState.selected)) {
|
if (states.contains(WidgetState.selected)) {
|
||||||
return const Icon(Icons.dark_mode);
|
return const Icon(Icons.dark_mode);
|
||||||
}
|
}
|
||||||
return const Icon(Icons.light_mode);
|
return const Icon(Icons.light_mode);
|
||||||
}),
|
}),
|
||||||
onChanged: (_) {
|
onChanged: (_) {
|
||||||
|
final ThemeMode oldTheme =
|
||||||
|
Arcane.theme.currentTheme;
|
||||||
Arcane.theme.switchTheme();
|
Arcane.theme.switchTheme();
|
||||||
|
Arcane.log(
|
||||||
|
"Switching theme",
|
||||||
|
metadata: {
|
||||||
|
"followingSystemTheme":
|
||||||
|
"${Arcane.theme.isFollowingSystemTheme}",
|
||||||
|
"newMode": Arcane.theme.currentTheme.name,
|
||||||
|
"oldMode": oldTheme.name,
|
||||||
|
},
|
||||||
|
);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
Row(
|
Row(
|
||||||
@@ -116,12 +135,34 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
Checkbox(
|
Checkbox(
|
||||||
value: Arcane.theme.isFollowingSystemTheme,
|
value: Arcane.theme.isFollowingSystemTheme,
|
||||||
onChanged: (value) {
|
onChanged: (value) {
|
||||||
|
final ThemeMode oldTheme =
|
||||||
|
Arcane.theme.currentTheme;
|
||||||
if (value == true) {
|
if (value == true) {
|
||||||
Arcane.theme.followSystemTheme(context);
|
Arcane.theme.followSystemTheme(context);
|
||||||
|
Arcane.log(
|
||||||
|
"Switching theme",
|
||||||
|
metadata: {
|
||||||
|
"followingSystemTheme":
|
||||||
|
"${Arcane.theme.isFollowingSystemTheme}",
|
||||||
|
"newMode":
|
||||||
|
Arcane.theme.currentTheme.name,
|
||||||
|
"oldMode": oldTheme.name,
|
||||||
|
},
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
Arcane.theme.switchTheme(
|
Arcane.theme.switchTheme(
|
||||||
themeMode: Arcane.theme.systemTheme,
|
themeMode: Arcane.theme.systemTheme,
|
||||||
);
|
);
|
||||||
|
Arcane.log(
|
||||||
|
"Switching theme",
|
||||||
|
metadata: {
|
||||||
|
"followingSystemTheme":
|
||||||
|
"${Arcane.theme.isFollowingSystemTheme}",
|
||||||
|
"newMode":
|
||||||
|
Arcane.theme.currentTheme.name,
|
||||||
|
"oldMode": oldTheme.name,
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
@@ -180,6 +221,55 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.all(16.0),
|
||||||
|
child: SizedBox(
|
||||||
|
height: 200,
|
||||||
|
child: Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
Text(
|
||||||
|
"Logging",
|
||||||
|
style: Theme.of(context).textTheme.headlineSmall,
|
||||||
|
),
|
||||||
|
if (latestLogs.isEmpty)
|
||||||
|
Text(
|
||||||
|
"Log messages will appear here",
|
||||||
|
style: Theme.of(context).textTheme.labelSmall?.copyWith(
|
||||||
|
fontStyle: FontStyle.italic,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
Expanded(
|
||||||
|
child: ListView.builder(
|
||||||
|
itemCount: latestLogs.length,
|
||||||
|
itemBuilder: (context, index) {
|
||||||
|
return Text(latestLogs[index]);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
_subscription = Arcane.logger.logStream.listen((message) {
|
||||||
|
setState(() {
|
||||||
|
latestLogs.insert(0, message);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_subscription.cancel();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,16 @@ class ArcaneLogger {
|
|||||||
/// Additional metadata that is included in all logs.
|
/// Additional metadata that is included in all logs.
|
||||||
Map<String, String> get additionalMetadata => I._additionalMetadata;
|
Map<String, String> get additionalMetadata => I._additionalMetadata;
|
||||||
|
|
||||||
|
final StreamController<String> _logStreamController =
|
||||||
|
StreamController<String>.broadcast(
|
||||||
|
onCancel: () {
|
||||||
|
I._logStreamController.close();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
/// Stream of log messages being received and sent to the registered interfaces.
|
||||||
|
Stream<String> get logStream => I._logStreamController.stream;
|
||||||
|
|
||||||
bool _initialized = false;
|
bool _initialized = false;
|
||||||
|
|
||||||
/// Whether the logger has been initialized.
|
/// Whether the logger has been initialized.
|
||||||
@@ -241,6 +251,14 @@ class ArcaneLogger {
|
|||||||
stackTrace: stackTrace,
|
stackTrace: stackTrace,
|
||||||
extra: extra,
|
extra: extra,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
_logStreamController.add(
|
||||||
|
"$message ${{
|
||||||
|
"level": level,
|
||||||
|
"metadata": metadata,
|
||||||
|
"extra": extra,
|
||||||
|
}}",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user