Skip to content

Improve logging #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
Language: Cpp

IndentWidth: 4
ContinuationIndentWidth: 8

ColumnLimit: 120

AlignAfterOpenBracket: false

AlignConsecutiveAssignments: true

# not supported in clang-format-3.7
# supported in clang-format-3.8 and above
AlignConsecutiveDeclarations: true

AlignTrailingComments: true

AllowAllParametersOfDeclarationOnNextLine: true

AllowShortBlocksOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false

# Control of individual brace wrapping cases
# not supported in clang-format-3.7
# supported in clang-format-3.8 and above
BraceWrapping: {
AfterClass: 'true'
AfterControlStatement: 'false'
AfterEnum: 'true'
AfterFunction: 'true'
AfterNamespace: 'true'
AfterStruct: 'true'
AfterUnion: 'true'
BeforeCatch: 'true'
BeforeElse: 'false'
IndentBraces: 'false'
}

BreakBeforeBraces: Stroustrup

IndentCaseLabels: true

KeepEmptyLinesAtTheStartOfBlocks: true

MaxEmptyLinesToKeep: 2

# Pointer is aligned to right side
PointerAlignment: Right

# High penalty to avoid line break just after return type
PenaltyReturnTypeOnItsOwnLine: 10000

SpaceAfterCStyleCast: true

SpacesInParentheses: true
SpaceInEmptyParentheses: false
SpaceBeforeParens: ControlStatements

UseTab: Never
12 changes: 12 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,15 @@ ChangeLog:
AUTHORS: ChangeLog
( echo "Authors and contributors, in alphabetical order: "; echo; \
sed -r "s/^Author: (.*)/\1/;t;d" $< | sort -u ) > $@

if HAVE_CLANG_FORMAT

.PHONY: reformat

reformat:
@echo "Reformatting header files..."
@CLANG_FORMAT@ -style=file -i `find . -name "*.h"`
@echo "Reformatting C files..."
@CLANG_FORMAT@ -style=file -i `find . -name "*.c"`

endif
6 changes: 6 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ AC_CONFIG_SRCDIR([src/igmpproxy.c])
AC_CONFIG_HEADERS([config.h])
AC_PROG_CC_C99

AC_CHECK_PROGS(
[CLANG_FORMAT], [clang-format clang-format-4.0 clang-format-3.9],
[AC_MSG_ERROR([clang-format was not not found during configure.])]
)
AM_CONDITIONAL([HAVE_CLANG_FORMAT], [test -n "$CLANG_FORMAT"])

AC_CANONICAL_HOST
case $host_os in
linux*|uclinux*) os=linux;;
Expand Down
161 changes: 83 additions & 78 deletions src/callout.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,91 +32,93 @@
**
*/


#include "igmpproxy.h"

/* the code below implements a callout queue */
static int id = 0;
static struct timeOutQueue *queue = 0; /* pointer to the beginning of timeout queue */
static int id = 0;
static struct timeOutQueue *queue = 0; /* pointer to the beginning of timeout queue */

struct timeOutQueue {
struct timeOutQueue *next; // Next event in queue
int id;
timer_f func; // function to call
void *data; // Data for function
int time; // Time offset for next event
struct timeOutQueue *next; // Next event in queue
int id;
timer_f func; // function to call
void * data; // Data for function
int time; // Time offset for next event
};

// Method for dumping the Queue to the log.
static void debugQueue(void);
static void debugQueue( void );

/**
* Initializes the callout queue
*/
void callout_init(void) {
void callout_init( void )
{
queue = NULL;
}

/**
* Clears all scheduled timeouts...
*/
void free_all_callouts(void) {
void free_all_callouts( void )
{
struct timeOutQueue *p;

while (queue) {
p = queue;
while ( queue ) {
p = queue;
queue = queue->next;
free(p);
free( p );
}
}


/**
* elapsed_time seconds have passed; perform all the events that should
* happen.
*/
void age_callout_queue(int elapsed_time) {
void age_callout_queue( int elapsed_time )
{
struct timeOutQueue *ptr;
struct timeOutQueue *_queue = NULL;
struct timeOutQueue *last = NULL;
int i = 0;
struct timeOutQueue *last = NULL;
int i = 0;

for (ptr = queue; ptr; ptr = ptr->next) {
if (ptr->time > elapsed_time) {
for ( ptr = queue; ptr; ptr = ptr->next ) {
if ( ptr->time > elapsed_time ) {
ptr->time -= elapsed_time;
break;
} else {
}
else {
elapsed_time -= ptr->time;
if (_queue == NULL)
if ( _queue == NULL )
_queue = ptr;
last = ptr;
}
last = ptr;
}
}

queue = ptr;
if (last) {
if ( last ) {
last->next = NULL;
}

/* process existing events */
for (ptr = _queue; ptr; ptr = _queue, i++) {
for ( ptr = _queue; ptr; ptr = _queue, i++ ) {
_queue = _queue->next;
my_log(LOG_DEBUG, 0, "About to call timeout %d (#%d)", ptr->id, i);
if (ptr->func)
ptr->func(ptr->data);
free(ptr);
my_log( LOG_DEBUG, 0, "About to call timeout %d (#%d)", ptr->id, i );
if ( ptr->func )
ptr->func( ptr->data );
free( ptr );
}
}

/**
* Return in how many seconds age_callout_queue() would like to be called.
* Return -1 if there are no events pending.
*/
int timer_nextTimer(void) {
if (queue) {
if (queue->time < 0) {
my_log(LOG_WARNING, 0, "timer_nextTimer top of queue says %d",
queue->time);
int timer_nextTimer( void )
{
if ( queue ) {
if ( queue->time < 0 ) {
my_log( LOG_WARNING, 0, "timer_nextTimer top of queue says %d", queue->time );
return 0;
}
return queue->time;
Expand All @@ -130,14 +132,15 @@ int timer_nextTimer(void) {
* @param action - The function to call on timeout.
* @param data - Pointer to the function data to supply...
*/
int timer_setTimer(int delay, timer_f action, void *data) {
struct timeOutQueue *ptr, *node, *prev;
int i = 0;
int timer_setTimer( int delay, timer_f action, void *data )
{
struct timeOutQueue *ptr, *node, *prev;
int i = 0;

/* create a node */
node = (struct timeOutQueue *)malloc(sizeof(struct timeOutQueue));
if (node == 0) {
my_log(LOG_WARNING, 0, "Malloc Failed in timer_settimer\n");
node = (struct timeOutQueue *) malloc( sizeof( struct timeOutQueue ) );
if ( node == 0 ) {
my_log( LOG_WARNING, 0, "Malloc Failed in timer_settimer\n" );
return -1;
}
node->func = action;
Expand All @@ -151,39 +154,38 @@ int timer_setTimer(int delay, timer_f action, void *data) {
/* insert node in the queue */

/* if the queue is empty, insert the node and return */
if (!queue) {
if ( !queue ) {
queue = node;
}
else {
/* chase the pointer looking for the right place */
while (ptr) {
if (delay < ptr->time) {
while ( ptr ) {
if ( delay < ptr->time ) {
// We found the correct node
node->next = ptr;
if (ptr == queue) {
if ( ptr == queue ) {
queue = node;
}
else {
prev->next = node;
}
ptr->time -= node->time;
my_log(LOG_DEBUG, 0,
"Created timeout %d (#%d) - delay %d secs",
node->id, i, node->time);
my_log( LOG_DEBUG, 0, "Created timeout %d (#%d) - delay %d secs", node->id, i, node->time );
debugQueue();
return node->id;
} else {
}
else {
// Continur to check nodes.
delay -= ptr->time; node->time = delay;
prev = ptr;
ptr = ptr->next;
delay -= ptr->time;
node->time = delay;
prev = ptr;
ptr = ptr->next;
}
i++;
}
prev->next = node;
}
my_log(LOG_DEBUG, 0, "Created timeout %d (#%d) - delay %d secs",
node->id, i, node->time);
my_log( LOG_DEBUG, 0, "Created timeout %d (#%d) - delay %d secs", node->id, i, node->time );
debugQueue();

return node->id;
Expand All @@ -192,16 +194,17 @@ int timer_setTimer(int delay, timer_f action, void *data) {
/**
* returns the time until the timer is scheduled
*/
int timer_leftTimer(int timer_id) {
int timer_leftTimer( int timer_id )
{
struct timeOutQueue *ptr;
int left = 0;
int left = 0;

if (!timer_id)
if ( !timer_id )
return -1;

for (ptr = queue; ptr; ptr = ptr->next) {
for ( ptr = queue; ptr; ptr = ptr->next ) {
left += ptr->time;
if (ptr->id == timer_id) {
if ( ptr->id == timer_id ) {
return left;
}
}
Expand All @@ -211,11 +214,12 @@ int timer_leftTimer(int timer_id) {
/**
* clears the associated timer. Returns 1 if succeeded.
*/
int timer_clearTimer(int timer_id) {
struct timeOutQueue *ptr, *prev;
int i = 0;
int timer_clearTimer( int timer_id )
{
struct timeOutQueue *ptr, *prev;
int i = 0;

if (!timer_id)
if ( !timer_id )
return 0;

prev = ptr = queue;
Expand All @@ -226,44 +230,45 @@ int timer_clearTimer(int timer_id) {
*/

debugQueue();
while (ptr) {
if (ptr->id == timer_id) {
while ( ptr ) {
if ( ptr->id == timer_id ) {
/* got the right node */

/* unlink it from the queue */
if (ptr == queue)
if ( ptr == queue )
queue = queue->next;
else
prev->next = ptr->next;

/* increment next node if any */
if (ptr->next != 0)
(ptr->next)->time += ptr->time;
if ( ptr->next != 0 )
( ptr->next )->time += ptr->time;

if (ptr->data)
free(ptr->data);
my_log(LOG_DEBUG, 0, "deleted timer %d (#%d)", ptr->id, i);
free(ptr);
if ( ptr->data )
free( ptr->data );
my_log( LOG_DEBUG, 0, "deleted timer %d (#%d)", ptr->id, i );
free( ptr );
debugQueue();
return 1;
}
prev = ptr;
ptr = ptr->next;
ptr = ptr->next;
i++;
}
// If we get here, the timer was not deleted.
my_log(LOG_DEBUG, 0, "failed to delete timer %d (#%d)", timer_id, i);
my_log( LOG_DEBUG, 0, "failed to delete timer %d (#%d)", timer_id, i );
debugQueue();
return 0;
}

/**
* debugging utility
*/
static void debugQueue(void) {
struct timeOutQueue *ptr;
static void debugQueue( void )
{
struct timeOutQueue *ptr;

for (ptr = queue; ptr; ptr = ptr->next) {
my_log(LOG_DEBUG, 0, "(Id:%d, Time:%d) ", ptr->id, ptr->time);
for ( ptr = queue; ptr; ptr = ptr->next ) {
my_log( LOG_DEBUG, 0, "(Id:%d, Time:%d) ", ptr->id, ptr->time );
}
}
Loading