]> git.tdb.fi Git - builder.git/commitdiff
Better logging system
authorMikko Rasa <tdb@tdb.fi>
Thu, 5 Jul 2012 11:05:36 +0000 (14:05 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 8 Jul 2012 21:08:54 +0000 (00:08 +0300)
source/builder.cpp
source/builder.h
source/csourcefile.cpp
source/gnucxxcompiler.cpp
source/logger.cpp [new file with mode: 0644]
source/logger.h [new file with mode: 0644]
source/package.cpp
source/packagemanager.cpp
source/sourcepackage.cpp
source/virtualfilesystem.cpp

index 9548288131e45fe975d548672bf69866b773848b..1787ec17ca05b9cee8ffbf54b4065bef15a2e62e 100644 (file)
@@ -43,7 +43,6 @@ Builder::Builder(int argc, char **argv):
        clean(0),
        dry_run(false),
        help(false),
-       verbose(1),
        show_progress(false),
        build_file("Build"),
        jobs(1),
@@ -60,6 +59,9 @@ Builder::Builder(int argc, char **argv):
        string prfx;
        string arch;
        bool no_externals = false;
+       unsigned verbose = 1;
+       bool silent = false;
+       list<string> log_channels;
 
        GetOpt getopt;
        getopt.add_option('a', "analyze",    analyze_mode,  GetOpt::REQUIRED_ARG).set_help("Perform analysis.  MODE can be deps, alldeps or rebuild.", "MODE");
@@ -68,7 +70,9 @@ Builder::Builder(int argc, char **argv):
        getopt.add_option('f', "file",       build_file,    GetOpt::REQUIRED_ARG).set_help("Read info from FILE instead of Build.", "FILE");
        getopt.add_option('h', "help",       help,          GetOpt::NO_ARG).set_help("Print this message.");
        getopt.add_option('j', "jobs",       jobs,          GetOpt::REQUIRED_ARG).set_help("Run NUM commands at once, whenever possible.", "NUM");
+       getopt.add_option('l', "log",        log_channels,  GetOpt::REQUIRED_ARG).set_help("Set log channels to be displayed.", "LIST");
        getopt.add_option('n', "dry-run",    dry_run,       GetOpt::NO_ARG).set_help("Don't actually do anything, only show what would be done.");
+       getopt.add_option('s', "silent",     silent,        GetOpt::NO_ARG).set_help("Don't print any messages other than errors.");
        getopt.add_option('v', "verbose",    verbose,       GetOpt::NO_ARG).set_help("Print more information about what's going on.");
        getopt.add_option('x', "no-externals",  no_externals, GetOpt::NO_ARG).set_help("Do not load external source packages.");
        getopt.add_option('A', "conf-all",   conf_all,      GetOpt::NO_ARG).set_help("Apply configuration to all packages.");
@@ -86,6 +90,40 @@ Builder::Builder(int argc, char **argv):
        helpmsg = getopt.generate_help();
        getopt(argc, argv);
 
+       if(silent)
+               --verbose;
+       if(verbose>=1)
+       {
+               logger.enable_channel("summary");
+               logger.enable_channel("tasks");
+       }
+       if(verbose>=2)
+       {
+               logger.enable_channel("packages");
+               logger.enable_channel("commands");
+       }
+       if(verbose>=3)
+       {
+               logger.enable_channel("packagemgr");
+               logger.enable_channel("configure");
+       }
+       if(verbose>=4)
+       {
+               logger.enable_channel("files");
+               logger.enable_channel("auxcommands");
+       }
+       if(verbose>=5)
+       {
+               logger.enable_channel("tools");
+               logger.enable_channel("vfs");
+       }
+       for(list<string>::const_iterator i=log_channels.begin(); i!=log_channels.end(); ++i)
+       {
+               vector<string> parts = split(*i, ',');
+               for(vector<string>::const_iterator j=parts.begin(); j!=parts.end(); ++j)
+                       logger.enable_channel(*j);
+       }
+
        if(!analyze_mode.empty())
        {
                analyzer = new Analyzer(*this);
@@ -233,34 +271,22 @@ int Builder::main()
        if(create_targets())
                return 1;
 
-       if(verbose>=2)
-       {
-               IO::print("Building on %s, for %s%s\n", native_arch.get_name(),
-                       current_arch->get_name(), (current_arch->is_native() ? " (native)" : ""));
-               IO::print("Prefix is %s\n", prefix);
-       }
+       logger.log("environment", format("Building on %s, for %s%s", native_arch.get_name(),
+               current_arch->get_name(), (current_arch->is_native() ? " (native)" : "")));
+       logger.log("environment", format("Prefix is %s", prefix));
 
-       if(verbose>=1)
+       const PackageManager::PackageMap &packages = package_manager.get_packages();
+       list<string> package_details;
+       for(PackageManager::PackageMap::const_iterator i=packages.begin(); i!=packages.end(); ++i)
        {
-               const PackageManager::PackageMap &packages = package_manager.get_packages();
-               unsigned n_packages = 0;
-               for(PackageManager::PackageMap::const_iterator i=packages.begin(); i!=packages.end(); ++i)
-                       if(i->second && i->second->is_configured())
-                               ++n_packages;
-               IO::print("%d active packages, %d targets\n", n_packages, targets.size());
-       }
+               if(!i->second || !i->second->is_configured())
+                       continue;
 
-       if(verbose>=2)
-       {
-               const PackageManager::PackageMap &packages = package_manager.get_packages();
-               for(PackageManager::PackageMap::const_iterator i=packages.begin(); i!=packages.end(); ++i)
+               string line = i->second->get_name();
+               if(dynamic_cast<SourcePackage *>(i->second))
                {
-                       if(!i->second->is_configured())
-                               continue;
+                       line += '*';
 
-                       IO::print(" %s", i->second->get_name());
-                       if(dynamic_cast<SourcePackage *>(i->second))
-                               IO::print("*");
                        unsigned count = 0;
                        unsigned to_be_built = 0;
                        for(TargetMap::iterator j=targets.begin(); j!=targets.end(); ++j)
@@ -272,15 +298,20 @@ int Builder::main()
                                }
                        if(count)
                        {
-                               IO::print(" (%d targets", count);
+                               line += format(" (%d targets", count);
                                if(to_be_built)
-                                       IO::print(", %d to be built", to_be_built);
-                               IO::print(")");
+                                       line += format(", %d to be built", to_be_built);
+                               line += ')';
                        }
-                       IO::print("\n");
                }
+
+               package_details.push_back(line);
        }
 
+       logger.log("summary", format("%d active packages, %d targets", package_details.size(), targets.size()));
+       for(list<string>::const_iterator i=package_details.begin(); i!=package_details.end(); ++i)
+               logger.log("packages", *i);
+
        if(analyzer)
                analyzer->analyze();
 
@@ -357,8 +388,7 @@ int Builder::load_build_file(const FS::Path &fn)
 
        IO::BufferedFile in(fn.str());
 
-       if(verbose>=3)
-               IO::print("Reading %s\n", fn);
+       logger.log("files", format("Reading %s", fn));
 
        DataFile::Parser parser(in, fn.str());
        Loader loader(*this, fn.subpath(0, fn.size()-1));
@@ -442,11 +472,10 @@ int Builder::do_build()
 
        if(!total)
        {
-               IO::print("Already up to date\n");
+               logger.log("summary", "Already up to date");
                return 0;
        }
-       if(verbose>=1)
-               IO::print("Will build %d target%s\n", total, (total!=1 ? "s" : ""));
+       logger.log("summary", format("Will build %d target%s", total, (total!=1 ? "s" : "")));
 
        vector<Task *> tasks;
 
@@ -463,12 +492,11 @@ int Builder::do_build()
                        if(tgt)
                        {
                                if(tgt->get_tool())
-                                       IO::print("%-4s  %s\n", tgt->get_tool()->get_tag(), tgt->get_name());
+                                       logger.log("tasks", format("%-4s  %s", tgt->get_tool()->get_tag(), tgt->get_name()));
                                Task *task = tgt->build();
                                if(task)
                                {
-                                       if(verbose>=2)
-                                               IO::print("%s\n", task->get_command());
+                                       logger.log("commands", format("%s", task->get_command()));
                                        if(dry_run)
                                        {
                                                task->signal_finished.emit(true);
@@ -512,9 +540,9 @@ int Builder::do_build()
        if(show_progress)
                IO::print("\033[K");
        if(fail)
-               IO::print("Build failed\n");
+               logger.log("summary", "Build failed");
        else if(show_progress)
-               IO::print("Build complete\n");
+               logger.log("summary", "Build complete");
 
        return fail;
 }
index 0dc16d175d5c75cfbd60a3f60db1a96f50af2dc0..78244e8c2980081bfcfb43ff85558cad443cddad 100644 (file)
@@ -9,6 +9,7 @@
 #include <msp/fs/path.h>
 #include "architecture.h"
 #include "config.h"
+#include "logger.h"
 #include "misc.h"
 #include "packagemanager.h"
 #include "problem.h"
@@ -77,6 +78,7 @@ private:
        ProfileTemplateMap profile_tmpl;
        Toolchain toolchain;
        VirtualFileSystem vfs;
+       Logger logger;
 
        ProblemList problems;
        Analyzer *analyzer;
@@ -84,7 +86,6 @@ private:
        unsigned clean;
        bool dry_run;
        bool help;
-       unsigned verbose;
        bool show_progress;
        std::string build_file;
        unsigned jobs;
@@ -104,7 +105,6 @@ public:
        ~Builder();
 
        int main();
-       unsigned get_verbose() const { return verbose; }
        bool get_dry_run() const { return dry_run; }
 
        PackageManager &get_package_manager() { return package_manager; }
@@ -124,6 +124,7 @@ public:
 
        const Toolchain &get_toolchain() const { return toolchain; }
        VirtualFileSystem &get_vfs() { return vfs; }
+       const Logger &get_logger() const { return logger; }
 
        /** Adds a target to both the target map and the new target queue.  Called
        from Target constructor. */
index dee807b2306027aa1a503e261f241dfbe4b4a0d7..d3cb807a781cc1f35cefafa8ece08079ee126f94 100644 (file)
@@ -37,8 +37,7 @@ void CSourceFile::find_depends()
        {
                IO::BufferedFile in(path.str());
 
-               if(builder.get_verbose()>=4)
-                       IO::print("Reading includes from %s\n", path.str());
+               builder.get_logger().log("files", format("Reading includes from %s", path.str()));
 
                Regex r_include("^[ \t]*#include[ \t]+([\"<].*)[\">]");
 
index 09235810e1fa3878dfdd11d538c3c5354b2f691f..3e0a4b87fae698ca248c345db05360c9bb306e07 100644 (file)
@@ -34,8 +34,7 @@ GnuCxxCompiler::GnuCxxCompiler(Builder &b):
                                FS::Path cxx_path = FS::Path("/usr/include/c++")/cxx_ver;
                                if(FS::is_dir(cxx_path))
                                {
-                                       if(builder.get_verbose()>=5)
-                                               IO::print("%s version is %s\n", name, cxx_ver);
+                                       builder.get_logger().log("tools", format("%s version is %s", name, cxx_ver));
                                        system_path.push_back(cxx_path);
                                        break;
                                }
diff --git a/source/logger.cpp b/source/logger.cpp
new file mode 100644 (file)
index 0000000..901800e
--- /dev/null
@@ -0,0 +1,21 @@
+#include <msp/io/print.h>
+#include "logger.h"
+
+using namespace std;
+using namespace Msp;
+
+void Logger::enable_channel(const string &chan)
+{
+       enabled_channels.insert(chan);
+}
+
+void Logger::disable_channel(const string &chan)
+{
+       enabled_channels.erase(chan);
+}
+
+void Logger::log(const string &chan, const string &message) const
+{
+       if(enabled_channels.count(chan))
+               IO::print("%s\n", message);
+}
diff --git a/source/logger.h b/source/logger.h
new file mode 100644 (file)
index 0000000..c22339f
--- /dev/null
@@ -0,0 +1,19 @@
+#ifndef LOGGER_H_
+#define LOGGER_H_
+
+#include <set>
+#include <string>
+
+class Logger
+{
+private:
+       std::set<std::string> enabled_channels;
+
+public:
+       void enable_channel(const std::string &);
+       void disable_channel(const std::string &);
+
+       void log(const std::string &, const std::string &) const;
+};
+
+#endif
index 8a05ddc89699950dd5d59ce2977741b4f4be47ad..38829f758c76af4f44cde0ec1a88565b42238a22 100644 (file)
@@ -22,8 +22,7 @@ void Package::configure(const StringMap &opts, unsigned flag)
        if(conf_done)
                return;
 
-       if(builder.get_verbose()>=3)
-               IO::print("Configuring %s\n", name);
+       builder.get_logger().log("configure", format("Configuring %s", name));
 
        do_configure(opts, flag);
 
index c3d693961dd332d188a7a6e7b93547ed1e486623..68189205abb8022ba1f272c0a066e4b1a210fa1f 100644 (file)
@@ -100,8 +100,7 @@ string PackageManager::run_pkgconfig(const string &pkg, const string &what)
                argv.push_back("--variable="+what);
        argv.push_back(pkg);
 
-       if(builder.get_verbose()>=4)
-               IO::print("Running %s\n", join(argv.begin(), argv.end()));
+       builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
 
        ExternalTask task(argv);
        task.set_stdout(ExternalTask::CAPTURE);
@@ -118,8 +117,7 @@ string PackageManager::run_pkgconfig(const string &pkg, const string &what)
 
 FS::Path PackageManager::get_package_location(const string &name)
 {
-       if(builder.get_verbose()>=3)
-               IO::print("Looking for package %s\n", name);
+       builder.get_logger().log("packagemgr", format("Looking for package %s", name));
 
        try
        {
@@ -143,8 +141,7 @@ FS::Path PackageManager::get_package_location(const string &name)
                                        pkg_dirs.push_back(full);
                        }
                }
-               if(builder.get_verbose()>=3)
-                       IO::print("%d packages found in path\n", pkg_dirs.size());
+               builder.get_logger().log("packagemgr", format("%d packages found in path", pkg_dirs.size()));
        }
 
        bool msp = !name.compare(0, 3, "msp");
index d66a36fb4b247c289db1825df1f8c2e667fb7543..ab66c0056651cbfddaa768fed2dbb47577779870 100644 (file)
@@ -130,8 +130,7 @@ void SourcePackage::do_configure(const StringMap &opts, unsigned flag)
 
        if(flag && config.update(opts))
        {
-               if(builder.get_verbose()>=2)
-                       IO::print("Configuration of %s changed\n", name);
+               builder.get_logger().log("configure", format("Configuration of %s changed", name));
                if(!builder.get_dry_run())
                        config.save();
        }
index 0ada219aabdae0b4b55d443a558fd4187327836b..b5e16d440ca2ec26b6a97871dff226da102855fa 100644 (file)
@@ -53,8 +53,7 @@ FileTarget *VirtualFileSystem::find_header(const string &name, const SearchPath
        if(i!=include_cache.end())
                return i->second;
 
-       if(builder.get_verbose()>=5)
-               IO::print("Looking for header %s with path %s\n", name, join(path.begin(), path.end()));
+       builder.get_logger().log("vfs", format("Looking for header %s with path %s", name, join(path.begin(), path.end())));
 
        // XXX This will cause trouble with multiple architectures in a single build
        const Tool *tool = builder.get_toolchain().get_tool_for_suffix(FS::extpart(FS::basename(name)), true);
@@ -87,8 +86,7 @@ FileTarget *VirtualFileSystem::find_library(const string &lib, const SearchPath
        const Tool &linker = builder.get_toolchain().get_tool("LINK");
        const Tool::SearchPath &syspath = linker.get_system_path();
 
-       if(builder.get_verbose()>=5)
-               IO::print("Looking for library %s with path %s\n", lib, join(path.begin(), path.end()));
+       builder.get_logger().log("vfs", format("Looking for library %s with path %s", lib, join(path.begin(), path.end())));
 
        FileTarget *tgt = 0;
        for(SearchPath::const_iterator j=path.begin(); (!tgt && j!=path.end()); ++j)