r34540 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r34539‎ | r34540 | r34541 >
Date:23:30, 9 May 2008
Author:river
Status:old
Tags:
Comment:
be quiet by default, unless -v is given
Modified paths:
  • /trunk/imgserv/imgserv-client/imgserv.cxx (modified) (history)

Diff [purge]

Index: trunk/imgserv/imgserv-client/imgserv.cxx
@@ -22,6 +22,7 @@
2323 int width, height;
2424 std::string server = "localhost";
2525 int port = 8765;
 26+ int verbose;
2627
2728 void usage();
2829 int safe_write(int s, void *data, size_t n);
@@ -35,7 +36,7 @@
3637 {
3738 int c;
3839 prognam = argv[0];
39 - while ((c = getopt(argc, argv, "i:o:s:p:w:h:")) != -1) {
 40+ while ((c = getopt(argc, argv, "i:o:s:p:w:h:v")) != -1) {
4041 switch (c) {
4142 case 'i':
4243 informat = optarg;
@@ -55,6 +56,9 @@
5657 case 'p':
5758 port = atoi(optarg);
5859 break;
 60+ case 'v':
 61+ verbose++;
 62+ break;
5963 default:
6064 usage();
6165 return 1;
@@ -112,7 +116,7 @@
113117 return 1;
114118 }
115119
116 - int sock, i;
 120+ int sock = -1, i;
117121 struct addrinfo hints, *res, *r;
118122 std::memset(&hints, 0, sizeof(hints));
119123 hints.ai_socktype = SOCK_STREAM;
@@ -138,26 +142,35 @@
139143 else
140144 canon = hostname;
141145
142 - std::cerr << "% Trying " << canon << " (" <<
143 - hostname << ") port " << portname << "... " << std::flush;
 146+ if (verbose)
 147+ std::cerr << "% Trying " << hostname << ":" << portname << "...\n";
144148 if ((sock = socket(r->ai_family, r->ai_socktype, r->ai_protocol)) == -1) {
145 - std::cerr << "failed: " << std::strerror(errno) << '\n';
 149+ std::cerr << "% Connection to " << hostname <<
 150+ ":" << port << " failed: " << std::strerror(errno) << '\n';
 151+ sock = -1;
146152 continue;
147153 }
148154
149155 if (connect(sock, r->ai_addr, r->ai_addrlen) == -1) {
150 - std::cerr << "failed: " << std::strerror(errno) << '\n';
 156+ std::cerr << "% Connection to " << hostname <<
 157+ ":" << port << " failed: " << std::strerror(errno) << '\n';
151158 close(sock);
 159+ sock = -1;
152160 continue;
153161 }
154162
155 - std::cerr << "connected.\n";
 163+ if (verbose)
 164+ std::cerr << "% Connected.\n";
156165 freeaddrinfo(res);
157166 break;
158167 }
159168
160 - std::cerr << "% Writing input file to server... " << std::flush;
 169+ if (sock == -1)
 170+ return 1;
161171
 172+ if (verbose)
 173+ std::cerr << "% Writing input file to server.\n";
 174+
162175 std::ostringstream strm;
163176 strm << "INFORMAT " << informat << "\r\n"
164177 << "OUTFORMAT " << outformat << "\r\n";
@@ -183,7 +196,7 @@
184197 insize -= infile.gcount();
185198
186199 if (insize < 0) {
187 - std::cerr << "input file was too long!\n";
 200+ std::cerr << "% Input file was too long!\n";
188201 return 1;
189202 }
190203
@@ -192,12 +205,14 @@
193206 }
194207
195208 if (insize) {
196 - std::cerr << "input file was too short!\n";
 209+ std::cerr << "% Input file was too short!\n";
197210 return 1;
198211 }
199212
200 - std::cerr << "done, " << wr << " bytes\n";
201 - std::cerr << "% Waiting for reply...";
 213+ if (verbose) {
 214+ std::cerr << "% Done, bytes written = " << wr << '\n';
 215+ std::cerr << "% Waiting for reply...\n";
 216+ }
202217
203218 enum {
204219 READING_STATUS,
@@ -243,12 +258,13 @@
244259 bufpos += len + 2;
245260 buflen -= len + 2;
246261 if (s == "OK") {
247 - std::cout << "ok.\n";
 262+ if (verbose)
 263+ std::cout << "% Server reports: success.\n";
248264 } else {
249265 if (s.substr(0, 5) == "ERROR") {
250 - std::cout << "error: " << s.substr(6) << '\n';
 266+ std::cout << "% Server error: " << s.substr(6) << '\n';
251267 } else {
252 - std::cout << "error: unknown status\n";
 268+ std::cout << "% Server error: unknown status\n";
253269 }
254270 return 1;
255271 }
@@ -258,7 +274,7 @@
259275 continue;
260276 } else {
261277 if (s.size() + buflen > 8192) {
262 - std::cout << "error: header too long\n";
 278+ std::cout << "% Protocol error: header too long\n";
263279 return 1;
264280 }
265281
@@ -312,7 +328,8 @@
313329 }
314330 done:;
315331
316 - std::cerr << "% Wrote " << outsize << " bytes to " << argv[1] << '\n';
 332+ if (verbose)
 333+ std::cerr << "% Wrote " << outsize << " bytes to " << argv[1] << '\n';
317334 }
318335
319336 namespace {
@@ -320,7 +337,7 @@
321338 void
322339 usage()
323340 {
324 - std::cerr << "usage: " << prognam << " [-i <informat>] [-o <outformat>] [-p port] [-s server] [-w width] [-h height] <infile> <outfile>\n";
 341+ std::cerr << "usage: " << prognam << " [-v] [-i <informat>] [-o <outformat>] [-p port] [-s server] [-w width] [-h height] <infile> <outfile>\n";
325342 }
326343
327344 int

Status & tagging log