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.
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.
Note: To see a list of possible products, run: fx list-products To see a list of possible boards, run: fx list-boards
When connecting your component to an additional service, you need to do the following:
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 the rustc_binary
target, which defines the executable.
After adding this dependency, the rustc_binary
in your BUILD.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" ] } …
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 the fuchsia_syslog
crate.
use fuchsia_syslog as syslog
Within main()
, initialize the fuchsia_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()); } …
Execute a build of the Fuchsia image that contains your modified component package.
fx build
Ensure that fx serve
is running in a shell tab. If it is not, open a shell tab and run fx serve
.
cd ~/fuchsia
fx serve
In a new shell tab, navigate to your fuchsia
directory and run fx 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
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!
.