File: log_source.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (87 lines) | stat: -rw-r--r-- 3,366 bytes parent folder | download | duplicates (6)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_ASH_CHROMEBOX_FOR_MEETINGS_ARTEMIS_LOG_SOURCE_H_
#define CHROME_BROWSER_ASH_CHROMEBOX_FOR_MEETINGS_ARTEMIS_LOG_SOURCE_H_

#include "base/memory/weak_ptr.h"
#include "chrome/browser/ash/chromebox_for_meetings/artemis/local_data_source.h"
#include "chrome/browser/ash/chromebox_for_meetings/artemis/log_file.h"
#include "chrome/browser/ash/chromebox_for_meetings/artemis/persistent_db.h"
#include "chromeos/services/chromebox_for_meetings/public/mojom/meet_devices_data_aggregator.mojom.h"

namespace ash::cfm {

inline constexpr char kCfmChromeLogFile[] = "/var/log/chrome/chrome";
inline constexpr char kCfmChromeUserLogFile[] = "/home/chronos/user/log/chrome";
inline constexpr char kCfmCrosEcLogFile[] = "/var/log/cros_ec.log";
inline constexpr char kCfmFwupdLogFile[] = "/var/log/fwupd.log";
inline constexpr char kCfmPowerdLogFile[] = "/var/log/powerd.out";
inline constexpr char kCfmSyslogLogFile[] = "/var/log/messages";
inline constexpr char kCfmUiLogFile[] = "/var/log/ui/ui.LATEST";
inline constexpr char kCfmUpdateEngineLogFile[] = "/var/log/update_engine.log";

// Dummy value for when a call to stat() fails to obtain
// a valid inode. Unlikely to be used, but be defensive.
inline constexpr int kInvalidFileInode = -1;

// This class tracks data from a single log file.
class LogSource : public LocalDataSource {
 public:
  LogSource(const std::string& filepath,
            base::TimeDelta poll_rate,
            size_t batch_size);
  LogSource(const LogSource&) = delete;
  LogSource& operator=(const LogSource&) = delete;
  ~LogSource() override;

  // LocalDataSource:
  void Fetch(FetchCallback callback) override;
  void Flush() override;
  const std::string& GetDisplayName() override;
  std::vector<std::string> GetNextData() override;

  // Getter that returns the proper LogSource child class depending
  // on the provided filename.
  static std::unique_ptr<LogSource> Create(const std::string& filename,
                                           base::TimeDelta poll_rate,
                                           size_t batch_size);

  bool InitializeFile();

 protected:
  int GetCurrentFileInode();
  bool DidFileRotate();

 private:
  std::streampos GetLastKnownOffsetFromStorage();
  void PersistCurrentOffsetToStorage();

  std::string filepath_;

  // The number of times we've attempted to open the file.
  int num_failed_open_attempts_ = 0;

  // Contains a handle to the log file on disk
  // TODO(b/320996557): this should be a collection of log files
  // after adding rotation support.
  LogFile log_file_;

  // Number of lines to read from the log file at each iteration.
  const size_t batch_size_;

  // Keep track of the last-known inode to detect when the underlying
  // file has rotated. Inodes will not change when the file is renamed.
  int last_known_inode_ = kInvalidFileInode;

  // File offset to seek to after a crash or reboot.
  // Cached here in memory, then flushed to disk on successful log upload.
  std::streampos recovery_offset_ = 0;

  // Must be the last class member.
  base::WeakPtrFactory<LogSource> weak_ptr_factory_{this};
};

}  // namespace ash::cfm
#endif  // CHROME_BROWSER_ASH_CHROMEBOX_FOR_MEETINGS_ARTEMIS_LOG_SOURCE_H_