blob: 9ca204656d82a88e1397b917a5c6a08ab67962f3 [file] [log] [blame]
Mike Frysinger3a446f22022-09-08 07:37:141// Copyright 2021 The ChromiumOS Authors
Jae Hoon Kim0fbd6472021-04-29 19:08:332// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef MINIOS_UTILS_H_
6#define MINIOS_UTILS_H_
7
8#include <string>
9#include <tuple>
10
11#include <base/files/file_path.h>
12
Vyshu70660892021-06-09 16:52:2613#include "minios/process_manager.h"
14
Jae Hoon Kim0fbd6472021-04-29 19:08:3315namespace minios {
16
Saketh Pothireddy2038a782023-03-03 00:00:5717// Alert Log error categories.
18extern const char kCategoryInit[];
19extern const char kCategoryReboot[];
20extern const char kCategoryUpdate[];
21
Jae Hoon Kim0fbd6472021-04-29 19:08:3322// Reads the content of `file_path` from `start_offset` to `end_offset` with
23// maximum characters per line being `max_columns` at max. If the file ends
24// before reading all bytes between `start_offset` and `end_offset` it will
25// return true.
26// - bool: Success or failure.
27// - std::string: The content read.
28std::tuple<bool, std::string> ReadFileContentWithinRange(
29 const base::FilePath& file_path,
30 int64_t start_offset,
31 int64_t end_offset,
32 int num_cols);
33
34// Reads the content of `file_path` from `offset`.
35// The `num_lines` and `num_cols` is the maximum amount of lines and characters
36// per line that will be read.
37// The return will include:
38// - bool: Success or failure.
39// - std::string: The content read.
40// - int64_t: The number of bytes read.
41// Note: The number of bytes read can differ than the length of the content
42// output in the second tuple element because the content read is formatted to
43// number of lines and columns format to fit onto the requested area of
44// `num_lines` * `num_cols`.
45std::tuple<bool, std::string, int64_t> ReadFileContent(
46 const base::FilePath& file_path,
47 int64_t offset,
48 int num_lines,
49 int num_cols);
50
Vyshu25e45bf2021-09-02 20:36:3851// Gets VPD region data given a key. Returns false on failure.
52bool GetCrosRegionData(ProcessManagerInterface* process_manager,
53 std::string key,
54 std::string* value);
55
56// Gets XKB keyboard data and extracts country code from it. Defaults to "us" on
57// failure.
58std::string GetKeyboardLayout(ProcessManagerInterface* process_manager);
Vyshu70660892021-06-09 16:52:2659
Vyshue9a22a7b2021-10-08 14:55:5360bool TriggerShutdown();
61
Saketh Pothireddy2038a782023-03-03 00:00:5762// Create a tag that can be added to an Error log message to allow easier
63// filtering from listnr logs. Expected to be used as the first field of a log
64// message. e.g.: `LOG(ERROR) << AlertLogTag(kCategoryName) << err_msg << ....;`
65inline std::string AlertLogTag(const std::string& category) {
66 return base::StringPrintf("[CoreServicesAlert<%s>] ", category.c_str());
67}
68
Jae Hoon Kim0fbd6472021-04-29 19:08:3369} // namespace minios
70
71#endif // MINIOS_UTILS_H__