001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020package org.apache.reef.util.logging;
021
022import org.apache.reef.tang.JavaConfigurationBuilder;
023import org.apache.reef.tang.Tang;
024import org.apache.reef.tang.annotations.Parameter;
025import org.apache.reef.wake.time.event.StartTime;
026
027import javax.inject.Inject;
028import java.util.logging.Level;
029import java.util.logging.Logger;
030
031/**
032 * Create Logging scope objects
033 */
034public class LoggingScopeFactory {
035
036  private static final Logger LOG = Logger.getLogger(LoggingScopeFactory.class.getName());
037  public static final String DRIVER_START = "Driver Start Handler";
038  public static final String DRIVER_STOP = "Driver Stop Handler";
039  public static final String BRIDGE_SETUP = "Bridge setup";
040  public static final String LOAD_LIB = "Load libraries";
041  public static final String EVALUATOR_REQUESTOR = "Evaluator requestor passed to C#";
042  public static final String EVALUATOR_BRIDGE_SUBMIT = "Evaluator request submit cross bridge";
043  public static final String EVALUATOR_SUBMIT = "Evaluator submit";
044  public static final String EVALUATOR_LAUNCH = "Evaluator launch";
045  public static final String EVALUATOR_ALLOCATED = "Evaluator allocated";
046  public static final String EVALUATOR_COMPLETED = "Evaluator completed";
047  public static final String EVALUATOR_FAILED = "Evaluator failed";
048  public static final String ACTIVE_CONTEXT = "Active context created";
049  public static final String TASK_RUNNING = "Task running";
050  public static final String TASK_COMPLETE = "Task complete";
051  public static final String TASK_MESSAGE = "Task message";
052  public static final String CONTEXT_MESSAGE = "Context message";
053  public static final String CONTEXT_CLOSE = "Context close";
054  public static final String DRIVER_RESTART = "Driver restart";
055  public static final String DRIVER_RESTART_COMPLETE = "Driver restart complete";
056  public static final String DRIVER_RESTART_RUNNING_TASK = "Driver restart running task";
057  public static final String DRIVER_RESTART_ACTIVE_CONTEXT = "Driver restart active context";
058  public static final String TASK_SUSPEND = "Task suspend";
059  public static final String DRIVER_SUBMIT = "Driver submit";
060  public static final String REEF_SUBMIT = "Reef submit";
061  public static final String LOCAL_JOB_SUBMIT = "Local job submit";
062  public static final String HTTP_REQUEST = "Http request";
063  public static final String HTTP_SERVER = "Http server";
064
065  /**
066   * Log level. Client can set it through LogLevelName named parameter
067   */
068  private final Level logLevel;
069
070  /**
071   * User can inject a LoggingScopeFactory with injected log level as a string
072   */
073  @Inject
074  private LoggingScopeFactory(@Parameter(LogLevelName.class) final String logLevelName) {
075    this.logLevel = Level.parse(logLevelName);
076  }
077
078  /**
079   * Get a new instance of LoggingScope with specified log level
080   * @param logLevel
081   * @param msg
082   * @return
083   */
084  public static LoggingScope getNewLoggingScope(final Level logLevel, final String msg) {
085    return new LoggingScopeImpl(LOG, logLevel, msg);
086  }
087
088  /**
089   * Get a new instance of LoggingScope with injected LoggingScopeFactory instance
090   * @param msg
091   * @return
092   */
093  public LoggingScope getNewLoggingScope(final String msg) {
094    return new LoggingScopeImpl(LOG, logLevel, msg);
095  }
096
097  /**
098   * Get a new instance of LoggingScope with msg and params through new
099   * @param msg
100   * @param params
101   * @return
102   */
103  public LoggingScope getNewLoggingScope(final String msg, final Object params[]) {
104    return new LoggingScopeImpl(LOG, logLevel, msg, params);
105  }
106
107  /**
108   * The method is to measure the time used to start the driver. It can be inserted to the code between start driver till it is started
109   * @param startTime
110   * @return
111   */
112  public LoggingScope driverStart(final StartTime startTime) {
113    return new LoggingScopeImpl(LOG, logLevel, DRIVER_START + " :" + startTime);
114  }
115
116  /**
117   * The method is to measure the time used to stop the driver. It can be inserted to the code between start driver stop till it is stopped
118   * @param timeStamp
119   * @return
120   */
121  public LoggingScope driverStop(final long timeStamp) {
122    return new LoggingScopeImpl(LOG, logLevel, this.DRIVER_STOP + " :" + timeStamp);
123  }
124
125  /**
126   * The method is to measure the time used to set up Java CRL bridge. It can be inserted to the code between beginning of bridge set up and the end of it
127   * @return
128   */
129  public LoggingScope setupBridge() {
130    return new LoggingScopeImpl(LOG, logLevel, BRIDGE_SETUP);
131  }
132
133  /**
134   * The method is to measure the time used to load global files and libraries
135   * @return
136   */
137  public LoggingScope loadLib() {
138    return new LoggingScopeImpl(LOG, logLevel, LOAD_LIB);
139  }
140
141  /**
142   * The method is to measure the time used to pass EvaluatorRequestor from Java to .Net. It can be inserted to the code between beginning to send EvaluatorRequestor to CLR until it is returned.
143   * @return
144   */
145  public LoggingScope evaluatorRequestorPassToCs() {
146    return new LoggingScopeImpl(LOG, logLevel, EVALUATOR_REQUESTOR);
147  }
148
149  /**
150   * The method is to measure the time used to submit Evaluator request from CLR to Java driver. It can be inserted to evaluator submit() method.
151   * @param evaluatorsNumber
152   * @return
153   */
154  public LoggingScope evaluatorRequestSubmitToJavaDriver(final int evaluatorsNumber) {
155    return new LoggingScopeImpl(LOG, logLevel, EVALUATOR_BRIDGE_SUBMIT + ":" + evaluatorsNumber);
156  }
157
158  /**
159   * The method is to measure the time used to submit a Evaluator request at java side
160   * @param evaluatorNumber
161   * @return
162   */
163  public LoggingScope evaluatorSubmit(final int evaluatorNumber) {
164    return new LoggingScopeImpl(LOG, logLevel, EVALUATOR_SUBMIT + ":" + evaluatorNumber);
165  }
166
167  /**
168   * This is to measure the time on evaluatorAllocated handler
169   * @param evaluatorId
170   * @return
171   */
172  public LoggingScope evaluatorAllocated(final String evaluatorId) {
173    return new LoggingScopeImpl(LOG, logLevel, EVALUATOR_ALLOCATED + " :" + evaluatorId);
174  }
175
176  /**
177   * This is to measure the time to launch an evaluator
178   * @param evaluatorId
179   * @return
180   */
181  public LoggingScope evaluatorLaunch(final String evaluatorId) {
182    return new LoggingScopeImpl(LOG, logLevel, EVALUATOR_LAUNCH + " :" + evaluatorId);
183  }
184
185  /**
186   * This is to measure the time in calling evaluatorCompleted handler
187   * @param evaluatorId
188   * @return
189   */
190  public LoggingScope evaluatorCompleted(final String evaluatorId) {
191    return new LoggingScopeImpl(LOG, logLevel, EVALUATOR_COMPLETED + " :" + evaluatorId);
192  }
193
194  /**
195   * This is to measure the time in calling evaluatorFailed handler
196   * @param evaluatorId
197   * @return
198   */
199  public LoggingScope evaluatorFailed(final String evaluatorId) {
200    return new LoggingScopeImpl(LOG, logLevel, this.EVALUATOR_FAILED + " :" + evaluatorId);
201  }
202
203  /**
204   * This is to measure the time in calling activeContext handler
205   * @param contextId
206   * @return
207   */
208  public LoggingScope activeContextReceived(final String contextId) {
209    return new LoggingScopeImpl(LOG, logLevel, ACTIVE_CONTEXT + " :" + contextId);
210  }
211
212  /**
213   * This is to measure the time in calling closedContext handler
214   * @param contextId
215   * @return
216   */
217  public LoggingScope closedContext(final String contextId) {
218    return new LoggingScopeImpl(LOG, logLevel, this.CONTEXT_CLOSE + " :" + contextId);
219  }
220
221  /**
222   * This is to measure the time in calling runningTaskHandler
223   * @param taskId
224   * @return
225   */
226  public LoggingScope taskRunning(final String taskId) {
227    return new LoggingScopeImpl(LOG, logLevel, TASK_RUNNING + " :" + taskId);
228  }
229
230  /**
231   * This is to measure the time in calling taskCompletedHandler
232   * @param taskId
233   * @return
234   */
235  public LoggingScope taskCompleted(final String taskId) {
236    return new LoggingScopeImpl(LOG, logLevel, TASK_COMPLETE + " :" + taskId);
237  }
238
239  /**
240   * This is to measure the time in calling taskSuspendedHandler
241   * @param taskId
242   * @return
243   */
244  public LoggingScope taskSuspended(final String taskId) {
245    return new LoggingScopeImpl(LOG, logLevel, TASK_SUSPEND + " :" + taskId);
246  }
247
248  /**
249   * This is to measure the time in calling taskMessageReceivedHandler
250   * @param msg
251   * @return
252   */
253  public LoggingScope taskMessageReceived(final String msg) {
254    return new LoggingScopeImpl(LOG, logLevel, TASK_MESSAGE + " :" + msg);
255  }
256
257  /**
258   * This is to measure the time in calling contextMessageReceivedHandler
259   * @param msg
260   * @return
261   */
262  public LoggingScope contextMessageReceived(final String msg) {
263    return new LoggingScopeImpl(LOG, logLevel, CONTEXT_MESSAGE + " :" + msg);
264  }
265
266  /**
267   * This is to measure the time in calling driverRestartHandler
268   * @param startTime
269   * @return
270   */
271  public LoggingScope driverRestart(final StartTime startTime) {
272    return new LoggingScopeImpl(LOG, logLevel, DRIVER_RESTART + " :" + startTime);
273  }
274
275  /**
276   * This is to measure the time in calling driverRestartCompletedHandler
277   * @param timeStamp
278   * @return
279   */
280  public LoggingScope driverRestartCompleted(final long timeStamp) {
281    return new LoggingScopeImpl(LOG, logLevel, DRIVER_RESTART_COMPLETE + " :" + timeStamp);
282  }
283
284  /**
285   * This is to measure the time in calling driverRestartRunningTaskHandler
286   * @param taskId
287   * @return
288   */
289  public LoggingScope driverRestartRunningTask(final String taskId) {
290    return new LoggingScopeImpl(LOG, logLevel, DRIVER_RESTART_RUNNING_TASK + " :" + taskId);
291  }
292
293  /**
294   * This is to measure the time in calling driverRestartActiveContextReceivedHandler
295   * @param contextId
296   * @return
297   */
298  public LoggingScope driverRestartActiveContextReceived(final String contextId) {
299    return new LoggingScopeImpl(LOG, logLevel, DRIVER_RESTART_ACTIVE_CONTEXT + " :" + contextId);
300  }
301
302  /**
303   * This is to measure the time in handling a http request
304   * @param uri
305   * @return
306   */
307  public LoggingScope httpRequest(final String uri) {
308    return new LoggingScopeImpl(LOG, logLevel, this.HTTP_REQUEST + " :" + uri);
309  }
310
311  /**
312   * This is to measure the time used to create HttpServer
313   * @return
314   */
315  public LoggingScope httpServer() {
316    return new LoggingScopeImpl(LOG, logLevel, this.HTTP_SERVER);
317  }
318
319  /**
320   * This is to measure the time to submit a driver
321   * @param submitDriver
322   * @return
323   */
324  public LoggingScope driverSubmit(final Boolean submitDriver) {
325    return new LoggingScopeImpl(LOG, logLevel, DRIVER_SUBMIT + " :" + submitDriver);
326  }
327
328  /**
329   * This is to measure the time to call Reef.Submit
330   * @return
331   */
332  public LoggingScope reefSubmit() {
333    return new LoggingScopeImpl(LOG, logLevel, this.REEF_SUBMIT);
334  }
335
336  /**
337   * This is to measure the time for a job submission
338   * @return
339   */
340  public LoggingScope localJobSubmission() {
341    return new LoggingScopeImpl(LOG, logLevel, this.LOCAL_JOB_SUBMIT);
342  }
343}