blob: 4cd61edceee3821a20d1e894ab493facf58cbc83 [file] [log] [blame]
Po-Hsien Wang00777b22019-04-24 16:37:09 -07001// 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 Wang00777b22019-04-24 16:37:09 -07005#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 Wang42e116c2020-06-09 16:10:27 -070015#include "utils.h"
16
Po-Hsien Wang00777b22019-04-24 16:37:09 -070017const std::string kTemperatureScript =
18 "/usr/local/autotest/bin/temperature.py --maximum";
19
20void 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. */
33bool 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
49std::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 Wang00777b22019-04-24 16:37:09 -070061bool 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}