cmCPackLog.h 4.0 KB

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