blob: c2dcf629145fada8e21e5367270b39237671e192 [file] [log] [blame]
Darin Petkovbd0dcc82012-11-29 10:51:12 +01001// 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 Petkov38066762012-12-17 15:35:45 +01005#include "shill/shims/net_diags_upload.h"
6
Darin Petkovbd0dcc82012-11-29 10:51:12 +01007#include <stdlib.h>
8
Darin Petkov38066762012-12-17 15:35:45 +01009#include <string>
10
Darin Petkovfac01f02012-12-06 12:44:07 +010011#include <base/at_exit.h>
12#include <base/command_line.h>
13#include <base/logging.h>
Darin Petkov38066762012-12-17 15:35:45 +010014#include <base/stringprintf.h>
Darin Petkovfac01f02012-12-06 12:44:07 +010015#include <chromeos/syslog_logging.h>
16
Darin Petkov38066762012-12-17 15:35:45 +010017using std::string;
18
Darin Petkovfac01f02012-12-06 12:44:07 +010019namespace switches {
20
21static const char kHelp[] = "help";
22static const char kUpload[] = "upload";
23
24// The help message shown if help flag is passed to the program.
25static 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
34namespace shill {
35
Darin Petkov38066762012-12-17 15:35:45 +010036namespace shims {
37
Darin Petkovfac01f02012-12-06 12:44:07 +010038void 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 Petkov38066762012-12-17 15:35:45 +010042 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 Petkovfac01f02012-12-06 12:44:07 +010046 LOG(ERROR) << "Unable to stash net.log.";
47 } else {
48 LOG(INFO) << "net.log stashed.";
49 }
50}
51
Darin Petkov38066762012-12-17 15:35:45 +010052} // namespace shims
53
Darin Petkovfac01f02012-12-06 12:44:07 +010054} // namespace shill
55
Darin Petkovbd0dcc82012-11-29 10:51:12 +010056int main(int argc, char **argv) {
Darin Petkovfac01f02012-12-06 12:44:07 +010057 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 Petkov38066762012-12-17 15:35:45 +010068 shill::shims::StashLogs();
Darin Petkovfac01f02012-12-06 12:44:07 +010069
70 if (cl->HasSwitch(switches::kUpload)) {
71 // Crash so that crash_reporter can upload the logs.
72 abort();
73 }
74
75 return 0;
Darin Petkovbd0dcc82012-11-29 10:51:12 +010076}