1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174

ash / system / diagnostics / telemetry_log.cc [blame]

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/system/diagnostics/telemetry_log.h"

#include <sstream>
#include <utility>

#include "base/i18n/time_formatting.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"

namespace ash {
namespace diagnostics {
namespace {

const char kNewline[] = "\n";

// SystemInfo constants:
const char kSystemInfoSectionName[] = "--- System Info ---";
const char kSystemInfoCurrentTimeTitle[] = "Snapshot Time: ";
const char kSystemInfoBoardNameTitle[] = "Board Name: ";
const char kSystemInfoMarketingNameTitle[] = "Marketing Name: ";
const char kSystemInfoCpuModelNameTitle[] = "CpuModel Name: ";
const char kSystemInfoTotalMemoryTitle[] = "Total Memory (kib): ";
const char kSystemInfoCpuThreadCountTitle[] = "Thread Count:  ";
const char kSystemInfoCpuMaxClockSpeedTitle[] = "Cpu Max Clock Speed (kHz):  ";
const char kSystemInfoMilestoneVersionTitle[] = "Version: ";
const char kSystemInfoHasBatteryTitle[] = "Has Battery: ";

// BatteryChargeStatus constants:
const char kBatteryChargeStatusSectionName[] = "--- Battery Charge Status ---";
const char kBatteryChargeStatusBatteryStateTitle[] = "Battery State: ";
const char kBatteryChargeStatusPowerSourceTitle[] = "Power Source: ";
const char kBatteryChargeStatusPowerTimeTitle[] = "Power Time: ";
const char kBatteryChargeStatusCurrentNowTitle[] = "Current Now (mA): ";
const char kBatteryChargeStatusChargeNowTitle[] = "Chage Now (mAh): ";

// BatteryHealth constants:
const char kBatteryHealthSectionName[] = "--- Battery Health ---";
const char kBatteryHealthChargeFullNowTitle[] = "Charge Full Now (mAh): ";
const char kBatteryHealthChargeFullDesignTitle[] = "Charge Full Design (mAh): ";
const char kBatteryHealthCycleCountTitle[] = "Cycle Count: ";
const char kBatteryHealthWearPercentageTitle[] = "Wear Percentage: ";

// MemoryUsage constants:
const char kMemoryUsageSectionName[] = "--- Memory Usage ---";
const char kMemoryUsageTotalMemoryTitle[] = "Total Memory (kib): ";
const char kMemoryUsageAvailableMemoryTitle[] = "Available Memory (kib): ";
const char kMemoryUsageFreeMemoryTitle[] = "Free Memory (kib): ";

// CpuUsage constants:
const char kCpuUsageSectionName[] = "--- Cpu Usage ---";
const char kCpuUsageUserTitle[] = "Usage User (%): ";
const char kCpuUsageSystemTitle[] = "Usage System (%): ";
const char kCpuUsageFreeTitle[] = "Usage Free (%): ";
const char kCpuUsageAvgTempTitle[] = "Avg Temp (C): ";
const char kCpuUsageScalingFrequencyTitle[] =
    "Current scaled frequency (kHz): ";

std::string GetCurrentDateTimeWithTimeZoneAsString() {
  return base::UTF16ToUTF8(
      base::TimeFormatShortDateAndTimeWithTimeZone(base::Time::Now()));
}

}  // namespace

TelemetryLog::TelemetryLog() = default;
TelemetryLog::~TelemetryLog() = default;

void TelemetryLog::UpdateSystemInfo(mojom::SystemInfoPtr latest_system_info) {
  latest_system_info_ = std::move(latest_system_info);
}

void TelemetryLog::UpdateBatteryChargeStatus(
    mojom::BatteryChargeStatusPtr latest_battery_charge_status) {
  latest_battery_charge_status_ = std::move(latest_battery_charge_status);
}

void TelemetryLog::UpdateBatteryHealth(
    mojom::BatteryHealthPtr latest_battery_health) {
  latest_battery_health_ = std::move(latest_battery_health);
}

void TelemetryLog::UpdateMemoryUsage(
    mojom::MemoryUsagePtr latest_memory_usage) {
  latest_memory_usage_ = std::move(latest_memory_usage);
}

void TelemetryLog::UpdateCpuUsage(mojom::CpuUsagePtr latest_cpu_usage) {
  latest_cpu_usage_ = std::move(latest_cpu_usage);
}

std::string TelemetryLog::GetContents() const {
  std::stringstream output;
  if (latest_system_info_) {
    output << kSystemInfoSectionName << kNewline << kSystemInfoCurrentTimeTitle
           << GetCurrentDateTimeWithTimeZoneAsString() << kNewline
           << kSystemInfoBoardNameTitle << latest_system_info_->board_name
           << kNewline << kSystemInfoMarketingNameTitle
           << latest_system_info_->marketing_name << kNewline
           << kSystemInfoCpuModelNameTitle
           << latest_system_info_->cpu_model_name << kNewline
           << kSystemInfoTotalMemoryTitle
           << latest_system_info_->total_memory_kib << kNewline
           << kSystemInfoCpuThreadCountTitle
           << latest_system_info_->cpu_threads_count << kNewline
           << kSystemInfoCpuMaxClockSpeedTitle
           << latest_system_info_->cpu_max_clock_speed_khz << kNewline
           << kSystemInfoMilestoneVersionTitle
           << latest_system_info_->version_info->full_version_string << kNewline
           << kSystemInfoHasBatteryTitle
           << ((latest_system_info_->device_capabilities->has_battery)
                   ? "true"
                   : "false")
           << kNewline << kNewline;
  }
  if (latest_battery_charge_status_) {
    output << kBatteryChargeStatusSectionName << kNewline
           << kBatteryChargeStatusBatteryStateTitle
           << latest_battery_charge_status_->battery_state << kNewline
           << kBatteryChargeStatusPowerSourceTitle
           << latest_battery_charge_status_->power_adapter_status << kNewline
           << kBatteryChargeStatusPowerTimeTitle
           << latest_battery_charge_status_->power_time << kNewline
           << kBatteryChargeStatusCurrentNowTitle
           << latest_battery_charge_status_->current_now_milliamps << kNewline
           << kBatteryChargeStatusChargeNowTitle
           << latest_battery_charge_status_->charge_now_milliamp_hours
           << kNewline << kNewline;
  }
  if (latest_battery_health_) {
    output << kBatteryHealthSectionName << kNewline
           << kBatteryHealthChargeFullNowTitle
           << latest_battery_health_->charge_full_now_milliamp_hours << kNewline
           << kBatteryHealthChargeFullDesignTitle
           << latest_battery_health_->charge_full_design_milliamp_hours
           << kNewline << kBatteryHealthCycleCountTitle
           << latest_battery_health_->cycle_count << kNewline
           << kBatteryHealthWearPercentageTitle
           << base::NumberToString(
                  latest_battery_health_->battery_wear_percentage)
           << kNewline << kNewline;
  }
  if (latest_memory_usage_) {
    output << kMemoryUsageSectionName << kNewline
           << kMemoryUsageTotalMemoryTitle
           << latest_memory_usage_->total_memory_kib << kNewline
           << kMemoryUsageAvailableMemoryTitle
           << latest_memory_usage_->available_memory_kib << kNewline
           << kMemoryUsageFreeMemoryTitle
           << latest_memory_usage_->free_memory_kib << kNewline << kNewline;
  }
  if (latest_cpu_usage_) {
    output << kCpuUsageSectionName << kNewline << kCpuUsageUserTitle
           << base::NumberToString(latest_cpu_usage_->percent_usage_user)
           << kNewline << kCpuUsageSystemTitle
           << base::NumberToString(latest_cpu_usage_->percent_usage_system)
           << kNewline << kCpuUsageFreeTitle
           << base::NumberToString(latest_cpu_usage_->percent_usage_free)
           << kNewline << kCpuUsageAvgTempTitle
           << latest_cpu_usage_->average_cpu_temp_celsius << kNewline
           << kCpuUsageScalingFrequencyTitle
           << latest_cpu_usage_->scaling_current_frequency_khz << kNewline
           << kNewline;
  }

  return output.str();
}

}  // namespace diagnostics
}  // namespace ash