Add logging to hello world

The following guide discusses adding the Rust Fuchsia logger library to the existing hello world example component. In this guide, development takes place within the Fuchsia source tree.

With the Fuchsia logger library, you can interact with log collection services. You can use Fuchsia’s logging tools to log and analyze services and components that are written to run on Fuchsia.

Prerequisites {#prerequisites}

  • A hardware device that is set up to run Fuchsia.
    • The device should be paved and running. If you haven’t already installed Fuchsia, see Get Started.
  • Rust installed on your environment.
    • The Fuchsia build installs a version of Rust that can be used for Fuchsia development. If you have already built Fuchsia, you don’t need to install Rust again.

Set build to include example {#include-example}

This guide modifies the existing hello world Rust example component. In order to run that component later, you must set the hello world component with the fx tool.

Run fx set, replacing PRODUCT and BOARD with your chosen product and board.

  1. fx set PRODUCT.BOARD --with //examples/hello_world

Note: To see a list of possible products, run:

  1. fx list-products

To see a list of possible boards, run:

  1. fx list-boards

Edit the component package {#edit-component-package}

When connecting your component to an additional service, you need to do the following:

  1. Edit the BUILD.gn.

  2. Edit the source file containing the main().

  3. Edit the component manifest.

Edit the BUILD.gn {#edit-the-buildgn}

You can declare your component’s dependencies and source files in the BUILD.gn.

For more information, see Introduction to GN.

  1. Open the BUILD.gn in your chosen text editor.

    1. vi ~/fuchsia/examples/hello_world/rust/BUILD.gn
  2. Add "//src/lib/syslog/rust:syslog" to the dependencies array in the rustc_binary target, which defines the executable.

    After adding this dependency, the rustc_binary in your BUILD.gn should look like this:

    1. rustc_binary("bin") {
    2. name = "hello_world_rust"
    3. with_unit_tests = true
    4. edition = "2018"
    5. deps = [
    6. "//src/lib/syslog/rust:syslog",
    7. ]
    8. test_deps = [ "//garnet/public/rust/fuchsia-async" ]
    9. }

Edit the source file {#edit-the-source-file}

The source files are included in the src directory of your component’s package. In this guide, the source file is main.rs.

  1. Open the source file, main.rs, with your chosen text editor.

    1. vi ~/fuchsia/examples/hello_world/rust/src/main.rs
  2. Add a use declaration for the fuchsia_syslog crate.

    1. use fuchsia_syslog as syslog
  3. Within main(), initialize the fuchsia_syslog crate.

    1. syslog::init().expect("should not fail");
  4. Within main(), add your log message.

    1. syslog::fx_log_info!("{}, log!", greeting());

    At this point, main.rs should look like this:

    1. use fuchsia_syslog as syslog;
    2. fn main() {
    3. syslog::init().expect("should not fail");
    4. syslog::fx_log_info!("{}, log!", greeting());
    5. println!("{}, world!", greeting());
    6. }

Edit the component manifest {#edit-the-component-manifest}

  1. See the following language-agnostic instructions.

  2. Execute a build of the Fuchsia image that contains your modified component package.

    1. fx build

Test logging {#test-logging}

  1. Ensure that fx serve is running in a shell tab. If it is not, open a shell tab and run fx serve.

    1. cd ~/fuchsia
    1. fx serve
  2. In a new shell tab, navigate to your fuchsia directory and run fx log.

    1. cd ~/fuchsia
    1. fx log
  3. In a new shell tab, navigate to your fuchsia directory and run the hello_world_rust component:

    1. cd ~/fuchsia
    1. fx shell run fuchsia-pkg://fuchsia.com/hello-world-rust#meta/hello-world-rust.cmx
    2. ":hello-world-rust-component-v1"
  4. Navigate to the shell tab where you ran fx log.

    You should be able to see your logging text, which in this example is Hello log!.