Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <time.h> |
| 8 | #include <unistd.h> |
| 9 | #include <chrono> |
| 10 | #include <fstream> |
| 11 | #include <sstream> |
| 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
Po-Hsien Wang | 42e116c | 2020-06-09 16:10:27 -0700 | [diff] [blame^] | 15 | #include "utils.h" |
| 16 | |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 17 | const std::string kTemperatureScript = |
| 18 | "/usr/local/autotest/bin/temperature.py --maximum"; |
| 19 | |
| 20 | void PrintDateTime() { |
| 21 | time_t timer; |
| 22 | char buffer[26]; |
| 23 | time(&timer); |
| 24 | struct tm* tm_info = localtime(&timer); |
| 25 | strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info); |
| 26 | LOG("# DateTime: %s", buffer) |
| 27 | } |
| 28 | |
| 29 | /* Execute a shell command and return its file descriptor for reading output. */ |
| 30 | /* @param command: command to be run. */ |
| 31 | /* @param result: the stdout of the command output. */ |
| 32 | /* @return true if the command is executed successfully. */ |
| 33 | bool ExecuteCommand(const std::string& kCommand, |
| 34 | std::string* result = nullptr) { |
| 35 | FILE* fd = popen(kCommand.c_str(), "r"); |
| 36 | if (!fd) { |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | if (result) { |
| 41 | fseek(fd, 0, SEEK_END); |
| 42 | long size = ftell(fd); |
| 43 | fseek(fd, 0, SEEK_SET); |
| 44 | fread(&result[0], sizeof(char), size, fd); |
| 45 | } |
| 46 | return pclose(fd) == 0; |
| 47 | } |
| 48 | |
| 49 | std::vector<std::string> SplitString(const std::string& kInput, |
| 50 | char delimiter) { |
| 51 | std::vector<std::string> result; |
| 52 | std::stringstream srcStream(kInput); |
| 53 | |
| 54 | std::string token; |
| 55 | while (getline(srcStream, token, delimiter)) { |
| 56 | result.push_back(token); |
| 57 | } |
| 58 | return result; |
| 59 | } |
| 60 | |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 61 | bool IsItemInVector(const std::vector<std::string>& list, |
| 62 | const char* value, |
| 63 | bool empty_value = false) { |
| 64 | if (list.empty()) |
| 65 | return empty_value; |
| 66 | return !(find(list.begin(), list.end(), std::string(value)) == list.end()); |
| 67 | } |