| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "platform/metrics_daemon/metrics_daemon.h" | 5 #include "platform/metrics_daemon/metrics_daemon.h" |
| 6 | 6 |
| 7 #include <glib-object.h> | 7 #include <glib-object.h> |
| 8 #include <limits.h> | 8 #include <limits.h> |
| 9 #include <stdio.h> | 9 #include <stdio.h> |
| 10 #include <stdlib.h> | 10 #include <stdlib.h> |
| 11 #include <sys/file.h> | 11 #include <sys/file.h> |
| 12 #include <sys/stat.h> | 12 #include <sys/stat.h> |
| 13 #include <unistd.h> |
| 13 | 14 |
| 14 extern "C" { | 15 extern "C" { |
| 15 #include "marshal_void__string_boxed.h" | 16 #include "marshal_void__string_boxed.h" |
| 16 } | 17 } |
| 17 | 18 |
| 18 #include <base/logging.h> | 19 #include <base/logging.h> |
| 19 | 20 |
| 20 #define SAFE_MESSAGE(e) ((e && e->message) ? e->message : "unknown error") | 21 #define SAFE_MESSAGE(e) ((e && e->message) ? e->message : "unknown error") |
| 21 #define READ_WRITE_ALL_FILE_FLAGS \ | 22 #define READ_WRITE_ALL_FILE_FLAGS \ |
| 22 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) | 23 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) |
| 23 | 24 |
| 24 const char* MetricsDaemon::kMetricsFilePath = "/tmp/.chromeos-metrics"; | 25 const char* MetricsDaemon::kMetricsFilePath = "/tmp/.chromeos-metrics"; |
| 25 | 26 |
| 26 MetricsDaemon::NetworkState | 27 MetricsDaemon::NetworkState |
| 27 MetricsDaemon::network_states_[MetricsDaemon::kNumberNetworkStates] = { | 28 MetricsDaemon::network_states_[MetricsDaemon::kNumberNetworkStates] = { |
| 28 #define STATE(name, capname) { #name, "Connman" # capname }, | 29 #define STATE(name, capname) { #name, "Connman" # capname }, |
| 29 #include "network_states.h" | 30 #include "network_states.h" |
| 30 }; | 31 }; |
| 31 | 32 |
| 32 void MetricsDaemon::Run(bool testing) { | 33 void MetricsDaemon::Run(bool run_as_daemon, bool testing) { |
| 33 Init(testing); | 34 Init(testing); |
| 34 Loop(); | 35 if (!run_as_daemon || daemon(0, 0) == 0) { |
| 36 Loop(); |
| 37 } |
| 35 } | 38 } |
| 36 | 39 |
| 37 void MetricsDaemon::Init(bool testing) { | 40 void MetricsDaemon::Init(bool testing) { |
| 38 testing_ = testing; | 41 testing_ = testing; |
| 39 network_state_id_ = kUnknownNetworkStateId; | 42 network_state_id_ = kUnknownNetworkStateId; |
| 40 | 43 |
| 41 // Opens the file to communicate with Chrome, and keep it open. | 44 // Opens the file to communicate with Chrome, and keep it open. |
| 42 metrics_fd_ = open(kMetricsFilePath, O_WRONLY | O_APPEND | O_CREAT, | 45 metrics_fd_ = open(kMetricsFilePath, O_WRONLY | O_APPEND | O_CREAT, |
| 43 READ_WRITE_ALL_FILE_FLAGS); | 46 READ_WRITE_ALL_FILE_FLAGS); |
| 44 PLOG_IF(FATAL, metrics_fd_ < 0) << kMetricsFilePath; | 47 PLOG_IF(FATAL, metrics_fd_ < 0) << kMetricsFilePath; |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 | 195 |
| 193 if (flock(metrics_fd_, LOCK_UN) < 0) { | 196 if (flock(metrics_fd_, LOCK_UN) < 0) { |
| 194 // error("unlock", path, errno); | 197 // error("unlock", path, errno); |
| 195 return; | 198 return; |
| 196 } | 199 } |
| 197 } | 200 } |
| 198 | 201 |
| 199 void MetricsDaemon::TestPublishMetric(const char* name, const char* value) { | 202 void MetricsDaemon::TestPublishMetric(const char* name, const char* value) { |
| 200 LOG(INFO) << "received metric: " << name << " " << value; | 203 LOG(INFO) << "received metric: " << name << " " << value; |
| 201 } | 204 } |
| OLD | NEW |