configreader.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #ifndef SYSTEM_EXTRAS_PERFPROFD_CONFIGREADER_H_
  18. #define SYSTEM_EXTRAS_PERFPROFD_CONFIGREADER_H_
  19. #include <memory>
  20. #include <string>
  21. #include "config.h"
  22. namespace android {
  23. namespace perfprofd {
  24. class ProfilingConfig; // Config proto.
  25. } // namespace perfprofd
  26. } // namespace android
  27. //
  28. // This table describes the perfprofd config file syntax in terms of
  29. // key/value pairs. Values come in two flavors: strings, or unsigned
  30. // integers. In the latter case the reader sets allowable
  31. // minimum/maximum for the setting.
  32. //
  33. class ConfigReader {
  34. public:
  35. ConfigReader();
  36. ~ConfigReader();
  37. // Ask for the current setting of a config item
  38. unsigned getUnsignedValue(const char *key) const;
  39. bool getBoolValue(const char *key) const;
  40. std::string getStringValue(const char *key) const;
  41. // read the specified config file, applying any settings it contains
  42. // returns true for successful read, false if conf file cannot be opened.
  43. bool readFile();
  44. bool Read(const std::string& data, bool fail_on_error, std::string* error_msg);
  45. // set/get path to config file
  46. static void setConfigFilePath(const char *path);
  47. static const char *getConfigFilePath();
  48. // override a config item (for unit testing purposes)
  49. void overrideUnsignedEntry(const char *key, unsigned new_value);
  50. void FillConfig(Config* config);
  51. static std::string ConfigToString(const Config& config);
  52. static void ProtoToConfig(const android::perfprofd::ProfilingConfig& in, Config* out);
  53. private:
  54. void addUnsignedEntry(const char *key,
  55. unsigned default_value,
  56. unsigned min_value,
  57. unsigned max_value);
  58. void addStringEntry(const char *key, const char *default_value);
  59. void addDefaultEntries();
  60. bool parseLine(const std::string& key,
  61. const std::string& value,
  62. unsigned linecount,
  63. std::string* error_msg);
  64. struct Data;
  65. std::unique_ptr<Data> data_;
  66. };
  67. #endif