cmCPackLog.h 3.9 KB

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