[1/2] pactree: Add --debug

Message ID 20200105054724.11590-1-develinthedetail@gmail.com
State Superseded, archived
Delegated to: Johannes Löthberg
Headers show
Series [1/2] pactree: Add --debug | expand

Commit Message

Edward E Jan. 5, 2020, 5:47 a.m. UTC
Signed-off-by: Edward E <develinthedetail@gmail.com>
---
This code is basically borrowed from pacman; libalpm debug messages
were crucially helpful for tracking down the `pactree -s` gpgdir bug.
 doc/pactree.8.txt |  3 +++
 src/pactree.c     | 47 ++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 49 insertions(+), 1 deletion(-)

Comments

Johannes Löthberg Jan. 5, 2020, 1:32 p.m. UTC | #1
Excerpts from Edward E's message of January 5, 2020 6:47:
> Signed-off-by: Edward E <develinthedetail@gmail.com>
> ---
> This code is basically borrowed from pacman; libalpm debug messages
> were crucially helpful for tracking down the `pactree -s` gpgdir bug.
>  doc/pactree.8.txt |  3 +++
>  src/pactree.c     | 47 ++++++++++++++++++++++++++++++++++++++++++++++-
>  2 files changed, 49 insertions(+), 1 deletion(-)
> 
> diff --git a/doc/pactree.8.txt b/doc/pactree.8.txt
> index b177788..57a91cb 100644
> --- a/doc/pactree.8.txt
> +++ b/doc/pactree.8.txt
> @@ -61,6 +61,9 @@ Options
>  *\--config <file>*::
>  	Specify an alternate pacman configuration file.
>  
> +*\--debug*::
> +	Print log messages produced by libalpm.
> +
>  
>  See Also
>  --------
> diff --git a/src/pactree.c b/src/pactree.c
> index a862f9e..8b77f05 100644
> --- a/src/pactree.c
> +++ b/src/pactree.c
> @@ -78,6 +78,8 @@ struct color_choices {
>  	const char *branch2;
>  	const char *leaf1;
>  	const char *leaf2;
> +	const char *error;
> +	const char *info;
>  	const char *off;
>  };
>  
> @@ -86,6 +88,8 @@ static struct color_choices use_color = {
>  	"\033[0;37m", /* white */
>  	"\033[1;32m", /* bold green */
>  	"\033[0;32m", /* green */
> +	"\033[1;31m", /* bold red */
> +	"\033[1;36m", /* bold cyan */
>  	"\033[0m"
>  };
>  
> @@ -94,12 +98,15 @@ static struct color_choices no_color = {
>  	"",
>  	"",
>  	"",
> +	"",
> +	"",
>  	""
>  };
>  
>  /* long operations */
>  enum {
> -	OP_CONFIG = 1000
> +	OP_CONFIG = 1000,
> +	OP_DEBUG
>  };
>  
>  /* globals */
> @@ -115,9 +122,38 @@ static int max_depth = -1;
>  static int reverse = 0;
>  static int unique = 0;
>  static int searchsyncs = 0;
> +static int debug = 0;
>  static const char *dbpath = DBPATH;
>  static const char *configfile = CONFFILE;
>  
> +void cb_log(alpm_loglevel_t level, const char *fmt, va_list args)
> +{
> +	const char *log_color = "";
> +	const char *log_title = "";
> +
> +	switch(level) {
> +		case ALPM_LOG_ERROR:
> +			log_color = color->error;
> +			log_title = "error: ";
> +			break;
> +		case ALPM_LOG_WARNING:
> +			log_color = color->error;
> +			log_title = "warning: ";
> +			break;

Warnings shouldn't look the same as errors.  Please make this "\033[1;33m" instead.


> +		case ALPM_LOG_DEBUG:
> +			log_color = color->info;
> +			log_title = "debug: ";
> +			break;
> +		case ALPM_LOG_FUNCTION:
> +			log_color = color->info;
> +			log_title = "function: ";
> +			break;

Please leave these uncolored, as making all debug log lines colorized 
doesn't really add anything, and makes the visual difference between 
output and logs smaller.


> +		}
> +
> +	fprintf(stderr, "%s%s%s", log_color, log_title, color->off);
> +	vfprintf(stderr, fmt, args);
> +}
> +
>  /* Trim whitespace and newlines from a string
>   */
>  static size_t strtrim(char *str)
> @@ -222,6 +258,7 @@ static void usage(void)
>  			"  -s, --sync           search sync databases instead of local\n"
>  			"  -u, --unique         show dependencies with no duplicates (implies -l)\n"
>  			"  -v, --version        display the version\n"
> +			"      --debug          display debug messages\n"
>  			"      --config <path>  set an alternate configuration file\n");
>  }
>  
> @@ -249,6 +286,7 @@ static int parse_options(int argc, char *argv[])
>  		{"version", no_argument,          0, 'v'},
>  
>  		{"config",  required_argument,    0, OP_CONFIG},
> +		{"debug",   no_argument,          0, OP_DEBUG},
>  		{0, 0, 0, 0}
>  	};
>  
> @@ -269,6 +307,9 @@ static int parse_options(int argc, char *argv[])
>  			case OP_CONFIG:
>  				configfile = optarg;
>  				break;
> +			case OP_DEBUG:
> +				debug = 1;
> +				break;
>  			case 'b':
>  				dbpath = optarg;
>  				break;
> @@ -500,6 +541,10 @@ int main(int argc, char *argv[])
>  		cleanup(1);
>  	}
>  
> +	if(debug) {
> +		alpm_option_set_logcb(handle, cb_log);
> +	}
> +
>  	if(searchsyncs) {
>  		if(register_syncs() != 0) {
>  			cleanup(1);
> -- 
> 2.24.1
> 

Other than that, it looks good.  Thanks!

Patch

diff --git a/doc/pactree.8.txt b/doc/pactree.8.txt
index b177788..57a91cb 100644
--- a/doc/pactree.8.txt
+++ b/doc/pactree.8.txt
@@ -61,6 +61,9 @@  Options
 *\--config <file>*::
 	Specify an alternate pacman configuration file.
 
+*\--debug*::
+	Print log messages produced by libalpm.
+
 
 See Also
 --------
diff --git a/src/pactree.c b/src/pactree.c
index a862f9e..8b77f05 100644
--- a/src/pactree.c
+++ b/src/pactree.c
@@ -78,6 +78,8 @@  struct color_choices {
 	const char *branch2;
 	const char *leaf1;
 	const char *leaf2;
+	const char *error;
+	const char *info;
 	const char *off;
 };
 
@@ -86,6 +88,8 @@  static struct color_choices use_color = {
 	"\033[0;37m", /* white */
 	"\033[1;32m", /* bold green */
 	"\033[0;32m", /* green */
+	"\033[1;31m", /* bold red */
+	"\033[1;36m", /* bold cyan */
 	"\033[0m"
 };
 
@@ -94,12 +98,15 @@  static struct color_choices no_color = {
 	"",
 	"",
 	"",
+	"",
+	"",
 	""
 };
 
 /* long operations */
 enum {
-	OP_CONFIG = 1000
+	OP_CONFIG = 1000,
+	OP_DEBUG
 };
 
 /* globals */
@@ -115,9 +122,38 @@  static int max_depth = -1;
 static int reverse = 0;
 static int unique = 0;
 static int searchsyncs = 0;
+static int debug = 0;
 static const char *dbpath = DBPATH;
 static const char *configfile = CONFFILE;
 
+void cb_log(alpm_loglevel_t level, const char *fmt, va_list args)
+{
+	const char *log_color = "";
+	const char *log_title = "";
+
+	switch(level) {
+		case ALPM_LOG_ERROR:
+			log_color = color->error;
+			log_title = "error: ";
+			break;
+		case ALPM_LOG_WARNING:
+			log_color = color->error;
+			log_title = "warning: ";
+			break;
+		case ALPM_LOG_DEBUG:
+			log_color = color->info;
+			log_title = "debug: ";
+			break;
+		case ALPM_LOG_FUNCTION:
+			log_color = color->info;
+			log_title = "function: ";
+			break;
+		}
+
+	fprintf(stderr, "%s%s%s", log_color, log_title, color->off);
+	vfprintf(stderr, fmt, args);
+}
+
 /* Trim whitespace and newlines from a string
  */
 static size_t strtrim(char *str)
@@ -222,6 +258,7 @@  static void usage(void)
 			"  -s, --sync           search sync databases instead of local\n"
 			"  -u, --unique         show dependencies with no duplicates (implies -l)\n"
 			"  -v, --version        display the version\n"
+			"      --debug          display debug messages\n"
 			"      --config <path>  set an alternate configuration file\n");
 }
 
@@ -249,6 +286,7 @@  static int parse_options(int argc, char *argv[])
 		{"version", no_argument,          0, 'v'},
 
 		{"config",  required_argument,    0, OP_CONFIG},
+		{"debug",   no_argument,          0, OP_DEBUG},
 		{0, 0, 0, 0}
 	};
 
@@ -269,6 +307,9 @@  static int parse_options(int argc, char *argv[])
 			case OP_CONFIG:
 				configfile = optarg;
 				break;
+			case OP_DEBUG:
+				debug = 1;
+				break;
 			case 'b':
 				dbpath = optarg;
 				break;
@@ -500,6 +541,10 @@  int main(int argc, char *argv[])
 		cleanup(1);
 	}
 
+	if(debug) {
+		alpm_option_set_logcb(handle, cb_log);
+	}
+
 	if(searchsyncs) {
 		if(register_syncs() != 0) {
 			cleanup(1);