cmCPackLog.cxx 3.9 KB

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