cmCPackLog.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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) { this->Log(LOG_OUTPUT, file, line, msg); }
  50. void Log(const char* file, int line, const char* msg, size_t length) { this->Log(LOG_OUTPUT, file, line, msg, length); }
  51. void Log(int tag, const char* file, int line, const char* msg) { this->Log(tag, file, line, msg, strlen(msg)); }
  52. void Log(int tag, const char* file, int line, const char* msg, size_t length);
  53. //! Set Verbose
  54. void VerboseOn() { this->SetVerbose(true); }
  55. void VerboseOff() { this->SetVerbose(true); }
  56. void SetVerbose(bool verb) { m_Verbose = verb; }
  57. bool GetVerbose() { return m_Verbose; }
  58. //! Set Debug
  59. void DebugOn() { this->SetDebug(true); }
  60. void DebugOff() { this->SetDebug(true); }
  61. void SetDebug(bool verb) { m_Debug = verb; }
  62. bool GetDebug() { return m_Debug; }
  63. //! Set Quiet
  64. void QuietOn() { this->SetQuiet(true); }
  65. void QuietOff() { this->SetQuiet(true); }
  66. void SetQuiet(bool verb) { m_Quiet = verb; }
  67. bool GetQuiet() { return m_Quiet; }
  68. //! Set the output stream
  69. void SetOutputStream(std::ostream* os) { m_DefaultOutput = os; }
  70. //! Set the error stream
  71. void SetErrorStream(std::ostream* os) { m_DefaultError = os; }
  72. //! Set the log output stream
  73. void SetLogOutputStream(std::ostream* os);
  74. //! Set the log output file. The cmCPackLog will try to create file. If it
  75. // cannot, it will report an error.
  76. bool SetLogOutputFile(const char* fname);
  77. //! Set the various prefixes for the logging. SetPrefix sets the generic
  78. // prefix that overwrittes missing ones.
  79. void SetPrefix(std::string pfx) { m_Prefix = pfx; }
  80. void SetOutputPrefix(std::string pfx) { m_OutputPrefix = pfx; }
  81. void SetVerbosePrefix(std::string pfx) { m_VerbosePrefix = pfx; }
  82. void SetDebugPrefix(std::string pfx) { m_DebugPrefix = pfx; }
  83. void SetWarningPrefix(std::string pfx) { m_WarningPrefix = pfx; }
  84. void SetErrorPrefix(std::string pfx) { m_ErrorPrefix = pfx; }
  85. private:
  86. bool m_Verbose;
  87. bool m_Debug;
  88. bool m_Quiet;
  89. bool m_NewLine;
  90. int m_LastTag;
  91. std::string m_Prefix;
  92. std::string m_OutputPrefix;
  93. std::string m_VerbosePrefix;
  94. std::string m_DebugPrefix;
  95. std::string m_WarningPrefix;
  96. std::string m_ErrorPrefix;
  97. std::ostream *m_DefaultOutput;
  98. std::ostream *m_DefaultError;
  99. std::string m_LogOutputFileName;
  100. std::ostream *m_LogOutput;
  101. // Do we need to cleanup log output stream
  102. bool m_LogOutputCleanup;
  103. };
  104. class cmCPackLogWrite
  105. {
  106. public:
  107. cmCPackLogWrite(const char* data, size_t length) : Data(data), Length(length) {}
  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