emit messages to stderr when write fails
[awesomized/libmemcached] / lib / memcached_verbosity.c
1 #include <memcached.h>
2
3 memcached_return memcached_verbosity(memcached_st *ptr, unsigned int verbosity)
4 {
5 unsigned int x;
6 size_t send_length, sent_length;
7 memcached_return rc;
8 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
9
10 rc= memcached_connect(ptr);
11
12 if (rc != MEMCACHED_SUCCESS)
13 rc= MEMCACHED_SOME_ERRORS;
14
15 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
16 "verbosity %u\r\n", verbosity);
17 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
18 return MEMCACHED_WRITE_FAILURE;
19
20 for (x= 0; x < ptr->number_of_hosts; x++)
21 {
22 memcached_return rc;
23
24 sent_length= write(ptr->hosts[x].fd, buffer, send_length);
25
26 if (sent_length == -1)
27 {
28 fprintf(stderr, "error %s: write: %m\n", __FUNCTION__);
29 return MEMCACHED_WRITE_FAILURE;
30 }
31
32 if (sent_length != send_length)
33 {
34 fprintf(stderr, "error %s: short write %d %d: %m\n",
35 __FUNCTION__, sent_length, send_length);
36 return MEMCACHED_WRITE_FAILURE;
37 }
38
39 rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, x);
40
41 if (rc != MEMCACHED_SUCCESS)
42 rc= MEMCACHED_SOME_ERRORS;
43 }
44
45 return rc;
46 }