diff --git a/lib/api/core.dart b/lib/api/core.dart index aa14f2d114..9826151182 100644 --- a/lib/api/core.dart +++ b/lib/api/core.dart @@ -1,8 +1,9 @@ import 'dart:convert'; -import 'package:flutter/foundation.dart'; import 'package:http/http.dart' as http; +import '../log.dart'; + class Auth { Auth({required this.realmUrl, required this.email, required this.apiKey}) : assert(realmUrl.query.isEmpty && realmUrl.fragment.isEmpty); @@ -74,7 +75,7 @@ class LiveApiConnection extends ApiConnection { assert(_isOpen); final url = auth.realmUrl.replace( path: "/api/v1/$route", queryParameters: encodeParameters(params)); - if (kDebugMode) print("GET $url"); + assert(debugLog("GET $url")); final response = await _client.get(url, headers: _headers()); if (response.statusCode != 200) { throw Exception("error on GET $route: status ${response.statusCode}"); diff --git a/lib/log.dart b/lib/log.dart new file mode 100644 index 0000000000..2f8de60963 --- /dev/null +++ b/lib/log.dart @@ -0,0 +1,32 @@ + +/// Whether [debugLog] should do anything. +/// +/// This has an effect only in a debug build. +bool debugLogEnabled = false; + +/// Print a log message, if debug logging is enabled. +/// +/// In a debug build, if [debugLogEnabled] is true, this prints the given +/// message to the log. Otherwise it does nothing. +/// +/// Typically we set [debugLogEnabled] so that this will print when running +/// the app in a debug build, but not when running tests. +/// +/// Call sites of this function should be enclosed in `assert` expressions, so +/// that any interpolation to construct the message happens only in debug mode. +/// To help make that convenient, this function always returns true. +/// +/// Example usage: +/// ```dart +/// assert(debugLog("Got frobnitz: $frobnitz")); +/// ``` +bool debugLog(String message) { + assert(() { + // TODO(log): make it convenient to enable these logs in tests for debugging a failing test + if (debugLogEnabled) { + print(message); // ignore: avoid_print + } + return true; + }()); + return true; +} diff --git a/lib/main.dart b/lib/main.dart index 7d3ca8d6b8..74acbd678f 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,7 +1,12 @@ import 'package:flutter/material.dart'; +import 'log.dart'; import 'widgets/app.dart'; void main() { + assert(() { + debugLogEnabled = true; + return true; + }()); runApp(const ZulipApp()); }