cmCPackLog.cxx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  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. #include "cmCPackLog.h"
  11. #include "cmGeneratedFileStream.h"
  12. #include "cmSystemTools.h"
  13. cmCPackLog::cmCPackLog()
  14. {
  15. this->Verbose = false;
  16. this->Debug = false;
  17. this->Quiet = false;
  18. this->NewLine = true;
  19. this->LastTag = cmCPackLog::NOTAG;
  20. #undef cerr
  21. #undef cout
  22. this->DefaultOutput = &std::cout;
  23. this->DefaultError = &std::cerr;
  24. this->LogOutput = CM_NULLPTR;
  25. this->LogOutputCleanup = false;
  26. }
  27. cmCPackLog::~cmCPackLog()
  28. {
  29. this->SetLogOutputStream(CM_NULLPTR);
  30. }
  31. void cmCPackLog::SetLogOutputStream(std::ostream* os)
  32. {
  33. if (this->LogOutputCleanup && this->LogOutput) {
  34. delete this->LogOutput;
  35. }
  36. this->LogOutputCleanup = false;
  37. this->LogOutput = os;
  38. }
  39. bool cmCPackLog::SetLogOutputFile(const char* fname)
  40. {
  41. cmGeneratedFileStream* cg = CM_NULLPTR;
  42. if (fname) {
  43. cg = new cmGeneratedFileStream(fname);
  44. }
  45. if (cg && !*cg) {
  46. delete cg;
  47. cg = CM_NULLPTR;
  48. }
  49. this->SetLogOutputStream(cg);
  50. if (!cg) {
  51. return false;
  52. }
  53. this->LogOutputCleanup = true;
  54. return true;
  55. }
  56. void cmCPackLog::Log(int tag, const char* file, int line, const char* msg,
  57. size_t length)
  58. {
  59. // By default no logging
  60. bool display = false;
  61. // Display file and line number if debug
  62. bool useFileAndLine = this->Debug;
  63. bool output = false;
  64. bool debug = false;
  65. bool warning = false;
  66. bool error = false;
  67. bool verbose = false;
  68. // When writing in file, add list of tags whenever tag changes.
  69. std::string tagString;
  70. bool needTagString = false;
  71. if (this->LogOutput && this->LastTag != tag) {
  72. needTagString = true;
  73. }
  74. if (tag & LOG_OUTPUT) {
  75. output = true;
  76. display = true;
  77. if (needTagString) {
  78. if (!tagString.empty()) {
  79. tagString += ",";
  80. }
  81. tagString = "VERBOSE";
  82. }
  83. }
  84. if (tag & LOG_WARNING) {
  85. warning = true;
  86. display = true;
  87. if (needTagString) {
  88. if (!tagString.empty()) {
  89. tagString += ",";
  90. }
  91. tagString = "WARNING";
  92. }
  93. }
  94. if (tag & LOG_ERROR) {
  95. error = true;
  96. display = true;
  97. if (needTagString) {
  98. if (!tagString.empty()) {
  99. tagString += ",";
  100. }
  101. tagString = "ERROR";
  102. }
  103. }
  104. if (tag & LOG_DEBUG && this->Debug) {
  105. debug = true;
  106. display = true;
  107. if (needTagString) {
  108. if (!tagString.empty()) {
  109. tagString += ",";
  110. }
  111. tagString = "DEBUG";
  112. }
  113. useFileAndLine = true;
  114. }
  115. if (tag & LOG_VERBOSE && this->Verbose) {
  116. verbose = true;
  117. display = true;
  118. if (needTagString) {
  119. if (!tagString.empty()) {
  120. tagString += ",";
  121. }
  122. tagString = "VERBOSE";
  123. }
  124. }
  125. if (this->Quiet) {
  126. display = false;
  127. }
  128. if (this->LogOutput) {
  129. if (needTagString) {
  130. *this->LogOutput << "[" << file << ":" << line << " " << tagString
  131. << "] ";
  132. }
  133. this->LogOutput->write(msg, length);
  134. }
  135. this->LastTag = tag;
  136. if (!display) {
  137. return;
  138. }
  139. if (this->NewLine) {
  140. if (error && !this->ErrorPrefix.empty()) {
  141. *this->DefaultError << this->ErrorPrefix;
  142. } else if (warning && !this->WarningPrefix.empty()) {
  143. *this->DefaultError << this->WarningPrefix;
  144. } else if (output && !this->OutputPrefix.empty()) {
  145. *this->DefaultOutput << this->OutputPrefix;
  146. } else if (verbose && !this->VerbosePrefix.empty()) {
  147. *this->DefaultOutput << this->VerbosePrefix;
  148. } else if (debug && !this->DebugPrefix.empty()) {
  149. *this->DefaultOutput << this->DebugPrefix;
  150. } else if (!this->Prefix.empty()) {
  151. *this->DefaultOutput << this->Prefix;
  152. }
  153. if (useFileAndLine) {
  154. if (error || warning) {
  155. *this->DefaultError << file << ":" << line << " ";
  156. } else {
  157. *this->DefaultOutput << file << ":" << line << " ";
  158. }
  159. }
  160. }
  161. if (error || warning) {
  162. this->DefaultError->write(msg, length);
  163. this->DefaultError->flush();
  164. } else {
  165. this->DefaultOutput->write(msg, length);
  166. this->DefaultOutput->flush();
  167. }
  168. if (msg[length - 1] == '\n' || length > 2) {
  169. this->NewLine = true;
  170. }
  171. if (error) {
  172. cmSystemTools::SetErrorOccured();
  173. }
  174. }