Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [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.content; |
| 18 | |
Makoto Onuki | 5fc6002 | 2019-01-15 09:34:05 -0800 | [diff] [blame] | 19 | import android.accounts.Account; |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 20 | import android.app.job.JobParameters; |
| 21 | import android.os.Build; |
| 22 | import android.os.Environment; |
| 23 | import android.os.FileUtils; |
| 24 | import android.os.SystemProperties; |
| 25 | import android.text.format.DateUtils; |
Makoto Onuki | 6a6ae04 | 2017-07-20 13:30:12 -0700 | [diff] [blame] | 26 | import android.util.Log; |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 27 | import android.util.Slog; |
| 28 | |
| 29 | import com.android.internal.annotations.GuardedBy; |
Makoto Onuki | 5fc6002 | 2019-01-15 09:34:05 -0800 | [diff] [blame] | 30 | import com.android.server.content.SyncManager.ActiveSyncContext; |
| 31 | import com.android.server.content.SyncStorageEngine.EndPoint; |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 32 | |
| 33 | import libcore.io.IoUtils; |
| 34 | |
| 35 | import java.io.BufferedReader; |
| 36 | import java.io.File; |
| 37 | import java.io.FileReader; |
| 38 | import java.io.FileWriter; |
| 39 | import java.io.IOException; |
| 40 | import java.io.PrintWriter; |
| 41 | import java.io.Reader; |
| 42 | import java.io.Writer; |
| 43 | import java.text.SimpleDateFormat; |
| 44 | import java.util.Arrays; |
| 45 | import java.util.Date; |
| 46 | import java.util.concurrent.TimeUnit; |
| 47 | |
| 48 | /** |
| 49 | * Implements a rotating file logger for the sync manager, which is enabled only on userdebug/eng |
| 50 | * builds (unless debug.synclog is set to 1). |
| 51 | * |
| 52 | * Note this class could be used for other purposes too, but in general we don't want various |
| 53 | * system components to log to files, so it's put in a local package here. |
| 54 | */ |
| 55 | public class SyncLogger { |
| 56 | private static final String TAG = "SyncLogger"; |
| 57 | |
| 58 | private static SyncLogger sInstance; |
| 59 | |
Makoto Onuki | d476430 | 2018-03-30 17:32:57 -0700 | [diff] [blame] | 60 | // Special UID used for logging to denote the self process. |
| 61 | public static final int CALLING_UID_SELF = -1; |
| 62 | |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 63 | SyncLogger() { |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @return the singleton instance. |
| 68 | */ |
| 69 | public static synchronized SyncLogger getInstance() { |
| 70 | if (sInstance == null) { |
Makoto Onuki | d476430 | 2018-03-30 17:32:57 -0700 | [diff] [blame] | 71 | final boolean enable = |
| 72 | Build.IS_DEBUGGABLE |
| 73 | || "1".equals(SystemProperties.get("debug.synclog")) |
| 74 | || Log.isLoggable(TAG, Log.VERBOSE); |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 75 | if (enable) { |
| 76 | sInstance = new RotatingFileLogger(); |
| 77 | } else { |
| 78 | sInstance = new SyncLogger(); |
| 79 | } |
| 80 | } |
| 81 | return sInstance; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Write strings to the log file. |
| 86 | */ |
| 87 | public void log(Object... message) { |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Remove old log files. |
| 92 | */ |
| 93 | public void purgeOldLogs() { |
| 94 | // The default implementation is no-op. |
| 95 | } |
| 96 | |
| 97 | public String jobParametersToString(JobParameters params) { |
| 98 | // The default implementation is no-op. |
| 99 | return ""; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Dump all existing log files into a given writer. |
| 104 | */ |
| 105 | public void dumpAll(PrintWriter pw) { |
| 106 | } |
| 107 | |
| 108 | /** |
Makoto Onuki | bbf6d8c | 2017-08-11 12:11:39 -0700 | [diff] [blame] | 109 | * @return whether log is enabled or not. |
| 110 | */ |
| 111 | public boolean enabled() { |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | /** |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 116 | * Actual implementation which is only used on userdebug/eng builds (by default). |
| 117 | */ |
| 118 | private static class RotatingFileLogger extends SyncLogger { |
| 119 | private final Object mLock = new Object(); |
| 120 | |
| 121 | private final long mKeepAgeMs = TimeUnit.DAYS.toMillis(7); |
| 122 | |
| 123 | private static final SimpleDateFormat sTimestampFormat |
| 124 | = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); |
| 125 | |
| 126 | private static final SimpleDateFormat sFilenameDateFormat |
| 127 | = new SimpleDateFormat("yyyy-MM-dd"); |
| 128 | |
| 129 | @GuardedBy("mLock") |
| 130 | private final Date mCachedDate = new Date(); |
| 131 | |
| 132 | @GuardedBy("mLock") |
| 133 | private final StringBuilder mStringBuilder = new StringBuilder(); |
| 134 | |
| 135 | private final File mLogPath; |
| 136 | |
| 137 | @GuardedBy("mLock") |
| 138 | private long mCurrentLogFileDayTimestamp; |
| 139 | |
| 140 | @GuardedBy("mLock") |
| 141 | private Writer mLogWriter; |
| 142 | |
| 143 | @GuardedBy("mLock") |
| 144 | private boolean mErrorShown; |
| 145 | |
Makoto Onuki | 6a6ae04 | 2017-07-20 13:30:12 -0700 | [diff] [blame] | 146 | private static final boolean DO_LOGCAT = Log.isLoggable(TAG, Log.DEBUG); |
| 147 | |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 148 | RotatingFileLogger() { |
| 149 | mLogPath = new File(Environment.getDataSystemDirectory(), "syncmanager-log"); |
| 150 | } |
| 151 | |
Makoto Onuki | bbf6d8c | 2017-08-11 12:11:39 -0700 | [diff] [blame] | 152 | @Override |
| 153 | public boolean enabled() { |
| 154 | return true; |
| 155 | } |
| 156 | |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 157 | private void handleException(String message, Exception e) { |
| 158 | if (!mErrorShown) { |
| 159 | Slog.e(TAG, message, e); |
| 160 | mErrorShown = true; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public void log(Object... message) { |
| 166 | if (message == null) { |
| 167 | return; |
| 168 | } |
| 169 | synchronized (mLock) { |
| 170 | final long now = System.currentTimeMillis(); |
| 171 | openLogLocked(now); |
| 172 | if (mLogWriter == null) { |
| 173 | return; // Couldn't open log file? |
| 174 | } |
| 175 | |
| 176 | mStringBuilder.setLength(0); |
| 177 | mCachedDate.setTime(now); |
| 178 | mStringBuilder.append(sTimestampFormat.format(mCachedDate)); |
| 179 | mStringBuilder.append(' '); |
| 180 | |
| 181 | mStringBuilder.append(android.os.Process.myTid()); |
| 182 | mStringBuilder.append(' '); |
| 183 | |
Makoto Onuki | 6a6ae04 | 2017-07-20 13:30:12 -0700 | [diff] [blame] | 184 | final int messageStart = mStringBuilder.length(); |
| 185 | |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 186 | for (Object o : message) { |
| 187 | mStringBuilder.append(o); |
| 188 | } |
| 189 | mStringBuilder.append('\n'); |
| 190 | |
| 191 | try { |
| 192 | mLogWriter.append(mStringBuilder); |
| 193 | mLogWriter.flush(); |
Makoto Onuki | 6a6ae04 | 2017-07-20 13:30:12 -0700 | [diff] [blame] | 194 | |
| 195 | // Also write on logcat. |
| 196 | if (DO_LOGCAT) { |
| 197 | Log.d(TAG, mStringBuilder.substring(messageStart)); |
| 198 | } |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 199 | } catch (IOException e) { |
| 200 | handleException("Failed to write log", e); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 205 | @GuardedBy("mLock") |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 206 | private void openLogLocked(long now) { |
| 207 | // If we already have a log file opened and the date has't changed, just use it. |
| 208 | final long day = now % DateUtils.DAY_IN_MILLIS; |
| 209 | if ((mLogWriter != null) && (day == mCurrentLogFileDayTimestamp)) { |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | // Otherwise create a new log file. |
| 214 | closeCurrentLogLocked(); |
| 215 | |
| 216 | mCurrentLogFileDayTimestamp = day; |
| 217 | |
| 218 | mCachedDate.setTime(now); |
| 219 | final String filename = "synclog-" + sFilenameDateFormat.format(mCachedDate) + ".log"; |
| 220 | final File file = new File(mLogPath, filename); |
| 221 | |
| 222 | file.getParentFile().mkdirs(); |
| 223 | |
| 224 | try { |
| 225 | mLogWriter = new FileWriter(file, /* append= */ true); |
| 226 | } catch (IOException e) { |
| 227 | handleException("Failed to open log file: " + file, e); |
| 228 | } |
| 229 | } |
| 230 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 231 | @GuardedBy("mLock") |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 232 | private void closeCurrentLogLocked() { |
| 233 | IoUtils.closeQuietly(mLogWriter); |
| 234 | mLogWriter = null; |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public void purgeOldLogs() { |
| 239 | synchronized (mLock) { |
| 240 | FileUtils.deleteOlderFiles(mLogPath, /* keepCount= */ 1, mKeepAgeMs); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public String jobParametersToString(JobParameters params) { |
Makoto Onuki | 5397be1 | 2017-12-20 16:22:34 +0900 | [diff] [blame] | 246 | return SyncJobService.jobParametersToString(params); |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public void dumpAll(PrintWriter pw) { |
| 251 | synchronized (mLock) { |
| 252 | final String[] files = mLogPath.list(); |
| 253 | if (files == null || (files.length == 0)) { |
| 254 | return; |
| 255 | } |
| 256 | Arrays.sort(files); |
| 257 | |
| 258 | for (String file : files) { |
| 259 | dumpFile(pw, new File(mLogPath, file)); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | private void dumpFile(PrintWriter pw, File file) { |
| 265 | Slog.w(TAG, "Dumping " + file); |
| 266 | final char[] buffer = new char[32 * 1024]; |
| 267 | |
| 268 | try (Reader in = new BufferedReader(new FileReader(file))) { |
| 269 | int read; |
| 270 | while ((read = in.read(buffer)) >= 0) { |
| 271 | if (read > 0) { |
| 272 | pw.write(buffer, 0, read); |
| 273 | } |
| 274 | } |
| 275 | } catch (IOException e) { |
| 276 | } |
| 277 | } |
| 278 | } |
Makoto Onuki | 5fc6002 | 2019-01-15 09:34:05 -0800 | [diff] [blame] | 279 | |
| 280 | static String logSafe(Account account) { |
| 281 | return account == null ? "[null]" : "***/" + account.type; |
| 282 | } |
| 283 | |
| 284 | static String logSafe(EndPoint endPoint) { |
| 285 | return endPoint == null ? "[null]" : endPoint.toSafeString(); |
| 286 | } |
| 287 | |
| 288 | static String logSafe(SyncOperation operation) { |
| 289 | return operation == null ? "[null]" : operation.toSafeString(); |
| 290 | } |
| 291 | |
| 292 | static String logSafe(ActiveSyncContext asc) { |
| 293 | return asc == null ? "[null]" : asc.toSafeString(); |
| 294 | } |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 295 | } |