Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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.internal.util; |
| 18 | |
| 19 | import android.os.Handler; |
| 20 | |
| 21 | import java.io.PrintWriter; |
| 22 | import java.io.StringWriter; |
| 23 | |
| 24 | /** |
| 25 | * Helper functions for dumping the state of system services. |
| 26 | */ |
| 27 | public final class DumpUtils { |
| 28 | private DumpUtils() { |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Helper for dumping state owned by a handler thread. |
| 33 | * |
| 34 | * Because the caller might be holding an important lock that the handler is |
| 35 | * trying to acquire, we use a short timeout to avoid deadlocks. The process |
| 36 | * is inelegant but this function is only used for debugging purposes. |
| 37 | */ |
| 38 | public static void dumpAsync(Handler handler, final Dump dump, PrintWriter pw, long timeout) { |
| 39 | final StringWriter sw = new StringWriter(); |
| 40 | if (handler.runWithScissors(new Runnable() { |
| 41 | @Override |
| 42 | public void run() { |
| 43 | PrintWriter lpw = new PrintWriter(sw); |
| 44 | dump.dump(lpw); |
| 45 | lpw.close(); |
| 46 | } |
| 47 | }, timeout)) { |
| 48 | pw.print(sw.toString()); |
| 49 | } else { |
| 50 | pw.println("... timed out"); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public interface Dump { |
| 55 | void dump(PrintWriter pw); |
| 56 | } |
| 57 | } |