1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

simplified info/debug/error messages

git-svn-id: https://zerberus.eonerc.rwth-aachen.de:8443/svn/s2ss/trunk@19 8ec27952-4edc-4aab-86aa-e87bb2611832
This commit is contained in:
Steffen Vogel 2014-06-05 09:34:46 +00:00
parent 084009d9ab
commit e89a128c53
6 changed files with 55 additions and 32 deletions

View file

@ -10,13 +10,14 @@
#include <stdarg.h>
#include <netinet/ip.h>
enum log_level
{
DEBUG,
INFO,
WARN,
ERROR,
FATAL
ERROR
};
/**
@ -26,20 +27,40 @@ enum log_level
*/
void print(enum log_level lvl, const char *fmt, ...);
#define assert(exp) do { \
if (exp); print(FATAL, "Assertion failed: '%s' in %s, %s:%d", \
#exp, __FUNCTION__, __BASE_FILE__, __LINE__); \
} while(0)
/**
* @brief Resolve host/service name by local databases and/or nameservers
*
* @param addr A string containing the hostname/ip and port seperated by a colon
* @param sa A pointer to the resolved address
* @return
* - 0 on success
*/
int resolve(const char *addr, struct sockaddr_in *sa);
#define debug(lvl, ...) do { \
#define assert(exp) do { \
if (!(exp)) { \
print(ERROR, "Assertion failed: '%s' in %s, %s:%d", \
#exp, __FUNCTION__, __BASE_FILE__, __LINE__); \
exit(EXIT_FAILURE); \
} } while (0)
#define debug(lvl, msg, ...) do { \
if (lvl <= V) \
print(DEBUG, __VA_ARGS__); \
print(DEBUG, msg, ##__VA_ARGS__); \
} while (0)
/**
* @brief Print short usage info to stdout
*/
void usage();
#define info(msg, ...) do { \
print(INFO, msg, ##__VA_ARGS__); \
} while (0)
#define warn(msg, ...) do { \
print(WARN, msg, ##__VA_ARGS__); \
} while (0)
#define error(msg, ...) do { \
print(ERROR, msg, ##__VA_ARGS__); \
exit(EXIT_FAILURE); \
} while (0)
#endif /* _UTILS_H_ */

View file

@ -35,33 +35,36 @@ struct node* node_create(const char *name, enum node_type type, const char *loca
n->name = strdup(name);
n->type = type;
resolve(local, &n->local);
resolve(remote, &n->remote);
if (!resolve(local, &n->local))
error("Failed to resolve local address '%s' of node '%s'", local, name);
if (!resolve(remote, &n->remote))
error("Failed to resolve remote address '%s' of node '%s'", remote, name);
/* create and connect socket */
/* Create socket */
n->sd = socket(AF_INET, SOCK_DGRAM, 0);
if (n->sd < 0) {
node_destroy(n);
print(FATAL, "Failed to create socket: %s", strerror(errno));
error("Failed to create socket: %s", strerror(errno));
return NULL;
}
/* Bind socket for receiving */
ret = bind(n->sd, (struct sockaddr *) &n->local, sizeof(struct sockaddr_in));
if (ret < 0) {
node_destroy(n);
print(FATAL, "Failed to bind socket: %s", strerror(errno));
error("Failed to bind socket: %s", strerror(errno));
return NULL;
}
debug(1, "We listen for node %s at %s:%u", name, inet_ntoa(n->local.sin_addr), ntohs(n->local.sin_port));
/* Connect socket for sending */
ret = connect(n->sd, (struct sockaddr *) &n->remote, sizeof(struct sockaddr_in));
if (ret < 0) {
node_destroy(n);
print(FATAL, "Failed to connect socket: %s", strerror(errno));
error("Failed to connect socket: %s", strerror(errno));
return NULL;
}
print(DEBUG, "We listen for node %s at %s:%u", name, inet_ntoa(n->local.sin_addr), ntohs(n->local.sin_port));
print(DEBUG, "We sent to node %s at %s:%u", name, inet_ntoa(n->remote.sin_addr), ntohs(n->remote.sin_port));
debug(1, "We sent to node %s at %s:%u", name, inet_ntoa(n->remote.sin_addr), ntohs(n->remote.sin_port));
return n;
}
@ -117,7 +120,7 @@ void node_destroy(struct node* n)
int node_send(struct node *n, struct msg *m)
{
send(n->sd, m, sizeof(struct msg), 0);
print(DEBUG, "Message sent to node %s", n->name);
debug(1, "Message sent to node %s", n->name);
msg_fprint(stdout, m);
}
@ -125,7 +128,7 @@ int node_recv(struct node *n, struct msg *m)
{
size_t ret = recv(n->sd, m, sizeof(struct msg), 0);
if (ret < 0)
print(ERROR, "Recv failed: %s", strerror(errno));
error("Recv failed: %s", strerror(errno));
print(DEBUG, "Message received from node %s", n->name);
debug(1, "Message received from node %s", n->name);
}

View file

@ -44,7 +44,7 @@ static void * path_run(void *arg)
pfd.fd = p->in->sd;
pfd.events = POLLIN;
print(DEBUG, "Established path: %12s => %s => %-12s", p->in->name, NAME, p->out->name);
debug(1, "Established path: %12s => %s => %-12s", p->in->name, NAME, p->out->name);
/* main thread loop */
while (p->state == RUNNING) {

View file

@ -80,7 +80,7 @@ int main(int argc, char *argv[])
signal(SIGINT, quit);
pause();
print(INFO, "Good night!");
info("Good night!");
return 0;
}

View file

@ -26,7 +26,7 @@ int sd;
void quit()
{
print(INFO, "Goodbye");
debug(1, "Goodbye");
exit(EXIT_SUCCESS);
}
@ -55,11 +55,11 @@ int main(int argc, char *argv[])
dev_id = atoi(argv[1]);
int ret;
print(INFO, "Test node started on %s:%s with id=%u", argv[2], argv[3], dev_id);
info("Test node started on %s:%s with id=%u", argv[2], argv[3], dev_id);
sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd < 0)
print(FATAL, "Failed to create socket: %s", strerror(errno));
error("Failed to create socket: %s", strerror(errno));
struct sockaddr_in sa = {
.sin_family = AF_INET,
@ -77,7 +77,7 @@ int main(int argc, char *argv[])
ret = connect(sd, (struct sockaddr *) &sa, sizeof(struct sockaddr_in));
if (ret < 0)
print(FATAL, "Failed to connect socket: %s", strerror(errno));
error("Failed to connect socket: %s", strerror(errno));
struct sigevent si = {
.sigev_notify = SIGEV_SIGNAL,

View file

@ -16,8 +16,7 @@ static const char *log_prefix[] = {
"Debug",
"Info",
"Warning",
"Error",
"Fatal"
"Error"
};
void print(enum log_level lvl, const char *fmt, ...)