Alan Viverette | 3da604b | 2020-06-10 18:34:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.server.backup.transport; |
| 18 | |
| 19 | import android.annotation.IntDef; |
| 20 | import android.annotation.Nullable; |
| 21 | import android.os.DeadObjectException; |
| 22 | import android.util.Log; |
| 23 | import android.util.Slog; |
| 24 | |
| 25 | import com.android.internal.backup.IBackupTransport; |
| 26 | |
| 27 | import java.lang.annotation.Retention; |
| 28 | import java.lang.annotation.RetentionPolicy; |
| 29 | |
| 30 | /** Utility methods for transport-related operations. */ |
| 31 | public class TransportUtils { |
| 32 | private static final String TAG = "TransportUtils"; |
| 33 | |
| 34 | /** |
| 35 | * Throws {@link TransportNotAvailableException} if {@param transport} is null. The semantics is |
| 36 | * similar to a {@link DeadObjectException} coming from a dead transport binder. |
| 37 | */ |
| 38 | public static IBackupTransport checkTransportNotNull(@Nullable IBackupTransport transport) |
| 39 | throws TransportNotAvailableException { |
| 40 | if (transport == null) { |
| 41 | log(Priority.ERROR, TAG, "Transport not available"); |
| 42 | throw new TransportNotAvailableException(); |
| 43 | } |
| 44 | return transport; |
| 45 | } |
| 46 | |
| 47 | static void log(@Priority int priority, String tag, String message) { |
| 48 | if (priority == Priority.WTF) { |
| 49 | Slog.wtf(tag, message); |
| 50 | } else if (Log.isLoggable(tag, priority)) { |
| 51 | Slog.println(priority, tag, message); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | static String formatMessage(@Nullable String prefix, @Nullable String caller, String message) { |
| 56 | StringBuilder string = new StringBuilder(); |
| 57 | if (prefix != null) { |
| 58 | string.append(prefix).append(" "); |
| 59 | } |
| 60 | if (caller != null) { |
| 61 | string.append("[").append(caller).append("] "); |
| 62 | } |
| 63 | return string.append(message).toString(); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Create our own constants so we can log WTF using the same APIs. Except for {@link |
| 68 | * Priority#WTF} all the others have the same value, so can be used directly |
| 69 | */ |
| 70 | @IntDef({Priority.VERBOSE, Priority.DEBUG, Priority.INFO, Priority.WARN, Priority.WTF}) |
| 71 | @Retention(RetentionPolicy.SOURCE) |
| 72 | @interface Priority { |
| 73 | int VERBOSE = Log.VERBOSE; |
| 74 | int DEBUG = Log.DEBUG; |
| 75 | int INFO = Log.INFO; |
| 76 | int WARN = Log.WARN; |
| 77 | int ERROR = Log.ERROR; |
| 78 | int WTF = -1; |
| 79 | } |
| 80 | |
| 81 | private TransportUtils() {} |
| 82 | } |