static inline void phpdbg_close_sockets(int (*socket)[2], FILE *streams[2]) /* {{{ */
{
- if ((*socket)[0]) {
- shutdown((*socket)[0], SHUT_RDWR);
+ if ((*socket)[0] >= 0) {
+ shutdown(
+ (*socket)[0], SHUT_RDWR);
close((*socket)[0]);
}
fclose(streams[0]);
}
- if ((*socket)[1]) {
- shutdown((*socket)[1], SHUT_RDWR);
+ if ((*socket)[1] >= 0) {
+ shutdown(
+ (*socket)[1], SHUT_RDWR);
close((*socket)[1]);
}
if ((*listen)[0] < 0 || (*listen)[1] < 0) {
if ((*listen)[0] < 0) {
- fprintf(stderr,
- "Failed to start remote console (stdin) on port %d\n", port[0]);
+ phpdbg_rlog(stderr,
+ "console failed to initialize (stdin) on %s:%d", address, port[0]);
}
if ((*listen)[1] < 0) {
- fprintf(stderr,
- "Failed to open remote console (stdout) on port %d\n", port[1]);
+ phpdbg_rlog(stderr,
+ "console failed to initialize (stdout) on %s:%d", address, port[1]);
}
if ((*listen)[0] >= 0) {
phpdbg_close_sockets(socket, streams);
- fprintf(stderr,
- "Remote console accepting (stdin/stdout) on ports %d/%d\n", port[0], port[1]);
+ phpdbg_rlog(stderr,
+ "accepting connections on %s:%d/%d", address, port[0], port[1]);
{
struct sockaddr_in address;
socklen_t size = sizeof(address);
(*listen)[0], (struct sockaddr *) &address, &size);
inet_ntop(AF_INET, &address.sin_addr, buffer, sizeof(buffer));
- fprintf(stderr,
- "Remote console (stdin) connection from %s\n", buffer);
+ phpdbg_rlog(stderr, "connection (stdin) from %s", buffer);
}
{
(*listen)[1], (struct sockaddr *) &address, &size);
inet_ntop(AF_INET, &address.sin_addr, buffer, sizeof(buffer));
- fprintf(stderr,
- "Remote console (stdout) connection from %s\n", buffer);
+ phpdbg_rlog(stderr, "connection (stdout) from %s", buffer);
}
}
return rc;
} /* }}} */
+PHPDBG_API int phpdbg_rlog(FILE *fp, const char *fmt, ...) { /* {{{ */
+ int rc = 0;
+
+ va_list args;
+ time_t now;
+ struct timeval tp;
+
+ va_start(args, fmt);
+ if (gettimeofday(&tp, NULL) == SUCCESS)
+ {
+ char friendly[100];
+ char *format = NULL, *buffer = NULL;
+
+ strftime(friendly, 100, "%a %b %d %T.%%04d %Y", localtime(&tp.tv_sec));
+ asprintf(
+ &buffer, friendly, tp.tv_usec/1000);
+ asprintf(
+ &format, "[%s]: %s\n", buffer, fmt);
+ rc = vfprintf(
+ fp, format, args);
+
+ free(format);
+ free(buffer);
+ }
+ va_end(args);
+
+ return rc;
+} /* }}} */
+
PHPDBG_API const phpdbg_color_t *phpdbg_get_color(const char *name, size_t name_length TSRMLS_DC) /* {{{ */
{
const phpdbg_color_t *color = colors;
PHPDBG_API char *phpdbg_trim(const char*, size_t, size_t*);
/**
- * Error/notice/formatting helper
+ * Error/notice/formatting helpers
*/
enum {
P_ERROR = 1,
PHPDBG_API int phpdbg_print(int TSRMLS_DC, FILE*, const char*, ...) PHP_ATTRIBUTE_FORMAT(printf, 3, 4);
#endif
+PHPDBG_API int phpdbg_rlog(FILE *stream, const char *fmt, ...);
+
#define phpdbg_error(fmt, ...) phpdbg_print(P_ERROR TSRMLS_CC, PHPDBG_G(io)[PHPDBG_STDOUT], fmt, ##__VA_ARGS__)
#define phpdbg_notice(fmt, ...) phpdbg_print(P_NOTICE TSRMLS_CC, PHPDBG_G(io)[PHPDBG_STDOUT], fmt, ##__VA_ARGS__)
#define phpdbg_writeln(fmt, ...) phpdbg_print(P_WRITELN TSRMLS_CC, PHPDBG_G(io)[PHPDBG_STDOUT], fmt, ##__VA_ARGS__)