cmCPackLog.cxx 4.2 KB

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