Skip to content

log: Do some logging only in debug app, not in tests #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/api/core.dart
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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}");
Expand Down
32 changes: 32 additions & 0 deletions lib/log.dart
Original file line number Diff line number Diff line change
@@ -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;
}
5 changes: 5 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -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());
}