Decentralized error handling

This commit is contained in:
Matteo Paonessa 2016-09-20 10:51:17 +02:00
parent c4f8d310a0
commit c4ce35e1ce
6 changed files with 110 additions and 71 deletions

13
.gitignore vendored
View File

@ -5,3 +5,16 @@ build/*
autom4te.cache
aclocal.m4
configure
.vscode/*
# Folder view configuration files
.DS_Store
Desktop.ini
# Thumbnail cache files
._*
Thumbs.db
# Files that might appear on external disks
.Spotlight-V100
.Trashes

View File

@ -5,7 +5,7 @@
###### REQUIREMENTS
* [mozjpeg](https://github.com/mozilla/mozjpeg)
* [zopfli](https://github.com/google/zopfli)
* [zopflipng](https://github.com/google/zopfli)
* [lodepng](https://github.com/lvandeve/lodepng)
----------
@ -13,7 +13,6 @@
###### TESTED PLATFORMS
* Mac OS X El Capitan (v10.11.4)
* Arch Linux
* Ubuntu 14.04.2
----------

View File

@ -251,8 +251,7 @@ void cclt_start(cclt_parameters* pars, off_t* i_t_size, off_t* o_t_size) {
status = stat(output_filename, &st_buf);
if (status != 0) {
//TODO This is not critical, but still something to be tracked
fprintf(stderr, "[ERROR] Failed to get output file stats.\n");
exit(-12);
trigger_error(12, true, output_filename);
}
o_size = st_buf.st_size;
*(o_t_size) += o_size;

View File

@ -4,6 +4,11 @@
#include <stdlib.h>
#include <stdarg.h>
/*
* We could leave error messages where they happens,
* but I want a more centralized way to track what went wrong
*/
#define parse_error_level(level) ((level) ? "ERROR" : "WARNING")
void trigger_error(int code, bool is_critical, ...) {
@ -46,6 +51,26 @@ void trigger_error(int code, bool is_critical, ...) {
case 11:
vfprintf(stderr,
"Failed to get input file stats: %s", args);
break;
case 12:
vfprintf(stderr,
"Failed to get output file stats: %s", args);
break;
case 13:
vfprintf(stderr,
"Failed to open file (markers): %s", args);
break;
case 16:
vfprintf(stderr,
"Failed to open PNG file: %s", args);
break;
case 17:
fprintf(stderr,
"Error while optimizing PNG.");
break;
case 18:
vfprintf(stderr,
"Error while writing PNG: %s", args);
case 20:
fprintf(stderr,
"Found folder along with input files.");
@ -68,6 +93,15 @@ void trigger_error(int code, bool is_critical, ...) {
case 104:
vfprintf(stderr,
"Unknown file type: %s", args);
break;
case 105:
vfprintf(stderr,
"Failed to open file (input): %s", args);
break;
case 106:
vfprintf(stderr,
"Failed to open file (output): %s", args);
break;
default:
//Every unlisted code is critical
is_critical = true;
@ -78,7 +112,7 @@ void trigger_error(int code, bool is_critical, ...) {
fprintf(stderr, "\n");
va_end (args);
va_end(args);
if (is_critical) {
exit(EXIT_FAILURE);

View File

@ -26,8 +26,7 @@ struct jpeg_decompress_struct cclt_get_markers(char* input) {
//Check for errors
//TODO Use UNIX error messages
if (fp == NULL) {
printf("[ERROR] Failed to open file (markers) \"%s\".\n", input);
exit(-13);
trigger_error(13, true, input);
}
//Create the IO instance for the input file
@ -75,8 +74,7 @@ int cclt_jpeg_optimize(char* input_file, char* output_file, int exif_flag, char*
//Check for errors
//TODO Use UNIX error messages
if (fp == NULL) {
printf("[ERROR] Failed to open file (input) \"%s\".\n", input_file);
return -1;
trigger_error(105, true, input_file);
}
//Create the IO istance for the input file
@ -111,8 +109,7 @@ int cclt_jpeg_optimize(char* input_file, char* output_file, int exif_flag, char*
//Check for errors
//TODO Use UNIX error messages
if (fp == NULL) {
printf("[ERROR] Failed to open file (output) \"%s\".\n", output_file);
return -2;
trigger_error(106, true, output_file);
}
//CRITICAL - This is the optimization step
@ -161,8 +158,7 @@ void cclt_jpeg_compress(char* output_file, unsigned char* image_buffer, cclt_jpe
//Check for errors
//TODO Use UNIX error messages
if (fp == NULL) {
printf("[ERROR] Failed to open output \"%s\".\n", output_file);
return;
trigger_error(106, true, output_file);
}
output_buffer = NULL;

View File

@ -5,6 +5,7 @@
#include "lodepng.h"
#include <zopflipng/zopflipng_lib.h>
#include "png.h"
#include "error.h"
void cclt_png_optimize(char* input, char* output, cclt_png_parameters* pars) {
//TODO Error handling
@ -28,8 +29,7 @@ void cclt_png_optimize(char* input, char* output, cclt_png_parameters* pars) {
png_options.auto_filter_strategy = pars->auto_filter_strategy;
if (lodepng_load_file(&orig_buffer, &orig_buffer_size, input) != 0) {
fprintf(stderr, "[ERROR] Error while loading PNG.\n");
exit(-16);
trigger_error(16, true, input);
}
if (CZopfliPNGOptimize(orig_buffer,
@ -38,13 +38,11 @@ void cclt_png_optimize(char* input, char* output, cclt_png_parameters* pars) {
0,
&resultpng,
&resultpng_size) != 0) {
fprintf(stderr, "[ERROR] Error while optimizing PNG.\n");
exit(-17);
trigger_error(17, true);
}
if (lodepng_save_file(resultpng, resultpng_size, output) != 0) {
fprintf(stderr, "[ERROR] Error while writing PNG.\n");
exit(-18);
trigger_error(18, true, output);
}
free(orig_buffer);