Rework error/debug output.

This commit is contained in:
Michael R Sweet 2021-05-10 08:40:52 -04:00
parent 67ed214c59
commit cfb5ca0ddc
No known key found for this signature in database
GPG Key ID: 999559A027815955
5 changed files with 55 additions and 107 deletions

View File

@ -305,24 +305,23 @@ pdfioArrayCreate(pdfio_file_t *pdf) // I - PDF file
} }
#ifdef DEBUG
// //
// '_pdfioArrayDebug()' - Print the contents of an array. // '_pdfioArrayDebug()' - Print the contents of an array.
// //
void void
_pdfioArrayDebug(pdfio_array_t *a) // I - Array _pdfioArrayDebug(pdfio_array_t *a, // I - Array
FILE *fp) // I - Output file
{ {
size_t i; // Looping var size_t i; // Looping var
_pdfio_value_t *v; // Current value _pdfio_value_t *v; // Current value
PDFIO_DEBUG("["); putc('[', fp);
for (i = a->num_values, v = a->values; i > 0; i --, v ++) for (i = a->num_values, v = a->values; i > 0; i --, v ++)
_pdfioValueDebug(v); _pdfioValueDebug(v, fp);
PDFIO_DEBUG("]"); putc(']', fp);
} }
#endif // DEBUG
// //

View File

@ -107,13 +107,13 @@ pdfioDictCreate(pdfio_file_t *pdf) // I - PDF file
} }
#ifdef DEBUG
// //
// '_pdfioDictDebug()' - Dump a dictionary to stderr. // '_pdfioDictDebug()' - Dump a dictionary to stderr.
// //
void void
_pdfioDictDebug(pdfio_dict_t *dict) // I - Dictionary _pdfioDictDebug(pdfio_dict_t *dict, // I - Dictionary
FILE *fp) // I - Output file
{ {
size_t i; // Looping var size_t i; // Looping var
_pdfio_pair_t *pair; // Current pair _pdfio_pair_t *pair; // Current pair
@ -121,11 +121,10 @@ _pdfioDictDebug(pdfio_dict_t *dict) // I - Dictionary
for (i = dict->num_pairs, pair = dict->pairs; i > 0; i --, pair ++) for (i = dict->num_pairs, pair = dict->pairs; i > 0; i --, pair ++)
{ {
PDFIO_DEBUG("/%s", pair->key); fprintf(fp, "/%s", pair->key);
_pdfioValueDebug(&pair->value); _pdfioValueDebug(&pair->value, fp);
} }
} }
#endif // DEBUG
// //

View File

@ -48,9 +48,9 @@
# ifdef DEBUG # ifdef DEBUG
# define PDFIO_DEBUG(...) fprintf(stderr, __VA_ARGS__) # define PDFIO_DEBUG(...) fprintf(stderr, __VA_ARGS__)
# define PDFIO_DEBUG_ARRAY(array) _pdfioArrayDebug(array) # define PDFIO_DEBUG_ARRAY(array) _pdfioArrayDebug(array, stderr)
# define PDFIO_DEBUG_DICT(dict) _pdfioDictDebug(dict) # define PDFIO_DEBUG_DICT(dict) _pdfioDictDebug(dict, stderr)
# define PDFIO_DEBUG_VALUE(value) _pdfioValueDebug(value) # define PDFIO_DEBUG_VALUE(value) _pdfioValueDebug(value, stderr)
# else # else
# define PDFIO_DEBUG(...) # define PDFIO_DEBUG(...)
# define PDFIO_DEBUG_ARRAY(array) # define PDFIO_DEBUG_ARRAY(array)
@ -217,17 +217,13 @@ struct _pdfio_stream_s // Stream
// Functions... // Functions...
// //
# ifdef DEBUG extern void _pdfioArrayDebug(pdfio_array_t *a, FILE *fp) PDFIO_INTERNAL;
extern void _pdfioArrayDebug(pdfio_array_t *a) PDFIO_INTERNAL;
# endif // DEBUG
extern void _pdfioArrayDelete(pdfio_array_t *a) PDFIO_INTERNAL; extern void _pdfioArrayDelete(pdfio_array_t *a) PDFIO_INTERNAL;
extern _pdfio_value_t *_pdfioArrayGetValue(pdfio_array_t *a, size_t n) PDFIO_INTERNAL; extern _pdfio_value_t *_pdfioArrayGetValue(pdfio_array_t *a, size_t n) PDFIO_INTERNAL;
extern pdfio_array_t *_pdfioArrayRead(pdfio_file_t *pdf, _pdfio_token_t *ts) PDFIO_INTERNAL; extern pdfio_array_t *_pdfioArrayRead(pdfio_file_t *pdf, _pdfio_token_t *ts) PDFIO_INTERNAL;
extern bool _pdfioArrayWrite(pdfio_array_t *a) PDFIO_INTERNAL; extern bool _pdfioArrayWrite(pdfio_array_t *a) PDFIO_INTERNAL;
# ifdef DEBUG extern void _pdfioDictDebug(pdfio_dict_t *dict, FILE *fp) PDFIO_INTERNAL;
extern void _pdfioDictDebug(pdfio_dict_t *dict) PDFIO_INTERNAL;
# endif // DEBUG
extern void _pdfioDictDelete(pdfio_dict_t *dict) PDFIO_INTERNAL; extern void _pdfioDictDelete(pdfio_dict_t *dict) PDFIO_INTERNAL;
extern _pdfio_value_t *_pdfioDictGetValue(pdfio_dict_t *dict, const char *key) PDFIO_INTERNAL; extern _pdfio_value_t *_pdfioDictGetValue(pdfio_dict_t *dict, const char *key) PDFIO_INTERNAL;
extern pdfio_dict_t *_pdfioDictRead(pdfio_file_t *pdf, _pdfio_token_t *ts) PDFIO_INTERNAL; extern pdfio_dict_t *_pdfioDictRead(pdfio_file_t *pdf, _pdfio_token_t *ts) PDFIO_INTERNAL;
@ -263,9 +259,7 @@ extern void _pdfioTokenPush(_pdfio_token_t *ts, const char *token) PDFIO_INTERN
extern bool _pdfioTokenRead(_pdfio_token_t *ts, char *buffer, size_t bufsize); extern bool _pdfioTokenRead(_pdfio_token_t *ts, char *buffer, size_t bufsize);
extern _pdfio_value_t *_pdfioValueCopy(pdfio_file_t *pdfdst, _pdfio_value_t *vdst, pdfio_file_t *pdfsrc, _pdfio_value_t *vsrc) PDFIO_INTERNAL; extern _pdfio_value_t *_pdfioValueCopy(pdfio_file_t *pdfdst, _pdfio_value_t *vdst, pdfio_file_t *pdfsrc, _pdfio_value_t *vsrc) PDFIO_INTERNAL;
# ifdef DEBUG extern void _pdfioValueDebug(_pdfio_value_t *v, FILE *fp) PDFIO_INTERNAL;
extern void _pdfioValueDebug(_pdfio_value_t *v) PDFIO_INTERNAL;
# endif // DEBUG
extern void _pdfioValueDelete(_pdfio_value_t *v) PDFIO_INTERNAL; extern void _pdfioValueDelete(_pdfio_value_t *v) PDFIO_INTERNAL;
extern _pdfio_value_t *_pdfioValueRead(pdfio_file_t *pdf, _pdfio_token_t *ts, _pdfio_value_t *v) PDFIO_INTERNAL; extern _pdfio_value_t *_pdfioValueRead(pdfio_file_t *pdf, _pdfio_token_t *ts, _pdfio_value_t *v) PDFIO_INTERNAL;
extern bool _pdfioValueWrite(pdfio_file_t *pdf, _pdfio_value_t *v) PDFIO_INTERNAL; extern bool _pdfioValueWrite(pdfio_file_t *pdf, _pdfio_value_t *v) PDFIO_INTERNAL;

View File

@ -80,63 +80,62 @@ _pdfioValueCopy(pdfio_file_t *pdfdst, // I - Destination PDF file
} }
#ifdef DEBUG
// //
// '_pdfioValueDebug()' - Print the contents of a value. // '_pdfioValueDebug()' - Print the contents of a value.
// //
void void
_pdfioValueDebug(_pdfio_value_t *v) // I - Value _pdfioValueDebug(_pdfio_value_t *v, // I - Value
FILE *fp) // I - Output file
{ {
switch (v->type) switch (v->type)
{ {
case PDFIO_VALTYPE_ARRAY : case PDFIO_VALTYPE_ARRAY :
_pdfioArrayDebug(v->value.array); _pdfioArrayDebug(v->value.array, fp);
break; break;
case PDFIO_VALTYPE_BINARY : case PDFIO_VALTYPE_BINARY :
{ {
size_t i; // Looping var size_t i; // Looping var
unsigned char *ptr; // Pointer into data unsigned char *ptr; // Pointer into data
PDFIO_DEBUG("<"); putc('<', fp);
for (i = v->value.binary.datalen, ptr = v->value.binary.data; i > 0; i --, ptr ++) for (i = v->value.binary.datalen, ptr = v->value.binary.data; i > 0; i --, ptr ++)
PDFIO_DEBUG("%02X", *ptr); fprintf(fp, "%02X", *ptr);
PDFIO_DEBUG(">"); putc('>', fp);
} }
break; break;
case PDFIO_VALTYPE_BOOLEAN : case PDFIO_VALTYPE_BOOLEAN :
PDFIO_DEBUG(v->value.boolean ? "true" : "false"); fputs(v->value.boolean ? " true" : " false", fp);
break; break;
case PDFIO_VALTYPE_DATE : case PDFIO_VALTYPE_DATE :
// TODO: Implement date value support // TODO: Implement date value support
PDFIO_DEBUG("(D:YYYYMMDDhhmmssZ)"); fputs("(D:YYYYMMDDhhmmssZ)", fp);
break; break;
case PDFIO_VALTYPE_DICT : case PDFIO_VALTYPE_DICT :
PDFIO_DEBUG("<<"); fputs("<<", fp);
_pdfioDictDebug(v->value.dict); _pdfioDictDebug(v->value.dict, fp);
PDFIO_DEBUG(">>"); fputs(">>", fp);
break; break;
case PDFIO_VALTYPE_INDIRECT : case PDFIO_VALTYPE_INDIRECT :
PDFIO_DEBUG(" %lu %u R", (unsigned long)v->value.indirect.number, v->value.indirect.generation); fprintf(fp, " %lu %u R", (unsigned long)v->value.indirect.number, v->value.indirect.generation);
break; break;
case PDFIO_VALTYPE_NAME : case PDFIO_VALTYPE_NAME :
PDFIO_DEBUG("/%s", v->value.name); fprintf(fp, "/%s", v->value.name);
break; break;
case PDFIO_VALTYPE_NULL : case PDFIO_VALTYPE_NULL :
PDFIO_DEBUG(" null"); fputs(" null", fp);
break; break;
case PDFIO_VALTYPE_NUMBER : case PDFIO_VALTYPE_NUMBER :
PDFIO_DEBUG(" %g", v->value.number); fprintf(fp, " %g", v->value.number);
break; break;
case PDFIO_VALTYPE_STRING : case PDFIO_VALTYPE_STRING :
PDFIO_DEBUG("(%s)", v->value.string); fprintf(fp, "(%s)", v->value.string);
break; break;
default : default :
break; break;
} }
} }
#endif // DEBUG
// //

View File

@ -20,7 +20,7 @@
static int do_test_file(const char *filename); static int do_test_file(const char *filename);
static int do_unit_tests(void); static int do_unit_tests(void);
static bool error_cb(bool *error, const char *message); static bool error_cb(pdfio_file_t *pdf, const char *message, bool *error);
static ssize_t token_consume_cb(const char **s, size_t bytes); static ssize_t token_consume_cb(const char **s, size_t bytes);
static ssize_t token_peek_cb(const char **s, char *buffer, size_t bytes); static ssize_t token_peek_cb(const char **s, char *buffer, size_t bytes);
@ -39,7 +39,7 @@ main(int argc, // I - Number of command-line arguments
for (i = 1; i < argc; i ++) for (i = 1; i < argc; i ++)
{ {
if (!do_test_file(argv[i])) if (do_test_file(argv[i]))
return (1); return (1);
} }
} }
@ -59,6 +59,7 @@ main(int argc, // I - Number of command-line arguments
static int // O - Exit status static int // O - Exit status
do_test_file(const char *filename) // I - PDF filename do_test_file(const char *filename) // I - PDF filename
{ {
bool error = false; // Have we shown an error yet?
pdfio_file_t *pdf; // PDF file pdfio_file_t *pdf; // PDF file
size_t n, // Object/page index size_t n, // Object/page index
num_objs, // Number of objects num_objs, // Number of objects
@ -69,13 +70,16 @@ do_test_file(const char *filename) // I - PDF filename
// Try opening the file... // Try opening the file...
if ((pdf = pdfioFileOpen(filename, NULL, NULL)) != NULL) printf("pdfioFileOpen(\"%s\", ...): ", filename);
if ((pdf = pdfioFileOpen(filename, (pdfio_error_cb_t)error_cb, &error)) != NULL)
{ {
puts("PASS");
// Show basic stats... // Show basic stats...
num_objs = pdfioFileGetNumObjects(pdf); num_objs = pdfioFileGetNumObjects(pdf);
num_pages = pdfioFileGetNumPages(pdf); num_pages = pdfioFileGetNumPages(pdf);
printf("%s: PDF %s, %d pages, %d objects.\n", filename, pdfioFileGetVersion(pdf), (int)num_pages, (int)num_objs); printf(" PDF %s, %d pages, %d objects.\n", pdfioFileGetVersion(pdf), (int)num_pages, (int)num_objs);
// Show a summary of each page... // Show a summary of each page...
for (n = 0; n < num_pages; n ++) for (n = 0; n < num_pages; n ++)
@ -100,7 +104,7 @@ do_test_file(const char *filename) // I - PDF filename
} }
} }
printf("%s: Page #%d is %gx%g.\n", filename, (int)n + 1, media_box.x2, media_box.y2); printf(" Page #%d is %gx%g.\n", (int)n + 1, media_box.x2, media_box.y2);
} }
} }
@ -109,64 +113,16 @@ do_test_file(const char *filename) // I - PDF filename
{ {
if ((obj = pdfioFileGetObject(pdf, n)) == NULL) if ((obj = pdfioFileGetObject(pdf, n)) == NULL)
{ {
printf("%s: Unable to get object #%d.\n", filename, (int)n); printf(" Unable to get object #%d.\n", (int)n);
} }
else else
{ {
size_t np; // Number of pairs
_pdfio_pair_t *pair; // Current pair
dict = pdfioObjGetDict(obj); dict = pdfioObjGetDict(obj);
printf("%s: %u %u obj dict=%p(%lu pairs)\n", filename, (unsigned)pdfioObjGetNumber(obj), (unsigned)pdfioObjGetGeneration(obj), dict, dict ? (unsigned long)dict->num_pairs : 0UL); printf(" %u %u obj dict=%p(%lu pairs)\n", (unsigned)pdfioObjGetNumber(obj), (unsigned)pdfioObjGetGeneration(obj), dict, dict ? (unsigned long)dict->num_pairs : 0UL);
if (dict) fputs(" ", stdout);
{ _pdfioValueDebug(&obj->value, stdout);
// Show a summary of each pair in the dictionary... putchar('\n');
for (np = dict->num_pairs, pair = dict->pairs; np > 0; np --, pair ++)
{
switch (pair->value.type)
{
case PDFIO_VALTYPE_INDIRECT :
printf(" /%s %u %u R\n", pair->key, (unsigned)pair->value.value.indirect.number, pair->value.value.indirect.generation);
break;
case PDFIO_VALTYPE_NAME :
printf(" /%s /%s\n", pair->key, pair->value.value.name);
break;
case PDFIO_VALTYPE_STRING :
printf(" /%s (%s)\n", pair->key, pair->value.value.string);
break;
case PDFIO_VALTYPE_BINARY :
{
size_t bn;
unsigned char *bptr;
printf(" /%s <", pair->key);
for (bn = pair->value.value.binary.datalen, bptr = pair->value.value.binary.data; bn > 0; bn --, bptr ++)
printf("%02X", *bptr);
puts(">");
}
break;
case PDFIO_VALTYPE_NUMBER :
printf(" /%s %g\n", pair->key, pair->value.value.number);
break;
case PDFIO_VALTYPE_BOOLEAN :
printf(" /%s %s\n", pair->key, pair->value.value.boolean ? "true" : "false");
break;
case PDFIO_VALTYPE_NULL :
printf(" /%s null\n", pair->key);
break;
case PDFIO_VALTYPE_ARRAY :
printf(" /%s [...]\n", pair->key);
break;
case PDFIO_VALTYPE_DICT :
printf(" /%s <<...>>\n", pair->key);
break;
default :
printf(" /%s ...\n", pair->key);
break;
}
}
}
} }
} }
@ -208,7 +164,7 @@ do_unit_tests(void)
// First open the test PDF file... // First open the test PDF file...
fputs("pdfioFileOpen(testpdfio.pdf): ", stdout); fputs("pdfioFileOpen(\"testpdfio.pdf\"): ", stdout);
if ((pdf = pdfioFileOpen("testpdfio.pdf", (pdfio_error_cb_t)error_cb, &error)) != NULL) if ((pdf = pdfioFileOpen("testpdfio.pdf", (pdfio_error_cb_t)error_cb, &error)) != NULL)
puts("PASS"); puts("PASS");
else else
@ -244,21 +200,22 @@ do_unit_tests(void)
// //
static bool // O - `true` to stop, `false` to continue static bool // O - `true` to stop, `false` to continue
error_cb(bool *error, // IO - Have we displayed an error? error_cb(pdfio_file_t *pdf, // I - PDF file
const char *message) // I - Error message const char *message, // I - Error message
bool *error) // IO - Have we displayed an error?
{ {
(void)pdf;
if (!*error) if (!*error)
{ {
// First error, so show a "FAIL" indicator // First error, so show a "FAIL" indicator
*error = true; *error = true;
printf("FAIL (%s)\n", message); puts("FAIL");
} }
else
{ // Indent error messages...
// Subsequent errors are just indented...
printf(" %s\n", message); printf(" %s\n", message);
}
// Continue to catch more errors... // Continue to catch more errors...
return (false); return (false);