Commit 90c9f182 authored by Manuel Stoeckl's avatar Manuel Stoeckl
Browse files

Introduce simple logging

parent 9a6979ca
......@@ -42,7 +42,7 @@ int run_client(const char *socket_path)
{
struct wl_display *display = wl_display_connect(NULL);
if (!display) {
fprintf(stderr, "Failed to connect to a wayland server.\n");
wp_log(WP_ERROR, "Failed to connect to a wayland server.\n");
return EXIT_FAILURE;
}
int displayfd = wl_display_get_fd(display);
......@@ -51,7 +51,8 @@ int run_client(const char *socket_path)
int channelsock;
if (strlen(socket_path) >= sizeof(saddr.sun_path)) {
fprintf(stderr, "Socket path is too long and would be truncated: %s\n",
wp_log(WP_ERROR,
"Socket path is too long and would be truncated: %s\n",
socket_path);
return EXIT_FAILURE;
}
......@@ -60,17 +61,18 @@ int run_client(const char *socket_path)
strncpy(saddr.sun_path, socket_path, sizeof(saddr.sun_path) - 1);
channelsock = socket(AF_UNIX, SOCK_STREAM, 0);
if (channelsock == -1) {
fprintf(stderr, "Error creating socket: %s\n", strerror(errno));
wp_log(WP_ERROR, "Error creating socket: %s\n",
strerror(errno));
return EXIT_FAILURE;
}
if (bind(channelsock, (struct sockaddr *)&saddr, sizeof(saddr)) == -1) {
fprintf(stderr, "Error binding socket: %s\n", strerror(errno));
wp_log(WP_ERROR, "Error binding socket: %s\n", strerror(errno));
close(channelsock);
return EXIT_FAILURE;
}
if (listen(channelsock, 1) == -1) {
fprintf(stderr, "Error listening to socket: %s\n",
wp_log(WP_ERROR, "Error listening to socket: %s\n",
strerror(errno));
close(channelsock);
unlink(socket_path);
......@@ -80,14 +82,14 @@ int run_client(const char *socket_path)
int maxmsg = 4096;
char *buffer = calloc(1, maxmsg + 1);
fprintf(stderr, "I'm a client on %s!\n", socket_path);
wp_log(WP_DEBUG, "I'm a client on %s!\n", socket_path);
for (int i = 0; i < 1; i++) {
// Q: multiple parallel remote client support? then multiplex
// over all accepted clients?
int chanclient = accept(channelsock, NULL, NULL);
if (chanclient == -1) {
fprintf(stderr, "Skipping connection\n");
wp_log(WP_DEBUG, "Skipping connection\n");
continue;
}
......@@ -106,29 +108,21 @@ int run_client(const char *socket_path)
int r = pselect(maxfd + 1, &readfds, NULL, NULL,
&timeout, NULL);
if (r == -1) {
fprintf(stderr, "Select failed, stopping\n");
wp_log(WP_ERROR, "Select failed, stopping\n");
break;
}
fprintf(stderr, "Post select %d %d %d\n", r,
wp_log(WP_DEBUG, "Post select %d %d %d\n", r,
FD_ISSET(chanclient, &readfds),
FD_ISSET(displayfd, &readfds));
if (r == 0) {
const char *msg = "magic";
ssize_t nb = write(chanclient, msg,
strlen(msg) + 1);
if (nb == -1) {
fprintf(stderr, "Write failed, retrying anyway\n");
}
continue;
}
if (FD_ISSET(chanclient, &readfds)) {
fprintf(stderr, "client isset\n");
wp_log(WP_DEBUG, "client isset\n");
struct muxheader header;
if (read(chanclient, &header,
sizeof(struct muxheader)) <
sizeof(struct muxheader)) {
fprintf(stderr, "FD header read failure: %s\n",
wp_log(WP_ERROR,
"FD header read failure: %s\n",
strerror(errno));
break;
}
......@@ -144,39 +138,43 @@ int run_client(const char *socket_path)
nread += nr;
}
if (nread < header.length) {
fprintf(stderr, "FD body read failure %ld/%ld: %s\n",
wp_log(WP_ERROR,
"FD body read failure %ld/%ld: %s\n",
nread, header.length,
strerror(errno));
break;
}
fprintf(stderr, "read bytes: %d = %d\n", nread,
wp_log(WP_DEBUG, "read bytes: %d = %d\n", nread,
header.length);
int wc = iovec_write(displayfd, tmpbuf, nread,
NULL, NULL);
if (wc == -1) {
fprintf(stderr, "FD Write failure %ld: %s\n",
wp_log(WP_ERROR,
"FD Write failure %ld: %s\n",
wc, strerror(errno));
break;
}
free(tmpbuf);
fprintf(stderr, "client done\n");
wp_log(WP_DEBUG, "client done\n");
}
if (FD_ISSET(displayfd, &readfds)) {
fprintf(stderr, "displayfd isset\n");
wp_log(WP_DEBUG, "displayfd isset\n");
int fdbuf[28];
int nfds = 28;
int rc = iovec_read(displayfd, buffer, maxmsg,
fdbuf, &nfds);
if (rc == -1) {
fprintf(stderr, "CS Read failure %ld: %s\n",
wp_log(WP_ERROR,
"CS Read failure %ld: %s\n",
rc, strerror(errno));
break;
}
if (rc > 0) {
if (nfds > 0) {
for (int i = 0; i < nfds; i++) {
fprintf(stderr, "Got FD = %d\n",
wp_log(WP_DEBUG,
"Got FD = %d\n",
fdbuf[i]);
identify_fd(fdbuf[i]);
}
......@@ -188,26 +186,30 @@ int run_client(const char *socket_path)
if (write(chanclient, &header,
sizeof(header)) ==
-1) {
fprintf(stderr, "CS write header failure: %s\n",
wp_log(WP_ERROR,
"CS write header failure: %s\n",
strerror(errno));
break;
}
if (write(chanclient, buffer, rc) ==
-1) {
fprintf(stderr, "CS write body failure: %s\n",
wp_log(WP_ERROR,
"CS write body failure: %s\n",
strerror(errno));
break;
}
} else {
fprintf(stderr, "the display shut down\n");
wp_log(WP_DEBUG,
"the display shut down\n");
break;
}
}
}
fprintf(stderr, "...\n");
close(chanclient);
wp_log(WP_DEBUG, "...\n");
}
fprintf(stderr, "Closing\n");
wp_log(WP_DEBUG, "Closing client\n");
close(displayfd);
close(channelsock);
unlink(socket_path);
......
......@@ -44,8 +44,8 @@
int run_server(const char *socket_path, int app_argc, char *const app_argv[])
{
fprintf(stderr, "I'm a server on %s!\n", socket_path);
fprintf(stderr, "Trying to run %d:", app_argc);
wp_log(WP_DEBUG, "I'm a server on %s!\n", socket_path);
wp_log(WP_DEBUG, "Trying to run %d:", app_argc);
for (int i = 0; i < app_argc; i++) {
fprintf(stderr, " %s", app_argv[i]);
}
......@@ -59,7 +59,7 @@ int run_server(const char *socket_path, int app_argc, char *const app_argv[])
pid_t pid = fork();
if (pid == -1) {
fprintf(stderr, "Fork failed\n");
wp_log(WP_ERROR, "Fork failed\n");
return EXIT_FAILURE;
} else if (pid == 0) {
char bufs2[16];
......@@ -70,13 +70,13 @@ int run_server(const char *socket_path, int app_argc, char *const app_argv[])
setenv("WAYLAND_SOCKET", bufs2, 0);
execv(app_argv[0], app_argv);
fprintf(stderr, "Failed to execv: %s\n", strerror(errno));
wp_log(WP_ERROR, "Failed to execv: %s\n", strerror(errno));
return EXIT_FAILURE;
}
struct wl_display *display = wl_display_create();
if (wl_display_add_socket_fd(display, csockpair[0]) == -1) {
fprintf(stderr, "Failed to add socket to display object\n");
wp_log(WP_ERROR, "Failed to add socket to display object\n");
wl_display_destroy(display);
return EXIT_FAILURE;
}
......@@ -87,7 +87,8 @@ int run_server(const char *socket_path, int app_argc, char *const app_argv[])
int channelfd;
if (strlen(socket_path) >= sizeof(saddr.sun_path)) {
fprintf(stderr, "Socket path is too long and would be truncated: %s\n",
wp_log(WP_ERROR,
"Socket path is too long and would be truncated: %s\n",
socket_path);
return EXIT_FAILURE;
}
......@@ -96,12 +97,13 @@ int run_server(const char *socket_path, int app_argc, char *const app_argv[])
strncpy(saddr.sun_path, socket_path, sizeof(saddr.sun_path) - 1);
channelfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (channelfd == -1) {
fprintf(stderr, "Error creating socket: %s\n", strerror(errno));
wp_log(WP_ERROR, "Error creating socket: %s\n",
strerror(errno));
return EXIT_FAILURE;
}
if (connect(channelfd, (struct sockaddr *)&saddr, sizeof(saddr)) ==
-1) {
fprintf(stderr, "Error connecting socket: %s\n",
wp_log(WP_ERROR, "Error connecting socket: %s\n",
strerror(errno));
close(channelfd);
return EXIT_FAILURE;
......@@ -134,22 +136,22 @@ int run_server(const char *socket_path, int app_argc, char *const app_argv[])
int r = pselect(maxfd + 1, &readfds, NULL, NULL, &timeout,
NULL);
if (r < 0) {
fprintf(stderr, "Error selecting fds: %s\n",
wp_log(WP_ERROR, "Error selecting fds: %s\n",
strerror(errno));
return EXIT_FAILURE;
}
if (r == 0) {
// timeout!
fprintf(stderr, "timeout,?? \n");
wp_log(WP_DEBUG, "timeout,?? \n");
} else {
fprintf(stderr, "%d are set\n", r);
wp_log(WP_DEBUG, "%d are set\n", r);
}
if (FD_ISSET(channelfd, &readfds)) {
fprintf(stderr, "Readfd isset\n");
wp_log(WP_DEBUG, "Readfd isset\n");
struct muxheader header;
if (read(channelfd, &header, sizeof(struct muxheader)) <
sizeof(struct muxheader)) {
fprintf(stderr, "FD header read failure: %s\n",
wp_log(WP_ERROR, "FD header read failure: %s\n",
strerror(errno));
break;
}
......@@ -164,30 +166,31 @@ int run_server(const char *socket_path, int app_argc, char *const app_argv[])
nread += nr;
}
if (nread < header.length) {
fprintf(stderr, "FD body read failure %ld/%ld: %s\n",
wp_log(WP_ERROR,
"FD body read failure %ld/%ld: %s\n",
nread, header.length,
strerror(errno));
break;
}
fprintf(stderr, "Read from conn %d = %d bytes\n", nread,
header.length);
wp_log(WP_DEBUG, "Read from conn %d = %d bytes\n",
nread, header.length);
int wc = iovec_write(client_socket, tmpbuf, nread, NULL,
NULL);
if (wc == -1) {
fprintf(stderr, "FD Write failure %ld: %s\n",
wp_log(WP_ERROR, "FD Write failure %ld: %s\n",
wc, strerror(errno));
break;
}
free(tmpbuf);
}
if (FD_ISSET(client_socket, &readfds)) {
fprintf(stderr, "client socket isset\n");
wp_log(WP_DEBUG, "client socket isset\n");
int rc = iovec_read(client_socket, buffer, maxmsg, NULL,
NULL);
if (rc == -1) {
fprintf(stderr, "CS Read failure %ld: %s\n", rc,
strerror(errno));
wp_log(WP_ERROR, "CS Read failure %ld: %s\n",
rc, strerror(errno));
break;
}
if (rc > 0) {
......@@ -195,17 +198,19 @@ int run_server(const char *socket_path, int app_argc, char *const app_argv[])
.metadata = 0, .length = rc};
if (write(channelfd, &header, sizeof(header)) ==
-1) {
fprintf(stderr, "CS write header failure: %s\n",
wp_log(WP_ERROR,
"CS write header failure: %s\n",
strerror(errno));
break;
}
if (write(channelfd, buffer, rc) == -1) {
fprintf(stderr, "CS write body failure: %s\n",
wp_log(WP_ERROR,
"CS write body failure: %s\n",
strerror(errno));
break;
}
} else {
fprintf(stderr, "the client shut down\n");
wp_log(WP_DEBUG, "the client shut down\n");
break;
}
}
......@@ -218,6 +223,6 @@ int run_server(const char *socket_path, int app_argc, char *const app_argv[])
// todo: scope manipulation, to ensure all cleanups are done
waitpid(pid, &status, 0);
fprintf(stderr, "Program ended\n");
wp_log(WP_DEBUG, "Program ended\n");
return EXIT_SUCCESS;
}
......@@ -6,9 +6,9 @@ root=`pwd`
# program=`which weston-flower`
program=`which demo.py`
($root/waypipe client /tmp/socket-client 2>&1 | sed 's/.*/\x1b[33m&\x1b[0m/') &
($root/waypipe -d client /tmp/socket-client 2>&1 | sed 's/.*/\x1b[33m&\x1b[0m/') &
# ssh-to-self; should have a local keypair set up
(ssh -R/tmp/socket-server:/tmp/socket-client localhost $root/waypipe server /tmp/socket-server -- $program) 2>&1 | sed 's/.*/\x1b[34m&\x1b[0m/'
(ssh -R/tmp/socket-server:/tmp/socket-client localhost $root/waypipe server -d /tmp/socket-server -- $program) 2>&1 | sed 's/.*/\x1b[34m&\x1b[0m/'
kill %1
rm -f /tmp/socket-client
rm -f /tmp/socket-server
......@@ -23,6 +23,10 @@
* SOFTWARE.
*/
#define _XOPEN_SOURCE 700
#include "util.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
......@@ -33,8 +37,21 @@
#include <sys/time.h>
#include <sys/types.h>
#include <sys/un.h>
#include <time.h>
#include <unistd.h>
log_cat_t wp_loglevel = WP_ERROR;
const char *static_timestamp(void)
{
static char msg[64];
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
double time = (ts.tv_sec % 100) * 1. + ts.tv_nsec * 1e-9;
sprintf(msg, "%9.6f", time);
return msg;
}
int iovec_read(int conn, char *buf, size_t buflen, int *fds, int *numfds)
{
char cmsgdata[(CMSG_LEN(28 * sizeof(int32_t)))];
......@@ -102,10 +119,10 @@ void identify_fd(int fd)
memset(&fsdata, 0, sizeof(fsdata));
int ret = fstat(fd, &fsdata);
if (ret == -1) {
fprintf(stderr, "Failed to identify %d as a file: %s\n", fd,
wp_log(WP_ERROR, "Failed to identify %d as a file: %s\n", fd,
strerror(errno));
} else {
fprintf(stderr, "The filedesc %d is a file, of size %d!\n", fd,
wp_log(WP_DEBUG, "The filedesc %d is a file, of size %d!\n", fd,
fsdata.st_size);
// then we can open the file, read the contents, create a mirror
// file, make diffs, and transfer them out of band!
......@@ -114,7 +131,7 @@ void identify_fd(int fd)
char *data = mmap(NULL, fsdata.st_size, PROT_READ, MAP_SHARED,
fd, 0);
if (!data) {
fprintf(stderr, "Mmap failed!\n");
wp_log(WP_ERROR, "Mmap failed!\n");
}
munmap(data, fsdata.st_size);
......
......@@ -34,6 +34,20 @@ int chan_write(int conn, char *buf, size_t buflen);
void identify_fd(int fd);
typedef enum { WP_DEBUG = 1, WP_ERROR = 2 } log_cat_t;
// void wp_log(log_cat_t cat, );
extern log_cat_t wp_loglevel;
// mutates a static local, hence can only be called singlethreaded
const char *static_timestamp(void);
// no trailing ;, user must supply
#define wp_log(level, fmt, ...) \
if ((level) >= wp_loglevel) \
fprintf(stderr, "%s [%s:%3d] " fmt, static_timestamp(), __FILE__, \
__LINE__, ##__VA_ARGS__)
struct muxheader {
int metadata;
int length;
......
......@@ -23,6 +23,8 @@
* SOFTWARE.
*/
#include "util.h"
#include <getopt.h>
#include <stdarg.h>
#include <stdbool.h>
......@@ -45,6 +47,7 @@ static int usage(int retcode)
"wayland compositor, run as server on the side with the wayland client\n"
"and link the sockets with ssh or some other transport.\n\n");
fprintf(ostream, "options:\n");
fprintf(stderr, " -d, --debug Print debug messages.\n");
fprintf(stderr, " -h, --help Display this help and exit.\n");
fprintf(stderr, " -v, --version Print waypipe version.\n");
return retcode;
......@@ -57,15 +60,17 @@ int main(int argc, char **argv)
bool help = false;
bool version = false;
bool fail = false;
bool debug = false;
bool is_client;
const char *socketpath;
int opt;
static const struct option options[] = {
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'}, {0, 0, NULL, 0}};
{"version", no_argument, NULL, 'v'},
{"debug", no_argument, NULL, 'd'}, {0, 0, NULL, 0}};
while (1) {
opt = getopt_long(argc, argv, "hvcs", options, NULL);
opt = getopt_long(argc, argv, "hvd", options, NULL);
if (opt == -1)
break;
......@@ -77,6 +82,9 @@ int main(int argc, char **argv)
case 'v':
version = true;
break;
case 'd':
debug = true;
break;
default:
fail = true;
break;
......@@ -111,6 +119,8 @@ int main(int argc, char **argv)
argv++;
argc--;
}
wp_loglevel = debug ? WP_DEBUG : WP_ERROR;
if (is_client) {
return run_client(socketpath);
} else {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment