printf.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. __UNUSED(f);
  33. b->buf[b->idx++] = ch;
  34. if (b->idx == PRINTBUF_SIZE - 1) {
  35. _DkPrintConsole(b->buf, b->idx);
  36. b->idx = 0;
  37. }
  38. b->cnt++;
  39. return 0;
  40. }
  41. int
  42. vprintf(const char * fmt, va_list *ap)
  43. {
  44. struct printbuf b;
  45. b.idx = 0;
  46. b.cnt = 0;
  47. vfprintfmt((void *) &fputch, NULL, &b, fmt, ap);
  48. _DkPrintConsole(b.buf, b.idx);
  49. return b.cnt;
  50. }
  51. int
  52. printf(const char * fmt, ...)
  53. {
  54. va_list ap;
  55. int cnt;
  56. va_start(ap, fmt);
  57. cnt = vprintf(fmt, &ap);
  58. va_end(ap);
  59. return cnt;
  60. }
  61. EXTERN_ALIAS(printf);
  62. #endif