libdebug: Removed C-style debug_print*() functions, ported everything to debug_out_*().

This commit is contained in:
Alexander Shaduri
2022-02-10 16:45:33 +04:00
parent 085f066958
commit 53acbd9594
9 changed files with 35 additions and 222 deletions
+3 -3
View File
@@ -2,15 +2,15 @@
# Exceptions:
# bugprone-branch-clone is too noisy in parsing code.
# cert-dcl50-cpp is triggered by debug_print()'s C style.
# cert-dcl50-cpp is triggered by hz::string_sprintf()'s C style.
# clang-analyzer-deadcode.DeadStores is too noisy.
# clang-analyzer-cplusplus.NewDeleteLeaks doesn't understand Gtkmm memory management.
# cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers is triggered by attribute database.
# cppcoreguidelines-owning-memory doesn't support deleting Gtkmm objects.
# cppcoreguidelines-pro-type-cstyle-cast needed by GTK C casts.
# cppcoreguidelines-pro-type-reinterpret-cast is needed by WinAPI-facing functions
# cppcoreguidelines-pro-type-vararg is triggered by libdebug's debug_print().
# cppcoreguidelines-pro-bounds-array-to-pointer-decay is triggered by va_start in libdebug's debug_print().
# cppcoreguidelines-pro-type-vararg is triggered by hz::string_sprintf().
# cppcoreguidelines-pro-bounds-array-to-pointer-decay is triggered by va_start in hz::string_sprintf().
# cppcoreguidelines-pro-bounds-pointer-arithmetic is useless (triggered by argv) until we have std::span.
# misc-no-recursion is too noisy.
# modernize-raw-string-literal is very noisy.
+4 -4
View File
@@ -172,17 +172,17 @@ extern "C" {
case G_LOG_FLAG_FATAL:
case G_LOG_LEVEL_ERROR: // fatal
case G_LOG_LEVEL_CRITICAL:
debug_print_error("gtk", "%s\n", message);
debug_out_error("gtk", message << std::endl);
break;
case G_LOG_LEVEL_WARNING:
debug_print_warn("gtk", "%s\n", message);
debug_out_warn("gtk", message << std::endl);
break;
case G_LOG_LEVEL_MESSAGE:
case G_LOG_LEVEL_INFO:
debug_print_info("gtk", "%s\n", message);
debug_out_info("gtk", message << std::endl);
break;
case G_LOG_LEVEL_DEBUG:
debug_print_dump("gtk", "%s\n", message);
debug_out_dump("gtk", message << std::endl);
break;
case G_LOG_LEVEL_MASK:
break;
+12 -12
View File
@@ -71,15 +71,15 @@ inline fs::path data_file_find(const std::string& domain, const std::string& fil
return {};
if (fs::u8path(filename).is_absolute()) { // shouldn't happen
debug_print_error("app", "%s: Data file \"%s\" must be relative.\n",
DBG_FUNC, filename.c_str());
debug_out_error("app", DBG_FUNC_MSG
<< "Data file \"" << filename << "\" must be relative.\n");
return {};
}
auto dirs = data_file_get_search_directories(domain);
if (dirs.empty()) { // shouldn't happen
debug_print_error("app", "%s: No search directories registered for domain \"%s\".\n",
DBG_FUNC, domain.c_str());
debug_out_error("app", DBG_FUNC_MSG
<< "No search directories registered for domain \"" << domain << "\".\n");
return {};
}
@@ -89,18 +89,18 @@ inline fs::path data_file_find(const std::string& domain, const std::string& fil
std::error_code ec;
if (fs::exists(file_path, ec)) {
if (!allow_to_be_directory && fs::is_directory(file_path, ec)) {
debug_print_error("app", "%s: Data file \"[%s:]%s\" file found at \"%s\", but it is a directory.\n",
DBG_FUNC, domain.c_str(), file_path.string().c_str(), dir.string().c_str());
debug_out_error("app", DBG_FUNC_MSG
<< "Data file \"[" << domain << ":]" << file_path << "\" file found at \"" << dir << "\", but it is a directory.\n");
return {};
}
debug_print_info("app", "%s: Data file \"[%s:]%s\" found at \"%s\".\n",
DBG_FUNC, domain.c_str(), file_path.string().c_str(), dir.string().c_str());
debug_out_info("app", DBG_FUNC_MSG
<< "Data file \"[" << domain << ":]" << file_path << "\" file found at \"" << dir << "\".\n");
return file_path;
}
}
debug_print_error("app", "%s: Data file \"[%s:]%s\" not found.\n",
DBG_FUNC, domain.c_str(), filename.c_str());
debug_out_error("app", DBG_FUNC_MSG
<< "Data file \"[" << domain << ":]" << filename << "\" not found.\n");
return {};
}
@@ -116,8 +116,8 @@ inline std::string data_file_get_contents(const std::string& domain, const std::
if (!ec) {
return contents;
}
debug_print_error("app", "%s: Data file \"[%s:]%s\" cannot be loaded: %s.\n",
DBG_FUNC, domain.c_str(), filename.c_str(), ec.message().c_str());
debug_out_error("app", DBG_FUNC_MSG
<< "Data file \"[" << domain << ":]" << filename << "\" cannot be loaded: " << ec.message() << ".\n");
}
return {};
}
-83
View File
@@ -14,10 +14,6 @@ Copyright:
#include <cstdio> // std::fprintf(), std::vfprintf()
#ifndef __GNUC__
#include <cstdarg> // std::va_start, va_list macro and friends
#endif
/*
#include <cassert>
@@ -64,22 +60,6 @@ anyway).
#undef debug_out_fatal
#endif
#ifdef debug_print_dump
#undef debug_print_dump
#endif
#ifdef debug_print_info
#undef debug_print_info
#endif
#ifdef debug_print_warn
#undef debug_print_warn
#endif
#ifdef debug_print_error
#undef debug_print_error
#endif
#ifdef debug_print_fatal
#undef debug_print_fatal
#endif
#ifdef DBG_FILE
#undef DBG_FILE
#endif
@@ -151,63 +131,6 @@ anyway).
std::cerr << "<fatal> [" << (domain) << "] " << output
#ifdef __GNUC__
// The "trim trailing comma" and "##" extensions are GNU features (works with intel too).
// The ## part is needed to avoid requirement of at least one argument after "format".
#define debug_print_dump(domain, format, ...) \
std::fprintf(stderr, (std::string("<dump> [") + (domain) + "] " + format).c_str(), ## __VA_ARGS__)
#define debug_print_info(domain, format, ...) \
std::fprintf(stderr, (std::string("<info> [") + (domain) + "] " + format).c_str(), ## __VA_ARGS__)
#define debug_print_warn(domain, format, ...) \
std::fprintf(stderr, (std::string("<warn> [") + (domain) + "] " + format).c_str(), ## __VA_ARGS__)
#define debug_print_error(domain, format, ...) \
std::fprintf(stderr, (std::string("<error> [") + (domain) + "] " + format).c_str(), ## __VA_ARGS__)
#define debug_print_fatal(domain, format, ...) \
std::fprintf(stderr, (std::string("<fatal> [") + (domain) + "] " + format).c_str(), ## __VA_ARGS__)
#else // non-gcc compilers:
namespace hz {
namespace internal {
inline void debug_print_impl(const std::string& header, const char* format, ...)
{
std::va_list ap;
va_start(ap, format);
std::vfprintf(stderr, (header + format).c_str(), ap);
va_end(ap);
}
}
}
#define debug_print_dump(domain, ...) \
hz::internal::debug_print_impl(std::string("<dump> [") + (domain) + "] ", __VA_ARGS__)
#define debug_print_info(domain, ...) \
hz::internal::debug_print_impl(std::string("<info> [") + (domain) + "] ", __VA_ARGS__)
#define debug_print_warn(domain, ...) \
hz::internal::debug_print_impl(std::string("<warn> [") + (domain) + "] ", __VA_ARGS__)
#define debug_print_error(domain, ...) \
hz::internal::debug_print_impl(std::string("<error> [") + (domain) + "] ", __VA_ARGS__)
#define debug_print_fatal(domain, ...) \
hz::internal::debug_print_impl(std::string("<fatal> [") + (domain) + "] ", __VA_ARGS__)
#endif
#define DBG_FILE __FILE__
#define DBG_LINE __LINE__
#define DBG_FUNC_NAME __func__
@@ -287,12 +210,6 @@ anyway).
#define debug_out_error(domain, output) if(true){}else(void)0
#define debug_out_fatal(domain, output) if(true){}else(void)0
#define debug_print_dump(domain, format, ...) if(true){}else(void)0
#define debug_print_info(domain, format, ...) if(true){}else(void)0
#define debug_print_warn(domain, format, ...) if(true){}else(void)0
#define debug_print_error(domain, format, ...) if(true){}else(void)0
#define debug_print_fatal(domain, format, ...) if(true){}else(void)0
#define DBG_FILE ""
#define DBG_LINE 0
-19
View File
@@ -12,9 +12,6 @@ Copyright:
#include <string>
#include <iosfwd> // std::ostream definition
#include <sstream>
#include <cstdarg> // std::va_start, va_list macro and friends
#include "hz/string_sprintf.h" // string_vsprintf()
#include "dout.h"
#include "dflags.h"
@@ -51,22 +48,6 @@ std::ostream& debug_out(debug_level::flag level, const std::string& domain)
void debug_print(debug_level::flag level, const std::string& domain, const char* format, ...)
{
std::va_list ap;
va_start(ap, format);
std::string s = hz::string_vsprintf(format, ap);
va_end(ap);
debug_out(level, domain) << s;
}
// Start / stop prefix printing. Useful for large dumps
void debug_begin()
-33
View File
@@ -58,39 +58,6 @@ std::ostream& debug_out(debug_level::flag level, const std::string& domain);
/// Send a printf-like-formatted string to libdebug stream.
void debug_print(debug_level::flag level, const std::string& domain,
const char* format, ...) HZ_FUNC_PRINTF_ISO_CHECK(3, 4);
/// Send a printf-like-formatted string to libdebug stream. For example:
/// \code
/// debug_print_error("app", "Error in %s while handling input parameters.\n", DBG_FUNC);
/// debug_print_dump("app", "Parameter value: %d.\n", value);
/// \endcode
#define debug_print_dump(domain, ...) \
debug_print(debug_level::dump, domain, __VA_ARGS__)
/// Send a printf-like-formatted string to libdebug stream. \see debug_print_dump().
#define debug_print_info(domain, ...) \
debug_print(debug_level::info, domain, __VA_ARGS__)
/// Send a printf-like-formatted string to libdebug stream. \see debug_print_dump().
#define debug_print_warn(domain, ...) \
debug_print(debug_level::warn, domain, __VA_ARGS__)
/// Send a printf-like-formatted string to libdebug stream. \see debug_print_dump().
#define debug_print_error(domain, ...) \
debug_print(debug_level::error, domain, __VA_ARGS__)
/// Send a printf-like-formatted string to libdebug stream. \see debug_print_dump().
#define debug_print_fatal(domain, ...) \
debug_print(debug_level::fatal, domain, __VA_ARGS__)
/// Start prefix printing. Useful for large dumps where you don't want prefixes to
/// be printed on each debug_* call.
void debug_begin();
+3 -56
View File
@@ -83,9 +83,9 @@ int main_impl()
int op = 5;
debug_print_dump("dom", "Dumping something: %s\n", something.c_str());
debug_print_info("dom", "Doing something: %s\n", something.c_str());
debug_print_error("dom", "Error while doing something\n");
debug_out_dump("dom", "Dumping something: " << something << std::endl);
debug_out_info("dom", "Doing something: " << something << std::endl);
debug_out_error("dom", "Error while doing something\n");
debug_out_info("dom", "Doing something with " << obj << " object\n");
debug_out_fatal("dom", "Fatal error while performing operation " << op << "\n");
@@ -94,61 +94,8 @@ int main_impl()
DBG_ASSERT_MSG(1 == 0, "One does not equal 0");
DBG_ASSERT(1 == 0);
// debug::out(debug::dump) << debug::libdebug_info;
// or
// printd(debug::dump, "%s", debug::libdebug_info_str().c_str());
// debug::out() << "info1\n"; // info level, default domain
// or
// debug::print("info1\n");
// debug::out(debug::error) << "error1\n";
// or
// debug::out_error() << "error1\n";
// or
// debug::print(debug::error, "error1\n");
// or
// debug::print_error("error1\n");
// debug::out(debug::warn, "dom") << debug::indent << "\nwarning1\nwarning2" << debug::unindent << "\n";
// or
// debug::indent++;
// debug::printd_warn("dom", "\nwarning1\nwarning2");
// debug::indent--;
// reseting indentation is done by
// debug::out("dom", debug::prnone) << debug::indent(0);
// or
// indent_reset();
// debug::out("dom", debug::prnone) << "info2\n";
// or
// debug::out(debug::info, "dom") << "info2\n";
// or
// debug::printd("dom", "info2\n");
// debug::out(debug::error, "dom") << "error2\n";
// or
// debug::printd_error("dom", "error2\n");
// debug::out(debug::dump, "default", debug::prnone) << "dump1, no prefixes here\n";
// print out current function name
// debug::out(debug::dump) << "dump2, " << DBG_POS(debug::posfunc) << "\n";
// or
// debug::print_dump("%s", DBG_POS(debug::posfunc).get_text().c_str());
// debug::out(debug::dump, "default", debug::prdate) << "date prefix here\n";
debug_out_dump("default", DBG_POS << "\n");
debug_out_dump("default", DBG_POS.func << "\n");
// or
debug_print_info("default", "%s\n", DBG_POS.str().c_str());
DBG_TRACE_POINT_MSG(1);
+7 -7
View File
@@ -64,11 +64,11 @@ extern "C" {
return FALSE; // remove timeout, disable autosave for real.
auto file = impl::autosave_config_file;
debug_print_info("rconfig", "Autosaving config to \"%s\".\n", file.u8string().c_str());
debug_out_info("rconfig", "Autosaving config to \"" << file << "\"." << std::endl);
std::error_code ec;
if ((hz::fs::exists(file, ec) && !hz::fs::is_regular_file(file, ec)) || !hz::fs_path_is_writable(file, ec)) {
debug_out_error("rconfig", "Autosave failed: Cannot write to file: " << ec.message() << "\n");
debug_out_error("rconfig", "Autosave failed: Cannot write to file: " << ec.message() << std::endl);
return static_cast<gboolean>(force); // if manual, return failure. else, don't stop the timeout.
}
@@ -88,13 +88,13 @@ extern "C" {
inline bool autosave_set_config_file(const hz::fs::path& file)
{
if (file.empty()) {
debug_print_error("rconfig", "autosave_set_config_file(): Error: Filename is empty.\n");
debug_out_error("rconfig", DBG_FUNC_MSG << "Error: Filename is empty." << std::endl);
return false;
}
impl::autosave_config_file = file;
debug_print_info("rconfig", "Setting autosave config file to \"%s\"\n", file.u8string().c_str());
debug_out_info("rconfig", "Setting autosave config file to \"" << file << "\"." << std::endl);
return true;
}
@@ -104,12 +104,12 @@ inline bool autosave_set_config_file(const hz::fs::path& file)
inline bool autosave_start(std::chrono::seconds sec_interval)
{
if (impl::autosave_enabled) { // already autosaving, you should stop it first.
debug_print_warn("rconfig", "Error while starting config autosave: Autosave is active already.\n");
debug_out_warn("rconfig", "Error while starting config autosave: Autosave is active already." << std::endl);
return false;
}
impl::autosave_enabled = true;
debug_print_info("rconfig", "Starting config autosave with %d sec. interval.\n", int(sec_interval.count()));
debug_out_info("rconfig", "Starting config autosave with " << sec_interval.count() << " sec. interval." << std::endl);
g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, guint(std::chrono::milliseconds(sec_interval).count()),
&autosave_timeout_callback, nullptr, nullptr);
@@ -122,7 +122,7 @@ inline bool autosave_start(std::chrono::seconds sec_interval)
/// Disable autosave
inline void autosave_stop()
{
debug_print_info("rconfig", "Stopping config autosave.\n");
debug_out_info("rconfig", "Stopping config autosave." << std::endl);
// set the stop flag. it will make autosave stop on next timeout callback call.
impl::autosave_enabled = false;
+6 -5
View File
@@ -31,8 +31,8 @@ inline bool load_from_file(const hz::fs::path& file)
std::string json_str;
auto ec = hz::fs_file_get_contents(file, json_str, 10*1024*1024); // 10M
if (ec) {
debug_print_error("rconfig", "load_from_file(): Unable to read from file \"%s\": %s\n",
file.u8string().c_str(), ec.message().c_str());
debug_out_error("rconfig", "load_from_file(): Unable to read from file \""
<< file << "\": " << ec.message() << std::endl);
return false;
}
@@ -40,7 +40,8 @@ inline bool load_from_file(const hz::fs::path& file)
get_config_branch() = json::parse(json_str);
}
catch (json::parse_error& e) {
debug_out_warn("rconfig", "Cannot load config file \"" << file.u8string() << "\": " << e.what() << "\n");
debug_out_warn("rconfig", "Cannot load config file \""
<< file << "\": " << e.what() << std::endl);
return false;
}
return true;
@@ -55,8 +56,8 @@ inline bool save_to_file(const hz::fs::path& file)
auto ec = hz::fs_file_put_contents(file, json_str);
if (ec) {
debug_print_error("rconfig", "save_to_file(): Unable to write to file \"%s\": %s.\n",
file.u8string().c_str(), ec.message().c_str());
debug_out_error("rconfig", DBG_FUNC_MSG
<< "Unable to write to file \"" << file << "\": " << ec.message() << "." << std::endl);
return false;
}
return true;