Skip to content

Commit

Permalink
Convert printf_errf in common.h
Browse files Browse the repository at this point in the history
And remove some unused functions.

And improve some error messages.

Signed-off-by: Yuxuan Shui <[email protected]>
  • Loading branch information
yshui committed Dec 20, 2018
1 parent 833eb96 commit 2af0b48
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 65 deletions.
69 changes: 6 additions & 63 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ parse_vsync(session_t *ps, const char *str) {
return true;
}

printf_errf("(\"%s\"): Invalid vsync argument.", str);
log_error("Invalid vsync argument: %s", str);
return false;
}

Expand All @@ -1119,7 +1119,7 @@ parse_backend(session_t *ps, const char *str) {
ps->o.backend = BKEND_XR_GLX_HYBRID;
return true;
}
printf_errf("(\"%s\"): Invalid backend argument.", str);
log_error("Invalid backend argument: %s", str);
return false;
}

Expand Down Expand Up @@ -1154,18 +1154,18 @@ parse_glx_swap_method(session_t *ps, const char *str) {
char *pc = NULL;
int age = strtol(str, &pc, 0);
if (!pc || str == pc) {
printf_errf("(\"%s\"): Invalid number.", str);
log_error("glx-swap-method is an invalid number: %s", str);
return false;
}

for (; *pc; ++pc)
if (!isspace(*pc)) {
printf_errf("(\"%s\"): Trailing characters.", str);
log_error("Trailing characters in glx-swap-method option: %s", str);
return false;
}

if (age > CGLX_MAX_BUFFER_AGE + 1 || age < -1) {
printf_errf("(\"%s\"): Number too large / too small.", str);
log_error("Number for glx-swap-method is too large / too small: %s", str);
return false;
}

Expand Down Expand Up @@ -1495,7 +1495,7 @@ xr_sync(session_t *ps, Drawable d, XSyncFence *pfence) {
assert(!XSyncQueryFence(ps->dpy, *pfence, &triggered) || triggered);
}
else {
printf_errf("(%#010lx): Failed to create X Sync fence.", d);
log_error("Failed to create X Sync fence for %#010lx", d);
}
free_fence(ps, &tmp_fence);
if (*pfence)
Expand Down Expand Up @@ -1561,63 +1561,6 @@ opts_set_no_fading_openclose(session_t *ps, bool newval);
//!@}
#endif

/**
* @brief Dump the given data to a file.
*/
static inline bool
write_binary_data(const char *path, const unsigned char *data, int length) {
if (!data)
return false;
FILE *f = fopen(path, "wb");
if (!f) {
printf_errf("(\"%s\"): Failed to open file for writing.", path);
return false;
}
int wrote_len = fwrite(data, sizeof(unsigned char), length, f);
fclose(f);
if (wrote_len != length) {
printf_errf("(\"%s\"): Failed to write all blocks: %d / %d", path,
wrote_len, length);
return false;
}
return true;
}

/**
* @brief Dump raw bytes in HEX format.
*
* @param data pointer to raw data
* @param len length of data
*/
static inline void
hexdump(const char *data, int len) {
static const int BYTE_PER_LN = 16;

if (len <= 0)
return;

// Print header
printf("%10s:", "Offset");
for (int i = 0; i < BYTE_PER_LN; ++i)
printf(" %2d", i);
putchar('\n');

// Dump content
for (int offset = 0; offset < len; ++offset) {
if (!(offset % BYTE_PER_LN))
printf("0x%08x:", offset);

printf(" %02hhx", data[offset]);

if ((BYTE_PER_LN - 1) == offset % BYTE_PER_LN)
putchar('\n');
}
if (len % BYTE_PER_LN)
putchar('\n');

fflush(stdout);
}

/**
* Set a <code>bool</code> array of all wintypes to true.
*/
Expand Down
25 changes: 23 additions & 2 deletions src/compton.c
Original file line number Diff line number Diff line change
Expand Up @@ -4046,15 +4046,36 @@ session_destroy(session_t *ps) {
log_deinit_tls();
}

/*
#if 0
/**
* @brief Dump the given data to a file.
*/
static inline bool
write_binary_data(const char *path, const unsigned char *data, int length) {
if (!data)
return false;
FILE *f = fopen(path, "wb");
if (!f) {
log_error("Failed to open \"%s\" for writing.", path);
return false;
}
int wrote_len = fwrite(data, sizeof(unsigned char), length, f);
fclose(f);
if (wrote_len != length) {
printf_errf("(\"%s\"): Failed to write all blocks: %d / %d", path,
wrote_len, length);
return false;
}
return true;
}
static inline void
dump_img(session_t *ps) {
int len = 0;
unsigned char *d = glx_take_screenshot(ps, &len);
write_binary_data("/tmp/dump.raw", d, len);
free(d);
}
*/
#endif

/**
* Do the actual work.
Expand Down

0 comments on commit 2af0b48

Please sign in to comment.