OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS 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 |
5 #include <errno.h> | 5 #include <cstdio> |
6 #include <sys/file.h> | |
7 #include <string.h> | |
8 #include <stdio.h> | |
9 | |
10 #include <cstdlib> | 6 #include <cstdlib> |
11 #include <iostream> | |
12 | 7 |
13 #include "metrics_library.h" | 8 #include "metrics_library.h" |
14 | 9 |
15 using namespace std; | |
16 | |
17 // Usage: metrics_client [-ab] metric_name metric_value | 10 // Usage: metrics_client [-ab] metric_name metric_value |
18 int main(int argc, char** argv) { | 11 int main(int argc, char** argv) { |
19 bool send_to_autotest = false; | 12 bool send_to_autotest = false; |
20 bool send_to_chrome = true; | 13 bool send_to_chrome = true; |
| 14 bool secs_to_msecs = false; |
21 int metric_name_index = 1; | 15 int metric_name_index = 1; |
22 int metric_value_index = 2; | 16 int metric_value_index = 2; |
23 bool print_usage = false; | 17 bool print_usage = false; |
24 | 18 |
25 if (argc >= 3) { | 19 if (argc >= 3) { |
26 // Parse arguments | 20 // Parse arguments |
27 int flag; | 21 int flag; |
28 while ((flag = getopt(argc, argv, "ab")) != -1) { | 22 while ((flag = getopt(argc, argv, "abt")) != -1) { |
29 switch (flag) { | 23 switch (flag) { |
30 case 'a': | 24 case 'a': |
31 send_to_autotest = true; | 25 send_to_autotest = true; |
32 send_to_chrome = false; | 26 send_to_chrome = false; |
33 break; | 27 break; |
34 case 'b': | 28 case 'b': |
35 send_to_chrome = true; | 29 send_to_chrome = true; |
36 send_to_autotest = true; | 30 send_to_autotest = true; |
37 break; | 31 break; |
| 32 case 't': |
| 33 secs_to_msecs = true; |
| 34 break; |
38 default: | 35 default: |
39 print_usage = true; | 36 print_usage = true; |
40 break; | 37 break; |
41 } | 38 } |
42 } | 39 } |
43 metric_name_index = optind; | 40 metric_name_index = optind; |
44 metric_value_index = optind + 1; | 41 metric_value_index = optind + 1; |
45 } else { | 42 } else { |
46 print_usage = true; | 43 print_usage = true; |
47 } | 44 } |
48 | 45 |
49 // Metrics value should be the last argument passed | 46 // Metrics value should be the last argument passed |
50 if ((metric_value_index + 1) != argc) { | 47 if ((metric_value_index + 1) != argc) { |
51 print_usage = true; | 48 print_usage = true; |
52 } | 49 } |
53 | 50 |
54 if (print_usage) { | 51 if (print_usage) { |
55 cerr << "Usage: metrics_client [-ab] name value" << endl; | 52 fprintf(stderr, |
56 cerr << endl; | 53 "Usage: metrics_client [-abt] name value\n" |
57 cerr << " default: send metric to chrome only" << endl; | 54 "\n" |
58 cerr << " -a: send metric to autotest only" << endl; | 55 " default: send metric with integer value to chrome only\n" |
59 cerr << " -b: send metric to both chrome and autotest" << endl; | 56 " -a: send metric to autotest only\n" |
| 57 " -b: send metric to both chrome and autotest\n" |
| 58 " -t: convert value from float seconds to int milliseconds\n"); |
60 return 1; | 59 return 1; |
61 } | 60 } |
62 | 61 |
| 62 const char* name = argv[metric_name_index]; |
| 63 int value; |
| 64 if (secs_to_msecs) { |
| 65 float secs = strtof(argv[metric_value_index], NULL); |
| 66 value = static_cast<int>(secs * 1000.0f); |
| 67 } else { |
| 68 value = atoi(argv[metric_value_index]); |
| 69 } |
| 70 |
63 // Send metrics | 71 // Send metrics |
64 if (send_to_autotest) { | 72 if (send_to_autotest) { |
65 MetricsLibrary::SendToAutotest(argv[metric_name_index], | 73 MetricsLibrary::SendToAutotest(name, value); |
66 argv[metric_value_index]); | |
67 } | 74 } |
68 if (send_to_chrome) { | 75 if (send_to_chrome) { |
69 MetricsLibrary::SendToChrome(argv[metric_name_index], | 76 MetricsLibrary::SendToChrome(name, value); |
70 argv[metric_value_index]); | |
71 } | 77 } |
72 return 0; | 78 return 0; |
73 } | 79 } |
OLD | NEW |