routine_log.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_SYSTEM_DIAGNOSTICS_ROUTINE_LOG_H_
  5. #define ASH_SYSTEM_DIAGNOSTICS_ROUTINE_LOG_H_
  6. #include <memory>
  7. #include <string>
  8. #include "ash/ash_export.h"
  9. #include "ash/system/diagnostics/async_log.h"
  10. #include "ash/webui/diagnostics_ui/mojom/system_routine_controller.mojom.h"
  11. #include "base/containers/flat_map.h"
  12. #include "base/files/file_path.h"
  13. namespace ash {
  14. namespace diagnostics {
  15. // RoutineLog is used to record the status and outcome of Diagnostics Routines.
  16. // Each time `LogRoutineStarted()` or `LogRoutineCompleted()` is called, a new
  17. // line is add to the log for the routine's category. `log_base_path`
  18. // is the directory used to write the category logs which have the name
  19. // "diagnostics_routines_{category_name}.log".
  20. class ASH_EXPORT RoutineLog {
  21. public:
  22. enum class RoutineCategory {
  23. kNetwork = 0,
  24. kSystem = 1,
  25. };
  26. explicit RoutineLog(const base::FilePath& log_base_path);
  27. ~RoutineLog();
  28. RoutineLog(const RoutineLog&) = delete;
  29. RoutineLog& operator=(const RoutineLog&) = delete;
  30. // LogRoutine* functions schedule a task using sequence_task_runner_ to add an
  31. // entry in the routine log file. Tasks are run on blockable, sequenced task
  32. // runner to support I/O operations.
  33. void LogRoutineStarted(mojom::RoutineType type);
  34. void LogRoutineCompleted(mojom::RoutineType type,
  35. mojom::StandardRoutineResult result);
  36. void LogRoutineCancelled(mojom::RoutineType type);
  37. // Returns the current RoutineLog as a string.
  38. std::string GetContentsForCategory(const RoutineCategory category) const;
  39. private:
  40. // Append `text` to the category corresponding to `type`.
  41. void Append(mojom::RoutineType type, const std::string& text);
  42. // Get the path to the log file for `category`.
  43. base::FilePath GetCategoryLogFilePath(const RoutineCategory category);
  44. // The base directory for storing logs.
  45. const base::FilePath log_base_path_;
  46. // A map of log files where the key is the category.
  47. base::flat_map<RoutineCategory, std::unique_ptr<AsyncLog>> logs_;
  48. };
  49. } // namespace diagnostics
  50. } // namespace ash
  51. #endif // ASH_SYSTEM_DIAGNOSTICS_ROUTINE_LOG_H_