[Spice-devel] [spice-xpi 5/5] Add glib Windows logging

Marc-André Lureau marcandre.lureau at gmail.com
Sun Mar 24 15:27:41 PDT 2013


On Sun, Mar 24, 2013 at 12:16 PM, Christophe Fergeau
<cfergeau at redhat.com> wrote:
> As it's not very convenient to get logging output from the plugin
> while firefox is running, this commit adds a dumb glib logging
> implementation that writes the logging information to a file.
> As this is not efficient at all, it's disabled by default unless
> the SPICE_XPI_LOG_TO_FILE environment variable is set.
> ---
>  SpiceXPI/src/plugin/plugin.cpp | 41 +++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 41 insertions(+)
>
> diff --git a/SpiceXPI/src/plugin/plugin.cpp b/SpiceXPI/src/plugin/plugin.cpp
> index 2e59bfd..e975195 100644
> --- a/SpiceXPI/src/plugin/plugin.cpp
> +++ b/SpiceXPI/src/plugin/plugin.cpp
> @@ -173,6 +173,46 @@ void NS_DestroyPluginInstance(nsPluginInstanceBase *aPlugin)
>  //
>  // nsPluginInstance class implementation
>  //
> +static void glib_log_to_file(const gchar *log_domain,
> +                             GLogLevelFlags log_level,
> +                             const gchar *message,
> +                             gpointer user_data)
> +{
> +    if ((log_level & G_LOG_LEVEL_MASK) > G_LOG_LEVEL_MESSAGE) {
> +        return;
> +    }
> +    if (log_domain != NULL) {
> +        fwrite(log_domain, strlen(log_domain), 1, (FILE *)user_data);
> +        fwrite(": ", 2, 1, (FILE *)user_data);
> +    }
> +    if (message != NULL) {
> +        fwrite(message, strlen(message), 1, (FILE *)user_data);
> +    }
> +    fwrite("\r\n", 2, 1, (FILE *)user_data);
> +    fflush((FILE *)user_data);

it would be nice to avoid 6 casts of "user_data"

> +}
> +
> +static void glib_setup_logging(void)
> +{
> +#if defined(XP_WIN)
> +    FILE *log_file;
> +    gchar *log_filename;
> +
> +    if (!g_getenv("SPICE_XPI_LOG_TO_FILE"))
> +        return;

Imho, it would be more convenient to set the variable to the name of the file.

> +
> +    log_filename = g_build_filename(g_get_tmp_dir(), "SPICEXPI.LOG", NULL);
> +    log_file = fopen(log_filename, "w+");
> +    if (log_file != NULL) {
> +        g_log_set_default_handler(glib_log_to_file, log_file);
> +    } else {
> +        gchar *log_msg;
> +        log_msg = g_strdup_printf("failed to open %s", log_filename);
> +        g_free(log_msg);
> +    }
> +    g_free(log_filename);
> +#endif
> +}
>
>  nsPluginInstance::nsPluginInstance(NPP aInstance):
>      nsPluginInstanceBase(),
> @@ -191,6 +231,7 @@ nsPluginInstance::nsPluginInstance(NPP aInstance):
>  #if !GLIB_CHECK_VERSION(2, 35, 0)
>      g_type_init();
>  #endif
> +    glib_setup_logging();
>
>  #if defined(XP_WIN)
>      m_external_controller = new SpiceControllerWin(this);
> --
> 1.8.1.4
>
> _______________________________________________
> Spice-devel mailing list
> Spice-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/spice-devel



-- 
Marc-André Lureau


More information about the Spice-devel mailing list