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.
- fx set PRODUCT.BOARD --with //examples/hello_world
Note: To see a list of possible products, run:
To see a list of possible boards, run:
- fx list-products
- fx list-boards
Edit the component package {#edit-component-package}
When connecting your component to an additional service, you need to do the following:
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.
Open the
BUILD.gn
in your chosen text editor.vi ~/fuchsia/examples/hello_world/rust/BUILD.gn
Add
"//src/lib/syslog/rust:syslog"
to the dependencies array in therustc_binary
target, which defines the executable.After adding this dependency, the
rustc_binary
in yourBUILD.gn
should look like this:…
rustc_binary("bin") {
name = "hello_world_rust"
with_unit_tests = true
edition = "2018"
deps = [
"//src/lib/syslog/rust:syslog",
]
test_deps = [ "//garnet/public/rust/fuchsia-async" ]
}
…
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
.
Open the source file,
main.rs
, with your chosen text editor.vi ~/fuchsia/examples/hello_world/rust/src/main.rs
Add a
use
declaration for thefuchsia_syslog
crate.use fuchsia_syslog as syslog
Within
main()
, initialize thefuchsia_syslog
crate.syslog::init().expect("should not fail");
Within
main()
, add your log message.syslog::fx_log_info!("{}, log!", greeting());
At this point,
main.rs
should look like this:use fuchsia_syslog as syslog;
fn main() {
syslog::init().expect("should not fail");
syslog::fx_log_info!("{}, log!", greeting());
println!("{}, world!", greeting());
}
…
Edit the component manifest {#edit-the-component-manifest}
Execute a build of the Fuchsia image that contains your modified component package.
fx build
Test logging {#test-logging}
Ensure that
fx serve
is running in a shell tab. If it is not, open a shell tab and runfx serve
.cd ~/fuchsia
fx serve
In a new shell tab, navigate to your
fuchsia
directory and runfx log
.cd ~/fuchsia
fx log
In a new shell tab, navigate to your fuchsia directory and run the
hello_world_rust
component:cd ~/fuchsia
fx shell run fuchsia-pkg://fuchsia.com/hello-world-rust#meta/hello-world-rust.cmx
":hello-world-rust-component-v1"
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!
.