msg-output.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * log message interface
  3. *
  4. * Copyright (c) 2009 Openmoko Inc.
  5. *
  6. * Authors Daniel Mack <daniel@caiaq.de>
  7. * Christopher Hall <hsw@openmoko.com>
  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 as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <stdio.h>
  23. #include <stdarg.h>
  24. #include <string.h>
  25. #include <regs.h>
  26. #include <wikilib.h>
  27. #include <input.h>
  28. #include <interrupt.h>
  29. #include "msg.h"
  30. #include "serial.h"
  31. typedef char message_line_type[128];
  32. static serial_buffer_type messages[50];
  33. static message_line_type lines[ARRAY_SIZE(messages)];
  34. static serial_buffer_type *free_list;
  35. static uint32_t lost_messages = 0;
  36. serial_callback_type free_buffer;
  37. // output everything by default
  38. static int loglevel = MSG_LEVEL_MAX;
  39. void msg_init(void)
  40. {
  41. static bool initialised = false;
  42. if (!initialised) {
  43. size_t i = 0;
  44. free_list = NULL;
  45. // create linked list of free buffers
  46. for (i = 0; i < ARRAY_SIZE(messages); ++i) {
  47. messages[i].text = lines[i];
  48. messages[i].size = sizeof(lines[i]);
  49. messages[i].callback = free_buffer;
  50. messages[i].link = free_list;
  51. free_list = &messages[i];
  52. }
  53. serial_init();
  54. initialised = true;
  55. }
  56. }
  57. void msg(int level, const char *fmt, ...)
  58. {
  59. serial_buffer_type *m;
  60. va_list va;
  61. if (level > loglevel) {
  62. return;
  63. }
  64. if (NULL == free_list) {
  65. lost_messages++;
  66. return;
  67. }
  68. va_start(va, fmt);
  69. {
  70. // critcal code, since free is called from interrupt state
  71. InterruptType s = Interrupt_disable();
  72. m = free_list;
  73. free_list = free_list->link;
  74. Interrupt_enable(s);
  75. }
  76. //m->level = level;
  77. vsnprintf(m->text, m->size, fmt, va);
  78. va_end(va);
  79. serial_put(m);
  80. }
  81. // this is called at interrupt state, keep it short
  82. void free_buffer(serial_buffer_type *buffer)
  83. {
  84. buffer->link = free_list;
  85. free_list = buffer;
  86. }
  87. void set_loglevel(int level)
  88. {
  89. if (level < MSG_ERROR || level > MSG_LEVEL_MAX) {
  90. return;
  91. }
  92. loglevel = level;
  93. }