blob: 5d335dbb08f360c8f5bce0640bd067d1a5cb5b56 [file] [log] [blame]
/*
* Copyright 2014-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.facebook.buck.event;
import com.google.common.collect.ImmutableMap;
/**
* Helper for creating a trace event with a start and end.
*/
public class TraceEventLogger implements AutoCloseable {
private final BuckEventBus eventBus;
private final String name;
private TraceEventLogger(BuckEventBus eventBus, String name) {
this.eventBus = eventBus;
this.name = name;
}
public static TraceEventLogger start(BuckEventBus eventBus, String name) {
return start(eventBus, name, ImmutableMap.<String, String>of());
}
public static TraceEventLogger start(
BuckEventBus eventBus,
String name,
ImmutableMap<String, String> startProperties) {
eventBus.post(new TraceEvent(name, ChromeTraceEvent.Phase.BEGIN, startProperties));
return new TraceEventLogger(eventBus, name);
}
public void end() {
end(ImmutableMap.<String, String>of());
}
public void end(ImmutableMap<String, String> endProperties) {
eventBus.post(new TraceEvent(name, ChromeTraceEvent.Phase.END, endProperties));
}
@Override
public void close() {
end();
}
}