Index: metrics_client.cc |
diff --git a/metrics_client.cc b/metrics_client.cc |
index e85c4d473b8e2041eddf61868a26e30480c85c8a..ce22e987ed7ded016bd1a499bc6e0cad21b1edca 100644 |
--- a/metrics_client.cc |
+++ b/metrics_client.cc |
@@ -7,63 +7,29 @@ |
#include "metrics_library.h" |
-int main(int argc, char** argv) { |
- bool send_to_autotest = false; |
- bool send_to_chrome = true; |
- bool send_enum = false; |
- bool secs_to_msecs = false; |
- int name_index = 1; |
- bool print_usage = false; |
- |
- if (argc >= 3) { |
- // Parse arguments |
- int flag; |
- while ((flag = getopt(argc, argv, "abet")) != -1) { |
- switch (flag) { |
- case 'a': |
- send_to_autotest = true; |
- send_to_chrome = false; |
- break; |
- case 'b': |
- send_to_chrome = true; |
- send_to_autotest = true; |
- break; |
- case 'e': |
- send_enum = true; |
- break; |
- case 't': |
- secs_to_msecs = true; |
- break; |
- default: |
- print_usage = true; |
- break; |
- } |
- } |
- name_index = optind; |
- } else { |
- print_usage = true; |
- } |
- |
- int num_args = send_enum ? 3 : 5; |
- if ((name_index + num_args) != argc || |
- (send_enum && secs_to_msecs)) { |
- print_usage = true; |
- } |
- |
- if (print_usage) { |
- fprintf(stderr, |
- "Usage: metrics_client [-ab] [-t] name sample min max nbuckets\n" |
- " metrics_client [-ab] -e name sample max\n" |
- "\n" |
- " default: send metric with integer values to Chrome only\n" |
- " |min| > 0, |min| <= sample < |max|\n" |
- " -a: send metric (name/sample) to Autotest only\n" |
- " -b: send metric to both Chrome and Autotest\n" |
- " -e: send linear/enumeration histogram data\n" |
- " -t: convert sample from double seconds to int milliseconds\n"); |
- return 1; |
- } |
+void ShowUsage() { |
+ fprintf(stderr, |
+ "Usage: metrics_client [-ab] [-t] name sample min max nbuckets\n" |
+ " metrics_client [-ab] -e name sample max\n" |
+ " metrics_client [-cg]\n" |
+ "\n" |
+ " default: send metric with integer values to Chrome only\n" |
+ " |min| > 0, |min| <= sample < |max|\n" |
+ " -a: send metric (name/sample) to Autotest only\n" |
+ " -b: send metric to both Chrome and Autotest\n" |
+ " -c: return exit status 0 if user consents to stats, 1 otherwise\n" |
+ " -e: send linear/enumeration histogram data\n" |
+ " -g: return exit status 0 if machine in guest mode, 1 otherwise\n" |
+ " -t: convert sample from double seconds to int milliseconds\n"); |
+ exit(1); |
+} |
+static int SendStats(char* argv[], |
+ int name_index, |
+ bool send_enum, |
+ bool secs_to_msecs, |
+ bool send_to_autotest, |
+ bool send_to_chrome) { |
const char* name = argv[name_index]; |
int sample; |
if (secs_to_msecs) { |
@@ -92,3 +58,89 @@ int main(int argc, char** argv) { |
} |
return 0; |
} |
+ |
+static int HasConsent() { |
+ MetricsLibrary metrics_lib; |
+ metrics_lib.Init(); |
+ return metrics_lib.AreMetricsEnabled() ? 0 : 1; |
+} |
+ |
+static int IsGuestMode() { |
+ MetricsLibrary metrics_lib; |
+ metrics_lib.Init(); |
+ return metrics_lib.IsGuestMode() ? 0 : 1; |
+} |
+ |
+int main(int argc, char** argv) { |
+ enum Mode { |
+ kModeSendStats, |
+ kModeHasConsent, |
+ kModeIsGuestMode |
+ } mode = kModeSendStats; |
+ bool send_to_autotest = false; |
+ bool send_to_chrome = true; |
+ bool send_enum = false; |
+ bool secs_to_msecs = false; |
+ bool print_usage = false; |
+ |
+ // Parse arguments |
+ int flag; |
+ while ((flag = getopt(argc, argv, "abcegt")) != -1) { |
+ switch (flag) { |
+ case 'a': |
+ mode = kModeSendStats; |
+ send_to_autotest = true; |
+ send_to_chrome = false; |
+ break; |
+ case 'b': |
+ mode = kModeSendStats; |
+ send_to_chrome = true; |
+ send_to_autotest = true; |
+ break; |
+ case 'c': |
+ mode = kModeHasConsent; |
+ break; |
+ case 'e': |
+ send_enum = true; |
+ break; |
+ case 'g': |
+ mode = kModeIsGuestMode; |
+ break; |
+ case 't': |
+ secs_to_msecs = true; |
+ break; |
+ default: |
+ print_usage = true; |
+ break; |
+ } |
+ } |
+ int name_index = optind; |
+ |
+ int expected_args = 0; |
+ if (mode == kModeSendStats) |
+ expected_args = send_enum ? 3 : 5; |
+ |
+ if ((name_index + expected_args) != argc) { |
+ ShowUsage(); |
+ } |
+ |
+ switch(mode) { |
+ case kModeSendStats: |
+ if (send_enum && secs_to_msecs) { |
+ ShowUsage(); |
+ } |
+ return SendStats(argv, |
+ name_index, |
+ send_enum, |
+ secs_to_msecs, |
+ send_to_autotest, |
+ send_to_chrome); |
+ case kModeHasConsent: |
+ return HasConsent(); |
+ case kModeIsGuestMode: |
+ return IsGuestMode(); |
+ default: |
+ ShowUsage(); |
+ return 0; |
+ } |
+} |