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 autom4te.cache
aclocal.m4 aclocal.m4
configure 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 ###### REQUIREMENTS
* [mozjpeg](https://github.com/mozilla/mozjpeg) * [mozjpeg](https://github.com/mozilla/mozjpeg)
* [zopfli](https://github.com/google/zopfli) * [zopflipng](https://github.com/google/zopfli)
* [lodepng](https://github.com/lvandeve/lodepng) * [lodepng](https://github.com/lvandeve/lodepng)
---------- ----------
@ -13,7 +13,6 @@
###### TESTED PLATFORMS ###### TESTED PLATFORMS
* Mac OS X El Capitan (v10.11.4) * Mac OS X El Capitan (v10.11.4)
* Arch Linux * Arch Linux
* Ubuntu 14.04.2
---------- ----------

View File

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

View File

@ -4,6 +4,11 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.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") #define parse_error_level(level) ((level) ? "ERROR" : "WARNING")
void trigger_error(int code, bool is_critical, ...) { void trigger_error(int code, bool is_critical, ...) {
@ -11,74 +16,103 @@ void trigger_error(int code, bool is_critical, ...) {
va_start(args, is_critical); va_start(args, is_critical);
fprintf(stderr, "%s %d: ", fprintf(stderr, "%s %d: ",
parse_error_level(is_critical), parse_error_level(is_critical),
code); code);
switch (code) { switch (code) {
case 1: case 1:
fprintf(stderr, fprintf(stderr,
"-l option can't be used with -q. Either use one or the other."); "-l option can't be used with -q. Either use one or the other.");
break; break;
case 2: case 2:
fprintf(stderr, fprintf(stderr,
"Either -l or -q must be set."); "Either -l or -q must be set.");
break; break;
case 3: case 3:
fprintf(stderr, fprintf(stderr,
"Quality must be within a [1-100] range."); "Quality must be within a [1-100] range.");
break; break;
case 4: case 4:
fprintf(stderr, fprintf(stderr,
"No -o option pointing to the destination folder."); "No -o option pointing to the destination folder.");
break; break;
case 5: case 5:
fprintf(stderr, fprintf(stderr,
"Failed to create output directory. Permission issue?"); "Failed to create output directory. Permission issue?");
break; break;
case 6: case 6:
vfprintf(stderr, vfprintf(stderr,
"Option -%c requires an argument.", args); "Option -%c requires an argument.", args);
break; break;
case 9: case 9:
fprintf(stderr, fprintf(stderr,
"No input files."); "No input files.");
break; break;
case 11: case 11:
vfprintf(stderr, vfprintf(stderr,
"Failed to get input file stats: %s", args); "Failed to get input file stats: %s", args);
case 20: break;
fprintf(stderr, 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."); "Found folder along with input files.");
case 100: case 100:
vfprintf(stderr, vfprintf(stderr,
"Unknown option `-%c'.", args); "Unknown option `-%c'.", args);
break; break;
case 101: case 101:
vfprintf(stderr, vfprintf(stderr,
"Unknown option character `\\x%x'.", args); "Unknown option character `\\x%x'.", args);
break; break;
case 102: case 102:
fprintf(stderr, fprintf(stderr,
"Parameter expected."); "Parameter expected.");
break; break;
case 103: case 103:
fprintf(stderr, fprintf(stderr,
"Folder found, skipping all other inputs."); "Folder found, skipping all other inputs.");
break; break;
case 104: case 104:
vfprintf(stderr, vfprintf(stderr,
"Unknown file type: %s", args); "Unknown file type: %s", args);
default: break;
//Every unlisted code is critical case 105:
is_critical = true; vfprintf(stderr,
fprintf(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;
fprintf(stderr,
"Cs-137 spreading out. Good luck."); "Cs-137 spreading out. Good luck.");
break; break;
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
va_end (args); va_end(args);
if (is_critical) { if (is_critical) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);

View File

@ -26,8 +26,7 @@ struct jpeg_decompress_struct cclt_get_markers(char* input) {
//Check for errors //Check for errors
//TODO Use UNIX error messages //TODO Use UNIX error messages
if (fp == NULL) { if (fp == NULL) {
printf("[ERROR] Failed to open file (markers) \"%s\".\n", input); trigger_error(13, true, input);
exit(-13);
} }
//Create the IO instance for the input file //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 //Check for errors
//TODO Use UNIX error messages //TODO Use UNIX error messages
if (fp == NULL) { if (fp == NULL) {
printf("[ERROR] Failed to open file (input) \"%s\".\n", input_file); trigger_error(105, true, input_file);
return -1;
} }
//Create the IO istance for the 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 //Check for errors
//TODO Use UNIX error messages //TODO Use UNIX error messages
if (fp == NULL) { if (fp == NULL) {
printf("[ERROR] Failed to open file (output) \"%s\".\n", output_file); trigger_error(106, true, output_file);
return -2;
} }
//CRITICAL - This is the optimization step //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 //Check for errors
//TODO Use UNIX error messages //TODO Use UNIX error messages
if (fp == NULL) { if (fp == NULL) {
printf("[ERROR] Failed to open output \"%s\".\n", output_file); trigger_error(106, true, output_file);
return;
} }
output_buffer = NULL; output_buffer = NULL;

View File

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