Ninja
build_log.h
Go to the documentation of this file.
00001 // Copyright 2011 Google Inc. All Rights Reserved.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //     http://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #ifndef NINJA_BUILD_LOG_H_
00016 #define NINJA_BUILD_LOG_H_
00017 
00018 #include <map>
00019 #include <string>
00020 using namespace std;
00021 
00022 #include "hash_map.h"
00023 
00024 struct BuildConfig;
00025 struct Edge;
00026 
00027 /// Store a log of every command ran for every build.
00028 /// It has a few uses:
00029 ///
00030 /// 1) historical command lines for output files, so we know
00031 ///    when we need to rebuild due to the command changing
00032 /// 2) historical timing information
00033 /// 3) maybe we can generate some sort of build overview output
00034 ///    from it
00035 struct BuildLog {
00036   BuildLog();
00037   ~BuildLog() { Close(); }
00038 
00039   void SetConfig(BuildConfig* config) { config_ = config; }
00040   bool OpenForWrite(const string& path, string* err);
00041   void RecordCommand(Edge* edge, int start_time, int end_time,
00042                      time_t restat_mtime = 0);
00043   void Close();
00044 
00045   /// Load the on-disk log.
00046   bool Load(const string& path, string* err);
00047 
00048   struct LogEntry {
00049     string output;
00050     string command;
00051     int start_time;
00052     int end_time;
00053     time_t restat_mtime;
00054 
00055     // Used by tests.
00056     bool operator==(const LogEntry& o) {
00057       return output == o.output && command == o.command &&
00058           start_time == o.start_time && end_time == o.end_time &&
00059           restat_mtime == o.restat_mtime;
00060     }
00061   };
00062 
00063   /// Lookup a previously-run command by its output path.
00064   LogEntry* LookupByOutput(const string& path);
00065 
00066   /// Serialize an entry into a log file.
00067   void WriteEntry(FILE* f, const LogEntry& entry);
00068 
00069   /// Rewrite the known log entries, throwing away old data.
00070   bool Recompact(const string& path, string* err);
00071 
00072   typedef ExternalStringHashMap<LogEntry*>::Type Log;
00073   Log log_;
00074   FILE* log_file_;
00075   BuildConfig* config_;
00076   bool needs_recompaction_;
00077 };
00078 
00079 #endif // NINJA_BUILD_LOG_H_