This patchset indends to make some cleanup and send printf
error messages to stderr.
The changes were performed with coccinelle for failiure
messages and manual for other cases. The following script
was used:
@r@
expression e1, e2, stdout;
@@
(printf(e1);
|
printf(e1, e2);
|
fprintf(stdout, e1);
|
fprintf(stdout, e1, e2);
)
@script: python get_string@
e << r.e1;
tdres;
@@
if 'could not' in e.lower() or 'fail' in e.lower() \
or 'problem' in e.lower() or 'not set' in e.lower():
coccinelle.tdres = e
else:
cocci.include_match(False)
@r_match@
expression r.stdout, r.e1, r.e2;
identifier get_string.tdres;
@@
(
- printf(e1);
+ fprintf(stderr, tdres);
|
- printf(e1, e2);
+ fprintf(stderr, tdres, e2);
|
- fprintf(stdout, e1)
+ fprintf(stderr, tdres)
|
- fprintf(stdout, e1, e2)
+ fprintf(stderr, tdres, e2)
)
Cristina Opriceana (4):
tools: iio: Move printf failiure messages to stderr
tools: iio: Send usage error messages to stderr
tools: iio: generic_buffer: Maintain fprintf() messages consistent
tools: iio: iio_utils: Move general error messages to stderr
tools/iio/generic_buffer.c | 21 ++++++++++--------
tools/iio/iio_event_monitor.c | 8 +++----
tools/iio/iio_utils.c | 51 +++++++++++++++++++++++++------------------
tools/iio/lsiio.c | 2 +-
4 files changed, 47 insertions(+), 35 deletions(-)
--
1.9.1
Replace printf error messages with fprintf(stderr, ...) in order
to ensure consistency and to make faults easier to identify.
This patch uses coccinelle to detect and apply the changes.
Signed-off-by: Cristina Opriceana <[email protected]>
---
tools/iio/generic_buffer.c | 17 ++++++++++-------
tools/iio/iio_event_monitor.c | 6 +++---
tools/iio/iio_utils.c | 34 ++++++++++++++++++++--------------
3 files changed, 33 insertions(+), 24 deletions(-)
diff --git a/tools/iio/generic_buffer.c b/tools/iio/generic_buffer.c
index 0e73723..2f4e12f 100644
--- a/tools/iio/generic_buffer.c
+++ b/tools/iio/generic_buffer.c
@@ -271,7 +271,7 @@ int main(int argc, char **argv)
}
if (device_name == NULL) {
- printf("Device name not set\n");
+ fprintf(stderr, "Device name not set\n");
print_usage();
return -1;
}
@@ -279,7 +279,7 @@ int main(int argc, char **argv)
/* Find the device requested */
dev_num = find_type_by_name(device_name, "iio:device");
if (dev_num < 0) {
- printf("Failed to find the %s\n", device_name);
+ fprintf(stderr, "Failed to find the %s\n", device_name);
return dev_num;
}
@@ -307,7 +307,8 @@ int main(int argc, char **argv)
/* Verify the trigger exists */
trig_num = find_type_by_name(trigger_name, "trigger");
if (trig_num < 0) {
- printf("Failed to find the trigger %s\n", trigger_name);
+ fprintf(stderr, "Failed to find the trigger %s\n",
+ trigger_name);
ret = trig_num;
goto error_free_triggername;
}
@@ -323,7 +324,7 @@ int main(int argc, char **argv)
*/
ret = build_channel_array(dev_dir_name, &channels, &num_channels);
if (ret) {
- printf("Problem reading scan element information\n");
+ fprintf(stderr, "Problem reading scan element information\n");
printf("diag %s\n", dev_dir_name);
goto error_free_triggername;
}
@@ -350,7 +351,8 @@ int main(int argc, char **argv)
dev_dir_name,
trigger_name);
if (ret < 0) {
- printf("Failed to write current_trigger file\n");
+ fprintf(stderr,
+ "Failed to write current_trigger file\n");
goto error_free_buf_dir_name;
}
}
@@ -382,7 +384,7 @@ int main(int argc, char **argv)
fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
if (fp == -1) { /* TODO: If it isn't there make the node */
ret = -errno;
- printf("Failed to open %s\n", buffer_access);
+ fprintf(stderr, "Failed to open %s\n", buffer_access);
goto error_free_buffer_access;
}
@@ -431,7 +433,8 @@ int main(int argc, char **argv)
ret = write_sysfs_string("trigger/current_trigger",
dev_dir_name, "NULL");
if (ret < 0)
- printf("Failed to write to %s\n", dev_dir_name);
+ fprintf(stderr, "Failed to write to %s\n",
+ dev_dir_name);
error_close_buffer_access:
if (close(fp) == -1)
diff --git a/tools/iio/iio_event_monitor.c b/tools/iio/iio_event_monitor.c
index 703f4cb..843bc4c 100644
--- a/tools/iio/iio_event_monitor.c
+++ b/tools/iio/iio_event_monitor.c
@@ -278,14 +278,14 @@ int main(int argc, char **argv)
fd = open(chrdev_name, 0);
if (fd == -1) {
ret = -errno;
- fprintf(stdout, "Failed to open %s\n", chrdev_name);
+ fprintf(stderr, "Failed to open %s\n", chrdev_name);
goto error_free_chrdev_name;
}
ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
if (ret == -1 || event_fd == -1) {
ret = -errno;
- fprintf(stdout, "Failed to retrieve event fd\n");
+ fprintf(stderr, "Failed to retrieve event fd\n");
if (close(fd) == -1)
perror("Failed to close character device file");
@@ -311,7 +311,7 @@ int main(int argc, char **argv)
}
if (ret != sizeof(event)) {
- printf("Reading event failed!\n");
+ fprintf(stderr, "Reading event failed!\n");
ret = -EIO;
break;
}
diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c
index 8fb3214..46dfa3f 100644
--- a/tools/iio/iio_utils.c
+++ b/tools/iio/iio_utils.c
@@ -140,7 +140,8 @@ int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used,
sysfsfp = fopen(filename, "r");
if (sysfsfp == NULL) {
ret = -errno;
- printf("failed to open %s\n", filename);
+ fprintf(stderr, "failed to open %s\n",
+ filename);
goto error_free_filename;
}
@@ -152,7 +153,8 @@ int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used,
&padint, shift);
if (ret < 0) {
ret = -errno;
- printf("failed to pass scan type description\n");
+ fprintf(stderr,
+ "failed to pass scan type description\n");
goto error_close_sysfsfp;
} else if (ret != 5) {
ret = -EIO;
@@ -170,7 +172,8 @@ int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used,
*is_signed = (signchar == 's');
if (fclose(sysfsfp)) {
ret = -errno;
- printf("Failed to close %s\n", filename);
+ fprintf(stderr, "Failed to close %s\n",
+ filename);
goto error_free_filename;
}
@@ -454,7 +457,8 @@ int build_channel_array(const char *device_dir,
sysfsfp = fopen(filename, "r");
if (sysfsfp == NULL) {
ret = -errno;
- printf("failed to open %s\n", filename);
+ fprintf(stderr, "failed to open %s\n",
+ filename);
free(filename);
goto error_cleanup_array;
}
@@ -581,11 +585,13 @@ int find_type_by_name(const char *name, const char *type)
ret = sscanf(ent->d_name + strlen(type), "%d", &number);
if (ret < 0) {
ret = -errno;
- printf("failed to read element number\n");
+ fprintf(stderr,
+ "failed to read element number\n");
goto error_close_dir;
} else if (ret != 1) {
ret = -EIO;
- printf("failed to match element number\n");
+ fprintf(stderr,
+ "failed to match element number\n");
goto error_close_dir;
}
@@ -664,7 +670,7 @@ static int _write_sysfs_int(const char *filename, const char *basedir, int val,
sysfsfp = fopen(temp, "w");
if (sysfsfp == NULL) {
ret = -errno;
- printf("failed to open %s\n", temp);
+ fprintf(stderr, "failed to open %s\n", temp);
goto error_free;
}
@@ -685,7 +691,7 @@ static int _write_sysfs_int(const char *filename, const char *basedir, int val,
sysfsfp = fopen(temp, "r");
if (sysfsfp == NULL) {
ret = -errno;
- printf("failed to open %s\n", temp);
+ fprintf(stderr, "failed to open %s\n", temp);
goto error_free;
}
@@ -750,7 +756,7 @@ static int _write_sysfs_string(const char *filename, const char *basedir,
char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
if (temp == NULL) {
- printf("Memory allocation failed\n");
+ fprintf(stderr, "Memory allocation failed\n");
return -ENOMEM;
}
@@ -761,7 +767,7 @@ static int _write_sysfs_string(const char *filename, const char *basedir,
sysfsfp = fopen(temp, "w");
if (sysfsfp == NULL) {
ret = -errno;
- printf("Could not open %s\n", temp);
+ fprintf(stderr, "Could not open %s\n", temp);
goto error_free;
}
@@ -782,7 +788,7 @@ static int _write_sysfs_string(const char *filename, const char *basedir,
sysfsfp = fopen(temp, "r");
if (sysfsfp == NULL) {
ret = -errno;
- printf("Could not open file to verify\n");
+ fprintf(stderr, "Could not open file to verify\n");
goto error_free;
}
@@ -856,7 +862,7 @@ int read_sysfs_posint(const char *filename, const char *basedir)
char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
if (temp == NULL) {
- printf("Memory allocation failed");
+ fprintf(stderr, "Memory allocation failed");
return -ENOMEM;
}
@@ -903,7 +909,7 @@ int read_sysfs_float(const char *filename, const char *basedir, float *val)
char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
if (temp == NULL) {
- printf("Memory allocation failed");
+ fprintf(stderr, "Memory allocation failed");
return -ENOMEM;
}
@@ -950,7 +956,7 @@ int read_sysfs_string(const char *filename, const char *basedir, char *str)
char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
if (temp == NULL) {
- printf("Memory allocation failed");
+ fprintf(stderr, "Memory allocation failed");
return -ENOMEM;
}
--
1.9.1
Incorrect usage messages should be sent to stderr.
Signed-off-by: Cristina Opriceana <[email protected]>
---
tools/iio/generic_buffer.c | 2 +-
tools/iio/iio_event_monitor.c | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/tools/iio/generic_buffer.c b/tools/iio/generic_buffer.c
index 2f4e12f..af350cc 100644
--- a/tools/iio/generic_buffer.c
+++ b/tools/iio/generic_buffer.c
@@ -193,7 +193,7 @@ void process_scan(char *data,
void print_usage(void)
{
- printf("Usage: generic_buffer [options]...\n"
+ fprintf(stderr, "Usage: generic_buffer [options]...\n"
"Capture, convert and output data from IIO device buffer\n"
" -c <n> Do n conversions\n"
" -e Disable wait for event (new data)\n"
diff --git a/tools/iio/iio_event_monitor.c b/tools/iio/iio_event_monitor.c
index 843bc4c..1c138fc 100644
--- a/tools/iio/iio_event_monitor.c
+++ b/tools/iio/iio_event_monitor.c
@@ -251,7 +251,7 @@ int main(int argc, char **argv)
int fd, event_fd;
if (argc <= 1) {
- printf("Usage: %s <device_name>\n", argv[0]);
+ fprintf(stderr, "Usage: %s <device_name>\n", argv[0]);
return -1;
}
--
1.9.1
Ensure that both of the error messages get to the
same standard stream.
Signed-off-by: Cristina Opriceana <[email protected]>
---
tools/iio/generic_buffer.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/iio/generic_buffer.c b/tools/iio/generic_buffer.c
index af350cc..ac9b954 100644
--- a/tools/iio/generic_buffer.c
+++ b/tools/iio/generic_buffer.c
@@ -325,7 +325,7 @@ int main(int argc, char **argv)
ret = build_channel_array(dev_dir_name, &channels, &num_channels);
if (ret) {
fprintf(stderr, "Problem reading scan element information\n");
- printf("diag %s\n", dev_dir_name);
+ fprintf(stderr, "diag %s\n", dev_dir_name);
goto error_free_triggername;
}
--
1.9.1
This patch replaces stdout general error messages to a more appropriate
standard stream to ensure consistency.
Signed-off-by: Cristina Opriceana <[email protected]>
---
tools/iio/iio_utils.c | 17 ++++++++++-------
tools/iio/lsiio.c | 2 +-
2 files changed, 11 insertions(+), 8 deletions(-)
diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c
index 46dfa3f..8b218bd 100644
--- a/tools/iio/iio_utils.c
+++ b/tools/iio/iio_utils.c
@@ -158,7 +158,8 @@ int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used,
goto error_close_sysfsfp;
} else if (ret != 5) {
ret = -EIO;
- printf("scan type description didn't match\n");
+ fprintf(stderr,
+ "scan type description didn't match\n");
goto error_close_sysfsfp;
}
@@ -572,7 +573,7 @@ int find_type_by_name(const char *name, const char *type)
dp = opendir(iio_dir);
if (dp == NULL) {
- printf("No industrialio devices available\n");
+ fprintf(stderr, "No industrialio devices available\n");
return -ENODEV;
}
@@ -709,8 +710,9 @@ static int _write_sysfs_int(const char *filename, const char *basedir, int val,
}
if (test != val) {
- printf("Possible failure in int write %d to %s/%s\n",
- val, basedir, filename);
+ fprintf(stderr,
+ "Possible failure in int write %d to %s/%s\n",
+ val, basedir, filename);
ret = -1;
}
}
@@ -806,9 +808,10 @@ static int _write_sysfs_string(const char *filename, const char *basedir,
}
if (strcmp(temp, val) != 0) {
- printf("Possible failure in string write of %s "
- "Should be %s written to %s/%s\n", temp, val,
- basedir, filename);
+ fprintf(stderr,
+ "Possible failure in string write of %s "
+ "Should be %s written to %s/%s\n", temp, val,
+ basedir, filename);
ret = -1;
}
}
diff --git a/tools/iio/lsiio.c b/tools/iio/lsiio.c
index 7f432a5..31996de 100644
--- a/tools/iio/lsiio.c
+++ b/tools/iio/lsiio.c
@@ -108,7 +108,7 @@ static int dump_devices(void)
dp = opendir(iio_dir);
if (dp == NULL) {
- printf("No industrial I/O devices available\n");
+ fprintf(stderr, "No industrial I/O devices available\n");
return -ENODEV;
}
--
1.9.1
On Tue, Jul 7, 2015 at 1:46 AM, Cristina Opriceana
<[email protected]> wrote:
> This patchset indends to make some cleanup and send printf
> error messages to stderr.
> The changes were performed with coccinelle for failiure
> messages and manual for other cases. The following script
> was used:
>
> @r@
> expression e1, e2, stdout;
> @@
> (printf(e1);
> |
> printf(e1, e2);
> |
> fprintf(stdout, e1);
> |
> fprintf(stdout, e1, e2);
> )
> @script: python get_string@
> e << r.e1;
> tdres;
> @@
> if 'could not' in e.lower() or 'fail' in e.lower() \
> or 'problem' in e.lower() or 'not set' in e.lower():
> coccinelle.tdres = e
> else:
> cocci.include_match(False)
> @r_match@
> expression r.stdout, r.e1, r.e2;
> identifier get_string.tdres;
> @@
> (
> - printf(e1);
> + fprintf(stderr, tdres);
> |
> - printf(e1, e2);
> + fprintf(stderr, tdres, e2);
> |
> - fprintf(stdout, e1)
> + fprintf(stderr, tdres)
> |
> - fprintf(stdout, e1, e2)
> + fprintf(stderr, tdres, e2)
> )
Nit: s/failiure/failure this appears in several places.
With this fixed:
Reviewed-by: Daniel Baluta <[email protected]>
thanks,
Daniel.