@@ -38,6 +38,7 @@ import 'package:flutter_single_instance/src/focus.dart';
38
38
import 'package:flutter_single_instance/src/generated/focus.pbgrpc.dart' ;
39
39
import 'package:flutter_single_instance/src/instance.dart' ;
40
40
import 'package:grpc/grpc.dart' ;
41
+ import 'package:logging/logging.dart' ;
41
42
import 'package:path_provider/path_provider.dart' ;
42
43
import 'src/linux.dart' ;
43
44
import 'src/macos.dart' ;
@@ -56,16 +57,20 @@ abstract class FlutterSingleInstance {
56
57
Server ? _server;
57
58
Instance ? _instance;
58
59
60
+ /// Logger for this class.
61
+ @protected
62
+ Logger get logger => Logger ('FlutterSingleInstance.$runtimeType ' );
63
+
59
64
/// Provides utilities for checking if this is the first instance of the app.
60
65
/// Make sure to call `WidgetsFlutterBinding.ensureInitialized()` before using this class.
61
66
factory FlutterSingleInstance () {
62
67
_singelton ?? = Platform .isMacOS
63
- ? FlutterSingleInstanceMacOS ()
68
+ ? MacOS ()
64
69
: Platform .isLinux
65
- ? FlutterSingleInstanceLinux ()
70
+ ? Linux ()
66
71
: Platform .isWindows
67
- ? FlutterSingleInstanceWindows ()
68
- : FlutterSingleInstanceUnsopported ();
72
+ ? Windows ()
73
+ : Unsupported ();
69
74
70
75
return _singelton! ;
71
76
}
@@ -82,6 +87,7 @@ abstract class FlutterSingleInstance {
82
87
/// Automatically writes a pid file to the temp directory if this is the first instance.
83
88
Future <bool > isFirstInstance () async {
84
89
if (debugMode) {
90
+ logger.finest ("Debug mode enabled, reporting as first instance" );
85
91
return true ;
86
92
}
87
93
@@ -92,6 +98,8 @@ abstract class FlutterSingleInstance {
92
98
pidFile! ;
93
99
94
100
if (! pidFile.existsSync ()) {
101
+ logger.finest ("No pid file found, activating instance" );
102
+
95
103
// No pid file, so this is the first instance.
96
104
await activateInstance (processName);
97
105
return true ;
@@ -101,13 +109,19 @@ abstract class FlutterSingleInstance {
101
109
102
110
_instance = Instance .fromJson (jsonDecode (data));
103
111
112
+ logger.finest ("Pid file found, verifying instance: $_instance " );
113
+
104
114
final pidName = await getProcessName (_instance! .pid);
105
115
106
116
if (processName == pidName) {
117
+ logger.finest ("Process name matches, reporting as second instance" );
118
+
107
119
// Process exists, so this is not the first instance.
108
120
return false ;
109
121
}
110
122
123
+ logger.finest ("Process name does not match, activating instance" );
124
+
111
125
// Process does not exist, so we can activate this instance.
112
126
await activateInstance (processName);
113
127
@@ -127,6 +141,8 @@ abstract class FlutterSingleInstance {
127
141
);
128
142
129
143
await pidFile? .writeAsString (jsonEncode (instance.toJson ()));
144
+
145
+ logger.finest ("Instance activated: $instance at ${pidFile ?.path }" );
130
146
}
131
147
132
148
/// Returns the pid file.
@@ -140,6 +156,8 @@ abstract class FlutterSingleInstance {
140
156
/// Starts an RPC server that listens for focus requests.
141
157
@protected
142
158
Future <int > startRpcServer () async {
159
+ logger.finest ("Starting RPC server" );
160
+
143
161
_server = Server .create (
144
162
services: [FocusService ()],
145
163
codecRegistry: CodecRegistry (
@@ -152,6 +170,8 @@ abstract class FlutterSingleInstance {
152
170
153
171
await _server! .serve (port: 0 );
154
172
173
+ logger.finest ("RPC server started on port ${_server !.port }" );
174
+
155
175
return _server! .port! ;
156
176
}
157
177
@@ -161,6 +181,8 @@ abstract class FlutterSingleInstance {
161
181
if (_instance == null ) return "No instance to focus" ;
162
182
if (_server != null ) return "This is the first instance" ;
163
183
184
+ logger.finest ("Focusing instance: $_instance " );
185
+
164
186
try {
165
187
final channel = ClientChannel (
166
188
'localhost' ,
@@ -180,8 +202,17 @@ abstract class FlutterSingleInstance {
180
202
181
203
final response = await client.focus (FocusRequest ());
182
204
183
- return response.success ? null : response.error;
205
+ if (response.success) {
206
+ logger.finest ("Instance focused" );
207
+
208
+ return null ;
209
+ } else {
210
+ logger.finest ('Failed to focus instance' , response.error);
211
+
212
+ return response.error;
213
+ }
184
214
} catch (e) {
215
+ logger.finest ('Failed to focus instance' , e);
185
216
return e.toString ();
186
217
}
187
218
}
0 commit comments