1//========================================================================
2//
3// Error.cc
4//
5// Copyright 1996-2003 Glyph & Cog, LLC
6//
7//========================================================================
8
9//========================================================================
10//
11// Modified under the Poppler project - http://poppler.freedesktop.org
12//
13// All changes made under the Poppler project to this file are licensed
14// under GPL version 2 or later
15//
16// Copyright (C) 2005, 2007 Jeff Muizelaar <jeff@infidigm.net>
17// Copyright (C) 2005, 2018, 2022 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, 2017 Adrian Johnson <ajohnson@redneon.com>
21// Copyright (C) 2020 Adam Reichold <adam.reichold@t-online.de>
22//
23// To see a description of the changes please see the Changelog file that
24// came with your tarball or type make ChangeLog if you are building from git
25//
26//========================================================================
27
28#include <config.h>
29#include <poppler-config.h>
30
31#include <cstdio>
32#include <cstddef>
33#include <cstdarg>
34#include "GooString.h"
35#include "GlobalParams.h"
36#include "Error.h"
37
38static const char *errorCategoryNames[] = { "Syntax Warning", "Syntax Error", "Config Error", "Command Line Error", "I/O Error", "Permission Error", "Unimplemented Feature", "Internal Error" };
39
40static ErrorCallback errorCbk = nullptr;
41
42void setErrorCallback(ErrorCallback cbk)
43{
44 errorCbk = cbk;
45}
46
47void CDECL error(ErrorCategory category, Goffset pos, const char *msg, ...)
48{
49 va_list args;
50
51 // NB: this can be called before the globalParams object is created
52 if (!errorCbk && globalParams && globalParams->getErrQuiet()) {
53 return;
54 }
55 va_start(args, msg);
56 const std::unique_ptr<GooString> s = GooString::formatv(fmt: msg, argList: args);
57 va_end(args);
58
59 GooString sanitized;
60 for (int i = 0; i < s->getLength(); ++i) {
61 const char c = s->getChar(i);
62 if (c < (char)0x20 || c >= (char)0x7f) {
63 sanitized.appendf(fmt: "<{0:02x}>", c & 0xff);
64 } else {
65 sanitized.append(c);
66 }
67 }
68
69 if (errorCbk) {
70 (*errorCbk)(category, pos, sanitized.c_str());
71 } else {
72 if (pos >= 0) {
73 fprintf(stderr, format: "%s (%lld): %s\n", errorCategoryNames[category], (long long)pos, sanitized.c_str());
74 } else {
75 fprintf(stderr, format: "%s: %s\n", errorCategoryNames[category], sanitized.c_str());
76 }
77 fflush(stderr);
78 }
79}
80

source code of poppler/poppler/Error.cc