Skip to content

Commit 54fac78

Browse files
authored
Add error logging to GLFW and Flutter setup (#243)
Add error logging when starting the Flutter engine fails (on all platforms), and on GLFW errors. Also exit early with error logging for unrecoverable errors in the GLFW implementation. Fixes #241.
1 parent 3c276ec commit 54fac78

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

example/linux_fde/flutter_embedder_example.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ std::string GetExecutableDirectory() {
5454

5555
int main(int argc, char **argv) {
5656
if (!flutter_desktop_embedding::FlutterInit()) {
57-
std::cerr << "Couldn't init GLFW" << std::endl;
57+
std::cerr << "Unable to init GLFW; exiting." << std::endl;
58+
return EXIT_FAILURE;
5859
}
5960

6061
// Resources are located relative to the executable.
@@ -76,6 +77,7 @@ int main(int argc, char **argv) {
7677
640, 480, assets_path, icu_data_path, arguments);
7778
if (window == nullptr) {
7879
flutter_desktop_embedding::FlutterTerminate();
80+
std::cerr << "Unable to create Flutter window; exiting." << std::endl;
7981
return EXIT_FAILURE;
8082
}
8183

example/windows_fde/flutter_embedder_example.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
int main(int argc, char **argv) {
2121
if (!flutter_desktop_embedding::FlutterInit()) {
22-
std::cout << "Couldn't init GLFW" << std::endl;
22+
std::cerr << "Unable to init GLFW; exiting." << std::endl;
23+
return EXIT_FAILURE;
2324
}
2425
// Arguments for the Flutter Engine.
2526
std::vector<std::string> arguments;
@@ -33,6 +34,7 @@ int main(int argc, char **argv) {
3334
"..\\..\\library\\windows\\dependencies\\engine\\icudtl.dat", arguments);
3435
if (window == nullptr) {
3536
flutter_desktop_embedding::FlutterTerminate();
37+
std::cerr << "Unable to create Flutter window; exiting." << std::endl;
3638
return EXIT_FAILURE;
3739
}
3840

library/common/glfw/embedder.cc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ static void *GLFWProcResolver(void *user_data, const char *name) {
239239
return reinterpret_cast<void *>(glfwGetProcAddress(name));
240240
}
241241

242+
static void GLFWErrorCallback(int error_code, const char *description) {
243+
std::cerr << "GLFW error " << error_code << ": " << description << std::endl;
244+
}
245+
242246
// Spins up an instance of the Flutter Engine.
243247
//
244248
// This function launches the Flutter Engine in a background thread, supplying
@@ -276,17 +280,21 @@ static FlutterEngine RunFlutterEngine(
276280
auto result =
277281
FlutterEngineRun(FLUTTER_ENGINE_VERSION, &config, &args, window, &engine);
278282
if (result != kSuccess || engine == nullptr) {
283+
std::cerr << "Failed to start Flutter engine: error " << result
284+
<< std::endl;
279285
return nullptr;
280286
}
281287
return engine;
282288
}
283289

284290
namespace flutter_desktop_embedding {
285291

286-
// Initialize glfw
287-
bool FlutterInit() { return glfwInit(); }
292+
bool FlutterInit() {
293+
// Before making any GLFW calls, set up a logging error handler.
294+
glfwSetErrorCallback(GLFWErrorCallback);
295+
return glfwInit();
296+
}
288297

289-
// Tear down glfw
290298
void FlutterTerminate() { glfwTerminate(); }
291299

292300
PluginRegistrar *GetRegistrarForPlugin(GLFWwindow *flutter_window,

library/macos/FLEViewController.m

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,14 @@ - (BOOL)launchEngineInternalWithAssetsPath:(NSURL *)assets
297297
flutterArguments.command_line_argv = argv;
298298
flutterArguments.platform_message_callback = (FlutterPlatformMessageCallback)OnPlatformMessage;
299299

300-
BOOL result = FlutterEngineRun(FLUTTER_ENGINE_VERSION, &config, &flutterArguments,
301-
(__bridge void *)(self), &_engine) == kSuccess;
300+
FlutterResult result = FlutterEngineRun(FLUTTER_ENGINE_VERSION, &config, &flutterArguments,
301+
(__bridge void *)(self), &_engine);
302302
free(argv);
303-
return result;
303+
if (result != kSuccess) {
304+
NSLog(@"Failed to start Flutter engine: error %d", result);
305+
return NO;
306+
}
307+
return YES;
304308
}
305309

306310
+ (FlutterRendererConfig)createRenderConfigHeadless:(BOOL)headless {

0 commit comments

Comments
 (0)