Darin Petkov | bd0dcc8 | 2012-11-29 10:51:12 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS 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 | |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 5 | #include "shill/shims/net_diags_upload.h" |
| 6 | |
Darin Petkov | bd0dcc8 | 2012-11-29 10:51:12 +0100 | [diff] [blame] | 7 | #include <stdlib.h> |
| 8 | |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 9 | #include <string> |
| 10 | |
Darin Petkov | fac01f0 | 2012-12-06 12:44:07 +0100 | [diff] [blame] | 11 | #include <base/at_exit.h> |
| 12 | #include <base/command_line.h> |
| 13 | #include <base/logging.h> |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 14 | #include <base/stringprintf.h> |
Darin Petkov | fac01f0 | 2012-12-06 12:44:07 +0100 | [diff] [blame] | 15 | #include <chromeos/syslog_logging.h> |
| 16 | |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 17 | using std::string; |
| 18 | |
Darin Petkov | fac01f0 | 2012-12-06 12:44:07 +0100 | [diff] [blame] | 19 | namespace switches { |
| 20 | |
| 21 | static const char kHelp[] = "help"; |
| 22 | static const char kUpload[] = "upload"; |
| 23 | |
| 24 | // The help message shown if help flag is passed to the program. |
| 25 | static const char kHelpMessage[] = "\n" |
| 26 | "Available Switches:\n" |
| 27 | " --help\n" |
| 28 | " Show this help message.\n" |
| 29 | " --upload\n" |
| 30 | " Upload the diagnostics logs.\n"; |
| 31 | |
| 32 | } // namespace switches |
| 33 | |
| 34 | namespace shill { |
| 35 | |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 36 | namespace shims { |
| 37 | |
Darin Petkov | fac01f0 | 2012-12-06 12:44:07 +0100 | [diff] [blame] | 38 | void StashLogs() { |
| 39 | // Captures the last 10000 lines in the log regardless of log rotation. I.e., |
| 40 | // prints the log files in timestamp sorted order and gets the tail of the |
| 41 | // output. |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 42 | string cmdline = |
| 43 | base::StringPrintf("/bin/cat $(/bin/ls -rt /var/log/net.*log) | " |
| 44 | "/bin/tail -10000 > %s", kStashedNetLog); |
| 45 | if (system(cmdline.c_str())) { |
Darin Petkov | fac01f0 | 2012-12-06 12:44:07 +0100 | [diff] [blame] | 46 | LOG(ERROR) << "Unable to stash net.log."; |
| 47 | } else { |
| 48 | LOG(INFO) << "net.log stashed."; |
| 49 | } |
| 50 | } |
| 51 | |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 52 | } // namespace shims |
| 53 | |
Darin Petkov | fac01f0 | 2012-12-06 12:44:07 +0100 | [diff] [blame] | 54 | } // namespace shill |
| 55 | |
Darin Petkov | bd0dcc8 | 2012-11-29 10:51:12 +0100 | [diff] [blame] | 56 | int main(int argc, char **argv) { |
Darin Petkov | fac01f0 | 2012-12-06 12:44:07 +0100 | [diff] [blame] | 57 | base::AtExitManager exit_manager; |
| 58 | CommandLine::Init(argc, argv); |
| 59 | CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 60 | |
| 61 | if (cl->HasSwitch(switches::kHelp)) { |
| 62 | LOG(INFO) << switches::kHelpMessage; |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | chromeos::InitLog(chromeos::kLogToSyslog | chromeos::kLogHeader); |
| 67 | |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 68 | shill::shims::StashLogs(); |
Darin Petkov | fac01f0 | 2012-12-06 12:44:07 +0100 | [diff] [blame] | 69 | |
| 70 | if (cl->HasSwitch(switches::kUpload)) { |
| 71 | // Crash so that crash_reporter can upload the logs. |
| 72 | abort(); |
| 73 | } |
| 74 | |
| 75 | return 0; |
Darin Petkov | bd0dcc8 | 2012-11-29 10:51:12 +0100 | [diff] [blame] | 76 | } |