Skip to content

Commit

Permalink
Make error logging configurable and optional (#153)
Browse files Browse the repository at this point in the history
Co-authored-by: Dimitris Tzikas <dimitris.tzikas@ansys.com>
  • Loading branch information
dtzikas and Dimitris Tzikas committed Mar 28, 2023
1 parent ca23716 commit facd76e
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 106 deletions.
2 changes: 1 addition & 1 deletion src/cell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ ErrorCode Cell::write_svg(const char* filename, double scaling, uint32_t precisi

FILE* out = fopen(filename, "w");
if (out == NULL) {
fputs("[GDSTK] Unable to open file for SVG output.\n", stderr);
if (error_logger) fputs("[GDSTK] Unable to open file for SVG output.\n", error_logger);
return ErrorCode::OutputFileOpenError;
}

Expand Down
3 changes: 2 additions & 1 deletion src/clipper_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ static void link_holes(ClipperLib::PolyNode* node, ErrorCode& error_code) {
}

if (p_closest == p_end) {
fprintf(stderr, "[GDSTK] Unable to link hole in boolean operation.\n");
if (error_logger)
fprintf(error_logger, "[GDSTK] Unable to link hole in boolean operation.\n");
error_code = ErrorCode::BooleanError;
} else {
ClipperLib::IntPoint p_new(xnew, hole_min->Y);
Expand Down
22 changes: 15 additions & 7 deletions src/gdsii.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,20 @@ double gdsii_real_to_double(uint64_t real) {

ErrorCode gdsii_read_record(FILE* in, uint8_t* buffer, uint64_t& buffer_count) {
if (buffer_count < 4) {
fputs("[GDSTK] Insufficient memory in buffer.\n", stderr);
if (error_logger) fputs("[GDSTK] Insufficient memory in buffer.\n", error_logger);
return ErrorCode::InsufficientMemory;
}
uint64_t read_length = fread(buffer, 1, 4, in);
if (read_length < 4) {
DEBUG_PRINT("Read bytes (expected 4): %" PRIu64 "\n", read_length);
if (feof(in) != 0) {
fputs("[GDSTK] Unable to read input file. End of file reached unexpectedly.\n", stderr);
if (error_logger)
fputs("[GDSTK] Unable to read input file. End of file reached unexpectedly.\n",
error_logger);
} else {
fprintf(stderr, "[GDSTK] Unable to read input file. Error number %d\n.", ferror(in));
if (error_logger)
fprintf(error_logger, "[GDSTK] Unable to read input file. Error number %d\n.",
ferror(in));
}
buffer_count = read_length;
return ErrorCode::InputFileError;
Expand All @@ -58,15 +62,15 @@ ErrorCode gdsii_read_record(FILE* in, uint8_t* buffer, uint64_t& buffer_count) {
const uint32_t record_length = *((uint16_t*)buffer);
if (record_length < 4) {
DEBUG_PRINT("Record length should be at least 4. Found %" PRIu32 "\n", record_length);
fputs("[GDSTK] Invalid or corrupted GDSII file.\n", stderr);
if (error_logger) fputs("[GDSTK] Invalid or corrupted GDSII file.\n", error_logger);
buffer_count = read_length;
return ErrorCode::InvalidFile;
} else if (record_length == 4) {
buffer_count = read_length;
return ErrorCode::NoError;
}
if (buffer_count < 4 + record_length) {
fputs("[GDSTK] Insufficient memory in buffer.\n", stderr);
if (error_logger) fputs("[GDSTK] Insufficient memory in buffer.\n", error_logger);
buffer_count = read_length;
return ErrorCode::InsufficientMemory;
}
Expand All @@ -76,9 +80,13 @@ ErrorCode gdsii_read_record(FILE* in, uint8_t* buffer, uint64_t& buffer_count) {
DEBUG_PRINT("Read bytes (expected %" PRIu32 "): %" PRIu64 "\n", record_length - 4,
read_length);
if (feof(in) != 0) {
fputs("[GDSTK] Unable to read input file. End of file reached unexpectedly.\n", stderr);
if (error_logger)
fputs("[GDSTK] Unable to read input file. End of file reached unexpectedly.\n",
error_logger);
} else {
fprintf(stderr, "[GDSTK] Unable to read input file. Error number %d\n.", ferror(in));
if (error_logger)
fprintf(error_logger, "[GDSTK] Unable to read input file. Error number %d\n.",
ferror(in));
}
return ErrorCode::InputFileError;
}
Expand Down
2 changes: 1 addition & 1 deletion src/gdswriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ inline GdsWriter gdswriter_init(const char* filename, const char* library_name,

result.out = fopen(filename, "wb");
if (result.out == NULL) {
fputs("[GDSTK] Unable to open GDSII file for output.\n", stderr);
fputs("[GDSTK] Unable to open GDSII file for output.\n", error_logger);
if (error_code) *error_code = ErrorCode::OutputFileOpenError;
return result;
}
Expand Down
115 changes: 69 additions & 46 deletions src/library.cpp

Large diffs are not rendered by default.

21 changes: 12 additions & 9 deletions src/oasis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ ErrorCode oasis_read(void* buffer, size_t size, size_t count, OasisStream& in) {
in.cursor += total;
if (in.cursor >= in.data + in.data_size) {
if (in.cursor > in.data + in.data_size) {
fputs("[GDSTK] Error reading compressed data in file.\n", stderr);
if (error_logger)
fputs("[GDSTK] Error reading compressed data in file.\n", error_logger);
in.error_code = ErrorCode::InputFileError;
}
free_allocation(in.data);
in.data = NULL;
}
} else if (fread(buffer, size, count, in.file) < count) {
fputs("[GDSTK] Error reading OASIS file.\n", stderr);
if (error_logger) fputs("[GDSTK] Error reading OASIS file.\n", error_logger);
in.error_code = ErrorCode::InputFileError;
}
return in.error_code;
Expand All @@ -45,7 +46,7 @@ static uint8_t oasis_peek(OasisStream& in) {
byte = *in.cursor;
} else {
if (fread(&byte, 1, 1, in.file) < 1) {
fputs("[GDSTK] Error reading OASIS file.\n", stderr);
if (error_logger) fputs("[GDSTK] Error reading OASIS file.\n", error_logger);
if (in.error_code == ErrorCode::NoError) in.error_code = ErrorCode::InputFileError;
}
FSEEK64(in.file, -1, SEEK_CUR);
Expand Down Expand Up @@ -116,7 +117,8 @@ uint64_t oasis_read_unsigned_integer(OasisStream& in) {
while (byte & 0x80) {
if (oasis_read(&byte, 1, 1, in) != ErrorCode::NoError) return result;
if (num_bits == 63 && byte > 1) {
fputs("[GDSTK] Integer above maximal limit found. Clipping.\n", stderr);
if (error_logger)
fputs("[GDSTK] Integer above maximal limit found. Clipping.\n", error_logger);
if (in.error_code == ErrorCode::NoError) in.error_code = ErrorCode::Overflow;
return 0xFFFFFFFFFFFFFFFF;
}
Expand Down Expand Up @@ -166,7 +168,8 @@ static uint8_t oasis_read_int_internal(OasisStream& in, uint8_t skip_bits, int64
while (byte & 0x80) {
if (oasis_read(&byte, 1, 1, in) != ErrorCode::NoError) return bits;
if (num_bits > 56 && (byte >> (63 - num_bits)) > 0) {
fputs("[GDSTK] Integer above maximal limit found. Clipping.\n", stderr);
if (error_logger)
fputs("[GDSTK] Integer above maximal limit found. Clipping.\n", error_logger);
if (in.error_code == ErrorCode::NoError) in.error_code = ErrorCode::Overflow;
result = 0x7FFFFFFFFFFFFFFF;
return bits;
Expand Down Expand Up @@ -322,7 +325,7 @@ double oasis_read_real_by_type(OasisStream& in, OasisDataType type) {
return value;
}
default:
fputs("[GDSTK] Unable to determine real value.\n", stderr);
if (error_logger) fputs("[GDSTK] Unable to determine real value.\n", error_logger);
if (in.error_code == ErrorCode::NoError) in.error_code = ErrorCode::InvalidFile;
}
return 0;
Expand Down Expand Up @@ -418,7 +421,7 @@ uint64_t oasis_read_point_list(OasisStream& in, double scaling, bool closed, Arr
result.count += num;
} break;
default:
fputs("[GDSTK] Point list type not supported.\n", stderr);
if (error_logger) fputs("[GDSTK] Point list type not supported.\n", error_logger);
if (in.error_code == ErrorCode::NoError) in.error_code = ErrorCode::InvalidFile;
return 0;
}
Expand Down Expand Up @@ -578,7 +581,7 @@ void oasis_write_2delta(OasisStream& out, int64_t x, int64_t y) {
oasis_write_int_internal(out, x, 2, (uint8_t)OasisDirection::E);
}
} else {
fputs("[GDSTK] Error writing 2-delta.\n", stderr);
if (error_logger) fputs("[GDSTK] Error writing 2-delta.\n", error_logger);
}
}

Expand Down Expand Up @@ -609,7 +612,7 @@ void oasis_write_3delta(OasisStream& out, int64_t x, int64_t y) {
oasis_write_int_internal(out, x, 3, (uint8_t)OasisDirection::SE);
}
} else {
fputs("[GDSTK] Error writing 3-delta.\n", stderr);
if (error_logger) fputs("[GDSTK] Error writing 3-delta.\n", error_logger);
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,10 @@ ErrorCode Polygon::to_gds(FILE* out, double scaling) const {

uint64_t total = point_array.count + 1;
if (total > 8190) {
fputs(
"[GDSTK] Polygons with more than 8190 are not supported by the official GDSII specification. This GDSII file might not be compatible with all readers.\n",
stderr);
if (error_logger)
fputs(
"[GDSTK] Polygons with more than 8190 are not supported by the official GDSII specification. This GDSII file might not be compatible with all readers.\n",
error_logger);
error_code = ErrorCode::UnofficialSpecification;
}
Array<int32_t> coords = {};
Expand Down Expand Up @@ -1598,7 +1599,8 @@ ErrorCode contour(const double* data, uint64_t rows, uint64_t cols, double level
}
}
if (!found) {
fprintf(stderr, "[GDSTK] Unable to process polygon hole in contour.\n");
if (error_logger)
fprintf(error_logger, "[GDSTK] Unable to process polygon hole in contour.\n");
error_code = ErrorCode::BooleanError;
hole->clear();
free_allocation(hole);
Expand Down
7 changes: 4 additions & 3 deletions src/property.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,10 @@ ErrorCode properties_to_gds(const Property* properties, FILE* out) {
if (free_bytes) free_allocation(bytes);
}
if (count > 128) {
fputs(
"[GDSTK] Properties with count larger than 128 bytes are not officially supported by the GDSII specification. This file might not be compatible with all readers.\n",
stderr);
if (error_logger)
fputs(
"[GDSTK] Properties with count larger than 128 bytes are not officially supported by the GDSII specification. This file might not be compatible with all readers.\n",
error_logger);
return ErrorCode::UnofficialSpecification;
}
return ErrorCode::NoError;
Expand Down
11 changes: 7 additions & 4 deletions src/rawcell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ ErrorCode RawCell::to_gds(FILE* out) {
data = (uint8_t*)allocate(size);
int64_t result = source->offset_read(data, size, off);
if (result < 0 || (uint64_t)result != size) {
fputs("[GDSTK] Unable to read RawCell data form input file.\n", stderr);
if (error_logger)
fputs("[GDSTK] Unable to read RawCell data form input file.\n", error_logger);
error_code = ErrorCode::InputFileError;
size = 0;
}
Expand All @@ -98,7 +99,7 @@ Map<RawCell*> read_rawcells(const char* filename, ErrorCode* error_code) {
source->uses = 0;
source->file = fopen(filename, "rb");
if (source->file == NULL) {
fputs("[GDSTK] Unable to open input GDSII file.\n", stderr);
if (error_logger) fputs("[GDSTK] Unable to open input GDSII file.\n", error_logger);
if (error_code) *error_code = ErrorCode::InputFileOpenError;
return result;
}
Expand Down Expand Up @@ -129,7 +130,9 @@ Map<RawCell*> read_rawcells(const char* filename, ErrorCode* error_code) {
}
} else {
dependencies->remove_unordered(i);
fprintf(stderr, "[GDSTK] Referenced cell %s not found.\n", name);
if (error_logger)
fprintf(error_logger, "[GDSTK] Referenced cell %s not found.\n",
name);
if (error_code) *error_code = ErrorCode::MissingReference;
}
free_allocation(name);
Expand Down Expand Up @@ -194,7 +197,7 @@ Map<RawCell*> read_rawcells(const char* filename, ErrorCode* error_code) {
fclose(source->file);
free_allocation(source);
result.clear();
fprintf(stderr, "[GDSTK] Invalid GDSII file %s.\n", filename);
if (error_logger) fprintf(error_logger, "[GDSTK] Invalid GDSII file %s.\n", filename);
if (error_code) *error_code = ErrorCode::InvalidFile;
return result;
}
Expand Down
7 changes: 4 additions & 3 deletions src/reference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,10 @@ ErrorCode Reference::to_gds(FILE* out, double scaling) const {

if (array) {
if (repetition.columns > UINT16_MAX || repetition.rows > UINT16_MAX) {
fputs(
"[GDSTK] Repetition with more than 65535 columns or rows cannot be saved to a GDSII file.\n",
stderr);
if (error_logger)
fputs(
"[GDSTK] Repetition with more than 65535 columns or rows cannot be saved to a GDSII file.\n",
error_logger);
error_code = ErrorCode::InvalidRepetition;
buffer_array[2] = UINT16_MAX;
buffer_array[3] = UINT16_MAX;
Expand Down
36 changes: 20 additions & 16 deletions src/robustpath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1069,10 +1069,11 @@ ErrorCode RobustPath::spine_intersection(const SubPath &sub0, const SubPath &sub
du1 /= norm_v1;
}
}
fprintf(
stderr,
"[GDSTK] No intersection found in RobustPath spine construction around (%lg, %lg) and (%lg, %lg).\n",
p0.x, p0.y, p1.x, p1.y);
if (error_logger)
fprintf(
error_logger,
"[GDSTK] No intersection found in RobustPath spine construction around (%lg, %lg) and (%lg, %lg).\n",
p0.x, p0.y, p1.x, p1.y);
return ErrorCode::IntersectionNotFound;
}

Expand Down Expand Up @@ -1123,10 +1124,11 @@ ErrorCode RobustPath::center_intersection(const SubPath &sub0, const Interpolati
du1 /= norm_v1;
}
}
fprintf(
stderr,
"[GDSTK] No intersection found in RobustPath center construction around (%lg, %lg) and (%lg, %lg).\n",
p0.x, p0.y, p1.x, p1.y);
if (error_logger)
fprintf(
error_logger,
"[GDSTK] No intersection found in RobustPath center construction around (%lg, %lg) and (%lg, %lg).\n",
p0.x, p0.y, p1.x, p1.y);
return ErrorCode::IntersectionNotFound;
}

Expand Down Expand Up @@ -1178,10 +1180,11 @@ ErrorCode RobustPath::left_intersection(const SubPath &sub0, const Interpolation
du1 /= norm_v1;
}
}
fprintf(
stderr,
"[GDSTK] No intersection found in RobustPath left side construction around (%lg, %lg) and (%lg, %lg).\n",
p0.x, p0.y, p1.x, p1.y);
if (error_logger)
fprintf(
error_logger,
"[GDSTK] No intersection found in RobustPath left side construction around (%lg, %lg) and (%lg, %lg).\n",
p0.x, p0.y, p1.x, p1.y);
return ErrorCode::IntersectionNotFound;
}

Expand Down Expand Up @@ -1233,10 +1236,11 @@ ErrorCode RobustPath::right_intersection(const SubPath &sub0, const Interpolatio
du1 /= norm_v1;
}
}
fprintf(
stderr,
"[GDSTK] No intersection found in RobustPath right side construction around (%lg, %lg) and (%lg, %lg).\n",
p0.x, p0.y, p1.x, p1.y);
if (error_logger)
fprintf(
error_logger,
"[GDSTK] No intersection found in RobustPath right side construction around (%lg, %lg) and (%lg, %lg).\n",
p0.x, p0.y, p1.x, p1.y);
return ErrorCode::IntersectionNotFound;
}

Expand Down
10 changes: 7 additions & 3 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ LICENSE file or <http://www.boost.org/LICENSE_1_0.txt>

namespace gdstk {

FILE* error_logger = stderr;

void set_error_logger(FILE* log) { error_logger = log; }

char* copy_string(const char* str, uint64_t* len) {
uint64_t size = 1 + strlen(str);
char* result = (char*)allocate(size);
Expand Down Expand Up @@ -572,10 +576,10 @@ void convex_hull(const Array<Vec2> points, Array<Vec2>& result) {

qhT qh;
QHULL_LIB_CHECK;
qh_zero(&qh, stderr);
qh_zero(&qh, error_logger);
char command[256] = "qhull";
int exitcode = qh_new_qhull(&qh, 2, (int)points.count, (double*)points.items, false, command,
NULL, stderr);
NULL, error_logger);

if (exitcode == 0) {
result.ensure_slots(qh.num_facets);
Expand Down Expand Up @@ -617,7 +621,7 @@ void convex_hull(const Array<Vec2> points, Array<Vec2>& result) {
qh_memfreeshort(&qh, &curlong, &totlong); /* free short memory and memory allocator */
if (curlong || totlong) {
fprintf(
stderr,
error_logger,
"[GDSTK] Qhull internal warning: did not free %d bytes of long memory (%d pieces)\n",
totlong, curlong);
}
Expand Down
19 changes: 11 additions & 8 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ LICENSE file or <http://www.boost.org/LICENSE_1_0.txt>
#define DEBUG_HERE ((void)0)
#define DEBUG_PRINT(...) ((void)0)
#else
#define DEBUG_HERE \
do { \
fprintf(stderr, "%s:%d:%s\n", __FILE__, __LINE__, __func__); \
fflush(stderr); \
#define DEBUG_HERE \
do { \
fprintf(error_logger, "%s:%d:%s\n", __FILE__, __LINE__, __func__); \
fflush(error_logger); \
} while (false)
#define DEBUG_PRINT(...) \
do { \
fprintf(stderr, __VA_ARGS__); \
fflush(stderr); \
#define DEBUG_PRINT(...) \
do { \
fprintf(error_logger, __VA_ARGS__); \
fflush(error_logger); \
} while (false)
#endif

Expand Down Expand Up @@ -228,6 +228,9 @@ inline uint64_t hash(const char* key) {
return result;
}

extern FILE* error_logger;
void set_error_logger(FILE* log);

} // namespace gdstk

#endif

0 comments on commit facd76e

Please sign in to comment.