[Aggregator] Use verbose levels

Get rid of #ifdef DEBUG.
This commit is contained in:
Matteo Cypriani 2011-07-23 22:51:02 +02:00
parent 6536805578
commit d6f337dbf2
2 changed files with 99 additions and 99 deletions

View File

@ -7,8 +7,6 @@
#include <owlps.h>
#define DEBUG
/* Arguments & program configuration */
#define OPTIONS "Aa:c:C:f:hi:k:K:l:o:p:t:vV" // getopt string
@ -21,6 +19,14 @@
#define POSITIONER_DEFAULT_IP "127.0.0.1"
/* Verbosity levels */
#define VERBOSE_QUIET cfg_getint(cfg, "verbose") == 0
#define VERBOSE_WARNING cfg_getint(cfg, "verbose") >= 1
#define VERBOSE_INFO cfg_getint(cfg, "verbose") >= 2
#define VERBOSE_CHATTERBOX cfg_getint(cfg, "verbose") >= 3
#define VERBOSE_REQUESTS cfg_getint(cfg, "verbose") >= 4
/* Error codes */
#define ERR_NO_MESSAGE_RECEIVED 1 // Error when reading the socket
#define ERR_CREATING_SOCKET 2 // Erreur when creating listening socket

View File

@ -103,34 +103,38 @@ int main(int argc, char **argv)
/* Wait for the threads to terminate */
fprintf(stderr, "Waiting for the monitor thread... ") ;
if (VERBOSE_WARNING)
fprintf(stderr, "Waiting for the monitor thread... ") ;
if (pthread_join(monitor_thread, NULL) != 0)
perror("Cannot join monitor thread") ;
else
else if (VERBOSE_WARNING)
fprintf(stderr, "OK.\n") ;
if (cfg_getbool(cfg, "autocalibration"))
{
// We must cancel this thread because it can be blocked on the
// recvfrom() call:
fprintf(stderr,
"Cancelling the autocalibration hello thread... ") ;
if (VERBOSE_WARNING)
fprintf(stderr,
"Cancelling the autocalibration hello thread... ") ;
if (pthread_cancel(autocalibration_hello_thread) != 0)
perror("Cannot cancel autocalibration hello thread") ;
else
else if (VERBOSE_WARNING)
fprintf(stderr, "OK.\n") ;
fprintf(stderr,
"Waiting for the autocalibration hello thread... ") ;
if (VERBOSE_WARNING)
fprintf(stderr,
"Waiting for the autocalibration hello thread... ") ;
if (pthread_join(autocalibration_hello_thread, NULL) != 0)
perror("Cannot join autocalibration hello thread") ;
else
else if (VERBOSE_WARNING)
fprintf(stderr, "OK.\n") ;
fprintf(stderr, "Waiting for the monitor APs thread... ") ;
if (VERBOSE_WARNING)
fprintf(stderr, "Waiting for the monitor APs thread... ") ;
if (pthread_join(monitor_aps_thread, NULL) != 0)
perror("Cannot join monitor APs thread") ;
else
else if (VERBOSE_WARNING)
fprintf(stderr, "OK.\n") ;
}
@ -156,11 +160,12 @@ void initialise_configuration(int argc, char **argv)
parse_command_line(argc, argv) ;
check_configuration() ;
#ifdef DEBUG
/* Configuration printing */
fprintf(stderr, "Configuration:\n") ;
cfg_print(cfg, stderr) ;
#endif // DEBUG
if (VERBOSE_INFO)
{
fprintf(stderr, "Configuration:\n") ;
cfg_print(cfg, stderr) ;
}
}
@ -327,10 +332,9 @@ void check_configuration()
if (cfg_getint(cfg, "listening_port") < 1 ||
cfg_getint(cfg, "listening_port") > 65535)
{
#ifdef DEBUG
fprintf(stderr, "Warning! Bad listening_port:"
" failing back to the default value.\n") ;
#endif // DEBUG
if (VERBOSE_WARNING)
fprintf(stderr, "Warning! Bad listening_port:"
" failing back to the default value.\n") ;
cfg_setint(cfg, "listening_port", AGGREGATE_DEFAULT_PORT) ;
}
@ -338,10 +342,9 @@ void check_configuration()
if (cfg_getint(cfg, "positioner_port") < 1 ||
cfg_getint(cfg, "positioner_port") > 65535)
{
#ifdef DEBUG
fprintf(stderr, "Warning! Bad positioner_port:"
" failing back to the default value.\n") ;
#endif // DEBUG
if (VERBOSE_WARNING)
fprintf(stderr, "Warning! Bad positioner_port:"
" failing back to the default value.\n") ;
cfg_setint(cfg, "positioner_port", POSITIONER_DEFAULT_PORT) ;
}
@ -357,50 +360,45 @@ void check_configuration()
// aggregate_timeout //
if (cfg_getint(cfg, "aggregate_timeout") < 0)
{
#ifdef DEBUG
fprintf(stderr, "Warning! aggregate_timeout cannot be negative:"
" failing back to the default value.\n") ;
#endif // DEBUG
if (VERBOSE_WARNING)
fprintf(stderr, "Warning! aggregate_timeout cannot be negative:"
" failing back to the default value.\n") ;
cfg_setint(cfg, "aggregate_timeout", DEFAULT_AGGREGATE_TIMEOUT) ;
}
// keep_timeout //
if (cfg_getint(cfg, "keep_timeout") < 0)
{
#ifdef DEBUG
fprintf(stderr, "Warning! keep_timeout cannot be negative:"
" failing back to the default value.\n") ;
#endif // DEBUG
if (VERBOSE_WARNING)
fprintf(stderr, "Warning! keep_timeout cannot be negative:"
" failing back to the default value.\n") ;
cfg_setint(cfg, "keep_timeout", DEFAULT_KEEP_TIMEOUT) ;
}
// check_interval //
if (cfg_getint(cfg, "check_interval") < 0)
{
#ifdef DEBUG
fprintf(stderr, "Warning! check_interval cannot be negative:"
" failing back to the default value.\n") ;
#endif // DEBUG
if (VERBOSE_WARNING)
fprintf(stderr, "Warning! check_interval cannot be negative:"
" failing back to the default value.\n") ;
cfg_setint(cfg, "check_interval", DEFAULT_CHECK_INTERVAL) ;
}
// ap_keep_timeout //
if (cfg_getint(cfg, "ap_keep_timeout") < 0)
{
#ifdef DEBUG
fprintf(stderr, "Warning! ap_keep_timeout cannot be negative:"
" failing back to the default value.\n") ;
#endif // DEBUG
if (VERBOSE_WARNING)
fprintf(stderr, "Warning! ap_keep_timeout cannot be negative:"
" failing back to the default value.\n") ;
cfg_setint(cfg, "ap_keep_timeout", DEFAULT_AP_KEEP_TIMEOUT) ;
}
// ap_check_interval //
if (cfg_getint(cfg, "ap_check_interval") < 0)
{
#ifdef DEBUG
fprintf(stderr, "Warning! ap_check_interval cannot be negative:"
" failing back to the default value.\n") ;
#endif // DEBUG
if (VERBOSE_WARNING)
fprintf(stderr, "Warning! ap_check_interval cannot be negative:"
" failing back to the default value.\n") ;
cfg_setint(cfg, "ap_check_interval", DEFAULT_AP_CHECK_INTERVAL) ;
}
}
@ -445,7 +443,7 @@ int read_loop(int sockfd)
request.y_position = owl_ntohf(request.y_position) ;
request.z_position = owl_ntohf(request.z_position) ;
if (cfg_getint(cfg, "verbose"))
if (VERBOSE_REQUESTS)
{
inet_ntop(AF_INET, &request.mobile_ip_addr_bytes,
mobile_ip_str, INET_ADDRSTRLEN) ;
@ -480,11 +478,9 @@ int read_loop(int sockfd)
request.direction
) ;
}
#ifdef DEBUG
else
else if (VERBOSE_CHATTERBOX)
fprintf(stderr, "Request received from AP « %s ».\n",
owl_mac_bytes_to_string(request.ap_mac_addr_bytes)) ;
#endif // DEBUG
got_request(request) ;
}
@ -519,9 +515,8 @@ void* monitor_requests(void *NULL_value)
owl_request_info info;
int sockfd;
#ifdef DEBUG
fprintf(stderr, "Monitor requests thread launched.\n") ;
#endif // DEBUG
if (VERBOSE_WARNING)
fprintf(stderr, "Monitor requests thread launched.\n") ;
sockfd =
owl_create_udp_trx_socket(cfg_getstr(cfg, "positioner_ip"),
@ -565,14 +560,10 @@ void* monitor_requests(void *NULL_value)
// If the timeout is reached
if (sub > aggregate_timeout)
{
fprintf(stderr, "* Timeout reached.") ;
#ifdef DEBUG
fprintf(stderr, " sub=%"PRIuFAST32" >"
" aggregate_timeout=%"PRIuFAST32"\n",
sub, aggregate_timeout) ;
#else // DEBUG
putc('\n', stderr) ;
#endif // DEBUG
if (VERBOSE_CHATTERBOX)
fprintf(stderr, "* Aggregate timeout reached:"
" %"PRIuFAST32" > %"PRIuFAST32"\n",
sub, aggregate_timeout) ;
// Print mobile MAC address to the output file
owl_mac_bytes_to_string_r(request_ptr->
@ -657,14 +648,10 @@ void* monitor_requests(void *NULL_value)
{
request_list *request_tmp = request_ptr ;
fprintf(stderr, "* Keep timeout reached.") ;
#ifdef DEBUG
fprintf(stderr, " sub=%"PRIuFAST32" >"
" keep_timeout=%"PRIuFAST32"\n",
sub, keep_timeout) ;
#else // DEBUG
putc('\n', stderr) ;
#endif // DEBUG
if (VERBOSE_CHATTERBOX)
fprintf(stderr, "* Keep timeout reached:"
" %"PRIuFAST32" > %"PRIuFAST32"\n",
sub, keep_timeout) ;
request_ptr = request_ptr->next ;
@ -723,7 +710,9 @@ void got_request(owl_captured_request request)
tmp_request = requests ;
if (requests == NULL) // If the request list does not exist,
{
fprintf(stderr, "Creating request list.\n") ;
if (VERBOSE_INFO)
fprintf(stderr, "Creating request list with AP « %s ».\n",
owl_mac_bytes_to_string(request.ap_mac_addr_bytes)) ;
tmp_request = malloc(sizeof(request_list)) ; // create it.
tmp_request->type = request.type ;
memcpy(tmp_request->mobile_mac_addr_bytes,
@ -781,7 +770,9 @@ void got_request(owl_captured_request request)
if (tmp_request == NULL) // The request does not exist in the list
{
fprintf(stderr, "Create new request.\n") ;
if (VERBOSE_INFO)
fprintf(stderr, "Create new request from AP « %s ».\n",
owl_mac_bytes_to_string(request.ap_mac_addr_bytes)) ;
tmp_request = malloc(sizeof(request_list)) ; // create it
tmp_request->type = request.type ;
memcpy(tmp_request->mobile_mac_addr_bytes,
@ -809,12 +800,14 @@ void got_request(owl_captured_request request)
{
if (tmp_request->info == NULL)
{ // We already sent to the server data for this request
fprintf(stderr, "Request already treated.\n") ;
if (VERBOSE_CHATTERBOX)
fprintf(stderr, "Request already treated.\n") ;
free(tmp_info) ;
}
else
{
fprintf(stderr, "Add information to the request.\n") ;
if (VERBOSE_CHATTERBOX)
fprintf(stderr, "Add information to the request.\n") ;
tmp_info->next = tmp_request->info ; // Add data
tmp_request->info = tmp_info ;
}
@ -864,9 +857,8 @@ void* listen_for_aps(void *NULL_value)
owl_autocalibration_hello message ;
char ap_ip_addr[INET_ADDRSTRLEN] ;
#ifdef DEBUG
fprintf(stderr, "Autocalibration Hello thread launched.\n") ;
#endif // DEBUG
if (VERBOSE_WARNING)
fprintf(stderr, "Autocalibration Hello thread launched.\n") ;
listen_sockfd =
owl_create_udp_listening_socket(cfg_getint(cfg,
@ -893,10 +885,9 @@ void* listen_for_aps(void *NULL_value)
strncpy(ap_ip_addr, inet_ntoa(client.sin_addr), INET_ADDRSTRLEN) ;
#ifdef DEBUG
fprintf(stderr, "Got a Hello message from « %s »\n",
ap_ip_addr) ;
#endif // DEBUG
if (VERBOSE_INFO)
fprintf(stderr,
"Got a Hello message from « %s »\n", ap_ip_addr) ;
sem_wait(&lock_aps) ;
update_ap(message.ap_mac_addr_bytes, ap_ip_addr) ;
@ -960,11 +951,13 @@ ap_list* find_ap(uint8_t mac_addr_bytes[ETHER_ADDR_LEN])
*/
ap_list* add_ap_front(uint8_t mac_addr_bytes[ETHER_ADDR_LEN])
{
#ifdef DEBUG
char mac_str[OWL_ETHER_ADDR_STRLEN] ;
owl_mac_bytes_to_string_r(mac_addr_bytes, mac_str) ;
fprintf(stderr, "Creating AP with MAC address « %s »...\n", mac_str) ;
#endif // DEBUG
if (VERBOSE_INFO)
{
char mac_str[OWL_ETHER_ADDR_STRLEN] ;
owl_mac_bytes_to_string_r(mac_addr_bytes, mac_str) ;
fprintf(stderr,
"Creating AP with MAC address « %s »...\n", mac_str) ;
}
ap_list *ap = malloc(sizeof(ap_list)) ;
memcpy(ap->mac_addr_bytes, mac_addr_bytes, ETHER_ADDR_LEN) ;
@ -1026,9 +1019,8 @@ void push_ap(ap_list *ap)
*/
void* monitor_aps(void *NULL_value)
{
#ifdef DEBUG
fprintf(stderr, "Monitor AP thread launched.\n") ;
#endif // DEBUG
if (VERBOSE_WARNING)
fprintf(stderr, "Monitor AP thread launched.\n") ;
while (owl_run)
{
@ -1079,12 +1071,13 @@ void delete_old_aps()
*/
void delete_ap(ap_list *ap)
{
#ifdef DEBUG
assert(ap) ;
char mac_str[OWL_ETHER_ADDR_STRLEN] ;
owl_mac_bytes_to_string_r(token_aps->mac_addr_bytes, mac_str) ;
fprintf(stderr, "Deleting AP « %s »...\n", mac_str) ;
#endif // DEBUG
if (VERBOSE_INFO)
{
char mac_str[OWL_ETHER_ADDR_STRLEN] ;
assert(ap) ;
owl_mac_bytes_to_string_r(ap->mac_addr_bytes, mac_str) ;
fprintf(stderr, "Deleting AP « %s »...\n", mac_str) ;
}
unlink_ap(ap) ;
free(ap) ;
@ -1134,9 +1127,8 @@ void order_send(ap_list *ap)
int sockfd ;
ssize_t nsent ;
#ifdef DEBUG
fprintf(stderr, "Sending an order to %s...\n", ap->ip_addr) ;
#endif // DEBUG
if (VERBOSE_INFO)
fprintf(stderr, "Sending an order to %s...\n", ap->ip_addr) ;
sockfd =
owl_create_udp_trx_socket(ap->ip_addr,
@ -1272,8 +1264,8 @@ void print_usage()
printf("Usage:\n"
"\t%s"
" [-f config_file]"
" [-v]"
" [-o output_file]"
" [-v[v[v[v]]]]"
" -o output_file"
" [-i positionner_ip]"
" [-p positioner_port]"
" [-l listening_port]"
@ -1293,8 +1285,10 @@ void print_usage()
"\t-V\t\tShow version information.\n"
"\t-f config_file\tUse 'config_file' instead of the default"
" configuration file (%s).\n"
"\t-v\t\tBe verbose (you can use this option several times to"
" increase the level of verbosity).\n"
"\t-v\t\tBe verbose. You can use this option up to 4 times to"
" increase the level of verbosity (1 = warnings, 2 = useful"
" information, 3 = too much information, 4 = displays the"
" detail of each and every received request).\n"
"Output options:\n"
"\t-o output_file\t\tAggregated requests will be appended to"