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 "metrics_library.h" | 5 #include "metrics_library.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <sys/file.h> | 8 #include <sys/file.h> |
9 | 9 |
10 #include <cstdarg> | 10 #include <cstdarg> |
11 #include <cstdio> | 11 #include <cstdio> |
12 #include <cstring> | 12 #include <cstring> |
13 | 13 |
14 #define READ_WRITE_ALL_FILE_FLAGS \ | 14 #define READ_WRITE_ALL_FILE_FLAGS \ |
15 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) | 15 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) |
16 | 16 |
17 static const char kAutotestPath[] = | 17 static const char kAutotestPath[] = |
18 "/var/log/metrics/autotest-events"; | 18 "/var/log/metrics/autotest-events"; |
19 static const char kUMAEventsPath[] = | 19 static const char kUMAEventsPath[] = |
20 "/var/log/metrics/uma-events"; | 20 "/var/log/metrics/uma-events"; |
21 static const int32_t kBufferSize = 1024; | 21 static const int32_t kBufferSize = 1024; |
22 | 22 |
23 using namespace std; | 23 using std::string; |
24 | 24 |
25 // TODO(sosa@chromium.org) - use Chromium logger instead of stderr | 25 // TODO(sosa@chromium.org) - use Chromium logger instead of stderr |
26 static void PrintError(const char* message, const char* file, | 26 static void PrintError(const char* message, const char* file, |
27 int code) { | 27 int code) { |
28 static const char *kProgramName = "libmetrics"; | 28 static const char kProgramName[] = "libmetrics"; |
29 if (code == 0) { | 29 if (code == 0) { |
30 fprintf(stderr, "%s: %s\n", kProgramName, message); | 30 fprintf(stderr, "%s: %s\n", kProgramName, message); |
31 } else if (file == NULL) { | 31 } else if (file == NULL) { |
32 fprintf(stderr, "%s: ", kProgramName); | 32 fprintf(stderr, "%s: ", kProgramName); |
33 perror(message); | 33 perror(message); |
34 } else { | 34 } else { |
35 fprintf(stderr, "%s: %s: ", kProgramName, file); | 35 fprintf(stderr, "%s: %s: ", kProgramName, file); |
36 perror(message); | 36 perror(message); |
37 } | 37 } |
38 } | 38 } |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 // Prepend LENGTH to the message. | 106 // Prepend LENGTH to the message. |
107 memcpy(buffer, &message_length, len_size); | 107 memcpy(buffer, &message_length, len_size); |
108 return message_length; | 108 return message_length; |
109 } | 109 } |
110 | 110 |
111 void MetricsLibrary::Init() { | 111 void MetricsLibrary::Init() { |
112 uma_events_file_ = kUMAEventsPath; | 112 uma_events_file_ = kUMAEventsPath; |
113 } | 113 } |
114 | 114 |
115 bool MetricsLibrary::SendToAutotest(const string& name, int value) { | 115 bool MetricsLibrary::SendToAutotest(const string& name, int value) { |
116 FILE *autotest_file = fopen(kAutotestPath, "a+"); | 116 FILE* autotest_file = fopen(kAutotestPath, "a+"); |
117 if (autotest_file == NULL) { | 117 if (autotest_file == NULL) { |
118 PrintError("fopen", kAutotestPath, errno); | 118 PrintError("fopen", kAutotestPath, errno); |
119 return false; | 119 return false; |
120 } | 120 } |
121 | 121 |
122 fprintf(autotest_file, "%s=%d\n", name.c_str(), value); | 122 fprintf(autotest_file, "%s=%d\n", name.c_str(), value); |
123 fclose(autotest_file); | 123 fclose(autotest_file); |
124 return true; | 124 return true; |
125 } | 125 } |
126 | 126 |
(...skipping 21 matching lines...) Expand all Loading... |
148 FormatChromeMessage(kBufferSize, message, | 148 FormatChromeMessage(kBufferSize, message, |
149 "linearhistogram%c%s %d %d", '\0', | 149 "linearhistogram%c%s %d %d", '\0', |
150 name.c_str(), sample, max); | 150 name.c_str(), sample, max); |
151 | 151 |
152 if (message_length < 0) | 152 if (message_length < 0) |
153 return false; | 153 return false; |
154 | 154 |
155 // Send the message. | 155 // Send the message. |
156 return SendMessageToChrome(message_length, message); | 156 return SendMessageToChrome(message_length, message); |
157 } | 157 } |
OLD | NEW |