cmCPackLog.h 3.8 KB

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