Rework logging further

This commit is contained in:
Simon Quigley 2024-12-15 02:23:53 -06:00
parent 6988b0d082
commit 2f689f2a87

View File

@ -66,7 +66,7 @@ static std::ofstream log_file_stream;
static void log_all(const std::string &msg, bool is_error=false) { static void log_all(const std::string &msg, bool is_error=false) {
if (is_error) { if (is_error) {
std::cerr << msg; std::cerr << msg;
} else if (verbose) { } else {
std::cout << msg; std::cout << msg;
} }
if (log_file_stream.is_open()) { if (log_file_stream.is_open()) {
@ -87,6 +87,13 @@ static void log_error(const std::string &msg) {
log_all("[ERROR] " + msg + "\n", true); log_all("[ERROR] " + msg + "\n", true);
} }
// New function for verbose logging
static void log_verbose(const std::string &msg) {
if (verbose) {
log_all("[VERBOSE] " + msg + "\n");
}
}
static void print_help(const std::string &prog_name) { static void print_help(const std::string &prog_name) {
std::cout << "Usage: " << prog_name << " [OPTIONS] <config_path>\n" std::cout << "Usage: " << prog_name << " [OPTIONS] <config_path>\n"
<< "Options:\n" << "Options:\n"
@ -98,7 +105,6 @@ static void print_help(const std::string &prog_name) {
<< " --help, -h Display this help message.\n"; << " --help, -h Display this help message.\n";
} }
// Adjusted run_command_silent_on_success with conditional logging
static void run_command_silent_on_success(const std::vector<std::string> &cmd, const std::optional<fs::path> &cwd = std::nullopt) { static void run_command_silent_on_success(const std::vector<std::string> &cmd, const std::optional<fs::path> &cwd = std::nullopt) {
log_info("Running command: " + std::accumulate(cmd.begin(), cmd.end(), std::string(), log_info("Running command: " + std::accumulate(cmd.begin(), cmd.end(), std::string(),
[](const std::string &a, const std::string &b) -> std::string { return a + (a.empty() ? "" : " ") + b; })); [](const std::string &a, const std::string &b) -> std::string { return a + (a.empty() ? "" : " ") + b; }));
@ -129,7 +135,7 @@ static void run_command_silent_on_success(const std::vector<std::string> &cmd, c
log_error("Output:\n" + ss.str()); log_error("Output:\n" + ss.str());
throw std::runtime_error("Command execution failed"); throw std::runtime_error("Command execution failed");
} else { } else {
log_info("Command executed successfully: " + full_cmd); log_verbose("Command executed successfully: " + full_cmd);
} }
} }
@ -278,7 +284,7 @@ static void publish_lintian() {
if(ec) { if(ec) {
log_error("Failed to copy Lintian file: " + p.path().string() + " to " + dest.string() + ". Error: " + ec.message()); log_error("Failed to copy Lintian file: " + p.path().string() + " to " + dest.string() + ". Error: " + ec.message());
} else { } else {
log_info("Copied Lintian file: " + p.path().string() + " to " + dest.string()); log_verbose("Copied Lintian file: " + p.path().string() + " to " + dest.string());
} }
} }
} }
@ -316,7 +322,7 @@ static std::vector<std::string> get_exclusions(const fs::path &packaging) {
std::string token; std::string token;
while(iss>>token) { while(iss>>token) {
exclusions.push_back(token); exclusions.push_back(token);
log_info("Exclusion added: " + token); log_verbose("Exclusion added: " + token);
} }
} }
found = true; found = true;
@ -330,7 +336,7 @@ static std::vector<std::string> get_exclusions(const fs::path &packaging) {
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
// Parse command-line arguments // Parse command-line arguments first to set verbosity
std::string prog_name = fs::path(argv[0]).filename().string(); std::string prog_name = fs::path(argv[0]).filename().string();
for(int i=1; i<argc; i++) { for(int i=1; i<argc; i++) {
std::string arg=argv[i]; std::string arg=argv[i];
@ -496,7 +502,7 @@ int main(int argc, char** argv) {
of<<tag<<"\n"; of<<tag<<"\n";
} }
} }
log_info("Created Lintian suppression file: " + temp_file.string()); log_verbose("Created Lintian suppression file: " + temp_file.string());
std::string cmd = "lintian -EvIL +pedantic --suppress-tags-from-file " + temp_file.string() + " " + source_path.string() + " 2>&1"; std::string cmd = "lintian -EvIL +pedantic --suppress-tags-from-file " + temp_file.string() + " " + source_path.string() + " 2>&1";
FILE* pipe = popen(cmd.c_str(),"r"); FILE* pipe = popen(cmd.c_str(),"r");
std::stringstream ss; std::stringstream ss;
@ -507,7 +513,7 @@ int main(int argc, char** argv) {
} }
int ret = pclose(pipe); int ret = pclose(pipe);
fs::remove(temp_file); fs::remove(temp_file);
log_info("Lintian command exited with code: " + std::to_string(ret)); log_verbose("Lintian command exited with code: " + std::to_string(ret));
if(ret!=0) { if(ret!=0) {
log_error("Lintian reported issues for " + name + ":\n"+ss.str()); log_error("Lintian reported issues for " + name + ":\n"+ss.str());
if(!ss.str().empty()) { if(!ss.str().empty()) {
@ -528,7 +534,7 @@ int main(int argc, char** argv) {
fs::remove(temp_file); fs::remove(temp_file);
log_error("Failed to run Lintian for package: " + name); log_error("Failed to run Lintian for package: " + name);
} }
log_info("Completed Lintian run for package: " + name); log_verbose("Completed Lintian run for package: " + name);
}; };
auto dput_source = [&](const std::string &name, const std::string &upload_target, const std::vector<std::string> &changes_files, const std::vector<std::string> &devel_changes_files){ auto dput_source = [&](const std::string &name, const std::string &upload_target, const std::vector<std::string> &changes_files, const std::vector<std::string> &devel_changes_files){
@ -559,7 +565,13 @@ int main(int argc, char** argv) {
auto update_changelog = [&](const fs::path &packaging_dir, const std::string &release, const std::string &version_with_epoch){ auto update_changelog = [&](const fs::path &packaging_dir, const std::string &release, const std::string &version_with_epoch){
std::string name = packaging_dir.filename().string(); std::string name = packaging_dir.filename().string();
log_info("Updating changelog for " + name + " to version " + version_with_epoch + "-0ubuntu1~ppa1"); log_info("Updating changelog for " + name + " to version " + version_with_epoch + "-0ubuntu1~ppa1");
try {
run_command_silent_on_success({"git","checkout","debian/changelog"}, packaging_dir); run_command_silent_on_success({"git","checkout","debian/changelog"}, packaging_dir);
log_verbose("Checked out debian/changelog for " + name);
} catch (const std::exception &e) {
log_error("Failed to checkout debian/changelog for " + name + ": " + e.what());
throw;
}
std::vector<std::string> cmd={ std::vector<std::string> cmd={
"dch","--distribution",release,"--package",name,"--newversion",version_with_epoch+"-0ubuntu1~ppa1","--urgency",urgency_level_override,"CI upload." "dch","--distribution",release,"--package",name,"--newversion",version_with_epoch+"-0ubuntu1~ppa1","--urgency",urgency_level_override,"CI upload."
}; };
@ -578,7 +590,7 @@ int main(int argc, char** argv) {
} else { } else {
temp_dir = fs::temp_directory_path()/("tmp_build_"+name+"_"+env_vars.at("VERSION")); temp_dir = fs::temp_directory_path()/("tmp_build_"+name+"_"+env_vars.at("VERSION"));
fs::create_directories(temp_dir); fs::create_directories(temp_dir);
log_info("Created temporary build directory: " + temp_dir.string()); log_verbose("Created temporary build directory: " + temp_dir.string());
} }
std::error_code ec; std::error_code ec;
@ -588,14 +600,14 @@ int main(int argc, char** argv) {
log_error("Failed to create temporary packaging directory: " + temp_packaging_dir.string()); log_error("Failed to create temporary packaging directory: " + temp_packaging_dir.string());
throw std::runtime_error("Temporary packaging directory creation failed"); throw std::runtime_error("Temporary packaging directory creation failed");
} }
log_info("Temporary packaging directory created at: " + temp_packaging_dir.string()); log_verbose("Temporary packaging directory created at: " + temp_packaging_dir.string());
fs::copy(packaging_dir/"debian", temp_packaging_dir/"debian", fs::copy_options::recursive, ec); fs::copy(packaging_dir/"debian", temp_packaging_dir/"debian", fs::copy_options::recursive, ec);
if(ec) { if(ec) {
log_error("Failed to copy debian directory to temporary packaging directory: " + ec.message()); log_error("Failed to copy debian directory to temporary packaging directory: " + ec.message());
throw std::runtime_error("Failed to copy debian directory"); throw std::runtime_error("Failed to copy debian directory");
} }
log_info("Copied debian directory to temporary packaging directory."); log_verbose("Copied debian directory to temporary packaging directory.");
std::string tarball_name = name+"_"+env_vars.at("VERSION")+".orig.tar.gz"; std::string tarball_name = name+"_"+env_vars.at("VERSION")+".orig.tar.gz";
fs::path tarball_source = fs::path(BASE_DIR)/(name+"_MAIN.orig.tar.gz"); fs::path tarball_source = fs::path(BASE_DIR)/(name+"_MAIN.orig.tar.gz");
@ -605,11 +617,11 @@ int main(int argc, char** argv) {
log_error("Failed to copy tarball from " + tarball_source.string() + " to " + tarball_dest.string()); log_error("Failed to copy tarball from " + tarball_source.string() + " to " + tarball_dest.string());
throw std::runtime_error("Failed to copy tarball"); throw std::runtime_error("Failed to copy tarball");
} }
log_info("Copied tarball to " + tarball_dest.string()); log_verbose("Copied tarball to " + tarball_dest.string());
for (auto &e: env_vars) { for (auto &e: env_vars) {
setenv(e.first.c_str(), e.second.c_str(),1); setenv(e.first.c_str(), e.second.c_str(),1);
log_info("Set environment variable: " + e.first + " = " + e.second); log_verbose("Set environment variable: " + e.first + " = " + e.second);
} }
std::vector<std::string> cmd_build={"debuild","--no-lintian","-S","-d","-sa","-nc"}; std::vector<std::string> cmd_build={"debuild","--no-lintian","-S","-d","-sa","-nc"};
@ -625,7 +637,7 @@ int main(int argc, char** argv) {
fs::path dest=fs::path(OUTPUT_DIR)/fname; fs::path dest=fs::path(OUTPUT_DIR)/fname;
fs::copy_file(entry.path(), dest, fs::copy_options::overwrite_existing, ec); fs::copy_file(entry.path(), dest, fs::copy_options::overwrite_existing, ec);
if(!ec) { if(!ec) {
log_info("Copied built package " + fname + " to " + OUTPUT_DIR); log_verbose("Copied built package " + fname + " to " + OUTPUT_DIR);
} }
} }
} }
@ -642,7 +654,7 @@ int main(int argc, char** argv) {
if(ec) { if(ec) {
log_warning("Failed to remove temporary directory: " + temp_dir.string()); log_warning("Failed to remove temporary directory: " + temp_dir.string());
} else { } else {
log_info("Removed temporary build directory: " + temp_dir.string()); log_verbose("Removed temporary build directory: " + temp_dir.string());
} }
if(changes_file.empty()) { if(changes_file.empty()) {
@ -693,10 +705,10 @@ int main(int argc, char** argv) {
log_error("Failed to copy tarball for " + name + " to " + tarball_dest.string()); log_error("Failed to copy tarball for " + name + " to " + tarball_dest.string());
continue; continue;
} }
log_info("Copied tarball to " + tarball_dest.string()); log_verbose("Copied tarball to " + tarball_dest.string());
std::string version_for_dch = epoch.empty()? release_version_no_epoch : (epoch+":"+release_version_no_epoch); std::string version_for_dch = epoch.empty()? release_version_no_epoch : (epoch+":"+release_version_no_epoch);
log_info("Version for dch: " + version_for_dch); log_verbose("Version for dch: " + version_for_dch);
std::map<std::string,std::string> env_map; std::map<std::string,std::string> env_map;
env_map["DEBFULLNAME"]=DEBFULLNAME; env_map["DEBFULLNAME"]=DEBFULLNAME;
@ -718,7 +730,7 @@ int main(int argc, char** argv) {
if(ec) { if(ec) {
log_warning("Failed to remove tarball: " + tarball_dest.string()); log_warning("Failed to remove tarball: " + tarball_dest.string());
} else { } else {
log_info("Removed tarball: " + tarball_dest.string()); log_verbose("Removed tarball: " + tarball_dest.string());
} }
} }