Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(113)

Side by Side Diff: metrics_client.cc

Issue 3571009: metrics: Add guest mode detection to metrics library and client (Closed) Base URL: http://git.chromium.org/git/metrics.git
Patch Set: Fix metrics_client semantics bug Created 10 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | metrics_daemon.cc » ('j') | metrics_library.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <cstdio> 5 #include <cstdio>
6 #include <cstdlib> 6 #include <cstdlib>
7 7
8 #include "metrics_library.h" 8 #include "metrics_library.h"
9 9
10 int main(int argc, char** argv) { 10 void ShowUsage() {
11 bool send_to_autotest = false; 11 fprintf(stderr,
12 bool send_to_chrome = true; 12 "Usage: metrics_client [-ab] [-t] name sample min max nbuckets\n"
13 bool send_enum = false; 13 " metrics_client [-ab] -e name sample max\n"
14 bool secs_to_msecs = false; 14 " metrics_client [-cg]\n"
15 int name_index = 1; 15 "\n"
16 bool print_usage = false; 16 " default: send metric with integer values to Chrome only\n"
17 " |min| > 0, |min| <= sample < |max|\n"
18 " -a: send metric (name/sample) to Autotest only\n"
19 " -b: send metric to both Chrome and Autotest\n"
20 " -c: return exit status 0 if user consents to stats, 1 otherwise\n"
21 " -e: send linear/enumeration histogram data\n"
22 " -g: return exit status 0 if machine in guest mode, 1 otherwise\n"
23 " -t: convert sample from double seconds to int milliseconds\n");
24 exit(1);
25 }
17 26
18 if (argc >= 3) { 27 static int SendStats(char* argv[],
19 // Parse arguments 28 int name_index,
20 int flag; 29 bool send_enum,
21 while ((flag = getopt(argc, argv, "abet")) != -1) { 30 bool secs_to_msecs,
22 switch (flag) { 31 bool send_to_autotest,
23 case 'a': 32 bool send_to_chrome) {
24 send_to_autotest = true;
25 send_to_chrome = false;
26 break;
27 case 'b':
28 send_to_chrome = true;
29 send_to_autotest = true;
30 break;
31 case 'e':
32 send_enum = true;
33 break;
34 case 't':
35 secs_to_msecs = true;
36 break;
37 default:
38 print_usage = true;
39 break;
40 }
41 }
42 name_index = optind;
43 } else {
44 print_usage = true;
45 }
46
47 int num_args = send_enum ? 3 : 5;
48 if ((name_index + num_args) != argc ||
49 (send_enum && secs_to_msecs)) {
50 print_usage = true;
51 }
52
53 if (print_usage) {
54 fprintf(stderr,
55 "Usage: metrics_client [-ab] [-t] name sample min max nbuckets\n"
56 " metrics_client [-ab] -e name sample max\n"
57 "\n"
58 " default: send metric with integer values to Chrome only\n"
59 " |min| > 0, |min| <= sample < |max|\n"
60 " -a: send metric (name/sample) to Autotest only\n"
61 " -b: send metric to both Chrome and Autotest\n"
62 " -e: send linear/enumeration histogram data\n"
63 " -t: convert sample from double seconds to int milliseconds\n");
64 return 1;
65 }
66
67 const char* name = argv[name_index]; 33 const char* name = argv[name_index];
68 int sample; 34 int sample;
69 if (secs_to_msecs) { 35 if (secs_to_msecs) {
70 sample = static_cast<int>(atof(argv[name_index + 1]) * 1000.0); 36 sample = static_cast<int>(atof(argv[name_index + 1]) * 1000.0);
71 } else { 37 } else {
72 sample = atoi(argv[name_index + 1]); 38 sample = atoi(argv[name_index + 1]);
73 } 39 }
74 40
75 // Send metrics 41 // Send metrics
76 if (send_to_autotest) { 42 if (send_to_autotest) {
77 MetricsLibrary::SendToAutotest(name, sample); 43 MetricsLibrary::SendToAutotest(name, sample);
78 } 44 }
79 45
80 if (send_to_chrome) { 46 if (send_to_chrome) {
81 MetricsLibrary metrics_lib; 47 MetricsLibrary metrics_lib;
82 metrics_lib.Init(); 48 metrics_lib.Init();
83 if (send_enum) { 49 if (send_enum) {
84 int max = atoi(argv[name_index + 2]); 50 int max = atoi(argv[name_index + 2]);
85 metrics_lib.SendEnumToUMA(name, sample, max); 51 metrics_lib.SendEnumToUMA(name, sample, max);
86 } else { 52 } else {
87 int min = atoi(argv[name_index + 2]); 53 int min = atoi(argv[name_index + 2]);
88 int max = atoi(argv[name_index + 3]); 54 int max = atoi(argv[name_index + 3]);
89 int nbuckets = atoi(argv[name_index + 4]); 55 int nbuckets = atoi(argv[name_index + 4]);
90 metrics_lib.SendToUMA(name, sample, min, max, nbuckets); 56 metrics_lib.SendToUMA(name, sample, min, max, nbuckets);
91 } 57 }
92 } 58 }
93 return 0; 59 return 0;
94 } 60 }
61
62 static int HasConsent() {
63 MetricsLibrary metrics_lib;
64 metrics_lib.Init();
65 return metrics_lib.AreMetricsEnabled() ? 0 : 1;
66 }
67
68 static int IsGuestMode() {
69 MetricsLibrary metrics_lib;
70 metrics_lib.Init();
71 return metrics_lib.IsGuestMode() ? 0 : 1;
72 }
73
74 int main(int argc, char** argv) {
75 enum Mode {
76 kModeSendStats,
77 kModeHasConsent,
78 kModeIsGuestMode
79 } mode = kModeSendStats;
80 bool send_to_autotest = false;
81 bool send_to_chrome = true;
82 bool send_enum = false;
83 bool secs_to_msecs = false;
84 bool print_usage = false;
85
86 // Parse arguments
87 int flag;
88 while ((flag = getopt(argc, argv, "abcegt")) != -1) {
89 switch (flag) {
90 case 'a':
91 mode = kModeSendStats;
92 send_to_autotest = true;
93 send_to_chrome = false;
94 break;
95 case 'b':
96 mode = kModeSendStats;
97 send_to_chrome = true;
98 send_to_autotest = true;
99 break;
100 case 'c':
101 mode = kModeHasConsent;
102 break;
103 case 'e':
104 send_enum = true;
105 break;
106 case 'g':
107 mode = kModeIsGuestMode;
108 break;
109 case 't':
110 secs_to_msecs = true;
111 break;
112 default:
113 print_usage = true;
114 break;
115 }
116 }
117 int name_index = optind;
118
119 int expected_args = 0;
120 if (mode == kModeSendStats)
121 expected_args = send_enum ? 3 : 5;
122
123 if ((name_index + expected_args) != argc) {
124 ShowUsage();
125 }
126
127 switch(mode) {
128 case kModeSendStats:
129 if (send_enum && secs_to_msecs) {
130 ShowUsage();
131 }
132 return SendStats(argv,
133 name_index,
134 send_enum,
135 secs_to_msecs,
136 send_to_autotest,
137 send_to_chrome);
138 case kModeHasConsent:
139 return HasConsent();
140 case kModeIsGuestMode:
141 return IsGuestMode();
142 default:
143 ShowUsage();
144 return 0;
145 }
146 }
OLDNEW
« no previous file with comments | « no previous file | metrics_daemon.cc » ('j') | metrics_library.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698