Choco OS  V.0.16.9.0
Join to the chocolate world
oc_debug.c
Go to the documentation of this file.
1 
27 #include <oc_debug.h>
28 #include <stdio.h>
29 #include <oc_kprint.h>
30 #include <oc_intman.h>
31 #include <string.h>
32 #include <oc_system.h>
33 
39 #define _________________________________________VARIABLES_SECTION__________________________________________________________________________
40 
41 static char DebugLogs[CFG_UINT16_NUMBER_OF_DEBUG_LOGS][CFG_UINT16_MAX_DEBUGLOG_STRING_LENGTH];
42 static uint16_t PutIndex = 0;
43 static uint16_t GetIndex = 0;
44 static uint16_t GetOldestIndex = 0;
45 static uint16_t NumberOfLogs = 0;
46 static oC_LogType_t EnabledLogs = oC_LogType_Default;
47 static bool Locked = false;
48 
49 #undef _________________________________________VARIABLES_SECTION__________________________________________________________________________
50 
51 
55 #define _________________________________________INTERFACE_FUNCTIOS_SECTION_________________________________________________________________
56 
57 //==========================================================================================================================================
61 //==========================================================================================================================================
62 void enkdebuglog( oC_LogType_t LogType )
63 {
64  EnabledLogs = LogType;
65 }
66 
67 //==========================================================================================================================================
71 //==========================================================================================================================================
72 void lockkdebuglog( void )
73 {
74  Locked = true;
75 }
76 
77 //==========================================================================================================================================
81 //==========================================================================================================================================
82 void unlockkdebuglog( void )
83 {
84  Locked = false;
85 }
86 
87 //==========================================================================================================================================
95 //==========================================================================================================================================
96 void _kdebuglog( oC_LogType_t LogType , const char * Function, char * Format , ...)
97 {
98 #if CFG_ENABLE_DEBUG_PRINT == ON
99  if((EnabledLogs & LogType) && Locked == false)
100  {
101  oC_IntMan_EnterCriticalSection();
102  char * buffer = DebugLogs[PutIndex++];
103  if(NumberOfLogs < CFG_UINT16_NUMBER_OF_DEBUG_LOGS)
104  {
105  NumberOfLogs++;
106  }
107  GetIndex++;
108  if(GetIndex == CFG_UINT16_NUMBER_OF_DEBUG_LOGS)
109  {
110  GetIndex = 0;
111  }
112  if(PutIndex == CFG_UINT16_NUMBER_OF_DEBUG_LOGS)
113  {
114  PutIndex = 0;
115  }
116  oC_IntMan_ExitCriticalSection();
117  va_list argumentList;
118  oC_MemorySize_t size = CFG_UINT16_MAX_DEBUGLOG_STRING_LENGTH;
119  oC_MemorySize_t len = 0;
120  va_start(argumentList, Format);
121  memset(buffer,0,CFG_UINT16_MAX_DEBUGLOG_STRING_LENGTH);
122  if(LogType & oC_LogType_Error)
123  {
124  sprintf_s(buffer,size, oC_VT100_FG_GREEN "%09.3f " oC_VT100_FG_WHITE "|" oC_VT100_FG_BLUE " %-30s" oC_VT100_FG_WHITE ": " oC_VT100_FG_RED , gettimestamp(), Function);
125  }
126  else if(LogType & oC_LogType_Warning)
127  {
128  sprintf_s(buffer,size, oC_VT100_FG_GREEN "%09.3f " oC_VT100_FG_WHITE "|" oC_VT100_FG_BLUE " %-30s" oC_VT100_FG_WHITE ": " oC_VT100_FG_YELLOW , gettimestamp(), Function);
129  }
130  else if(LogType & oC_LogType_GoodNews)
131  {
132  sprintf_s(buffer,size, oC_VT100_FG_GREEN "%09.3f " oC_VT100_FG_WHITE "|" oC_VT100_FG_BLUE " %-30s" oC_VT100_FG_WHITE ": " oC_VT100_FG_GREEN , gettimestamp(), Function);
133  }
134  else
135  {
136  sprintf_s(buffer,size, oC_VT100_FG_GREEN "%09.3f " oC_VT100_FG_WHITE "|" oC_VT100_FG_BLUE " %-30s" oC_VT100_FG_WHITE ": " oC_VT100_FG_WHITE , gettimestamp(), Function);
137  }
138  len = strlen(buffer);
139  size -= len;
140  oC_KPrint_Format(&buffer[len],size,Format,argumentList);
141  va_end(argumentList);
142  }
143 #else
144 #endif
145 }
146 
147 //==========================================================================================================================================
151 //==========================================================================================================================================
152 bool readlastkdebuglog( char * outString , oC_UInt_t Size )
153 {
154  bool available = false;
155 
156  if(isram(outString) && NumberOfLogs > 0 && Size > 0)
157  {
158  oC_IntMan_EnterCriticalSection();
159  available = true;
160  NumberOfLogs--;
161 
162  if(GetIndex == 0)
163  {
164  GetIndex = CFG_UINT16_NUMBER_OF_DEBUG_LOGS - 1;
165  }
166  else
167  {
168  GetIndex--;
169  }
170  char * buffer = DebugLogs[GetIndex];
171  oC_IntMan_ExitCriticalSection();
172  strncpy(outString,buffer,Size);
173  }
174 
175  return available;
176 }
177 
178 //==========================================================================================================================================
182 //==========================================================================================================================================
183 bool readoldestkdebuglog( char * outString , oC_UInt_t Size )
184 {
185  bool available = false;
186 
187  if(isram(outString) && NumberOfLogs > 0 && Size > 0)
188  {
189  oC_IntMan_EnterCriticalSection();
190  available = true;
191  NumberOfLogs--;
192 
193  char * buffer = DebugLogs[GetOldestIndex++];
194 
195  if(GetOldestIndex >= CFG_UINT16_NUMBER_OF_DEBUG_LOGS)
196  {
197  GetOldestIndex = 0;
198  }
199  oC_IntMan_ExitCriticalSection();
200  strncpy(outString,buffer,Size);
201  }
202 
203  return available;
204 }
205 
206 //==========================================================================================================================================
210 //==========================================================================================================================================
211 void savekdebuglog( char * Log )
212 {
213  if(isaddresscorrect(Log) && strlen(Log) > 0)
214  {
215  oC_IntMan_EnterCriticalSection();
216  char * buffer = DebugLogs[PutIndex++];
217  if(NumberOfLogs < CFG_UINT16_NUMBER_OF_DEBUG_LOGS)
218  {
219  NumberOfLogs++;
220  }
221  GetIndex++;
222  if(GetIndex == CFG_UINT16_NUMBER_OF_DEBUG_LOGS)
223  {
224  GetIndex = 0;
225  }
226  if(PutIndex == CFG_UINT16_NUMBER_OF_DEBUG_LOGS)
227  {
228  PutIndex = 0;
229  }
230  oC_IntMan_ExitCriticalSection();
231  strncpy(buffer,Log,CFG_UINT16_MAX_DEBUGLOG_STRING_LENGTH);
232  }
233 }
234 
235 /* END OF SECTION */
236 #undef _________________________________________INTERFACE_FUNCTIOS_SECTION_________________________________________________________________
FILE__DESCRIPTION
FILE__DESCRIPTION
void unlockkdebuglog(void)
Definition: oc_debug.c:82
void lockkdebuglog(void)
Definition: oc_debug.c:72
bool readoldestkdebuglog(char *outString, oC_UInt_t Size)
Definition: oc_debug.c:183
The file with interface for kernel print operations.
The file with interface for interrupt manager.
void enkdebuglog(oC_LogType_t LogType)
Definition: oc_debug.c:62
void savekdebuglog(char *Log)
Definition: oc_debug.c:211
oC_ErrorCode_t oC_KPrint_Format(char *outBuffer, oC_UInt_t Size, const char *Format, va_list ArgumentList)
Definition: oc_kprint.c:196
bool readlastkdebuglog(char *outString, oC_UInt_t Size)
Definition: oc_debug.c:152