]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
feat: Allow specifying debug level (#1312)
authordsilakov <dsilakov@gmail.com>
Wed, 2 Aug 2023 11:24:49 +0000 (14:24 +0300)
committerGitHub <noreply@github.com>
Wed, 2 Aug 2023 11:24:49 +0000 (13:24 +0200)
Allow specifying debug level via a debug_level option or CCACHE_DEBUGLEVEL
environment variable.

With debug_level=2 (default), ccache behavior's in debug mode is not changed.

With debug_level=1, ccache will only dump *.ccache-log files (and not the
*input* ones). This is useful for getting per-file caching results.

doc/MANUAL.adoc
src/Config.cpp
src/Config.hpp
src/ccache.cpp
unittest/test_Config.cpp

index 4d3c6686c806c0c02a8b7caadf2808621afa12b0..05819f23c41914b34cfe9c1d8a4d1ee132bd5504 100644 (file)
@@ -681,6 +681,15 @@ is `/home/user` and the object file is `build/output.o` then the debug log will
 be written to `/example/home/user/build/output.o.ccache-log`. See also
 _<<Cache debugging>>_.
 
+[#config_debug_level]
+*debug_level* (*CCACHE_DEBUGLEVEL*)::
+
+    Specifies amount of information dumped when the <<config_debug,debug mode>>
+    is enabled. If set 1, only `<objectfile>.<timestamp>.ccache-log` files with
+    per-file caching statistics are dumped. Otherwise, all possible debug
+    data is dumped. See _<<Cache debugging>>_ for more information. The default
+    is 2.
+
 [#config_depend_mode]
 *depend_mode* (*CCACHE_DEPEND* or *CCACHE_NODEPEND*, see _<<Boolean values>>_ above)::
 
@@ -1678,6 +1687,12 @@ Log for this object file.
  The timestamp format is
 `<year><month><day>_<hour><minute><second>_<microsecond>`.
 
+`<objectfile>.<timestamp>.ccache-log` files can be useful for checking
+caching results for every file. If you only need such statistics and don't
+need `<objectfile>.<timestamp>.ccache-input-*` files then you can set
+<<config_debug,*debug_level*>> (or environment variable `CCACHE_DEBUGLEVEL`) 
+to 1.
+
 If <<config_debug_dir,*debug_dir*>> (environment variable `CCACHE_DEBUGDIR`) is
 set, the files above will be written to that directory with full absolute paths
 instead of next to the object file.
@@ -1702,7 +1717,6 @@ something like this:
    builds. This together with the `<objectfile>.<timestamp>.ccache-log` files
    should give you some clues about what is happening.
 
-
 == Compiling in different directories
 
 Some information included in the hash that identifies a unique compilation can
index 15b2c3d13bf5c8f38702ede23f2ea8fad2f04a05..7e99e3d2ec7a0352004c3d706509b9d3840bf473 100644 (file)
@@ -81,6 +81,7 @@ enum class ConfigItem {
   cpp_extension,
   debug,
   debug_dir,
+  debug_level,
   depend_mode,
   direct_mode,
   disable,
@@ -136,6 +137,7 @@ const std::unordered_map<std::string, ConfigKeyTableEntry> k_config_key_table =
     {"cpp_extension", {ConfigItem::cpp_extension}},
     {"debug", {ConfigItem::debug}},
     {"debug_dir", {ConfigItem::debug_dir}},
+    {"debug_level", {ConfigItem::debug_level}},
     {"depend_mode", {ConfigItem::depend_mode}},
     {"direct_mode", {ConfigItem::direct_mode}},
     {"disable", {ConfigItem::disable}},
@@ -184,6 +186,7 @@ const std::unordered_map<std::string, std::string> k_env_variable_table = {
   {"CPP2", "run_second_cpp"},
   {"DEBUG", "debug"},
   {"DEBUGDIR", "debug_dir"},
+  {"DEBUGLEVEL", "debug_level"},
   {"DEPEND", "depend_mode"},
   {"DIR", "cache_dir"},
   {"DIRECT", "direct_mode"},
@@ -785,6 +788,9 @@ Config::get_string_value(const std::string& key) const
   case ConfigItem::debug_dir:
     return m_debug_dir;
 
+  case ConfigItem::debug_level:
+    return FMT("{}", m_debug_level);
+
   case ConfigItem::depend_mode:
     return format_bool(m_depend_mode);
 
@@ -1023,6 +1029,11 @@ Config::set_item(const std::string& key,
     m_debug_dir = value;
     break;
 
+  case ConfigItem::debug_level:
+    m_debug_level = util::value_or_throw<core::Error>(
+      util::parse_unsigned(value, std::nullopt, std::nullopt, "debug_level"));
+    break;
+
   case ConfigItem::depend_mode:
     m_depend_mode = parse_bool(value, env_var_key, negate);
     break;
index dcb37ffa7da60ef8a84dec4100d98f0dc2356663..fc43895b3403d1bc87bca2a13d686a080b00e089 100644 (file)
@@ -64,6 +64,7 @@ public:
   const std::string& cpp_extension() const;
   bool debug() const;
   const std::string& debug_dir() const;
+  uint8_t debug_level() const;
   bool depend_mode() const;
   bool direct_mode() const;
   bool disable() const;
@@ -178,6 +179,7 @@ private:
   std::string m_cpp_extension;
   bool m_debug = false;
   std::string m_debug_dir;
+  uint8_t m_debug_level = 2;
   bool m_depend_mode = false;
   bool m_direct_mode = true;
   bool m_disable = false;
@@ -305,6 +307,12 @@ Config::debug_dir() const
   return m_debug_dir;
 }
 
+inline uint8_t
+Config::debug_level() const
+{
+  return m_debug_level;
+}
+
 inline bool
 Config::depend_mode() const
 {
index 8f8e405a444811bc0b8d7f4e645c045867159659..48aac63e6c26b0ff4d53c6e865dccd1386c25fdc 100644 (file)
@@ -220,7 +220,7 @@ init_hash_debug(Context& ctx,
                 std::string_view section_name,
                 FILE* debug_text_file)
 {
-  if (!ctx.config.debug()) {
+  if (!ctx.config.debug() || ctx.config.debug_level() < 2) {
     return;
   }
 
@@ -2500,7 +2500,7 @@ do_cache_compilation(Context& ctx)
   LOG("Object file: {}", ctx.args_info.output_obj);
   MTR_META_THREAD_NAME(ctx.args_info.output_obj.c_str());
 
-  if (ctx.config.debug()) {
+  if (ctx.config.debug() && ctx.config.debug_level() >= 2) {
     const auto path = prepare_debug_path(ctx.config.debug_dir(),
                                          ctx.time_of_invocation,
                                          ctx.args_info.orig_output_obj,
index c2f46e9d96c57de64c0051c149292fdc3207a353..ae2dc9094774052ed40674c556456fee771fcffc 100644 (file)
@@ -51,6 +51,7 @@ TEST_CASE("Config: default values")
   CHECK(config.cpp_extension().empty());
   CHECK(!config.debug());
   CHECK(config.debug_dir().empty());
+  CHECK(config.debug_level() == 2);
   CHECK(!config.depend_mode());
   CHECK(config.direct_mode());
   CHECK(!config.disable());
@@ -111,6 +112,7 @@ TEST_CASE("Config::update_from_file")
     "compression_level= 2\n"
     "cpp_extension = .foo\n"
     "debug_dir = $USER$/${USER}/.ccache_debug\n"
+    "debug_level = 2\n"
     "depend_mode = true\n"
     "direct_mode = false\n"
     "disable = true\n"
@@ -153,6 +155,7 @@ TEST_CASE("Config::update_from_file")
   CHECK(config.compression_level() == 2);
   CHECK(config.cpp_extension() == ".foo");
   CHECK(config.debug_dir() == FMT("{0}$/{0}/.ccache_debug", user));
+  CHECK(config.debug_level() == 2);
   CHECK(config.depend_mode());
   CHECK_FALSE(config.direct_mode());
   CHECK(config.disable());
@@ -396,6 +399,7 @@ TEST_CASE("Config::visit_items")
     "cpp_extension = ce\n"
     "debug = false\n"
     "debug_dir = /dd\n"
+    "debug_level = 2\n"
     "depend_mode = true\n"
     "direct_mode = false\n"
     "disable = true\n"
@@ -457,6 +461,7 @@ TEST_CASE("Config::visit_items")
     "(test.conf) cpp_extension = ce",
     "(test.conf) debug = false",
     "(test.conf) debug_dir = /dd",
+    "(test.conf) debug_level = 2",
     "(test.conf) depend_mode = true",
     "(test.conf) direct_mode = false",
     "(test.conf) disable = true",