cmCPackLog.h 4.1 KB

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