OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
Zhen Wang
2015/10/29 19:40:22
Can you also add some comments here to explain why
charliea (OOO until 10-5)
2015/10/29 21:12:11
Done.
| |
5 #include <iostream> | 5 #include <iostream> |
6 | 6 |
7 #include "base/bind.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/synchronization/waitable_event.h" | |
7 #include "tools/battor_agent/battor_agent.h" | 10 #include "tools/battor_agent/battor_agent.h" |
8 | 11 |
9 using std::cout; | 12 using std::cout; |
10 using std::endl; | 13 using std::endl; |
11 using std::string; | 14 using std::string; |
12 | 15 |
13 namespace { | 16 namespace { |
14 | 17 |
18 // An event used to signal that the BattOr Agent has finished executing its | |
19 // command. | |
20 base::WaitableEvent g_stop_tracing_complete_event(false, false); | |
21 | |
22 // A string that contains the trace that the BattOr has finished recording. | |
23 scoped_ptr<std::string> g_trace_output; | |
Zhen Wang
2015/10/29 19:40:22
Is this only for the purpose of printing it out fo
nednguyen
2015/10/29 19:57:32
Yeah, I am not a big fan of global. Can your StopT
charliea (OOO until 10-5)
2015/10/29 21:12:11
Done. We wouldn't have needed the global variable
| |
24 | |
15 void PrintUsage() { | 25 void PrintUsage() { |
16 cout << "Usage: battor_agent <command> <arguments>" << endl << endl | 26 cout << "Usage: battor_agent <command> <arguments>" << endl << endl |
17 << "Commands:" << endl << endl | 27 << "Commands:" << endl << endl |
18 << " StartTracing <path>" << endl | 28 << " StartTracing <path>" << endl |
19 << " StopTracing <path>" << endl | 29 << " StopTracing <path>" << endl |
20 << " SupportsExplicitClockSync <path>" << endl | 30 << " SupportsExplicitClockSync <path>" << endl |
21 << " RecordClockSyncMarker <path> <marker>" << endl | 31 << " RecordClockSyncMarker <path> <marker>" << endl |
22 << " IssueClockSyncMarker <path>" << endl | 32 << " IssueClockSyncMarker <path>" << endl |
23 << " Help" << endl; | 33 << " Help" << endl; |
24 } | 34 } |
25 | 35 |
26 // Retrieves argument argnum from the argument list and stores it into value, | 36 // Retrieves argument argnum from the argument list and stores it into value, |
27 // returning whether the operation was successful and printing the usage | 37 // returning whether the operation was successful and printing the usage |
28 // guidelines if it wasn't. | 38 // guidelines if it wasn't. |
29 bool GetArg(int argnum, int argc, char* argv[], string* value) { | 39 bool GetArg(int argnum, int argc, char* argv[], string* value) { |
30 if (argnum >= argc) { | 40 if (argnum >= argc) { |
31 PrintUsage(); | 41 PrintUsage(); |
32 return false; | 42 return false; |
33 } | 43 } |
34 | 44 |
35 *value = argv[argnum]; | 45 *value = argv[argnum]; |
36 return true; | 46 return true; |
37 } | 47 } |
38 | 48 |
49 void OnStopTracingComplete(scoped_ptr<std::string> trace_output) { | |
50 g_trace_output = trace_output.Pass(); | |
51 g_stop_tracing_complete_event.Signal(); | |
52 } | |
53 | |
39 } // namespace | 54 } // namespace |
40 | 55 |
41 int main(int argc, char* argv[]) { | 56 int main(int argc, char* argv[]) { |
42 string cmd; | 57 string cmd; |
43 if (!GetArg(1, argc, argv, &cmd)) | 58 if (!GetArg(1, argc, argv, &cmd)) |
44 return 1; | 59 return 1; |
45 | 60 |
46 if (cmd == "StartTracing") { | 61 if (cmd == "StartTracing") { |
47 string path; | 62 string path; |
48 if (!GetArg(2, argc, argv, &path)) | 63 if (!GetArg(2, argc, argv, &path)) |
49 return 1; | 64 return 1; |
50 | 65 |
51 cout << "Calling StartTracing()" << endl; | 66 cout << "Calling StartTracing()" << endl; |
52 battor::BattOrAgent(path).StartTracing(); | 67 battor::BattOrAgent(path).StartTracing(); |
53 } else if (cmd == "StopTracing") { | 68 } else if (cmd == "StopTracing") { |
54 string path; | 69 string path; |
55 if (!GetArg(2, argc, argv, &path)) | 70 if (!GetArg(2, argc, argv, &path)) |
56 return 1; | 71 return 1; |
57 | 72 |
58 string out_trace; | |
59 cout << "Calling StopTracing()" << endl; | 73 cout << "Calling StopTracing()" << endl; |
60 battor::BattOrAgent(path).StopTracing(&out_trace); | 74 battor::BattOrAgent(path).StopTracing(base::Bind(&OnStopTracingComplete)); |
61 cout << out_trace << endl; | 75 g_stop_tracing_complete_event.Wait(); |
Zhen Wang
2015/10/29 19:40:22
I am fine with treating this as a blocking call ri
nednguyen
2015/10/29 19:57:32
Sorry, but I feel like this is an unnecessary opti
charliea (OOO until 10-5)
2015/10/29 21:12:11
(I think we covered this in the meeting - we'll st
| |
76 cout << *g_trace_output.get() << endl; | |
62 } else if (cmd == "SupportsExplicitClockSync") { | 77 } else if (cmd == "SupportsExplicitClockSync") { |
63 cout << "Calling SupportsExplicitClockSync" << endl; | 78 cout << "Calling SupportsExplicitClockSync" << endl; |
64 cout << battor::BattOrAgent::SupportsExplicitClockSync() << endl; | 79 cout << battor::BattOrAgent::SupportsExplicitClockSync() << endl; |
65 } else if (cmd == "RecordClockSyncMarker") { | 80 } else if (cmd == "RecordClockSyncMarker") { |
66 string path, marker; | 81 string path, marker; |
67 if (!GetArg(2, argc, argv, &path) || !GetArg(3, argc, argv, &marker)) | 82 if (!GetArg(2, argc, argv, &path) || !GetArg(3, argc, argv, &marker)) |
68 return 1; | 83 return 1; |
69 | 84 |
70 cout << "Marker: " << marker << endl; | 85 cout << "Marker: " << marker << endl; |
71 cout << "Calling RecordClockSyncMarker()" << endl; | 86 cout << "Calling RecordClockSyncMarker()" << endl; |
72 battor::BattOrAgent(path).RecordClockSyncMarker(marker); | 87 battor::BattOrAgent(path).RecordClockSyncMarker(marker); |
73 } else if (cmd == "IssueClockSyncMarker") { | 88 } else if (cmd == "IssueClockSyncMarker") { |
74 string path; | 89 string path; |
75 if (!GetArg(2, argc, argv, &path)) | 90 if (!GetArg(2, argc, argv, &path)) |
76 return 1; | 91 return 1; |
77 | 92 |
78 cout << "Calling IssueClockSyncMarker" << endl; | 93 cout << "Calling IssueClockSyncMarker" << endl; |
79 battor::BattOrAgent(path).IssueClockSyncMarker(); | 94 battor::BattOrAgent(path).IssueClockSyncMarker(); |
80 } else { | 95 } else { |
81 PrintUsage(); | 96 PrintUsage(); |
82 return 1; | 97 return 1; |
83 } | 98 } |
84 | 99 |
85 return 0; | 100 return 0; |
86 } | 101 } |
OLD | NEW |