Une lib de log simple d'utilisation nécéssitant gint.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

291 lines
4.5 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include <gint/exc.h>
  2. #include <gint/keyboard.h>
  3. #include <gint/display.h>
  4. #include <gint/std/stdlib.h>
  5. #include <gint/std/stdio.h>
  6. #include <gint/defs/attributes.h>
  7. #include <liblog.h>
  8. #include <stdint.h>
  9. #include <gint/timer.h>
  10. #ifdef FX9860G
  11. static unsigned int number_max_messages=4096; // Ram 4Ko
  12. #define MAX_LENGHT 22
  13. #define WIDTH 8
  14. #endif
  15. #ifdef FXCG50
  16. static unsigned int number_max_messages=10000; // Ram 10 Ko
  17. #define MAX_LENGHT 132
  18. #define WIDTH 17
  19. #endif
  20. typedef struct log_line log_line;
  21. typedef struct log_line
  22. {
  23. void* previous;
  24. char text[MAX_LENGHT];
  25. void* next;
  26. int cursor;
  27. } log_line;
  28. static log_line first_line=
  29. {
  30. 0,
  31. "Log beginning:",
  32. 0,
  33. 0
  34. };
  35. static log_line* current_line=&first_line;
  36. static int number_messages=0;
  37. static log_level_t priority=LEVEL_INFO;
  38. static log_line* cut_line()
  39. {
  40. log_line* maillon = first_line.next;
  41. log_line* maillon_suivant = maillon->next;
  42. first_line.next=maillon_suivant;
  43. if (maillon_suivant)
  44. maillon_suivant->previous=&first_line;
  45. number_messages--;
  46. return maillon;
  47. }
  48. static void optimize()
  49. {
  50. while (number_messages*sizeof(log_line)>=number_max_messages)
  51. {
  52. log_line* line = cut_line();
  53. free(line);
  54. //number_messages--;
  55. }
  56. }
  57. void ll_set_size(int s)
  58. {
  59. number_max_messages=s;
  60. optimize();
  61. }
  62. int ll_get_size();
  63. static void link_line(log_line* line)
  64. {
  65. current_line->next=line;
  66. line->previous=current_line;
  67. current_line=line;
  68. }
  69. static void clear_line(log_line* line)
  70. {
  71. line->next=0;
  72. line->cursor=0;
  73. for (int i=0; i<MAX_LENGHT; i++)
  74. current_line->text[i]='\0';
  75. }
  76. static log_line* add_line()
  77. {
  78. int malloc_fail=0;
  79. log_line* line;
  80. int test=(number_messages*sizeof(log_line)>=number_max_messages);
  81. if (!test)
  82. {
  83. line = malloc(sizeof(log_line));
  84. if (0==line)
  85. malloc_fail=1;
  86. }
  87. if (test || malloc_fail) // fail du malloc ou dépassement de la limite de ram imposée lors du build
  88. line=cut_line();
  89. link_line(line);
  90. clear_line(line);
  91. number_messages++;
  92. optimize();
  93. return current_line;
  94. }
  95. void ll_clear()
  96. {
  97. number_messages=0;
  98. while (current_line->previous)
  99. {
  100. log_line* next=current_line->previous;
  101. free(current_line);
  102. current_line=next;
  103. }
  104. current_line->next=0;
  105. current_line->cursor=0;
  106. for (int i=0; i<MAX_LENGHT; i++)
  107. current_line->text[i]='\0';
  108. }
  109. static void append_character(char c)
  110. {
  111. if (current_line==&first_line)
  112. add_line();
  113. if (c=='\n')
  114. {
  115. current_line->text[current_line->cursor] = '\0';
  116. add_line();
  117. return;
  118. }
  119. current_line->text[current_line->cursor] = c;
  120. if (c!='\0')
  121. {
  122. current_line->cursor ++;
  123. if (current_line->cursor == MAX_LENGHT-1)
  124. add_line();
  125. //current_line->text[current_line->cursor] = '\0';
  126. #ifdef FXCG50
  127. else if (dsize(&current_line->text[0],0,0,0)>396-7)
  128. {
  129. add_line();
  130. }
  131. #endif
  132. }
  133. }
  134. // log something
  135. static void ll_send_internal(const char * txt)
  136. {
  137. if (priority!=LEVEL_QUIET)
  138. {
  139. char c=1;
  140. int i=0;
  141. while (c!='\0')
  142. {
  143. c=txt[i];
  144. append_character(c);
  145. i++;
  146. }
  147. }
  148. }
  149. void ll_send(log_level_t p, char const *format, ...)
  150. {
  151. if (p>=priority)
  152. {
  153. char str[512];
  154. va_list args;
  155. va_start(args, format);
  156. vsnprintf(str, 512, format, args);
  157. va_end(args);
  158. ll_send_internal(str);
  159. }
  160. }
  161. static void show_line(const log_line* l, int y)
  162. {
  163. font_t const * f=dfont(0);
  164. dtext(1, y, C_BLACK, &l->text[0]);
  165. dfont(f);
  166. }
  167. void ll_display_custom(log_line* line)
  168. {
  169. dclear(C_WHITE);
  170. for (int i=0; i<8; i++)
  171. {
  172. #ifdef FX9860G
  173. show_line(line, 63 - 8*(i+1));
  174. #endif
  175. #ifdef FXCG50
  176. show_line(line, 224 - 13*(i+1));
  177. #endif
  178. line=line->previous;
  179. if (!line)
  180. break;
  181. }
  182. //dupdate_noint(); broke with gint update :thinking:
  183. dupdate();
  184. }
  185. void ll_display()
  186. {
  187. ll_display_custom(current_line);
  188. }
  189. void ll_pause()
  190. {
  191. //for (int i=0; i<timer_count(); i++)
  192. // if (i!=3) // keyboard timer
  193. // timer_pause(i);
  194. dclear(C_WHITE);
  195. log_line* line=current_line;
  196. while (1)
  197. {
  198. ll_display_custom(line);
  199. int key = getkey().key;
  200. if (key==KEY_UP)
  201. {
  202. log_line* linet=line->previous;
  203. if (linet)
  204. line=linet;
  205. }
  206. if (key==KEY_DOWN)
  207. {
  208. log_line* linet=line->next;
  209. if (linet)
  210. line=linet;
  211. }
  212. if (key==KEY_EXIT)
  213. break;
  214. }
  215. //for (int i=0; i<timer_count(); i++)
  216. // if (i!=3)
  217. // timer_start(i);
  218. }
  219. GNORETURN void ll_panic(uint32_t code)
  220. {
  221. char str[10];
  222. sprintf(str, "\nException !\n>> ErrCode=%d\nUse restart :(",code);
  223. ll_set_level(LEVEL_INFO);
  224. ll_send(LEVEL_FATAL, str);
  225. while (1)
  226. ll_pause();
  227. }
  228. void ll_set_panic()
  229. {
  230. gint_panic_set(ll_panic);
  231. }
  232. void ll_set_level(log_level_t l)
  233. {
  234. priority=l;
  235. }
  236. log_level_t ll_get_level(void)
  237. {
  238. return priority;
  239. }