Skip to content

Commit 9dceba6

Browse files
committed
Add an example for event logging
1 parent e4f3909 commit 9dceba6

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ version = "0.0.0"
55
authors = [ "The Servo Project Developers" ]
66

77
[features]
8+
9+
# Enable event logging for generating benchmark traces.
10+
# See examples/event-log.
811
log-events = []
912

1013
[dependencies.string_cache_macros]

examples/event-log/Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
3+
name = "string-cache-event-log-example"
4+
version = "0.0.0"
5+
authors = [ "The Servo Project Developers" ]
6+
7+
[dependencies.string_cache]
8+
git = "https://github.com/servo/string-cache"
9+
features = ["log-events"]

examples/event-log/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
string-cache can record logs of what it's doing, which can be useful for
2+
guiding future changes to the library. This project demonstrates how to build
3+
string-cache with logging enabled (see `Cargo.toml`), and how to access the log
4+
at runtime.

examples/event-log/src/main.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2014 The Servo Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution.
3+
//
4+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7+
// option. This file may not be copied, modified, or distributed
8+
// except according to those terms.
9+
10+
extern crate string_cache;
11+
12+
use string_cache::Atom;
13+
use string_cache::event;
14+
15+
use std::io;
16+
17+
fn main() {
18+
println!("Reading stdin to end of file");
19+
let stdin = io::stdin().read_to_string().unwrap();
20+
let mut atoms = vec![];
21+
for word in stdin.as_slice().split(|c: char| c.is_whitespace()) {
22+
atoms.push(Atom::from_slice(word));
23+
}
24+
25+
let log = event::LOG.lock();
26+
27+
println!("Created {:u} atoms, logged {:u} events:", atoms.len(), log.len());
28+
for e in log.iter() {
29+
println!("{}", e);
30+
}
31+
}

0 commit comments

Comments
 (0)