InstallLog.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include "InstallLog.h"
  16. /**
  17. * Number of messages of each severity that are kept for the summary.
  18. */
  19. static const size_t kKeptMessages = 200;
  20. /**
  21. * Stream buffer that passes everything through to the original buffer and sends each complete line to the log.
  22. */
  23. class InstallLog::Tee : public std::streambuf{
  24. public:
  25. Tee(std::ostream& stream, InstallLog& log): stream_(stream), log_(log), original_(stream.rdbuf(this)){}
  26. ~Tee(){stream_.rdbuf(original_);}
  27. /**
  28. * Writes directly to the original stream, without capturing it.
  29. */
  30. void WriteOriginal(const std::string& text){
  31. std::lock_guard<std::mutex> lock(mutex_);
  32. original_->sputn(text.data(), text.size());
  33. }
  34. protected:
  35. int_type overflow(int_type c) override{
  36. if (c == traits_type::eof()) return traits_type::not_eof(c);
  37. // The streams are written from several threads (Ogre's worker threads, the audio conversions), so
  38. // the line buffer and the original buffer are only touched with the lock. The line is sent to the
  39. // log without it, since the log may write to the streams again.
  40. std::string line;
  41. {
  42. std::lock_guard<std::mutex> lock(mutex_);
  43. original_->sputc(static_cast<char>(c));
  44. if (c == '\n') line.swap(line_);
  45. else line_ += static_cast<char>(c);
  46. }
  47. if (!line.empty()) log_.AddCaptured(line);
  48. return c;
  49. }
  50. int sync() override{
  51. std::lock_guard<std::mutex> lock(mutex_);
  52. return original_->pubsync();
  53. }
  54. private:
  55. std::ostream& stream_;
  56. InstallLog& log_;
  57. std::streambuf* original_;
  58. std::string line_;
  59. std::mutex mutex_;
  60. };
  61. InstallLog::InstallLog(): counts_{0, 0, 0}{}
  62. InstallLog& InstallLog::Get(){
  63. static InstallLog log;
  64. return log;
  65. }
  66. void InstallLog::Open(const std::string& path, bool append){
  67. Close();
  68. std::lock_guard<std::mutex> lock(mutex_);
  69. path_ = path;
  70. file_.open(path, std::ios::out | (append ? std::ios::app : std::ios::trunc));
  71. for (int i = 0; i < 3; ++ i){
  72. counts_[i] = 0;
  73. first_[i].clear();
  74. }
  75. last_message_.clear();
  76. in_step_ = false;
  77. tee_out_ = std::make_unique<Tee>(std::cout, *this);
  78. tee_err_ = std::make_unique<Tee>(std::cerr, *this);
  79. }
  80. void InstallLog::Close(){
  81. // The tees restore the streams on destruction. Destroy them without holding the lock, since they
  82. // don't use it.
  83. tee_out_.reset();
  84. tee_err_.reset();
  85. std::lock_guard<std::mutex> lock(mutex_);
  86. if (file_.is_open()) file_.close();
  87. }
  88. void InstallLog::SetSink(Sink sink){
  89. std::lock_guard<std::mutex> lock(mutex_);
  90. sink_ = std::move(sink);
  91. }
  92. InstallLog::Level InstallLog::Classify(const std::string& message){
  93. if (message.compare(0, 7, "[ERROR]") == 0 || message.compare(0, 9, "Exception") == 0) return ERROR_LEVEL;
  94. if (message.compare(0, 9, "[WARNING]") == 0) return WARNING;
  95. return INFO;
  96. }
  97. void InstallLog::Add(const std::string& message, bool show){
  98. const Level level = Classify(message);
  99. Sink sink;
  100. {
  101. std::lock_guard<std::mutex> lock(mutex_);
  102. // Some messages are written twice in a row by the code that produces them.
  103. if (level != INFO && message == last_message_) return;
  104. last_message_ = message;
  105. ++ counts_[level];
  106. if (first_[level].size() < kKeptMessages) first_[level].push_back(message);
  107. if (file_.is_open()) file_ << message << '\n' << std::flush;
  108. if (show) sink = sink_;
  109. }
  110. // Echo to the terminal without capturing the message again.
  111. if (tee_out_) tee_out_->WriteOriginal(message + "\n");
  112. if (sink) sink(level, message);
  113. }
  114. void InstallLog::AddCaptured(const std::string& line){
  115. const Level level = Classify(line);
  116. Sink sink;
  117. {
  118. std::lock_guard<std::mutex> lock(mutex_);
  119. if (level != INFO && line == last_message_) return;
  120. if (level != INFO) last_message_ = line;
  121. ++ counts_[level];
  122. if (level != INFO && first_[level].size() < kKeptMessages) first_[level].push_back(line);
  123. if (file_.is_open()) file_ << line << '\n';
  124. // Only problems are shown in the window; the rest of the captured output is too verbose.
  125. if (level != INFO) sink = sink_;
  126. }
  127. if (sink) sink(level, line);
  128. }
  129. void InstallLog::BeginStep(const std::string& name){
  130. EndStep();
  131. std::lock_guard<std::mutex> lock(mutex_);
  132. step_name_ = name;
  133. for (int i = 0; i < 3; ++ i) step_start_[i] = counts_[i];
  134. in_step_ = true;
  135. }
  136. void InstallLog::EndStep(){
  137. std::string summary;
  138. {
  139. std::lock_guard<std::mutex> lock(mutex_);
  140. if (!in_step_) return;
  141. in_step_ = false;
  142. const size_t errors = counts_[ERROR_LEVEL] - step_start_[ERROR_LEVEL];
  143. const size_t warnings = counts_[WARNING] - step_start_[WARNING];
  144. if (errors == 0 && warnings == 0) return;
  145. summary = "Step \"" + step_name_ + "\" finished with " + std::to_string(errors) + " error(s) and "
  146. + std::to_string(warnings) + " warning(s).";
  147. }
  148. // Not prefixed, so it is information and doesn't count as a problem itself.
  149. Add(summary);
  150. }
  151. size_t InstallLog::Count(Level level) const{
  152. std::lock_guard<std::mutex> lock(mutex_);
  153. return counts_[level];
  154. }
  155. std::vector<std::string> InstallLog::First(Level level, size_t max) const{
  156. std::lock_guard<std::mutex> lock(mutex_);
  157. std::vector<std::string> messages = first_[level];
  158. if (messages.size() > max) messages.resize(max);
  159. return messages;
  160. }
  161. std::string InstallLog::Path() const{
  162. std::lock_guard<std::mutex> lock(mutex_);
  163. return path_;
  164. }