Mike Frysinger | 3a446f2 | 2022-09-08 07:37:14 | [diff] [blame] | 1 | // Copyright 2021 The ChromiumOS Authors |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 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 MINIOS_UTILS_H_ |
| 6 | #define MINIOS_UTILS_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <tuple> |
| 10 | |
| 11 | #include <base/files/file_path.h> |
| 12 | |
Vyshu | 7066089 | 2021-06-09 16:52:26 | [diff] [blame] | 13 | #include "minios/process_manager.h" |
| 14 | |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 15 | namespace minios { |
| 16 | |
Saketh Pothireddy | 2038a78 | 2023-03-03 00:00:57 | [diff] [blame^] | 17 | // Alert Log error categories. |
| 18 | extern const char kCategoryInit[]; |
| 19 | extern const char kCategoryReboot[]; |
| 20 | extern const char kCategoryUpdate[]; |
| 21 | |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 22 | // 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. |
| 28 | std::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`. |
| 45 | std::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 | |
Vyshu | 25e45bf | 2021-09-02 20:36:38 | [diff] [blame] | 51 | // Gets VPD region data given a key. Returns false on failure. |
| 52 | bool 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. |
| 58 | std::string GetKeyboardLayout(ProcessManagerInterface* process_manager); |
Vyshu | 7066089 | 2021-06-09 16:52:26 | [diff] [blame] | 59 | |
Vyshu | e9a22a7b | 2021-10-08 14:55:53 | [diff] [blame] | 60 | bool TriggerShutdown(); |
| 61 | |
Saketh Pothireddy | 2038a78 | 2023-03-03 00:00:57 | [diff] [blame^] | 62 | // 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 << ....;` |
| 65 | inline std::string AlertLogTag(const std::string& category) { |
| 66 | return base::StringPrintf("[CoreServicesAlert<%s>] ", category.c_str()); |
| 67 | } |
| 68 | |
Jae Hoon Kim | 0fbd647 | 2021-04-29 19:08:33 | [diff] [blame] | 69 | } // namespace minios |
| 70 | |
| 71 | #endif // MINIOS_UTILS_H__ |