Ninja
build_log.h
Go to the documentation of this file.
1// Copyright 2011 Google Inc. 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
15#ifndef NINJA_BUILD_LOG_H_
16#define NINJA_BUILD_LOG_H_
17
18#include <stdio.h>
19
20#include <memory>
21#include <string>
22
23#include "hash_map.h"
24#include "load_status.h"
25#include "timestamp.h"
26#include "util.h" // uint64_t
27
28struct DiskInterface;
29struct Edge;
30
31/// Can answer questions about the manifest for the BuildLog.
33 /// Return if a given output is no longer part of the build manifest.
34 /// This is only called during recompaction and doesn't have to be fast.
35 virtual bool IsPathDead(StringPiece s) const = 0;
36};
37
38/// Store a log of every command ran for every build.
39/// It has a few uses:
40///
41/// 1) (hashes of) command lines for existing output files, so we know
42/// when we need to rebuild due to the command changing
43/// 2) timing information, perhaps for generating reports
44/// 3) restat information
45struct BuildLog {
47 ~BuildLog();
48
49 /// Prepares writing to the log file without actually opening it - that will
50 /// happen when/if it's needed
51 bool OpenForWrite(const std::string& path, const BuildLogUser& user,
52 std::string* err);
53 bool RecordCommand(Edge* edge, int start_time, int end_time,
54 TimeStamp mtime = 0);
55 void Close();
56
57 /// Load the on-disk log.
58 LoadStatus Load(const std::string& path, std::string* err);
59
60 struct LogEntry {
61 std::string output;
63 int start_time = 0;
64 int end_time = 0;
66
67 static uint64_t HashCommand(StringPiece command);
68
69 // Used by tests.
70 bool operator==(const LogEntry& o) const {
71 return output == o.output && command_hash == o.command_hash &&
73 mtime == o.mtime;
74 }
75
76 explicit LogEntry(std::string output);
77 LogEntry(const std::string& output, uint64_t command_hash,
79 };
80
81 /// Lookup a previously-run command by its output path.
82 LogEntry* LookupByOutput(const std::string& path);
83
84 /// Serialize an entry into a log file.
85 bool WriteEntry(FILE* f, const LogEntry& entry);
86
87 /// Rewrite the known log entries, throwing away old data.
88 bool Recompact(const std::string& path, const BuildLogUser& user,
89 std::string* err);
90
91 /// Restat all outputs in the log
92 bool Restat(StringPiece path, const DiskInterface& disk_interface,
93 int output_count, char** outputs, std::string* err);
94
96 const Entries& entries() const { return entries_; }
97
98 private:
99 /// Should be called before using log_file_. When false is returned, errno
100 /// will be set.
102
104 FILE* log_file_ = nullptr;
105 std::string log_file_path_;
107};
108
109#endif // NINJA_BUILD_LOG_H_
LoadStatus
Definition load_status.h:18
Can answer questions about the manifest for the BuildLog.
Definition build_log.h:32
virtual bool IsPathDead(StringPiece s) const =0
Return if a given output is no longer part of the build manifest.
LogEntry(std::string output)
Definition build_log.cc:64
TimeStamp mtime
Definition build_log.h:65
bool operator==(const LogEntry &o) const
Definition build_log.h:70
uint64_t command_hash
Definition build_log.h:62
static uint64_t HashCommand(StringPiece command)
Definition build_log.cc:60
std::string output
Definition build_log.h:61
LogEntry * LookupByOutput(const std::string &path)
Lookup a previously-run command by its output path.
Definition build_log.cc:327
void Close()
Definition build_log.cc:125
FILE * log_file_
Definition build_log.h:104
bool OpenForWriteIfNeeded()
Should be called before using log_file_.
Definition build_log.cc:132
LoadStatus Load(const std::string &path, std::string *err)
Load the on-disk log.
Definition build_log.cc:208
bool Recompact(const std::string &path, const BuildLogUser &user, std::string *err)
Rewrite the known log entries, throwing away old data.
Definition build_log.cc:340
bool WriteEntry(FILE *f, const LogEntry &entry)
Serialize an entry into a log file.
Definition build_log.cc:334
bool Restat(StringPiece path, const DiskInterface &disk_interface, int output_count, char **outputs, std::string *err)
Restat all outputs in the log.
Definition build_log.cc:389
bool needs_recompaction_
Definition build_log.h:106
const Entries & entries() const
Definition build_log.h:96
std::string log_file_path_
Definition build_log.h:105
bool OpenForWrite(const std::string &path, const BuildLogUser &user, std::string *err)
Prepares writing to the log file without actually opening it - that will happen when/if it's needed.
Definition build_log.cc:77
ExternalStringHashMap< std::unique_ptr< LogEntry > >::Type Entries
Definition build_log.h:95
bool RecordCommand(Edge *edge, int start_time, int end_time, TimeStamp mtime=0)
Definition build_log.cc:90
Entries entries_
Definition build_log.h:103
Interface for accessing the disk.
An edge in the dependency graph; links between Nodes using Rules.
Definition graph.h:175
A template for hash_maps keyed by a StringPiece whose string is owned externally (typically by the va...
Definition hash_map.h:43
StringPiece represents a slice of a string whose memory is managed externally.
int64_t TimeStamp
Definition timestamp.h:31
unsigned long long uint64_t
Definition win32port.h:29