InstallLog.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #pragma once
  16. #include <fstream>
  17. #include <functional>
  18. #include <iostream>
  19. #include <memory>
  20. #include <mutex>
  21. #include <string>
  22. #include <vector>
  23. /**
  24. * The installer log.
  25. *
  26. * Collects every message of an installation in one place: it is written to a log file in the installation
  27. * directory, passed to an optional sink (the log window), and counted by severity to show a summary at the end.
  28. *
  29. * While open, anything the installer prints to std::cout or std::cerr is captured too, so messages printed by
  30. * code that knows nothing about the log are not lost. The severity of a line is taken from its prefix:
  31. * "[ERROR]" and "Exception" are errors, "[WARNING]" is a warning, anything else is information.
  32. *
  33. * It is thread-safe.
  34. */
  35. class InstallLog{
  36. public:
  37. /**
  38. * Message severity.
  39. */
  40. enum Level{INFO, WARNING, ERROR_LEVEL};
  41. /**
  42. * Receives the messages that must be shown to the user.
  43. */
  44. typedef std::function<void(Level, const std::string&)> Sink;
  45. /**
  46. * Gets the log.
  47. */
  48. static InstallLog& Get();
  49. /**
  50. * Starts a new log: truncates the file, resets the counters and starts capturing std::cout and std::cerr.
  51. *
  52. * @param[in] path Path of the log file.
  53. * @param[in] append True to add to the file instead of replacing it.
  54. */
  55. void Open(const std::string& path, bool append = false);
  56. /**
  57. * Stops capturing the standard streams and closes the file.
  58. */
  59. void Close();
  60. /**
  61. * Sets the sink that receives the messages that must be shown to the user.
  62. */
  63. void SetSink(Sink sink);
  64. /**
  65. * Adds a message.
  66. *
  67. * @param[in] message The message. Its prefix sets the severity.
  68. * @param[in] show True if the message must also be passed to the sink.
  69. */
  70. void Add(const std::string& message, bool show = true);
  71. /**
  72. * Starts a step of the installation. The previous step, if any, is ended.
  73. *
  74. * @param[in] name Name of the step, shown in its summary.
  75. */
  76. void BeginStep(const std::string& name);
  77. /**
  78. * Ends the current step. If it had errors or warnings, a line with their number is added to the log.
  79. */
  80. void EndStep();
  81. /**
  82. * Gets the number of messages of a severity.
  83. */
  84. size_t Count(Level level) const;
  85. /**
  86. * Gets the first messages of a severity.
  87. *
  88. * @param[in] level The severity.
  89. * @param[in] max Maximum number of messages.
  90. */
  91. std::vector<std::string> First(Level level, size_t max) const;
  92. /**
  93. * Gets the path of the log file.
  94. */
  95. std::string Path() const;
  96. /**
  97. * Gets the severity of a message according to its prefix.
  98. */
  99. static Level Classify(const std::string& message);
  100. private:
  101. class Tee;
  102. InstallLog();
  103. /**
  104. * Handles a line captured from a standard stream.
  105. */
  106. void AddCaptured(const std::string& line);
  107. mutable std::mutex mutex_;
  108. std::ofstream file_;
  109. std::string path_;
  110. Sink sink_;
  111. size_t counts_[3];
  112. std::vector<std::string> first_[3];
  113. std::string last_message_;
  114. std::string step_name_;
  115. size_t step_start_[3];
  116. bool in_step_ = false;
  117. std::unique_ptr<Tee> tee_out_;
  118. std::unique_ptr<Tee> tee_err_;
  119. };