kmlog.c 1.8 KB

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