OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/system_logs/debug_log_writer.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/file_util.h" |
| 10 #include "base/files/file.h" |
| 11 #include "chrome/common/logging_chrome.h" |
| 12 #include "chromeos/dbus/dbus_thread_manager.h" |
| 13 #include "chromeos/dbus/debug_daemon_client.h" |
| 14 #include "content/public/browser/browser_thread.h" |
| 15 |
| 16 namespace chromeos { |
| 17 |
| 18 namespace { |
| 19 |
| 20 // Called upon completion of |WriteDebugLogToFile|. Closes file |
| 21 // descriptor, deletes log file in the case of failure and calls |
| 22 // |callback|. |
| 23 void WriteDebugLogToFileCompleted( |
| 24 const DebugLogWriter::StoreDebugLogsCallback& callback, |
| 25 const base::FilePath& file_path, |
| 26 bool succeeded) { |
| 27 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 28 if (!succeeded) { |
| 29 bool posted = content::BrowserThread::PostBlockingPoolTaskAndReply( |
| 30 FROM_HERE, |
| 31 base::Bind(base::IgnoreResult(&base::DeleteFile), file_path, false), |
| 32 base::Bind(callback, file_path, false)); |
| 33 DCHECK(posted); |
| 34 return; |
| 35 } |
| 36 callback.Run(file_path, true); |
| 37 } |
| 38 |
| 39 // Stores into |file_path| debug logs in the .tgz format. Calls |
| 40 // |callback| upon completion. |
| 41 void WriteDebugLogToFile( |
| 42 const DebugLogWriter::StoreDebugLogsCallback& callback, |
| 43 base::File* file, |
| 44 const base::FilePath& file_path) { |
| 45 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 46 if (!file->IsValid()) { |
| 47 LOG(ERROR) << |
| 48 "Can't create debug log file: " << file_path.AsUTF8Unsafe() << ", " << |
| 49 "error: " << file->error_details(); |
| 50 return; |
| 51 } |
| 52 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()->GetDebugLogs( |
| 53 file->Pass(), |
| 54 base::Bind(&WriteDebugLogToFileCompleted, callback, file_path)); |
| 55 } |
| 56 |
| 57 } // namespace |
| 58 |
| 59 // static. |
| 60 void DebugLogWriter::StoreLogs( |
| 61 const base::FilePath& fileshelf, |
| 62 const StoreDebugLogsCallback& callback) { |
| 63 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 64 DCHECK(!callback.is_null()); |
| 65 |
| 66 const base::FilePath::CharType kLogFileName[] = |
| 67 FILE_PATH_LITERAL("debug-log.tgz"); |
| 68 |
| 69 base::FilePath file_path = fileshelf.Append(kLogFileName); |
| 70 file_path = logging::GenerateTimestampedName(file_path, base::Time::Now()); |
| 71 |
| 72 int flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE; |
| 73 base::File* file = new base::File; |
| 74 bool posted = content::BrowserThread::PostBlockingPoolTaskAndReply( |
| 75 FROM_HERE, |
| 76 base::Bind(&base::File::Initialize, |
| 77 base::Unretained(file), file_path, flags), |
| 78 base::Bind(&WriteDebugLogToFile, callback, base::Owned(file), file_path)); |
| 79 DCHECK(posted); |
| 80 } |
| 81 |
| 82 } // namespace chromeos |
OLD | NEW |