[gnome-terminal] client: legacy: Add verbosity control options



commit e970a984cf77c2f1c04a0acd309d66a9f2ac8250
Author: Christian Persch <chpe src gnome org>
Date:   Mon Oct 30 22:41:06 2017 +0100

    client: legacy: Add verbosity control options
    
    Add options to control the verbosity of the output, using --quiet/-q
    to silence all output, and --verbose/-v to increase verbosity.
    
    This will be used later on.

 src/terminal-options.c |   90 +++++++++++++++++++++++++++++++++---------------
 src/terminal-options.h |   24 +++++++++++++
 src/terminal.c         |   30 ++++++++--------
 3 files changed, 101 insertions(+), 43 deletions(-)
---
diff --git a/src/terminal-options.c b/src/terminal-options.c
index e0dc841..3c21846 100644
--- a/src/terminal-options.c
+++ b/src/terminal-options.c
@@ -27,6 +27,7 @@
 
 #include <glib.h>
 #include <glib/gi18n.h>
+#include <glib/gprintf.h>
 
 #include "terminal-options.h"
 #include "terminal-screen.h"
@@ -35,6 +36,23 @@
 #include "terminal-version.h"
 #include "terminal-libgsystem.h"
 
+static int verbosity = 1;
+
+void
+terminal_fprintf (FILE* fp,
+                  int verbosity_level,
+                  char const* format,
+                  ...)
+{
+        if (verbosity < verbosity_level)
+                return;
+
+        va_list args;
+        va_start(args, format);
+        g_vfprintf(fp, format, args);
+        va_end(args);
+}
+
 static GOptionContext *get_goption_context (TerminalOptions *options);
 
 static TerminalSettingsList *
@@ -220,9 +238,9 @@ add_new_window (TerminalOptions *options,
 static void
 deprecated_option_warning (const gchar *option_name)
 {
-  g_printerr (_("Option “%s” is deprecated and might be removed in a later version of gnome-terminal."),
-              option_name);
-  g_printerr ("\n");
+  terminal_printerr (_("Option “%s” is deprecated and might be removed in a later version of 
gnome-terminal."),
+                     option_name);
+  terminal_printerr ("\n");
 }
 
 static void
@@ -231,8 +249,8 @@ deprecated_command_option_warning (const char *option_name)
   deprecated_option_warning (option_name);
 
   /* %s is being replaced with "-- " (without quotes), which must be used literally, not translatable */
-  g_printerr (_("Use “%s” to terminate the options and put the command line to execute after it."), "-- ");
-  g_printerr ("\n");
+  terminal_printerr (_("Use “%s” to terminate the options and put the command line to execute after it."), 
"-- ");
+  terminal_printerr ("\n");
 }
 
 static gboolean
@@ -241,9 +259,9 @@ unsupported_option_callback (const gchar *option_name,
                              gpointer     data,
                              GError     **error)
 {
-  g_printerr (_("Option “%s” is no longer supported in this version of gnome-terminal."),
+  terminal_printerr (_("Option “%s” is no longer supported in this version of gnome-terminal."),
               option_name);
-  g_printerr ("\n");
+  terminal_printerr ("\n");
   return TRUE; /* we do not want to bail out here but continue */
 }
 
@@ -266,17 +284,31 @@ option_version_cb (const gchar *option_name,
                    gpointer     data,
                    GError     **error)
 {
-  g_print ("%s %s ", _("GNOME Terminal"), VERSION);
-  g_print (_("Using VTE version %u.%u.%u"),
-           vte_get_major_version (),
-           vte_get_minor_version (),
-           vte_get_micro_version ());
-  g_print (" %s\n", vte_get_features ());
+  terminal_print ("%s %s ", _("GNOME Terminal"), VERSION);
+  terminal_print (_("Using VTE version %u.%u.%u"),
+                  vte_get_major_version (),
+                  vte_get_minor_version (),
+                  vte_get_micro_version ());
+  terminal_print (" %s\n", vte_get_features ());
 
   exit (EXIT_SUCCESS);
 }
 
 static gboolean
+option_verbosity_cb (const gchar *option_name,
+                     const gchar *value,
+                     gpointer     data,
+                     GError     **error)
+{
+  if (g_str_equal (option_name, "--quiet") || g_str_equal (option_name, "-q"))
+    verbosity = 0;
+  else
+    verbosity++;
+
+  return TRUE;
+}
+
+static gboolean
 option_app_id_callback (const gchar *option_name,
                           const gchar *value,
                           gpointer     data,
@@ -349,8 +381,8 @@ option_profile_cb (const gchar *option_name,
                                                      value, error);
   if (profile == NULL)
   {
-      g_printerr ("Profile '%s' specified but not found. Attempting to fall back "
-                  "to the default profile.\n", value);
+      terminal_printerr ("Profile '%s' specified but not found. Attempting to fall back "
+                         "to the default profile.\n", value);
       g_clear_error (error);
       profile = terminal_profiles_list_dup_uuid_or_name (terminal_options_ensure_profiles_list (options),
                                                          NULL, error);
@@ -420,8 +452,8 @@ option_window_callback (const gchar *option_name,
 
   if (value && profile == NULL)
   {
-      g_printerr ("Profile '%s' specified but not found. Attempting to fall back "
-                  "to the default profile.\n", value);
+      terminal_printerr ("Profile '%s' specified but not found. Attempting to fall back "
+                         "to the default profile.\n", value);
       g_clear_error (error);
       profile = terminal_profiles_list_dup_uuid_or_name (terminal_options_ensure_profiles_list (options),
                                                          NULL, error);
@@ -502,8 +534,8 @@ option_show_menubar_callback (const gchar *option_name,
       iw = g_list_last (options->initial_windows)->data;
       if (iw->force_menubar_state && iw->menubar_state == TRUE)
         {
-          g_printerr (_("“%s” option given twice for the same window\n"),
-                        "--show-menubar");
+          terminal_printerr_detail (_("“%s” option given twice for the same window\n"),
+                                    "--show-menubar");
 
           return TRUE;
         }
@@ -535,8 +567,8 @@ option_hide_menubar_callback (const gchar *option_name,
 
       if (iw->force_menubar_state && iw->menubar_state == FALSE)
         {
-          g_printerr (_("“%s” option given twice for the same window\n"),
-                        "--hide-menubar");
+          terminal_printerr_detail (_("“%s” option given twice for the same window\n"),
+                                    "--hide-menubar");
           return TRUE;
         }
 
@@ -735,17 +767,17 @@ option_zoom_callback (const gchar *option_name,
 
   if (zoom < (TERMINAL_SCALE_MINIMUM + 1e-6))
     {
-      g_printerr (_("Zoom factor “%g” is too small, using %g\n"),
-                  zoom,
-                  TERMINAL_SCALE_MINIMUM);
+      terminal_printerr (_("Zoom factor “%g” is too small, using %g\n"),
+                         zoom,
+                         TERMINAL_SCALE_MINIMUM);
       zoom = TERMINAL_SCALE_MINIMUM;
     }
 
   if (zoom > (TERMINAL_SCALE_MAXIMUM - 1e-6))
     {
-      g_printerr (_("Zoom factor “%g” is too large, using %g\n"),
-                  zoom,
-                  TERMINAL_SCALE_MAXIMUM);
+      terminal_printerr (_("Zoom factor “%g” is too large, using %g\n"),
+                         zoom,
+                         TERMINAL_SCALE_MAXIMUM);
       zoom = TERMINAL_SCALE_MAXIMUM;
     }
 
@@ -823,7 +855,7 @@ terminal_options_parse (const char *working_directory,
   int i;
   char **argv = *argvp;
 
-  options = g_slice_new0 (TerminalOptions);
+  options = g_new0 (TerminalOptions, 1);
 
   options->remote_arguments = FALSE;
   options->default_window_menubar_forced = FALSE;
@@ -1100,6 +1132,8 @@ get_goption_context (TerminalOptions *options)
     },
     { "preferences", 0, 0, G_OPTION_ARG_NONE, &options->show_preferences, N_("Show preferences window"), 
NULL },
     { "version", 0, G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, option_version_cb, 
NULL, NULL },
+    { "verbose", 'v', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, option_verbosity_cb, N_("Increase 
diagnostic verbosity"), NULL },
+    { "quiet", 'q', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, option_verbosity_cb, N_("Suppress output"), 
NULL },
     { NULL, 0, 0, 0, NULL, NULL, NULL }
   };
 
diff --git a/src/terminal-options.h b/src/terminal-options.h
index bc63452..2285b14 100644
--- a/src/terminal-options.h
+++ b/src/terminal-options.h
@@ -23,6 +23,7 @@
 #define TERMINAL_OPTIONS_H
 
 #include <glib.h>
+#include <stdio.h>
 
 #include "terminal-profiles-list.h"
 
@@ -145,6 +146,29 @@ void terminal_options_ensure_window (TerminalOptions *options);
 
 void terminal_options_free (TerminalOptions *options);
 
+typedef enum {
+  TERMINAL_VERBOSITY_QUIET  = 0,
+  TERMINAL_VERBOSITY_NORMAL = 1,
+  TERMINAL_VERBOSITY_DETAIL = 2,
+  TERMINAL_VERBOSITY_DEBUG  = 3
+} TerminalVerbosity;
+
+void terminal_fprintf (FILE* fp,
+                       int verbosity_level,
+                       char const* format,
+                       ...) G_GNUC_PRINTF(3, 4);
+
+#define terminal_print_level(level,...) terminal_fprintf(stdout, TERMINAL_VERBOSITY_ ## level, __VA_ARGS__)
+#define terminal_printerr_level(level,...) terminal_fprintf(stderr, TERMINAL_VERBOSITY_ ## level, 
__VA_ARGS__)
+
+#define terminal_print(...) terminal_print_level(NORMAL, __VA_ARGS__)
+#define terminal_print_detail(...) terminal_print_level(DETAIL, __VA_ARGS__)
+#define terminal_print_debug(...) terminal_print_level(DEBUG, __VA_ARGS__)
+
+#define terminal_printerr_detail(...) terminal_print_level(DETAIL, __VA_ARGS__)
+#define terminal_printerr(...) terminal_print_level(NORMAL, __VA_ARGS__)
+#define terminal_printerr_debug(...) terminal_print_level(DEBUG, __VA_ARGS__)
+
 G_END_DECLS
 
 #endif /* !TERMINAL_OPTIONS_H */
diff --git a/src/terminal.c b/src/terminal.c
index b3ebe9c..30b8241 100644
--- a/src/terminal.c
+++ b/src/terminal.c
@@ -88,25 +88,25 @@ handle_factory_error (GError *error,
     return FALSE;
 
   g_dbus_error_strip_remote_error (error);
-  g_printerr ("%s\n\n", error->message);
+  terminal_printerr ("%s\n\n", error->message);
 
   switch (exit_status) {
   case _EXIT_FAILURE_WRONG_ID:
-    g_printerr ("You tried to run gnome-terminal-server with elevated privileged. This is not supported.\n");
+    terminal_printerr ("You tried to run gnome-terminal-server with elevated privileged. This is not 
supported.\n");
     break;
   case _EXIT_FAILURE_NO_UTF8:
-    g_printerr ("The environment that gnome-terminal-server was launched with specified a non-UTF-8 locale. 
This is not supported.\n");
+    terminal_printerr ("The environment that gnome-terminal-server was launched with specified a non-UTF-8 
locale. This is not supported.\n");
     break;
   case _EXIT_FAILURE_UNSUPPORTED_LOCALE:
-    g_printerr ("The environment that gnome-terminal-server was launched with specified an unsupported 
locale.\n");
+    terminal_printerr ("The environment that gnome-terminal-server was launched with specified an 
unsupported locale.\n");
     break;
   case _EXIT_FAILURE_GTK_INIT:
-    g_printerr ("The environment that gnome-terminal-server was launched with most likely contained an 
incorrect or unset \"DISPLAY\" variable.\n");
+    terminal_printerr ("The environment that gnome-terminal-server was launched with most likely contained 
an incorrect or unset \"DISPLAY\" variable.\n");
     break;
   default:
     break;
   }
-  g_printerr ("See https://wiki.gnome.org/Apps/Terminal/FAQ#Exit_status_%d for more information.\n", 
exit_status);
+  terminal_printerr ("See https://wiki.gnome.org/Apps/Terminal/FAQ#Exit_status_%d for more information.\n", 
exit_status);
 
   return TRUE;
 }
@@ -119,7 +119,7 @@ handle_create_instance_error (GError *error,
     return TRUE;
 
   g_dbus_error_strip_remote_error (error);
-  g_printerr ("Error creating terminal: %s\n", error->message);
+  terminal_printerr ("Error creating terminal: %s\n", error->message);
   return FALSE; /* don't abort */
 }
 
@@ -132,7 +132,7 @@ handle_create_receiver_proxy_error (GError *error,
     return TRUE;
 
   g_dbus_error_strip_remote_error (error);
-  g_printerr ("Failed to create proxy for terminal: %s\n", error->message);
+  terminal_printerr ("Failed to create proxy for terminal: %s\n", error->message);
   return FALSE; /* don't abort */
 }
 
@@ -144,7 +144,7 @@ handle_exec_error (GError *error,
     return TRUE;
 
   g_dbus_error_strip_remote_error (error);
-  g_printerr ("Error: %s\n", error->message);
+  terminal_printerr ("Error: %s\n", error->message);
   return FALSE; /* don't abort */
 }
 
@@ -158,7 +158,7 @@ handle_show_preferences (const char *service_name)
 
   bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
   if (bus == NULL) {
-    g_printerr ("Failed to get session bus: %s\n", error->message);
+    terminal_printerr ("Failed to get session bus: %s\n", error->message);
     return;
   }
 
@@ -186,7 +186,7 @@ handle_show_preferences (const char *service_name)
                                     30 * 1000 /* ms timeout */,
                                     NULL /* cancelleable */,
                                     &error)) {
-    g_printerr ("Activate call failed: %s\n", error->message);
+    terminal_printerr ("Activate call failed: %s\n", error->message);
     return;
   }
 }
@@ -376,7 +376,7 @@ main (int argc, char **argv)
                                     &argc, &argv,
                                     &error);
   if (options == NULL) {
-    g_printerr (_("Failed to parse arguments: %s\n"), error->message);
+    terminal_printerr (_("Failed to parse arguments: %s\n"), error->message);
     goto out;
   }
 
@@ -387,7 +387,7 @@ main (int argc, char **argv)
     options->startup_id = terminal_client_get_fallback_startup_id ();
   /* Still NULL? */
   if (options->startup_id == NULL)
-    g_printerr("Warning: DESKTOP_STARTUP_ID not set and no fallback available.\n");
+    terminal_printerr_detail ("Warning: DESKTOP_STARTUP_ID not set and no fallback available.\n");
 
   display = gdk_display_get_default ();
   display_name = gdk_display_get_name (display);
@@ -404,8 +404,8 @@ main (int argc, char **argv)
                                                      &error);
   if (factory == NULL) {
     if (!handle_factory_error (error, service_name))
-      g_printerr ("Error constructing proxy for %s:%s: %s\n",
-                  service_name, TERMINAL_FACTORY_OBJECT_PATH, error->message);
+      terminal_printerr ("Error constructing proxy for %s:%s: %s\n",
+                         service_name, TERMINAL_FACTORY_OBJECT_PATH, error->message);
 
     goto out;
   }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]