| 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 #include <sys/stat.h> |
| 9 | 10 |
| 10 #include <cstdarg> | 11 #include <cstdarg> |
| 11 #include <cstdio> | 12 #include <cstdio> |
| 12 #include <cstring> | 13 #include <cstring> |
| 13 | 14 |
| 14 #define READ_WRITE_ALL_FILE_FLAGS \ | 15 #define READ_WRITE_ALL_FILE_FLAGS \ |
| 15 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) | 16 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) |
| 16 | 17 |
| 17 static const char kAutotestPath[] = | 18 static const char kAutotestPath[] = "/var/log/metrics/autotest-events"; |
| 18 "/var/log/metrics/autotest-events"; | 19 static const char kUMAEventsPath[] = "/var/log/metrics/uma-events"; |
| 19 static const char kUMAEventsPath[] = | 20 static const char kConsentFile[] = "/home/chronos/Consent To Send Stats"; |
| 20 "/var/log/metrics/uma-events"; | |
| 21 static const int32_t kBufferSize = 1024; | 21 static const int32_t kBufferSize = 1024; |
| 22 | 22 |
| 23 time_t MetricsLibrary::cached_enabled_time_ = 0; |
| 24 bool MetricsLibrary::cached_enabled_ = false; |
| 25 |
| 23 using std::string; | 26 using std::string; |
| 24 | 27 |
| 25 // TODO(sosa@chromium.org) - use Chromium logger instead of stderr | 28 // TODO(sosa@chromium.org) - use Chromium logger instead of stderr |
| 26 static void PrintError(const char* message, const char* file, | 29 static void PrintError(const char* message, const char* file, |
| 27 int code) { | 30 int code) { |
| 28 static const char kProgramName[] = "libmetrics"; | 31 static const char kProgramName[] = "libmetrics"; |
| 29 if (code == 0) { | 32 if (code == 0) { |
| 30 fprintf(stderr, "%s: %s\n", kProgramName, message); | 33 fprintf(stderr, "%s: %s\n", kProgramName, message); |
| 31 } else if (file == NULL) { | 34 } else if (file == NULL) { |
| 32 fprintf(stderr, "%s: ", kProgramName); | 35 fprintf(stderr, "%s: ", kProgramName); |
| 33 perror(message); | 36 perror(message); |
| 34 } else { | 37 } else { |
| 35 fprintf(stderr, "%s: %s: ", kProgramName, file); | 38 fprintf(stderr, "%s: %s: ", kProgramName, file); |
| 36 perror(message); | 39 perror(message); |
| 37 } | 40 } |
| 38 } | 41 } |
| 39 | 42 |
| 40 MetricsLibrary::MetricsLibrary() | 43 MetricsLibrary::MetricsLibrary() |
| 41 : uma_events_file_(NULL) {} | 44 : uma_events_file_(NULL), |
| 45 consent_file_(kConsentFile) {} |
| 46 |
| 47 bool MetricsLibrary::AreMetricsEnabled() { |
| 48 static struct stat stat_buffer; |
| 49 time_t this_check_time = time(NULL); |
| 50 |
| 51 if (this_check_time != cached_enabled_time_) { |
| 52 cached_enabled_time_ = this_check_time; |
| 53 cached_enabled_ = (stat(consent_file_, &stat_buffer) >= 0); |
| 54 } |
| 55 return cached_enabled_; |
| 56 } |
| 42 | 57 |
| 43 bool MetricsLibrary::SendMessageToChrome(int32_t length, const char* message) { | 58 bool MetricsLibrary::SendMessageToChrome(int32_t length, const char* message) { |
| 59 if (!AreMetricsEnabled()) |
| 60 return true; |
| 61 |
| 44 int chrome_fd = open(uma_events_file_, | 62 int chrome_fd = open(uma_events_file_, |
| 45 O_WRONLY | O_APPEND | O_CREAT, | 63 O_WRONLY | O_APPEND | O_CREAT, |
| 46 READ_WRITE_ALL_FILE_FLAGS); | 64 READ_WRITE_ALL_FILE_FLAGS); |
| 47 // If we failed to open it, return. | 65 // If we failed to open it, return. |
| 48 if (chrome_fd < 0) { | 66 if (chrome_fd < 0) { |
| 49 PrintError("open", uma_events_file_, errno); | 67 PrintError("open", uma_events_file_, errno); |
| 50 return false; | 68 return false; |
| 51 } | 69 } |
| 52 | 70 |
| 53 // Need to chmod because open flags are anded with umask. Ignore the | 71 // Need to chmod because open flags are anded with umask. Ignore the |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 FormatChromeMessage(kBufferSize, message, | 166 FormatChromeMessage(kBufferSize, message, |
| 149 "linearhistogram%c%s %d %d", '\0', | 167 "linearhistogram%c%s %d %d", '\0', |
| 150 name.c_str(), sample, max); | 168 name.c_str(), sample, max); |
| 151 | 169 |
| 152 if (message_length < 0) | 170 if (message_length < 0) |
| 153 return false; | 171 return false; |
| 154 | 172 |
| 155 // Send the message. | 173 // Send the message. |
| 156 return SendMessageToChrome(message_length, message); | 174 return SendMessageToChrome(message_length, message); |
| 157 } | 175 } |
| OLD | NEW |