HLCPP object pretty printing

Prerequisites

This tutorial builds on the Compiling FIDL tutorial. For the full set of FIDL tutorials, refer to the overview.

Overview

This tutorial walks through how to add debug printing to HLCPP types, using the fostr tool.

If you want to write the code yourself, delete the following directories:

rm -r examples/fidl/hlcpp/fostr farnet/public/lib/fostr/fidl

This tutorial contains the following steps:

  • Define a fostr_fidl target for a FIDL library. This will allow debug printing for types in the library by defining an operator<< for each type.
  • Use the fostr_fidl target.
  • Write examples with the formatter code.

Define fostr_fidl target

  1. Create a directory for the BUILD file inside //src/lib/fostr/fidl:

    mkdir -p src/lib/fostr/fidl/fuchsia.examples
    
  2. Create the BUILD file:

    touch src/lib/fostr/fidl/fuchsia.examples/BUILD.gn
    
  3. Add the following definition to the build file:

    # Copyright 2020 The Fuchsia Authors. All rights reserved.
    # Use of this source code is governed by a BSD-style license that can be
    # found in the LICENSE file.
    
    import("//src/lib/fostr/build/fostr_fidl.gni")
    fostr_fidl("fuchsia.examples") {
      fidl_target = "//examples/fidl/fuchsia.examples"
    }
    
    

This defines a fostr target for the fuchsia.examples library. Any dependencies to the library needs to be specified in the build file as well, which doesn't apply to this tutorial as it has no dependencies. For more complex examples, refer to the existing definitions in the fostr fidl directory.

Add a dependency on the fostr_fidl target

The target library that the tutorial uses is a simple host test, which is located in examples/fidl/hlcpp/fostr. Notice that the BUILD file for the test includes the fostr_fidl target as a dependency:

executable("bin") {
  testonly = true
  output_name = "fostr_example_test"
  sources = [ "main.cc" ]
  deps = [
    "//examples/fidl/fuchsia.examples:fuchsia.examples_hlcpp",
    "//src/lib/fostr/fidl/fuchsia.examples",
    "//src/lib/fxl/test:gtest_main",
  ]
}

The included library automatically overloads the << operator. The path is based on the name of the FIDL library it is generated for:

#include <src/lib/fostr/fidl/fuchsia/examples/formatting.h>

Write examples using the formatter code

Write some placeholder tests to show off the output:

TEST(FidlExamples, Bits) {
  auto flags = fuchsia::examples::FileMode::READ | fuchsia::examples::FileMode::WRITE;
  std::cout << flags << std::endl;
}

TEST(FidlExamples, Enums) {
  auto enum_val = fuchsia::examples::LocationType::MUSEUM;
  std::cout << enum_val << std::endl;
}

TEST(FidlExamples, Structs) {
  fuchsia::examples::Color default_color;
  std::cout << default_color << std::endl;
}

TEST(FidlExamples, Unions) {
  auto int_val = fuchsia::examples::JsonValue::WithIntValue(1);
  std::cout << int_val << std::endl;
}

Run the example

To run the example:

  1. Configure the GN build to include the example:

    fx set core.x64  --with //examples/fidl/hlcpp/fostr
    
  2. Run the test:

    fx test -vo fostr-example-test