beta-0.89.2
[luatex.git] / source / libs / poppler / poppler-src / poppler / Error.cc
blob99ca3ad3f5333161c7c6ae16ebc2d7c6a5783b1f
1 //========================================================================
2 //
3 // Error.cc
4 //
5 // Copyright 1996-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
9 //========================================================================
11 // Modified under the Poppler project - http://poppler.freedesktop.org
13 // All changes made under the Poppler project to this file are licensed
14 // under GPL version 2 or later
16 // Copyright (C) 2005, 2007 Jeff Muizelaar <jeff@infidigm.net>
17 // Copyright (C) 2005 Albert Astals Cid <aacid@kde.org>
18 // Copyright (C) 2007 Krzysztof Kowalczyk <kkowalczyk@gmail.com>
19 // Copyright (C) 2012 Marek Kasik <mkasik@redhat.com>
20 // Copyright (C) 2013 Adrian Johnson <ajohnson@redneon.com>
22 // To see a description of the changes please see the Changelog file that
23 // came with your tarball or type make ChangeLog if you are building from git
25 //========================================================================
27 #include <config.h>
29 #ifdef USE_GCC_PRAGMAS
30 #pragma implementation
31 #endif
33 #include <stdio.h>
34 #include <stddef.h>
35 #include <stdarg.h>
36 #include "GooString.h"
37 #include "GlobalParams.h"
38 #include "Error.h"
40 static const char *errorCategoryNames[] = {
41 "Syntax Warning",
42 "Syntax Error",
43 "Config Error",
44 "Command Line Error",
45 "I/O Error",
46 "Permission Error",
47 "Unimplemented Feature",
48 "Internal Error"
51 static void (*errorCbk)(void *data, ErrorCategory category,
52 Goffset pos, char *msg) = NULL;
53 static void *errorCbkData = NULL;
55 void setErrorCallback(void (*cbk)(void *data, ErrorCategory category,
56 Goffset pos, char *msg),
57 void *data) {
58 errorCbk = cbk;
59 errorCbkData = data;
62 void CDECL error(ErrorCategory category, Goffset pos, const char *msg, ...) {
63 va_list args;
64 GooString *s, *sanitized;
66 // NB: this can be called before the globalParams object is created
67 if (!errorCbk && globalParams && globalParams->getErrQuiet()) {
68 return;
70 va_start(args, msg);
71 s = GooString::formatv(msg, args);
72 va_end(args);
74 sanitized = new GooString ();
75 for (int i = 0; i < s->getLength(); ++i) {
76 const char c = s->getChar(i);
77 if (c < (char)0x20 || c >= (char)0x7f) {
78 sanitized->appendf("<{0:02x}>", c & 0xff);
79 } else {
80 sanitized->append(c);
84 if (errorCbk) {
85 (*errorCbk)(errorCbkData, category, pos, sanitized->getCString());
86 } else {
87 if (pos >= 0) {
88 fprintf(stderr, "%s (%lld): %s\n",
89 errorCategoryNames[category], (long long)pos, sanitized->getCString());
90 } else {
91 fprintf(stderr, "%s: %s\n",
92 errorCategoryNames[category], sanitized->getCString());
94 fflush(stderr);
96 delete s;
97 delete sanitized;