Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use macro for printing to stderr #273

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions include/xlsxwriter/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
#define DEPRECATED(func, msg) func
#endif

#ifndef LXW_PRINT_ERR
#define LXW_PRINT_ERR(...) fprintf(stderr, __VA_ARGS__);
#endif
/** Integer data type to represent a row value. Equivalent to `uint32_t`.
*
* The maximum row in Excel is 1,048,576.
Expand Down Expand Up @@ -224,7 +227,7 @@ enum lxw_custom_property_types {
#define LXW_SCHEMA_CONTENT LXW_SCHEMA_ROOT "/package/2006/content-types"

#define LXW_ERROR(message) \
fprintf(stderr, "[ERROR][%s:%d]: " message "\n", __FILE__, __LINE__)
LXW_PRINT_ERR("[ERROR][%s:%d]: " message "\n", __FILE__, __LINE__)

#define LXW_MEM_ERROR() \
LXW_ERROR("Memory allocation failed.")
Expand Down Expand Up @@ -252,50 +255,50 @@ enum lxw_custom_property_types {
return error;

#define LXW_WARN(message) \
fprintf(stderr, "[WARNING]: " message "\n")
LXW_PRINT_ERR("[WARNING]: " message "\n")

/* We can't use variadic macros here since we support ANSI C. */
#define LXW_WARN_FORMAT(message) \
fprintf(stderr, "[WARNING]: " message "\n")
LXW_PRINT_ERR("[WARNING]: " message "\n")

#define LXW_WARN_FORMAT1(message, var) \
fprintf(stderr, "[WARNING]: " message "\n", var)
LXW_PRINT_ERR("[WARNING]: " message "\n", var)

#define LXW_WARN_FORMAT2(message, var1, var2) \
fprintf(stderr, "[WARNING]: " message "\n", var1, var2)
LXW_PRINT_ERR("[WARNING]: " message "\n", var1, var2)

/* Chart axis type checks. */
#define LXW_WARN_CAT_AXIS_ONLY(function) \
if (!axis->is_category) { \
fprintf(stderr, "[WARNING]: " \
LXW_PRINT_ERR("[WARNING]: " \
function "() is only valid for category axes\n"); \
return; \
}

#define LXW_WARN_VALUE_AXIS_ONLY(function) \
if (!axis->is_value) { \
fprintf(stderr, "[WARNING]: " \
LXW_PRINT_ERR("[WARNING]: " \
function "() is only valid for value axes\n"); \
return; \
}

#define LXW_WARN_DATE_AXIS_ONLY(function) \
if (!axis->is_date) { \
fprintf(stderr, "[WARNING]: " \
LXW_PRINT_ERR("[WARNING]: " \
function "() is only valid for date axes\n"); \
return; \
}

#define LXW_WARN_CAT_AND_DATE_AXIS_ONLY(function) \
if (!axis->is_category && !axis->is_date) { \
fprintf(stderr, "[WARNING]: " \
LXW_PRINT_ERR("[WARNING]: " \
function "() is only valid for category and date axes\n"); \
return; \
}

#define LXW_WARN_VALUE_AND_DATE_AXIS_ONLY(function) \
if (!axis->is_value && !axis->is_date) { \
fprintf(stderr, "[WARNING]: " \
LXW_PRINT_ERR("[WARNING]: " \
function "() is only valid for value and date axes\n"); \
return; \
}
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ CFLAGS += -DUSE_DOUBLE_FUNCTION
endif

# Flags passed to compiler.
CFLAGS += -g -O3 -Wall -Wextra -Wstrict-prototypes -pedantic -ansi
CFLAGS += -g -O3 -Wall -Wextra -Wstrict-prototypes -pedantic -std=c99

# Fix for modified zconf.h on Gentoo.
ifneq (,$(findstring gentoo, $(shell uname -sr)))
Expand Down
16 changes: 8 additions & 8 deletions src/workbook.c
Original file line number Diff line number Diff line change
Expand Up @@ -1893,7 +1893,7 @@ workbook_close(lxw_workbook *self)

/* If the packager fails it is generally due to a zip permission error. */
if (packager == NULL) {
fprintf(stderr, "[ERROR] workbook_close(): "
LXW_PRINT_ERR("[ERROR] workbook_close(): "
"Error creating '%s'. "
"System error = %s\n", self->filename, strerror(errno));

Expand All @@ -1909,48 +1909,48 @@ workbook_close(lxw_workbook *self)

/* Error and non-error conditions fall through to the cleanup code. */
if (error == LXW_ERROR_CREATING_TMPFILE) {
fprintf(stderr, "[ERROR] workbook_close(): "
LXW_PRINT_ERR("[ERROR] workbook_close(): "
"Error creating tmpfile(s) to assemble '%s'. "
"System error = %s\n", self->filename, strerror(errno));
}

/* If LXW_ERROR_ZIP_FILE_OPERATION then errno is set by zip. */
if (error == LXW_ERROR_ZIP_FILE_OPERATION) {
fprintf(stderr, "[ERROR] workbook_close(): "
LXW_PRINT_ERR("[ERROR] workbook_close(): "
"Zip ZIP_ERRNO error while creating xlsx file '%s'. "
"System error = %s\n", self->filename, strerror(errno));
}

/* If LXW_ERROR_ZIP_PARAMETER_ERROR then errno is set by zip. */
if (error == LXW_ERROR_ZIP_PARAMETER_ERROR) {
fprintf(stderr, "[ERROR] workbook_close(): "
LXW_PRINT_ERR("[ERROR] workbook_close(): "
"Zip ZIP_PARAMERROR error while creating xlsx file '%s'. "
"System error = %s\n", self->filename, strerror(errno));
}

/* If LXW_ERROR_ZIP_BAD_ZIP_FILE then errno is set by zip. */
if (error == LXW_ERROR_ZIP_BAD_ZIP_FILE) {
fprintf(stderr, "[ERROR] workbook_close(): "
LXW_PRINT_ERR("[ERROR] workbook_close(): "
"Zip ZIP_BADZIPFILE error while creating xlsx file '%s'. "
"This may require the use_zip64 option for large files. "
"System error = %s\n", self->filename, strerror(errno));
}

/* If LXW_ERROR_ZIP_INTERNAL_ERROR then errno is set by zip. */
if (error == LXW_ERROR_ZIP_INTERNAL_ERROR) {
fprintf(stderr, "[ERROR] workbook_close(): "
LXW_PRINT_ERR("[ERROR] workbook_close(): "
"Zip ZIP_INTERNALERROR error while creating xlsx file '%s'. "
"System error = %s\n", self->filename, strerror(errno));
}

/* The next 2 error conditions don't set errno. */
if (error == LXW_ERROR_ZIP_FILE_ADD) {
fprintf(stderr, "[ERROR] workbook_close(): "
LXW_PRINT_ERR("[ERROR] workbook_close(): "
"Zip error adding file to xlsx file '%s'.\n", self->filename);
}

if (error == LXW_ERROR_ZIP_CLOSE) {
fprintf(stderr, "[ERROR] workbook_close(): "
LXW_PRINT_ERR("[ERROR] workbook_close(): "
"Zip error closing xlsx file '%s'.\n", self->filename);
}

Expand Down