printf.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "pal_internal.h"
  16. #include "api.h"
  17. #ifndef NO_INTERNAL_PRINTF
  18. // Collect up to PRINTBUF_SIZE characters into a buffer
  19. // and perform ONE system call to print all of them,
  20. // in order to make the lines output to the console atomic
  21. // and prevent interrupts from causing context switches
  22. // in the middle of a console output line and such.
  23. #define PRINTBUF_SIZE 256
  24. struct printbuf {
  25. int idx; // current buffer index
  26. int cnt; // total bytes printed so far
  27. char buf[PRINTBUF_SIZE];
  28. };
  29. static int
  30. fputch(void * f, int ch, struct printbuf * b)
  31. {
  32. b->buf[b->idx++] = ch;
  33. if (b->idx == PRINTBUF_SIZE - 1) {
  34. _DkPrintConsole(b->buf, b->idx);
  35. b->idx = 0;
  36. }
  37. b->cnt++;
  38. return 0;
  39. }
  40. int
  41. vprintf(const char * fmt, va_list *ap)
  42. {
  43. struct printbuf b;
  44. b.idx = 0;
  45. b.cnt = 0;
  46. vfprintfmt((void *) &fputch, NULL, &b, fmt, ap);
  47. _DkPrintConsole(b.buf, b.idx);
  48. return b.cnt;
  49. }
  50. int
  51. printf(const char * fmt, ...)
  52. {
  53. va_list ap;
  54. int cnt;
  55. va_start(ap, fmt);
  56. cnt = vprintf(fmt, &ap);
  57. va_end(ap);
  58. return cnt;
  59. }
  60. extern_alias(printf);
  61. #endif