geany  1.38
About: Geany is a text editor (using GTK2) with basic features of an integrated development environment (syntax highlighting, code folding, symbol name auto-completion, ...). F: office T: editor programming GTK+ IDE
  Fossies Dox: geany-1.38.tar.bz2  ("unofficial" and yet experimental doxygen-generated source code documentation)  

error.c
Go to the documentation of this file.
1/*
2* Copyright (c) 2002-2003, Darren Hiebert
3*
4* This source code is released for free distribution under the terms of the
5* GNU General Public License version 2 or (at your option) any later version.
6*
7* This module contains a lose assortment of shared functions.
8*/
9
10#include "general.h" /* must always come first */
11#include <string.h>
12#include <errno.h>
13
14#include "error_p.h"
15#include "options_p.h"
16#include "routines_p.h"
17
18#ifdef HAVE_JANSSON
19#include <jansson.h>
20#endif
21
22#define selected(var,feature) (((int)(var) & (int)(feature)) == (int)feature)
23
25static void *errorPrinterData;
26
27extern void setErrorPrinter (errorPrintFunc printer, void *data)
28{
29 errorPrinter = printer;
30 errorPrinterData = data;
31}
32
33extern bool stderrDefaultErrorPrinter (const errorSelection selection,
34 const char *const format,
35 va_list ap, void *data CTAGS_ATTR_UNUSED)
36{
37 fprintf (stderr, "%s: %s", getExecutableName (),
38 selected (selection, WARNING) ? "Warning: " : "");
39 vfprintf (stderr, format, ap);
40 if (selected (selection, PERROR))
41 {
42#ifdef HAVE_STRERROR
43 fprintf (stderr, " : %s", strerror (errno));
44#else
45 perror (" ");
46#endif
47 }
48 fputs ("\n", stderr);
49
50 return (selected (selection, FATAL) || Option.fatalWarnings)? true: false;
51}
52
53extern void error (const errorSelection selection,
54 const char *const format, ...)
55{
56 va_list ap;
57 bool shouldExit;
58
59 va_start (ap, format);
60 shouldExit = (* errorPrinter) (selection, format, ap, errorPrinterData);
61 va_end (ap);
62
63 if (shouldExit)
64 exit (1);
65}
66
67#ifdef HAVE_JANSSON
68bool jsonErrorPrinter (const errorSelection selection, const char *const format, va_list ap,
69 void *data CTAGS_ATTR_UNUSED)
70{
71#define ERR_BUFFER_SIZE 4096
72 static char reason[ERR_BUFFER_SIZE];
73
74 vsnprintf (reason, ERR_BUFFER_SIZE, format, ap);
75 reason [ERR_BUFFER_SIZE - 1] = '\0'; /* Do we need this? */
76
77 json_t *response = json_object ();
78 json_object_set_new (response, "_type", json_string ("error"));
79 json_object_set_new (response, "message", json_string (reason));
80 if (selected (selection, WARNING))
81 json_object_set_new (response, "warning", json_true ());
82 if (selected (selection, FATAL))
83 json_object_set_new (response, "fatal", json_true ());
84 if (selected (selection, PERROR))
85 {
86 json_object_set_new (response, "errno", json_integer (errno));
87 json_object_set_new (response, "perror", json_string (strerror (errno)));
88 }
89 json_dumpf (response, stdout, JSON_PRESERVE_ORDER);
90 fprintf (stdout, "\n");
91
92 json_decref (response);
93
94 return false;
95}
96#endif
void error(const errorSelection selection, const char *const format,...)
Definition: error.c:53
static void * errorPrinterData
Definition: error.c:25
#define selected(var, feature)
Definition: error.c:22
void setErrorPrinter(errorPrintFunc printer, void *data)
Definition: error.c:27
bool stderrDefaultErrorPrinter(const errorSelection selection, const char *const format, va_list ap, void *data)
Definition: error.c:33
static errorPrintFunc errorPrinter
Definition: error.c:24
bool(* errorPrintFunc)(const errorSelection selection, const char *const format, va_list ap, void *data)
Definition: error_p.h:18
int errno
#define CTAGS_ATTR_UNUSED
Definition: gcc-attr.h:22
CobolFormat format
Definition: geany_cobol.c:137
bool jsonErrorPrinter(const errorSelection selection, const char *const format, va_list ap, void *data)
optionValues Option
Definition: options.c:137
const char * getExecutableName(void)
Definition: routines.c:192
@ PERROR
Definition: routines.h:37
@ FATAL
Definition: routines.h:37
@ WARNING
Definition: routines.h:37
int errorSelection
Definition: routines.h:36
bool fatalWarnings
Definition: options_p.h:117