fprintf(stderr,X); \
} else (void)0
+#define ndebug(content) ndebug_(__FILE__, __LINE__, content)
+#define ndebug_(file, line, content) if (debug_enabled) { \
+ std::cerr << file << '(' << line << ')' << ": pid=" << getpid() << ':' \
+ << content << std::endl; \
+ } else (void)0
+
#else /* __GNUC__ */
/* non-GCC compilers can't do the above macro define yet. */
#include "squid.h"
#include "helpers/defines.h"
-#include <cstring>
+#include <iostream>
+#include <string>
/**
* options:
* -d enable debugging.
* -h interface help.
*/
-char *program_name = NULL;
+std::string program_name;
static void
usage(void)
{
- fprintf(stderr,
- "Usage: %s [-d] [-v] [-h]\n"
- " -d enable debugging.\n"
- " -h this message\n\n",
- program_name);
+ std::cerr <<
+ "Usage: " << program_name << " [-d] [-h]" << std::endl <<
+ " -d enable debugging." << std::endl <<
+ " -h this message" << std::endl << std::endl;
}
static void
usage();
exit(0);
default:
- fprintf(stderr, "%s: FATAL: unknown option: -%c. Exiting\n", program_name, opt);
+ std::cerr << program_name << ": FATAL: unknown option: -" <<
+ static_cast<char>(optopt) << ". Exiting" << std::endl;
usage();
exit(1);
}
int
main(int argc, char *argv[])
{
- char buf[HELPER_INPUT_BUFFER];
- int buflen = 0;
-
- setbuf(stdout, NULL);
- setbuf(stderr, NULL);
-
program_name = argv[0];
-
process_options(argc, argv);
- debug("%s " VERSION " " SQUID_BUILD_INFO " starting up...\n", program_name);
-
- while (fgets(buf, HELPER_INPUT_BUFFER, stdin) != NULL) {
- char *p;
-
- if ((p = strchr(buf, '\n')) != NULL) {
- *p = '\0'; /* strip \n */
- buflen = p - buf; /* length is known already */
- } else
- buflen = strlen(buf); /* keep this so we only scan the buffer for \0 once per loop */
-
- debug("Got %d bytes '%s' from Squid\n", buflen, buf);
+ ndebug(program_name << ' ' << VERSION << ' ' << SQUID_BUILD_INFO <<
+ " starting up...");
+ std::string buf;
+ while (getline(std::cin,buf)) { // will return false at EOF
+ ndebug("Got " << buf.length() << " bytes '" << buf << "' from Squid");
/* send 'OK' result back to Squid */
SEND_OK("");
}
- debug("%s " VERSION " " SQUID_BUILD_INFO " shutting down...\n", program_name);
- exit(0);
+ ndebug(program_name << ' ' << VERSION << ' ' << SQUID_BUILD_INFO <<
+ " shutting down...");
+ return 0;
}
#define HELPER_INPUT_BUFFER 8196
/* send OK result to Squid with a string parameter. */
-#define SEND_OK(x) fprintf(stdout, "OK %s\n",x)
+#define SEND_OK(x) std::cout << "OK " << x << std::endl
/* send ERR result to Squid with a string parameter. */
-#define SEND_ERR(x) fprintf(stdout, "ERR %s\n",x)
+#define SEND_ERR(x) std::cout << "ERR " << x << std::endl
/* send ERR result to Squid with a string parameter. */
-#define SEND_BH(x) fprintf(stdout, "BH %s\n",x)
+#define SEND_BH(x) std::cout << "BH " << x << std::endl
/* send TT result to Squid with a string parameter. */
-#define SEND_TT(x) fprintf(stdout, "TT %s\n",x)
+#define SEND_TT(x) std::cout << "TT " << x << std::endl
#endif /* __SQUID_HELPERS_DEFINES_H */