Helpers.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. #include "llvm/Support/raw_ostream.h"
  7. using std::string;
  8. using llvm::raw_ostream;
  9. class Log
  10. {
  11. public:
  12. enum class LogLevel { Error, Info, Verbose };
  13. private:
  14. static LogLevel s_logLevel;
  15. public:
  16. static LogLevel GetLevel() { return s_logLevel; }
  17. static void SetLevel(LogLevel level) { s_logLevel = level; }
  18. static raw_ostream& errs()
  19. {
  20. return llvm::outs(); // use same outs stream for better output order
  21. }
  22. static raw_ostream& outs()
  23. {
  24. return s_logLevel >= LogLevel::Info ? llvm::outs() : llvm::nulls();
  25. }
  26. static raw_ostream& verbose()
  27. {
  28. return s_logLevel >= LogLevel::Verbose ? llvm::outs() : llvm::nulls();
  29. }
  30. };
  31. template <size_t N>
  32. bool StartsWith(const char* s, const char (&prefix)[N])
  33. {
  34. return strncmp(s, prefix, N - 1) == 0;
  35. }
  36. template <size_t N>
  37. bool StartsWith(const string& s, const char (&prefix)[N])
  38. {
  39. return s.length() >= N - 1 && s.compare(0, N - 1, prefix, N - 1) == 0;
  40. }
  41. inline bool StartsWith(const char* s, const string& prefix)
  42. {
  43. return strncmp(s, prefix.c_str(), prefix.length()) == 0;
  44. }
  45. template <class Set, class Item>
  46. bool Contains(const Set& s, const Item& item)
  47. {
  48. return s.find(item) != s.end();
  49. }
  50. inline bool Contains(const string& s, const char* sub)
  51. {
  52. return s.find(sub) != string::npos;
  53. }