1 #include "message_buffer.h"
4 void add_to_buffer(buffer *p_Buffer, char message[],char message_level){
9 printf("p_Buffer->tail=%d\n",p_Buffer->tail);
10 printf("p_Buffer->head=%d\n",p_Buffer->head);
11 if (p_Buffer->head >=BUFFER_SIZE) {
12 printf("buffer overflow\n");
13 p_Buffer->head -= BUFFER_SIZE; /*wrap the head around the buffer*/
14 printf("new p_Buffer->head=%d\n",p_Buffer->head);
16 /*if the buffer is full then delete last entry by moving the tail*/
17 if (p_Buffer->head==p_Buffer->tail){
18 printf("moving buffer tail from %d to ",p_Buffer->tail);
20 printf("%d\n",p_Buffer->tail);
21 if (p_Buffer->tail >=BUFFER_SIZE) p_Buffer->tail -= BUFFER_SIZE;/*wrap the tail around the buffer*/
24 /*move the head up one. the head always points at the last written data*/
30 p_Buffer->message_level[p_Buffer->head]=message_level; /*copy the message level*/
35 strcpy(p_Buffer->message[p_Buffer->head],message); /*copy the message*/
37 //convert the message into a string so it can be printed cleanly
38 if (message[length_of_message-1]!='\0') p_Buffer->message[p_Buffer->head][x+1]='\0';
40 if (p_Buffer->message_level[p_Buffer->head]<=DEBUG_LEVEL){
41 // printf("printing buffer 1\n");
42 print_buffer(p_Buffer,1); /*if the debug level is higher enough then print the new message*/
45 void print_buffer(buffer *p_Buffer, int number_of_messages_to_print){
46 /* printf("print buffer\n");
47 printf("buffer head:%d\n",p_Buffer->head);
48 printf("buffer tail:%d\n",p_Buffer->tail);
51 if (number_of_messages_to_print==PRINT_ALL) number_of_messages_to_print=BUFFER_SIZE;
52 // printf("number_of_messages_to_print=%d\n",number_of_messages_to_print);
53 for (i=0,x=p_Buffer->head; (x>=p_Buffer->tail) && (i<number_of_messages_to_print); i++, x--){
54 // printf("printing bufer\n");
56 if (x<0) x = BUFFER_SIZE; /*wrap x around buffer*/
57 printf("%s\n",p_Buffer->message[x]);