1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.camera.stats.profiler; 18 19 import com.android.camera.debug.Log; 20 import com.android.camera.debug.Log.Tag; 21 22 /** 23 * A set of common, easy to use profilers that fill the most 24 * common use cases when profiling parts of an app. 25 */ 26 public class Profilers { 27 private static final Tag TAG = new Tag("Profiler"); 28 29 private static Writer sErrorWriter = new ErrorWriter(); 30 private static Writer sWarningWriter = new WarningWriter(); 31 private static Writer sInfoWriter = new InfoWriter(); 32 private static Writer sDebugWriter = new DebugWriter(); 33 private static Writer sVerboseWriter = new VerboseWriter(); 34 35 private static class Singleton { 36 private static final Profilers INSTANCE = new Profilers( 37 new LoggingProfiler(sErrorWriter), 38 new LoggingProfiler(sWarningWriter), 39 new LoggingProfiler(sInfoWriter), 40 new LoggingProfiler(sDebugWriter), 41 new LoggingProfiler(sVerboseWriter), 42 new GuardingProfiler(sInfoWriter, sVerboseWriter)); 43 } 44 45 /** get a single shared Profilers instance */ 46 public static Profilers instance() { 47 return Singleton.INSTANCE; 48 } 49 50 private final LoggingProfiler mErrorProfiler; 51 private final LoggingProfiler mWarningProfiler; 52 private final LoggingProfiler mInfoProfiler; 53 private final LoggingProfiler mDebugProfiler; 54 private final LoggingProfiler mVerboseProfiler; 55 private final GuardingProfiler mGuardingProfiler; 56 57 private Profilers(LoggingProfiler errorProfiler, 58 LoggingProfiler warningProfiler, 59 LoggingProfiler infoProfiler, 60 LoggingProfiler debugProfiler, 61 LoggingProfiler verboseProfiler, 62 GuardingProfiler guardingProfiler) { 63 mErrorProfiler = errorProfiler; 64 mWarningProfiler = warningProfiler; 65 mInfoProfiler = infoProfiler; 66 mDebugProfiler = debugProfiler; 67 mVerboseProfiler = verboseProfiler; 68 mGuardingProfiler = guardingProfiler; 69 } 70 71 public LoggingProfiler e() { 72 return mErrorProfiler; 73 } 74 75 public Profile e(String name) { 76 return e().create(name).start(); 77 } 78 79 public LoggingProfiler w() { 80 return mWarningProfiler; 81 } 82 83 public Profile w(String name) { 84 return w().create(name).start(); 85 } 86 87 public LoggingProfiler i() { 88 return mInfoProfiler; 89 } 90 91 public Profile i(String name) { 92 return i().create(name).start(); 93 } 94 95 public LoggingProfiler d() { 96 return mDebugProfiler; 97 } 98 99 public Profile d(String name) { 100 return d().create(name).start(); 101 } 102 103 public LoggingProfiler v() { 104 return mVerboseProfiler; 105 } 106 107 public Profile v(String name) { 108 return v().create(name).start(); 109 } 110 111 public GuardingProfiler guard() { 112 return mGuardingProfiler; 113 } 114 115 public Profile guard(String name) { 116 return guard().create(name).start(); 117 } 118 119 public Profile guard(String name, int durationMillis) { 120 return guard().create(name, durationMillis).start(); 121 } 122 123 private static class DebugWriter implements Writer { 124 @Override 125 public void write(String message) { 126 Log.d(TAG, message); 127 } 128 } 129 130 private static class ErrorWriter implements Writer { 131 @Override 132 public void write(String message) { 133 Log.e(TAG, message); 134 } 135 } 136 137 private static class InfoWriter implements Writer { 138 @Override 139 public void write(String message) { 140 Log.i(TAG, message); 141 } 142 } 143 144 private static class VerboseWriter implements Writer { 145 @Override 146 public void write(String message) { 147 Log.v(TAG, message); 148 } 149 } 150 151 private static class WarningWriter implements Writer { 152 @Override 153 public void write(String message) { 154 Log.w(TAG, message); 155 } 156 } 157 } 158