yaffs Fixed a bug in quick tests.
[yaffs2.git] / direct / timothy_tests / dev / message_buffer.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2010 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Timothy Manning <timothy@yaffs.net>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 /*
15  * message_buffer.c contains code for a message buffer .
16  */
17
18 #include "message_buffer.h"
19
20 void append_to_buffer(buffer *p_Buffer, char *message,char message_level,char print){
21         /*wrapper function for add_to_buffer_root_function*/
22         add_to_buffer_root_function(p_Buffer,message, message_level,APPEND_MESSAGE,print);
23 }
24
25 void add_to_buffer(buffer *p_Buffer, char *message,char message_level,char print){
26         /*wrapper function for add_to_buffer_root_function*/
27         add_to_buffer_root_function(p_Buffer,message, message_level,DO_NOT_APPEND_MESSAGE,print);
28 }
29
30 void add_int_to_buffer(buffer *p_Buffer, int num,char message_level,char print){
31         char message[20];
32         sprintf(message, "%d",num);
33         add_to_buffer_root_function(p_Buffer,message, message_level,DO_NOT_APPEND_MESSAGE,print);
34 }
35
36 void append_int_to_buffer(buffer *p_Buffer, int num,char message_level,char print){
37         char message[20];
38         sprintf(message, "%d",num);
39         add_to_buffer_root_function(p_Buffer,message, message_level,APPEND_MESSAGE,print);
40 }
41
42
43 void add_to_buffer_root_function(buffer *p_Buffer, char *message,char message_level,char append,char print){
44         FILE *log_handle;
45         
46         if (append==APPEND_MESSAGE){    /* append current message*/
47                 strncat(p_Buffer->message[p_Buffer->head],message,BUFFER_MESSAGE_LENGTH);
48         }
49         else {
50
51                 /*move the head up one. the head always points at the last written data*/
52                 p_Buffer->head++;
53
54                 /*printf("p_Buffer->tail=%d\n",p_Buffer->tail);*/
55                 /*printf("p_Buffer->head=%d\n",p_Buffer->head);*/
56                 if (p_Buffer->head >=BUFFER_SIZE-1) {
57                         /*printf("buffer overflow\n");*/
58                         p_Buffer->head -= (BUFFER_SIZE-1);      /*wrap the head around the buffer*/
59                         /*printf("new p_Buffer->head=%d\n",p_Buffer->head);*/
60                 }       
61                 /*if the buffer is full then delete last entry by moving the tail*/
62                 if (p_Buffer->head==p_Buffer->tail){
63                         /*printf("moving buffer tail from %d to ",p_Buffer->tail);*/
64                         p_Buffer->tail++;       
65                         if (p_Buffer->tail >=BUFFER_SIZE) p_Buffer->tail -= BUFFER_SIZE;/*wrap the tail around the buffer*/
66                         /*printf("%d\n",p_Buffer->tail);*/
67
68                 }
69
70                 p_Buffer->message_level[p_Buffer->head]=message_level; /*copy the message level*/
71                 strncpy(p_Buffer->message[p_Buffer->head],message,BUFFER_MESSAGE_LENGTH); /*copy the message*/
72                 /*printf("copied %s into p_Buffer->message[p_Buffer->head]: %s\n",message,p_Buffer->message[p_Buffer->head]);
73                 printf("extra %s\n",p_Buffer->message[p_Buffer->head]);*/
74         }
75         if ((p_Buffer->message_level[p_Buffer->head]<=DEBUG_LEVEL)&& (print==PRINT)){
76                 /*printf("printing buffer 1\n");
77                 // the print buffer function is not working this is just a quick fix             
78                 print_buffer(p_Buffer,1); //if the debug level is higher enough then print the new message  
79                 */
80                 printf("%s\n",p_Buffer->message[p_Buffer->head]);
81                 log_handle=fopen(LOG_FILE,"a");
82                 if (log_handle!=NULL){
83                         fputs(p_Buffer->message[p_Buffer->head],log_handle);
84                         fputs("\n",log_handle);
85                         fclose(log_handle);
86                 }
87         }
88 }
89
90
91
92 void print_buffer(buffer *p_Buffer, int number_of_messages_to_print){   
93         int x=0;
94         int i=0;
95         printf("print buffer\n");
96         printf("buffer head:%d\n",p_Buffer->head);
97         printf("buffer tail:%d\n",p_Buffer->tail);
98
99         if (number_of_messages_to_print==PRINT_ALL) number_of_messages_to_print=BUFFER_SIZE;
100 //      printf("number_of_messages_to_print=%d\n",number_of_messages_to_print);
101         for (i=0,x=0; (x>=p_Buffer->tail) && (i<number_of_messages_to_print); i++, x--){
102 //              printf("printing buffer\n");
103 //              printf("x:%d\n",x);
104                 if (x<0) x = BUFFER_SIZE-1;             /*wrap x around buffer*/
105                 printf("%s\n",p_Buffer->message[p_Buffer->head]);
106                 printf("printed buffer\n");
107         }
108
109 }
110
111
112