errors.c 7 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
Elliot Lee's avatar
Elliot Lee committed
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
Elliot Lee's avatar
Elliot Lee committed
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
Elliot Lee's avatar
Elliot Lee committed
7 8 9 10 11 12 13 14
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
16
 */
17

18 19
#include "config.h"

20
#define _GNU_SOURCE  /* need the POSIX signal API */
21

Elliot Lee's avatar
Elliot Lee committed
22 23
#include <signal.h>
#include <stdarg.h>
24
#include <stdlib.h>
Elliot Lee's avatar
Elliot Lee committed
25
#include <sys/types.h>
26
#ifdef HAVE_UNISTD_H
Elliot Lee's avatar
Elliot Lee committed
27
#include <unistd.h>
28
#endif
Elliot Lee's avatar
Elliot Lee committed
29

30
#include <gio/gio.h>
31

32
#include "libgimpbase/gimpbase.h"
33

34
#include "core/core-types.h"
Sven Neumann's avatar
Sven Neumann committed
35

36
#include "core/gimp.h"
Michael Natterer's avatar
Michael Natterer committed
37

Elliot Lee's avatar
Elliot Lee committed
38
#include "errors.h"
39

40
#ifdef G_OS_WIN32
41 42 43
#include <windows.h>
#endif

44

45 46
/*  private variables  */

47
static Gimp                *the_errors_gimp   = NULL;
48 49 50 51
static gboolean             use_debug_handler = FALSE;
static GimpStackTraceMode   stack_trace_mode  = GIMP_STACK_TRACE_QUERY;
static gchar               *full_prog_name    = NULL;

52 53 54

/*  local function prototypes  */

55 56 57
static G_GNUC_NORETURN void  gimp_eek (const gchar *reason,
                                       const gchar *message,
                                       gboolean     use_handler);
58

59 60 61 62 63 64 65 66 67 68 69 70
static void   gimp_third_party_message_log_func (const gchar        *log_domain,
                                                 GLogLevelFlags      flags,
                                                 const gchar        *message,
                                                 gpointer            data);
static void   gimp_message_log_func             (const gchar        *log_domain,
                                                 GLogLevelFlags      flags,
                                                 const gchar        *message,
                                                 gpointer            data);
static void   gimp_error_log_func               (const gchar        *domain,
                                                 GLogLevelFlags      flags,
                                                 const gchar        *message,
                                                 gpointer            data) G_GNUC_NORETURN;
71

72 73


74 75
/*  public functions  */

76
void
77 78 79 80
errors_init (Gimp               *gimp,
             const gchar        *_full_prog_name,
             gboolean            _use_debug_handler,
             GimpStackTraceMode  _stack_trace_mode)
81
{
82 83 84 85 86 87 88 89 90 91 92
  const gchar * const log_domains[] =
  {
    "Gimp",
    "Gimp-Actions",
    "Gimp-Base",
    "Gimp-Composite",
    "Gimp-Config",
    "Gimp-Core",
    "Gimp-Dialogs",
    "Gimp-Display",
    "Gimp-File",
93
    "Gimp-GEGL",
94 95
    "Gimp-GUI",
    "Gimp-Menus",
96
    "Gimp-Operations",
97 98 99 100 101 102 103 104
    "Gimp-PDB",
    "Gimp-Paint",
    "Gimp-Paint-Funcs",
    "Gimp-Plug-In",
    "Gimp-Text",
    "Gimp-Tools",
    "Gimp-Vectors",
    "Gimp-Widgets",
105 106 107 108 109 110 111 112
    "Gimp-XCF",
    "LibGimpBase",
    "LibGimpColor",
    "LibGimpConfig",
    "LibGimpMath",
    "LibGimpModule",
    "LibGimpThumb",
    "LibGimpWidgets"
113 114 115
  };
  gint i;

116
  g_return_if_fail (GIMP_IS_GIMP (gimp));
117
  g_return_if_fail (_full_prog_name != NULL);
118
  g_return_if_fail (full_prog_name == NULL);
119

120
#ifdef GIMP_UNSTABLE
121
  g_printerr ("This is a development version of GIMP.  "
122
              "Debug messages may appear here.\n\n");
123
#endif /* GIMP_UNSTABLE */
124

125
  the_errors_gimp   = gimp;
126 127 128
  use_debug_handler = _use_debug_handler ? TRUE : FALSE;
  stack_trace_mode  = _stack_trace_mode;
  full_prog_name    = g_strdup (_full_prog_name);
129

130 131 132
  for (i = 0; i < G_N_ELEMENTS (log_domains); i++)
    g_log_set_handler (log_domains[i],
                       G_LOG_LEVEL_MESSAGE,
133
                       gimp_message_log_func, gimp);
Elliot Lee's avatar
Elliot Lee committed
134

135 136 137
  g_log_set_handler ("GEGL",
                     G_LOG_LEVEL_MESSAGE,
                     gimp_third_party_message_log_func, gimp);
138 139
  g_log_set_handler (NULL,
                     G_LOG_LEVEL_ERROR | G_LOG_FLAG_FATAL,
140
                     gimp_error_log_func, gimp);
141
}
Elliot Lee's avatar
Elliot Lee committed
142

143 144 145 146 147 148
void
errors_exit (void)
{
  the_errors_gimp = NULL;
}

149
void
150
gimp_fatal_error (const gchar *message)
151
{
152
  gimp_eek ("fatal error", message, TRUE);
Elliot Lee's avatar
Elliot Lee committed
153
}
154 155

void
156
gimp_terminate (const gchar *message)
157
{
158 159 160 161 162 163
  gimp_eek ("terminated", message, use_debug_handler);
}


/*  private functions  */

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
static void
gimp_third_party_message_log_func (const gchar    *log_domain,
                                   GLogLevelFlags  flags,
                                   const gchar    *message,
                                   gpointer        data)
{
  Gimp *gimp = data;

  if (gimp)
    {
      /* Whereas all GIMP messages are processed under the same domain,
       * we need to keep the log domain information for third party
       * messages.
       */
      gimp_show_message (gimp, NULL, GIMP_MESSAGE_WARNING,
                         log_domain, message);
    }
  else
    {
      g_printerr ("%s: %s\n\n", log_domain, message);
    }
}

187 188 189 190 191 192
static void
gimp_message_log_func (const gchar    *log_domain,
                       GLogLevelFlags  flags,
                       const gchar    *message,
                       gpointer        data)
{
193
  Gimp *gimp = data;
194

195
  if (gimp)
196
    {
197
      gimp_show_message (gimp, NULL, GIMP_MESSAGE_WARNING, NULL, message);
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    }
  else
    {
      g_printerr ("%s: %s\n\n",
                  gimp_filename_to_utf8 (full_prog_name), message);
    }
}

static void
gimp_error_log_func (const gchar    *domain,
                     GLogLevelFlags  flags,
                     const gchar    *message,
                     gpointer        data)
{
  gimp_fatal_error (message);
}

215 216 217 218 219
static void
gimp_eek (const gchar *reason,
          const gchar *message,
          gboolean     use_handler)
{
220
#ifndef G_OS_WIN32
221 222
  g_printerr ("%s: %s: %s\n", gimp_filename_to_utf8 (full_prog_name),
              reason, message);
223

224
  if (use_handler)
225
    {
226
      switch (stack_trace_mode)
227 228 229 230 231 232 233 234 235 236
        {
        case GIMP_STACK_TRACE_NEVER:
          break;

        case GIMP_STACK_TRACE_QUERY:
          {
            sigset_t sigset;

            sigemptyset (&sigset);
            sigprocmask (SIG_SETMASK, &sigset, NULL);
237 238 239 240

            if (the_errors_gimp)
              gimp_gui_ungrab (the_errors_gimp);

241 242 243 244 245 246 247 248 249 250
            g_on_error_query (full_prog_name);
          }
          break;

        case GIMP_STACK_TRACE_ALWAYS:
          {
            sigset_t sigset;

            sigemptyset (&sigset);
            sigprocmask (SIG_SETMASK, &sigset, NULL);
251

252 253 254 255 256 257 258
            g_on_error_stack_trace (full_prog_name);
          }
          break;

        default:
          break;
        }
259
    }
260
#else
261 262

  /* g_on_error_* don't do anything reasonable on Win32. */
263

264 265
  MessageBox (NULL, g_strdup_printf ("%s: %s", reason, message),
              full_prog_name, MB_OK|MB_ICONERROR);
266 267 268

#endif /* ! G_OS_WIN32 */

269
  exit (EXIT_FAILURE);
270
}