Use a wrapper for logging

This commit is contained in:
Igor Socec 2016-11-28 02:01:39 +01:00
parent 8725d1c1f1
commit 7973b5a5e8
7 changed files with 91 additions and 99 deletions

View file

@ -13,8 +13,7 @@ void client_close(client_t *client)
client->socket = -1;
time2string(time(NULL), timestamp);
fprintf(stderr,"[%s] socket closed for client %s @ %s\n", __func__,
client->ip_string, timestamp);
LOG("socket closed for client %s @ %s", client->ip_string, timestamp);
}
int client_read(client_t *client)
@ -25,15 +24,13 @@ int client_read(client_t *client)
len = recv(client->socket, client->data, sizeof(client->data)-1, 0);
if (len == -1)
{
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__,
errno, strerror(errno));
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
return -errno;
}
/* a disconnected client socket is ready for reading but read returns 0 */
if (len == 0)
{
fprintf(stderr, "[%s:%d] no data available from client\n",
__func__, __LINE__);
LOG("[@%d] no data available from client", __LINE__);
return -ENODATA;
}
@ -43,10 +40,10 @@ int client_read(client_t *client)
int i;
for(i = 0; i < len; i++)
{
fprintf(stderr, "client %s <- %u '%c'\n",
client->ip_string,
(unsigned char) client->data[i],
(unsigned char) client->data[i]);
LOG("client %s <- %u '%c'",
client->ip_string,
(unsigned char) client->data[i],
(unsigned char) client->data[i]);
}
}
@ -72,10 +69,10 @@ int client_write(client_t *client, char *databuf, int datalen)
int i;
for(i = 0; i < datalen; i++)
{
fprintf(stderr, "client %s -> %u '%c'\n",
client->ip_string,
(unsigned char) databuf[i],
(unsigned char) databuf[i]);
LOG("client %s -> %u '%c'",
client->ip_string,
(unsigned char) databuf[i],
(unsigned char) databuf[i]);
}
}
@ -83,8 +80,7 @@ int client_write(client_t *client, char *databuf, int datalen)
len = send(client->socket, databuf, datalen, 0);
if (len == -1)
{
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__,
errno, strerror(errno));
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
return -errno;
}

View file

@ -4,6 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
@ -12,12 +13,25 @@
/* ========================================================================== */
#define APPNAME "moxerver"
#define BUFFER_LEN 128 /* length of a data buffer */
/* ========================================================================== */
int debug_messages; /* if > 0 debug messages will be printed */
/**
* Wrapper for printing a log message to stderr.
* Uses a "printf" syntax with format and arguments. The newline character '\n'
* is appended to the message.
*/
#define LOG(...) \
do { \
fprintf(stderr, "[%s][%s] ", APPNAME, __func__); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
} while(0)
/* ========================================================================== */
#define TIMESTAMP_FORMAT "%Y-%m-%dT%H:%M:%S" /* follow ISO 8601 format */

View file

@ -9,8 +9,6 @@
#include <parser.h>
#include <signal.h> /* handling quit signals */
#define NAME "moxerver"
#define CONFILE "moxanix.cfg"
/* ========================================================================== */
@ -27,7 +25,7 @@ tty_t tty_dev; /* connected tty device */
static void usage()
{
//TODO maybe some styling should be done
fprintf(stdout, "Usage: %s -p tcp_port [-t tty_path] [-d] [-h]\n", NAME);
fprintf(stdout, "Usage: %s -p tcp_port [-t tty_path] [-d] [-h]\n", APPNAME);
fprintf(stdout, "\t-t\ttty dev path (if not specified %s needs to be defined)\n", CONFILE);
fprintf(stdout, "\t-d\tturns on debug messages\n");
fprintf(stdout, "\n");
@ -36,7 +34,7 @@ static void usage()
/* Performs resource cleanup. */
void cleanup()
{
fprintf(stderr, "[%s] performing cleanup\n", NAME);
LOG("performing cleanup");
// TODO: maybe pthread_kill() should be used for thread cleanup?
@ -58,7 +56,7 @@ void cleanup()
void quit_handler(int signum)
{
/* perform cleanup and exit with 0 */
fprintf(stderr, "[%s] received signal %d\n", NAME, signum);
LOG("received signal %d", signum);
exit(0);
}
@ -69,12 +67,12 @@ int parse_handler(void *user, const char *section, const char *name, const char
if (!strcmp(name, "speed") && (unsigned int)atoi(section) == server.port)
{
fprintf(stderr, "[%s] setting %s speed for port %s\n", __func__, value, section);
LOG("setting %s speed for port %s", value, section);
if (cfsetispeed(&(tty_dev.ttyset), baud_to_speed(atoi(value))) < 0 ||
cfsetospeed(&(tty_dev.ttyset), baud_to_speed(atoi(value))) < 0)
{
fprintf(stderr, "[%s] error configuring tty device speed\n", NAME);
LOG("error configuring tty device speed");
return -1;
}
}
@ -104,7 +102,7 @@ int main(int argc, char *argv[])
/* zero init tty_dev */
if (cfsetispeed(&(tty_dev.ttyset), B0) < 0 ||
cfsetospeed(&(tty_dev.ttyset), B0) < 0) {
fprintf(stderr, "[%s] error configuring tty device speed\n", NAME);
LOG("error configuring tty device speed");
return -1;
}
@ -130,7 +128,7 @@ int main(int argc, char *argv[])
case 't':
if ((strnlen(optarg, TTY_DEV_PATH_LEN) == 0) ||
(strnlen(optarg, TTY_DEV_PATH_LEN) > (TTY_DEV_PATH_LEN - 1))) {
fprintf(stderr, "[%s] error: tty path was not specified\n\n", NAME);
LOG("error: tty path was not specified\n");
usage();
return -1;
} else {
@ -148,7 +146,7 @@ int main(int argc, char *argv[])
usage();
return 0;
default:
fprintf(stderr, "[%s] error parsing arguments\n", NAME);
LOG("error parsing arguments");
usage();
return -1;
}
@ -162,36 +160,35 @@ int main(int argc, char *argv[])
/* parse config file if any */
if (!def_conf && ((ret = ini_parse(CONFILE, &parse_handler, NULL)) == -1)) {
fprintf(stderr, "[%s] error opening config file %s\n", NAME, CONFILE);
LOG("error opening config file %s", CONFILE);
usage();
return -1;
}
else if (!def_conf && ret) {
fprintf(stderr, "[%s] error parsing config file %s on line %d\n", NAME, CONFILE, ret);
LOG("error parsing config file %s on line %d", CONFILE, ret);
return -1;
}
if (!strcmp(tty_dev.path, "")) {
fprintf(stderr, "[%s] error: no tty device path given for TCP port: %d\n"
"\t\t-> check config file %s\n", NAME, tcp_port, CONFILE);
LOG("error: no tty device path given for TCP port: %d\n"
"\t\t-> check config file %s", tcp_port, CONFILE);
return -1;
}
/* open tty device */
if (tty_open(&tty_dev) < 0) {
fprintf(stderr, "[%s] error: opening of tty device at %s failed\n"
"\t\t-> continuing in echo mode\n", NAME, tty_dev.path);
LOG("error: opening of tty device at %s failed\n"
"\t\t-> continuing in echo mode", tty_dev.path);
debug_messages = 1;
}
fprintf(stderr, "[%s] TCP port: %d, TTY device path: %s\n", NAME, tcp_port, tty_dev.path);
LOG("TCP port: %d, TTY device path: %s", tcp_port, tty_dev.path);
/* start thread function that handles tty device */
resources_t r = {&server, &client, &new_client, &tty_dev};
ret = pthread_create(&tty_thread, NULL, thread_tty_data, &r);
if (ret) {
fprintf(stderr, "[%s] error starting serial monitor thread"
", pthread_create returned %d\n", NAME, ret);
LOG("error starting serial monitor thread, pthread_create returned %d", ret);
return -1;
}
@ -199,7 +196,7 @@ int main(int argc, char *argv[])
thread_client_data(&r);
/* unexpected break from client data loop, cleanup and exit with -1 */
fprintf(stderr, "[%s] unexpected condition\n", NAME);
LOG("unexpected condition");
cleanup();
return -1;
}

View file

@ -16,52 +16,47 @@ int server_setup(server_t *server, unsigned int port)
server->socket = socket(AF_INET, SOCK_STREAM, 0);
if (server->socket == -1)
{
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__,
errno, strerror(errno));
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
return -errno;
}
fprintf(stderr,"[%s] socket created\n", __func__);
LOG("socket created");
/* try to avoid "Address already in use" error */
opt = 1; /* true value for setsockopt option */
if (setsockopt(server->socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)) == -1)
{
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__,
errno, strerror(errno));
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
return -errno;
}
/* turn off Nagle algorithm */
opt = 1; /* true value for setsockopt option */
if (setsockopt(server->socket, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(int)) == -1)
{
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__,
errno, strerror(errno));
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
return -errno;
}
/* bind server address to a socket */
if (bind(server->socket, (struct sockaddr *) &server->address, sizeof(server->address)) == -1)
{
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__,
errno, strerror(errno));
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
return -errno;
}
fprintf(stderr,"[%s] bind successful\n", __func__);
LOG("bind successful");
/* save server port number */
server->port = port;
fprintf(stderr,"[%s] assigned port %u\n", __func__, server->port); // ntohs(server->address.sin_port)
LOG("assigned port %u", server->port); // ntohs(server->address.sin_port)
/* listen for a client connection, allow (max-1) connections in queue */
if (listen(server->socket, (SERVER_MAX_CONNECTIONS - 1)) == -1)
{
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__,
errno, strerror(errno));
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
return -errno;
}
time2string(time(NULL), timestamp);
fprintf(stderr,"[%s] server is up @ %s\n", __func__, timestamp);
LOG("server is up @ %s", timestamp);
return 0;
}
@ -76,7 +71,7 @@ void server_close(server_t *server)
}
time2string(time(NULL), timestamp);
fprintf(stderr,"[%s] socket closed, server is down @ %s\n", __func__, timestamp);
LOG("socket closed, server is down @ %s", timestamp);
}
int server_accept(server_t *server, client_t *accepted_client)
@ -89,13 +84,13 @@ int server_accept(server_t *server, client_t *accepted_client)
accepted_client->socket = accept(server->socket, (struct sockaddr *) &accepted_client->address, (socklen_t *) &namelen);
if (accepted_client->socket == -1)
{
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__, errno, strerror(errno));
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
return -errno;
}
/* make the client socket non-blocking */
if (fcntl(accepted_client->socket, F_SETFL, O_NONBLOCK) == -1)
{
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__, errno, strerror(errno));
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
return -errno;
}
@ -109,7 +104,6 @@ int server_accept(server_t *server, client_t *accepted_client)
/* print client information */
time2string(accepted_client->last_active, timestamp);
fprintf(stderr, "[%s] accepted client %s @ %s\n",
__func__, accepted_client->ip_string, timestamp);
LOG("accepted client %s @ %s", accepted_client->ip_string, timestamp);
return 0;
}

View file

@ -1,8 +1,6 @@
#include <task_threads.h>
#include <telnet.h>
#define NAME "moxerver"
void* thread_new_client_connection(void *args)
{
client_t temp_client;
@ -27,8 +25,7 @@ void* thread_new_client_connection(void *args)
client_close(&temp_client);
time2string(time(NULL), timestamp);
fprintf(stderr, "[%s] rejected new client request %s @ %s\n", __func__,
temp_client.ip_string, timestamp);
LOG("rejected new client request %s @ %s", temp_client.ip_string, timestamp);
return (void *) 0;
}
@ -72,8 +69,7 @@ void* thread_new_client_connection(void *args)
* connect this new client */
client_close(r->client);
fprintf(stderr, "[%s] dropped client %s @ %s\n", __func__,
r->client->ip_string, timestamp);
LOG("dropped client %s @ %s", r->client->ip_string, timestamp);
}
else
{
@ -81,8 +77,7 @@ void* thread_new_client_connection(void *args)
client_close(r->new_client);
time2string(time(NULL), timestamp);
fprintf(stderr, "[%s] rejected new client request %s @ %s\n", __func__,
temp_client.ip_string, timestamp);
LOG("rejected new client request %s @ %s", temp_client.ip_string, timestamp);
return (void *) 1;
}
@ -101,8 +96,7 @@ void* thread_tty_data(void *args)
/* get resources from args */
resources_t *r = (resources_t*) args;
fprintf(stderr, "[%s] tty thread started with device: %s\n",
NAME, r->tty_dev->path);
LOG("tty thread started with device: %s", r->tty_dev->path);
/* loop with timeouts waiting for data from the tty device */
while (1)
@ -125,11 +119,11 @@ void* thread_tty_data(void *args)
if (debug_messages)
{
fprintf(stderr, "[%s] tty thread alive\n", NAME);
LOG("tty thread alive");
}
} /* end main while() loop */
fprintf(stderr, "[%s] tty thread stopped\n", NAME);
LOG("tty thread stopped");
return (void*) 0;
}
@ -154,7 +148,7 @@ void* thread_client_data(void *args)
/* copy new client information */
memcpy(r->client, r->new_client, sizeof(client_t));
r->new_client->socket = -1;
fprintf(stderr, "[%s] client %s connected\n", NAME, r->client->ip_string);
LOG("client %s connected", r->client->ip_string);
/* ask client to provide a username before going to "character" mode */
if (client_ask_username(r->client) != 0)
{
@ -187,7 +181,7 @@ void* thread_client_data(void *args)
if (ret == -1)
{
//TODO do we really break here and stop server when select returns an error?
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__, errno, strerror(errno));
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
break;
}
/* handle incoming data on server and client sockets */
@ -196,12 +190,12 @@ void* thread_client_data(void *args)
/* check for new connection requests */
if (FD_ISSET(r->server->socket, &read_fds))
{
fprintf(stderr, "[%s] received client connection request\n", NAME);
LOG("received client connection request");
/* handle new client connection request in a separate thread */
if (pthread_create(&new_client_thread, NULL, thread_new_client_connection, r) != 0)
{
/* print error but continue waiting for connection requests */
fprintf(stderr, "[%s] problem with handling client connection request\n", NAME);
LOG("problem with handling client connection request");
continue;
}
}
@ -213,7 +207,7 @@ void* thread_client_data(void *args)
/* check if client disconnected */
if (ret == -ENODATA)
{
fprintf(stderr, "[%s] client %s disconnected\n", NAME, r->client->ip_string);
LOG("client %s disconnected", r->client->ip_string);
/* close client connection and continue waiting for new clients */
client_close(r->client);
continue;
@ -222,7 +216,7 @@ void* thread_client_data(void *args)
if (ret < 0)
{
/* print error but continue waiting for new data */
fprintf(stderr, "[%s] problem reading client\n", NAME);
LOG("problem reading from client, but will continue");
continue;
}
/* otherwise, pass received client data to the tty device */
@ -245,8 +239,8 @@ void* thread_client_data(void *args)
time_t current_time = time(NULL);
if (debug_messages)
{
fprintf(stderr, "[%s] client last active %u seconds ago\n",
NAME, (unsigned int) (current_time - r->client->last_active));
LOG("client last active %u seconds ago",
(unsigned int) (current_time - r->client->last_active));
}
}
/* do something while listening for client connections? */
@ -254,7 +248,7 @@ void* thread_client_data(void *args)
{
if (debug_messages)
{
fprintf(stderr, "[%s] listening for client connection\n", NAME);
LOG("listening for client connection");
}
}
}

View file

@ -59,9 +59,9 @@ static void telnet_handle_command(char *databuf, int datalen)
* we set the client, we don't adapt to client commands */
if (databuf[0] == telnet_option_value("IAC"))
{
fprintf(stderr, "[%s] received %s %s\n", __func__,
telnet_option_name(databuf[1]),
telnet_option_name(databuf[2]));
LOG("received %s %s",
telnet_option_name(databuf[1]),
telnet_option_name(databuf[2]));
}
}
@ -127,14 +127,14 @@ void telnet_filter_client_write(char *databuf, int *datalen)
/* pressed ENTER */
if (databuf[i] == 13)
{
fprintf(stderr, "[%s] handling ENTER\n", __func__);
LOG("handling ENTER");
newdata[newlen++] = '\r';
newdata[newlen++] = '\n';
}
/* pressed BACKSPACE */
if (databuf[i] == 127)
{
fprintf(stderr, "[%s] handling BACKSPACE\n", __func__);
LOG("handling BACKSPACE");
newdata[newlen++] = 8;
newdata[newlen++] = ' ';
newdata[newlen++] = 8;

33
tty.c
View file

@ -1,6 +1,5 @@
#include <tty.h>
#define NAME "tty"
#define TTY_DEFAULT_BAUDRATE B115200
int tty_open(tty_t *tty_dev)
@ -16,8 +15,8 @@ int tty_open(tty_t *tty_dev)
/* store default termios settings */
if (tcgetattr(tty_dev->fd, &(tty_dev->ttysetold)))
{
fprintf(stderr, "[%s] error reading device default config\n"
"\t\t-> default config will not be restored upon exit", __func__);
LOG("[@%d] error reading device default config\n"
"\t\t-> default config will not be restored upon exit", __LINE__);
}
/* set tty device parameters */
@ -35,20 +34,20 @@ int tty_open(tty_t *tty_dev)
if (cfgetispeed(&(tty_dev->ttyset)) == baud_to_speed(0) &&
cfsetispeed(&(tty_dev->ttyset), TTY_DEFAULT_BAUDRATE) < 0)
{
fprintf(stderr, "[%s] error configuring tty device speed\n", __func__);
LOG("error configuring tty device speed");
return -errno;
}
if (cfgetospeed(&(tty_dev->ttyset)) == baud_to_speed(0) &&
cfsetospeed(&(tty_dev->ttyset), TTY_DEFAULT_BAUDRATE) < 0)
{
fprintf(stderr, "[%s] error configuring tty device speed\n", __func__);
LOG("error configuring tty device speed");
return -errno;
}
/* apply tty device settings */
if (tcsetattr(tty_dev->fd, TCSANOW, &(tty_dev->ttyset)) < 0)
{
fprintf(stderr, "[%s] error configuring tty device\n", __func__);
LOG("error configuring tty device");
return -errno;
}
@ -60,11 +59,11 @@ int tty_close(tty_t *tty_dev)
int fd = tty_dev->fd;
tty_dev->fd = -1;
fprintf(stderr, "[%s] closing tty device \n", __func__);
LOG("closing tty device");
if (tcsetattr(fd, TCSANOW, &(tty_dev->ttysetold)) < 0)
{
fprintf(stderr, "[%s] error restoring tty device default config\n", __func__);
LOG("[@%d] error restoring tty device default config", __LINE__);
return -errno;
}
@ -83,8 +82,7 @@ int tty_read(tty_t *tty_dev)
len = read(tty_dev->fd, tty_dev->data, BUFFER_LEN);
if (len == -1)
{
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__,
errno, strerror(errno));
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
return -errno;
}
@ -94,9 +92,9 @@ int tty_read(tty_t *tty_dev)
int i;
for(i = 0; i < len; i++)
{
fprintf(stderr, "tty <- %u '%c'\n",
(unsigned char) tty_dev->data[i],
(unsigned char) tty_dev->data[i]);
LOG("tty <- %u '%c'",
(unsigned char) tty_dev->data[i],
(unsigned char) tty_dev->data[i]);
}
}
@ -110,8 +108,7 @@ int tty_write(tty_t *tty_dev, char *databuf, int datalen)
len = write(tty_dev->fd, databuf, datalen);
if (len == -1)
{
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__,
errno, strerror(errno));
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
return -errno;
}
@ -121,9 +118,9 @@ int tty_write(tty_t *tty_dev, char *databuf, int datalen)
int i;
for(i = 0; i < datalen; i++)
{
fprintf(stderr, "tty -> %u '%c'\n",
(unsigned char) databuf[i],
(unsigned char) databuf[i]);
LOG("tty -> %u '%c'",
(unsigned char) databuf[i],
(unsigned char) databuf[i]);
}
}