kmlog.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* kernel/kmlog.c
  2. *
  3. * This file is part of evx.
  4. *
  5. * evx is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * evx is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with evx. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <kernel/init.h>
  19. #include <kernel/module.h>
  20. #include <kernel/kernel.h>
  21. #include <kernel/kmlog.h>
  22. #include <kernel/tty.h>
  23. #include <arch/archinfo.h>
  24. #include <arch/string.h>
  25. #include <sys/types.h>
  26. #include <stdarg.h>
  27. /* write log to memory, before printk is ready.
  28. *
  29. * kmlog buffer sets by arch_mm_init (why it is always this
  30. * function...) and **MUST** be cleared by it.
  31. *
  32. * the buffer is always in the kernel heap, and it's always
  33. * KMLOG_SIZE bytes long.*/
  34. char *kmlog_buf;
  35. int kmlog_index = 1;
  36. int kmlog(char *s, ...) {
  37. int len, i, level;
  38. char buf[512];
  39. va_list args;
  40. memset(buf, 0, 512);
  41. va_start(args, s);
  42. if (*s == '#') {
  43. level = atoi(s[1]);
  44. if (level < KMLOG_LEVEL)
  45. return 0;
  46. s += 2;
  47. }
  48. len = vsprintf(buf, s, args);
  49. /* replace last zero */
  50. kmlog_index--;
  51. /* <= len copies a zero from buf */
  52. for (i = 0; i <= len; i++) {
  53. if (kmlog_index > KMLOG_SIZE)
  54. kmlog_index = 0;
  55. kmlog_buf[kmlog_index++] = buf[i];
  56. }
  57. if (likely(init_state == STATE_READY))
  58. tty_write_all(buf);
  59. va_end(args);
  60. return 0;
  61. }