Decentralized error handling
This commit is contained in:
parent
c4f8d310a0
commit
c4ce35e1ce
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
----------
|
||||
|
||||
|
|
|
@ -250,9 +250,8 @@ void cclt_start(cclt_parameters* pars, off_t* i_t_size, off_t* o_t_size) {
|
|||
//Get output stats
|
||||
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);
|
||||
//TODO This is not critical, but still something to be tracked
|
||||
trigger_error(12, true, output_filename);
|
||||
}
|
||||
o_size = st_buf.st_size;
|
||||
*(o_t_size) += o_size;
|
||||
|
|
138
src/error.c
138
src/error.c
|
@ -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, ...) {
|
||||
|
@ -11,74 +16,103 @@ void trigger_error(int code, bool is_critical, ...) {
|
|||
va_start(args, is_critical);
|
||||
|
||||
fprintf(stderr, "%s %d: ",
|
||||
parse_error_level(is_critical),
|
||||
code);
|
||||
parse_error_level(is_critical),
|
||||
code);
|
||||
|
||||
switch (code) {
|
||||
case 1:
|
||||
fprintf(stderr,
|
||||
case 1:
|
||||
fprintf(stderr,
|
||||
"-l option can't be used with -q. Either use one or the other.");
|
||||
break;
|
||||
case 2:
|
||||
fprintf(stderr,
|
||||
break;
|
||||
case 2:
|
||||
fprintf(stderr,
|
||||
"Either -l or -q must be set.");
|
||||
break;
|
||||
case 3:
|
||||
fprintf(stderr,
|
||||
break;
|
||||
case 3:
|
||||
fprintf(stderr,
|
||||
"Quality must be within a [1-100] range.");
|
||||
break;
|
||||
case 4:
|
||||
fprintf(stderr,
|
||||
break;
|
||||
case 4:
|
||||
fprintf(stderr,
|
||||
"No -o option pointing to the destination folder.");
|
||||
break;
|
||||
case 5:
|
||||
fprintf(stderr,
|
||||
break;
|
||||
case 5:
|
||||
fprintf(stderr,
|
||||
"Failed to create output directory. Permission issue?");
|
||||
break;
|
||||
case 6:
|
||||
vfprintf(stderr,
|
||||
"Option -%c requires an argument.", args);
|
||||
break;
|
||||
case 9:
|
||||
fprintf(stderr,
|
||||
break;
|
||||
case 6:
|
||||
vfprintf(stderr,
|
||||
"Option -%c requires an argument.", args);
|
||||
break;
|
||||
case 9:
|
||||
fprintf(stderr,
|
||||
"No input files.");
|
||||
break;
|
||||
case 11:
|
||||
vfprintf(stderr,
|
||||
"Failed to get input file stats: %s", args);
|
||||
case 20:
|
||||
fprintf(stderr,
|
||||
break;
|
||||
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.");
|
||||
case 100:
|
||||
vfprintf(stderr,
|
||||
"Unknown option `-%c'.", args);
|
||||
break;
|
||||
case 101:
|
||||
vfprintf(stderr,
|
||||
"Unknown option character `\\x%x'.", args);
|
||||
break;
|
||||
case 102:
|
||||
fprintf(stderr,
|
||||
case 100:
|
||||
vfprintf(stderr,
|
||||
"Unknown option `-%c'.", args);
|
||||
break;
|
||||
case 101:
|
||||
vfprintf(stderr,
|
||||
"Unknown option character `\\x%x'.", args);
|
||||
break;
|
||||
case 102:
|
||||
fprintf(stderr,
|
||||
"Parameter expected.");
|
||||
break;
|
||||
case 103:
|
||||
fprintf(stderr,
|
||||
break;
|
||||
case 103:
|
||||
fprintf(stderr,
|
||||
"Folder found, skipping all other inputs.");
|
||||
break;
|
||||
case 104:
|
||||
vfprintf(stderr,
|
||||
"Unknown file type: %s", args);
|
||||
default:
|
||||
//Every unlisted code is critical
|
||||
is_critical = true;
|
||||
fprintf(stderr,
|
||||
break;
|
||||
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;
|
||||
fprintf(stderr,
|
||||
"Cs-137 spreading out. Good luck.");
|
||||
break;
|
||||
break;
|
||||
}
|
||||
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
va_end (args);
|
||||
va_end(args);
|
||||
|
||||
if (is_critical) {
|
||||
exit(EXIT_FAILURE);
|
||||
|
|
12
src/jpeg.c
12
src/jpeg.c
|
@ -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;
|
||||
|
|
10
src/png.c
10
src/png.c
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue