1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66

ash / system / diagnostics / routine_log.h [blame]

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_SYSTEM_DIAGNOSTICS_ROUTINE_LOG_H_
#define ASH_SYSTEM_DIAGNOSTICS_ROUTINE_LOG_H_

#include <memory>
#include <string>

#include "ash/ash_export.h"
#include "ash/system/diagnostics/async_log.h"
#include "ash/webui/diagnostics_ui/mojom/system_routine_controller.mojom.h"
#include "base/containers/flat_map.h"
#include "base/files/file_path.h"

namespace ash {
namespace diagnostics {

// RoutineLog is used to record the status and outcome of Diagnostics Routines.
// Each time `LogRoutineStarted()` or `LogRoutineCompleted()` is called, a new
// line is add to the log for the routine's category. `log_base_path`
// is the directory used to write the category logs which have the name
// "diagnostics_routines_{category_name}.log".
class ASH_EXPORT RoutineLog {
 public:
  enum class RoutineCategory {
    kNetwork = 0,
    kSystem = 1,
  };

  explicit RoutineLog(const base::FilePath& log_base_path);
  ~RoutineLog();

  RoutineLog(const RoutineLog&) = delete;
  RoutineLog& operator=(const RoutineLog&) = delete;

  // LogRoutine* functions schedule a task using sequence_task_runner_ to add an
  // entry in the routine log file. Tasks are run on blockable, sequenced task
  // runner to support I/O operations.
  void LogRoutineStarted(mojom::RoutineType type);
  void LogRoutineCompleted(mojom::RoutineType type,
                           mojom::StandardRoutineResult result);
  void LogRoutineCancelled(mojom::RoutineType type);

  // Returns the current RoutineLog as a string.
  std::string GetContentsForCategory(const RoutineCategory category) const;

 private:
  // Append `text` to the category corresponding to `type`.
  void Append(mojom::RoutineType type, const std::string& text);

  // Get the path to the log file for `category`.
  base::FilePath GetCategoryLogFilePath(const RoutineCategory category);

  // The base directory for storing logs.
  const base::FilePath log_base_path_;

  // A map of log files where the key is the category.
  base::flat_map<RoutineCategory, std::unique_ptr<AsyncLog>> logs_;
};

}  // namespace diagnostics
}  // namespace ash

#endif  // ASH_SYSTEM_DIAGNOSTICS_ROUTINE_LOG_H_