cmCPackLog.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmCPackLog_h
  4. #define cmCPackLog_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <memory>
  7. #include <ostream>
  8. #include <string>
  9. #include <string.h>
  10. #define cmCPack_Log(ctSelf, logType, msg) \
  11. do { \
  12. std::ostringstream cmCPackLog_msg; \
  13. cmCPackLog_msg << msg; \
  14. (ctSelf)->Log(logType, __FILE__, __LINE__, cmCPackLog_msg.str().c_str()); \
  15. } while (false)
  16. /** \class cmCPackLog
  17. * \brief A container for CPack generators
  18. *
  19. */
  20. class cmCPackLog
  21. {
  22. public:
  23. cmCPackLog();
  24. ~cmCPackLog();
  25. cmCPackLog(const cmCPackLog&) = delete;
  26. cmCPackLog& operator=(const cmCPackLog&) = delete;
  27. enum __log_tags
  28. {
  29. NOTAG = 0,
  30. LOG_OUTPUT = 0x1,
  31. LOG_VERBOSE = 0x2,
  32. LOG_DEBUG = 0x4,
  33. LOG_WARNING = 0x8,
  34. LOG_ERROR = 0x10
  35. };
  36. //! Various signatures for logging.
  37. void Log(const char* file, int line, const char* msg)
  38. {
  39. this->Log(LOG_OUTPUT, file, line, msg);
  40. }
  41. void Log(const char* file, int line, const char* msg, size_t length)
  42. {
  43. this->Log(LOG_OUTPUT, file, line, msg, length);
  44. }
  45. void Log(int tag, const char* file, int line, const char* msg)
  46. {
  47. this->Log(tag, file, line, msg, strlen(msg));
  48. }
  49. void Log(int tag, const char* file, int line, const char* msg,
  50. size_t length);
  51. //! Set Verbose
  52. void VerboseOn() { this->SetVerbose(true); }
  53. void VerboseOff() { this->SetVerbose(true); }
  54. void SetVerbose(bool verb) { this->Verbose = verb; }
  55. bool GetVerbose() { return this->Verbose; }
  56. //! Set Debug
  57. void DebugOn() { this->SetDebug(true); }
  58. void DebugOff() { this->SetDebug(true); }
  59. void SetDebug(bool verb) { this->Debug = verb; }
  60. bool GetDebug() { return this->Debug; }
  61. //! Set Quiet
  62. void QuietOn() { this->SetQuiet(true); }
  63. void QuietOff() { this->SetQuiet(true); }
  64. void SetQuiet(bool verb) { this->Quiet = verb; }
  65. bool GetQuiet() { return this->Quiet; }
  66. //! Set the output stream
  67. void SetOutputStream(std::ostream* os) { this->DefaultOutput = os; }
  68. //! Set the error stream
  69. void SetErrorStream(std::ostream* os) { this->DefaultError = os; }
  70. //! Set the log output stream
  71. void SetLogOutputStream(std::ostream* os);
  72. //! Set the log output file. The cmCPackLog will try to create file. If it
  73. // cannot, it will report an error.
  74. bool SetLogOutputFile(const char* fname);
  75. //! Set the various prefixes for the logging. SetPrefix sets the generic
  76. // prefix that overwrites missing ones.
  77. void SetPrefix(std::string const& pfx) { this->Prefix = pfx; }
  78. void SetOutputPrefix(std::string const& pfx) { this->OutputPrefix = pfx; }
  79. void SetVerbosePrefix(std::string const& pfx) { this->VerbosePrefix = pfx; }
  80. void SetDebugPrefix(std::string const& pfx) { this->DebugPrefix = pfx; }
  81. void SetWarningPrefix(std::string const& pfx) { this->WarningPrefix = pfx; }
  82. void SetErrorPrefix(std::string const& pfx) { this->ErrorPrefix = pfx; }
  83. private:
  84. bool Verbose = false;
  85. bool Debug = false;
  86. bool Quiet = false;
  87. bool NewLine = true;
  88. int LastTag = cmCPackLog::NOTAG;
  89. std::string Prefix;
  90. std::string OutputPrefix;
  91. std::string VerbosePrefix;
  92. std::string DebugPrefix;
  93. std::string WarningPrefix;
  94. std::string ErrorPrefix;
  95. std::ostream* DefaultOutput = nullptr;
  96. std::ostream* DefaultError = nullptr;
  97. std::ostream* LogOutput = nullptr;
  98. std::unique_ptr<std::ostream> LogOutputStream;
  99. };
  100. class cmCPackLogWrite
  101. {
  102. public:
  103. cmCPackLogWrite(const char* data, size_t length)
  104. : Data(data)
  105. , Length(length)
  106. {
  107. }
  108. const char* Data;
  109. size_t Length;
  110. };
  111. inline std::ostream& operator<<(std::ostream& os, const cmCPackLogWrite& c)
  112. {
  113. os.write(c.Data, c.Length);
  114. os.flush();
  115. return os;
  116. }
  117. #endif