prune_crash_reports.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2015 The Crashpad Authors. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef CRASHPAD_CLIENT_PRUNE_CRASH_REPORTS_H_
  15. #define CRASHPAD_CLIENT_PRUNE_CRASH_REPORTS_H_
  16. #include <sys/types.h>
  17. #include <time.h>
  18. #include <memory>
  19. #include "base/macros.h"
  20. #include "client/crash_report_database.h"
  21. namespace crashpad {
  22. class PruneCondition;
  23. //! \brief Deletes crash reports from \a database that match \a condition.
  24. //!
  25. //! This function can be used to remove old or large reports from the database.
  26. //! The \a condition will be evaluated against each report in the \a database,
  27. //! sorted in descending order by CrashReportDatabase::Report::creation_time.
  28. //! This guarantee allows conditions to be stateful.
  29. //!
  30. //! \param[in] database The database from which crash reports will be deleted.
  31. //! \param[in] condition The condition against which all reports in the database
  32. //! will be evaluated.
  33. void PruneCrashReportDatabase(CrashReportDatabase* database,
  34. PruneCondition* condition);
  35. std::unique_ptr<PruneCondition> GetDefaultDatabasePruneCondition();
  36. //! \brief An abstract base class for evaluating crash reports for deletion.
  37. //!
  38. //! When passed to PruneCrashReportDatabase(), each crash report in the
  39. //! database will be evaluated according to ShouldPruneReport(). The reports
  40. //! are evaluated serially in descending sort order by
  41. //! CrashReportDatabase::Report::creation_time.
  42. class PruneCondition {
  43. public:
  44. //! \brief Returns a sensible default condition for removing obsolete crash
  45. //! reports.
  46. //!
  47. //! The default is to keep reports for one year or a maximum database size
  48. //! of 128 MB.
  49. //!
  50. //! \return A PruneCondition for use with PruneCrashReportDatabase().
  51. static std::unique_ptr<PruneCondition> GetDefault();
  52. virtual ~PruneCondition() {}
  53. //! \brief Evaluates a crash report for deletion.
  54. //!
  55. //! \param[in] report The crash report to evaluate.
  56. //!
  57. //! \return `true` if the crash report should be deleted, `false` if it
  58. //! should be kept.
  59. virtual bool ShouldPruneReport(const CrashReportDatabase::Report& report) = 0;
  60. };
  61. //! \brief A PruneCondition that deletes reports older than the specified number
  62. //! days.
  63. class AgePruneCondition final : public PruneCondition {
  64. public:
  65. //! \brief Creates a PruneCondition based on Report::creation_time.
  66. //!
  67. //! \param[in] max_age_in_days Reports created more than this many days ago
  68. //! will be deleted.
  69. explicit AgePruneCondition(int max_age_in_days);
  70. ~AgePruneCondition();
  71. bool ShouldPruneReport(const CrashReportDatabase::Report& report) override;
  72. private:
  73. const time_t oldest_report_time_;
  74. DISALLOW_COPY_AND_ASSIGN(AgePruneCondition);
  75. };
  76. //! \brief A PruneCondition that deletes older reports to keep the total
  77. //! Crashpad database size under the specified limit.
  78. class DatabaseSizePruneCondition final : public PruneCondition {
  79. public:
  80. //! \brief Creates a PruneCondition that will keep newer reports, until the
  81. //! sum of the size of all reports is not smaller than \a max_size_in_kb.
  82. //! After the limit is reached, older reports will be pruned.
  83. //!
  84. //! \param[in] max_size_in_kb The maximum number of kilobytes that all crash
  85. //! reports should consume.
  86. explicit DatabaseSizePruneCondition(size_t max_size_in_kb);
  87. ~DatabaseSizePruneCondition();
  88. bool ShouldPruneReport(const CrashReportDatabase::Report& report) override;
  89. private:
  90. const size_t max_size_in_kb_;
  91. size_t measured_size_in_kb_;
  92. DISALLOW_COPY_AND_ASSIGN(DatabaseSizePruneCondition);
  93. };
  94. //! \brief A PruneCondition that conjoins two other PruneConditions.
  95. class BinaryPruneCondition final : public PruneCondition {
  96. public:
  97. enum Operator {
  98. AND,
  99. OR,
  100. };
  101. //! \brief Evaluates two sub-conditions according to the specified logical
  102. //! operator.
  103. //!
  104. //! This implements left-to-right evaluation. For Operator::AND, this means
  105. //! if the \a lhs is `false`, the \a rhs will not be consulted. Similarly,
  106. //! with Operator::OR, if the \a lhs is `true`, the \a rhs will not be
  107. //! consulted.
  108. //!
  109. //! \param[in] op The logical operator to apply on \a lhs and \a rhs.
  110. //! \param[in] lhs The left-hand side of \a op. This class takes ownership.
  111. //! \param[in] rhs The right-hand side of \a op. This class takes ownership.
  112. BinaryPruneCondition(Operator op, PruneCondition* lhs, PruneCondition* rhs);
  113. ~BinaryPruneCondition();
  114. bool ShouldPruneReport(const CrashReportDatabase::Report& report) override;
  115. private:
  116. const Operator op_;
  117. std::unique_ptr<PruneCondition> lhs_;
  118. std::unique_ptr<PruneCondition> rhs_;
  119. DISALLOW_COPY_AND_ASSIGN(BinaryPruneCondition);
  120. };
  121. } // namespace crashpad
  122. #endif // CRASHPAD_CLIENT_PRUNE_CRASH_REPORTS_H_