1 /* 2 * 3 * Copyright 2015, The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 #ifndef SYSTEM_EXTRAS_PERFPROFD_CONFIGREADER_H_ 19 #define SYSTEM_EXTRAS_PERFPROFD_CONFIGREADER_H_ 20 21 #include <memory> 22 #include <string> 23 24 #include "config.h" 25 26 namespace android { 27 namespace perfprofd { 28 class ProfilingConfig; // Config proto. 29 } // namespace perfprofd 30 } // namespace android 31 32 // 33 // This table describes the perfprofd config file syntax in terms of 34 // key/value pairs. Values come in two flavors: strings, or unsigned 35 // integers. In the latter case the reader sets allowable 36 // minimum/maximum for the setting. 37 // 38 class ConfigReader { 39 40 public: 41 ConfigReader(); 42 ~ConfigReader(); 43 44 // Ask for the current setting of a config item 45 unsigned getUnsignedValue(const char *key) const; 46 bool getBoolValue(const char *key) const; 47 std::string getStringValue(const char *key) const; 48 49 // read the specified config file, applying any settings it contains 50 // returns true for successful read, false if conf file cannot be opened. 51 bool readFile(); 52 53 bool Read(const std::string& data, bool fail_on_error, std::string* error_msg); 54 55 // set/get path to config file 56 static void setConfigFilePath(const char *path); 57 static const char *getConfigFilePath(); 58 59 // override a config item (for unit testing purposes) 60 void overrideUnsignedEntry(const char *key, unsigned new_value); 61 62 void FillConfig(Config* config); 63 static std::string ConfigToString(const Config& config); 64 65 static void ProtoToConfig(const android::perfprofd::ProfilingConfig& in, Config* out); 66 67 private: 68 void addUnsignedEntry(const char *key, 69 unsigned default_value, 70 unsigned min_value, 71 unsigned max_value); 72 void addStringEntry(const char *key, const char *default_value); 73 void addDefaultEntries(); 74 bool parseLine(const std::string& key, 75 const std::string& value, 76 unsigned linecount, 77 std::string* error_msg); 78 79 struct Data; 80 std::unique_ptr<Data> data_; 81 }; 82 83 #endif 84