blob: 9f168111fdcb255301804b5354805c77c1a59c46 [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
5#ifndef __UTILS_H__
6#define __UTILS_H__
7
8#include <stdarg.h>
9#include <sys/time.h>
10#include <string>
11#include <vector>
12#include <vulkan/vulkan.hpp>
13
14extern int g_verbose;
15void PrintDateTime();
16std::vector<std::string> SplitString(const std::string& kInput, char delimiter);
17// For thermal monitoring of system.
18double GetMachineTemperature();
19// Wait for machine to cool with temperature in Celsius and timeout in seconds.
20// Returns the time spent waiting and sets the last observed temperature.
21double WaitForCoolMachine(double cold_temperature,
22 double timeout,
23 double* temperature);
24
25inline uint64_t GetUTime() {
26 struct timeval tv = {};
27 gettimeofday(&tv, nullptr);
28 return tv.tv_usec + 1000000ULL * static_cast<uint64_t>(tv.tv_sec);
29}
30
31bool IsItemInVector(const std::vector<std::string>& list,
32 const char* value,
33 bool empty_value);
34
35inline void DbgPrintf(const char* filename,
36 int line,
37 FILE* fileid,
38 const char* format,
39 ...) {
40 va_list args;
41 va_start(args, format);
42 char string_format[strlen(format) + 1024];
43 char debug_header[1024] = "\0";
44 if (g_verbose) {
45 sprintf(debug_header, "[%s:%d]", filename, line);
46 sprintf(string_format, "%30s %s\n", debug_header, format);
47 } else {
48 sprintf(string_format, "%s\n", format);
49 }
50 vfprintf(fileid, string_format, args);
51 va_end(args);
52}
53
54#define DEBUG(fmt, ...) \
55 { \
56 if (g_verbose) { \
57 LOG(fmt, ##__VA_ARGS__); \
58 } \
59 }
60#define LOG(fmt, ...) \
61 { DbgPrintf(__FILE__, __LINE__, stdout, fmt, ##__VA_ARGS__); }
62#define ERROR(fmt, ...) \
63 { DbgPrintf(__FILE__, __LINE__, stderr, fmt, ##__VA_ARGS__); }
64#define ABORT(code, fmt, ...) \
65 { \
66 ERROR(fmt, ##__VA_ARGS__); \
67 exit(code); \
68 }
69
70// Put this in the declarations for a class to be uncopyable.
71#define DISALLOW_COPY(TypeName) TypeName(const TypeName&) = delete
72
73// Put this in the declarations for a class to be unassignable.
74#define DISALLOW_ASSIGN(TypeName) TypeName& operator=(const TypeName&) = delete
75
76// Put this in the declarations for a class to be uncopyable and unassignable.
77#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
78 DISALLOW_COPY(TypeName); \
79 DISALLOW_ASSIGN(TypeName)
80
81// Ignore warning for linter for unused variable.
82#define UNUSED(x) (void)(x)
83
84#endif