smblog.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "headers.h"
  10. static QLock logreflock, logprintlock;
  11. static int locked;
  12. void
  13. smbloglock(void)
  14. {
  15. qlock(&logreflock);
  16. if (locked++ == 0)
  17. qlock(&logprintlock);
  18. qunlock(&logreflock);
  19. }
  20. void
  21. smblogunlock(void)
  22. {
  23. qlock(&logreflock);
  24. if (locked && --locked == 0)
  25. qunlock(&logprintlock);
  26. qunlock(&logreflock);
  27. }
  28. static int
  29. smbloglockedvprint(char *fmt, va_list ap)
  30. {
  31. if (smbglobals.log.fd >= 0)
  32. vfprint(smbglobals.log.fd, fmt, ap);
  33. if (smbglobals.log.print)
  34. vfprint(2, fmt, ap);
  35. return 0;
  36. }
  37. int
  38. smblogvprint(int cmd, char *fmt, va_list ap)
  39. {
  40. if (cmd < 0 || smboptable[cmd].debug) {
  41. smbloglock();
  42. smbloglockedvprint(fmt, ap);
  43. smblogunlock();
  44. }
  45. return 0;
  46. }
  47. int
  48. smblogprint(int cmd, char *fmt, ...)
  49. {
  50. if (cmd < 0 || smbtrans2optable[cmd].debug) {
  51. va_list ap;
  52. va_start(ap, fmt);
  53. smblogvprint(cmd, fmt, ap);
  54. va_end(ap);
  55. }
  56. return 0;
  57. }
  58. int
  59. translogprint(int cmd, char *fmt, ...)
  60. {
  61. if (cmd < 0 || smboptable[cmd].debug) {
  62. va_list ap;
  63. va_start(ap, fmt);
  64. smblogvprint(cmd, fmt, ap);
  65. va_end(ap);
  66. }
  67. return 0;
  68. }
  69. int
  70. smblogprintif(int v, char *fmt, ...)
  71. {
  72. if (v) {
  73. va_list ap;
  74. va_start(ap, fmt);
  75. smbloglock();
  76. smbloglockedvprint(fmt, ap);
  77. smblogunlock();
  78. va_end(ap);
  79. }
  80. return 0;
  81. }
  82. void
  83. smblogdata(int cmd, int (*print)(int cmd, char *fmt, ...), void *ap,
  84. int32_t n, int32_t limit)
  85. {
  86. uint8_t *p = ap;
  87. int32_t i;
  88. int32_t saven;
  89. i = 0;
  90. saven = n;
  91. if (saven > limit)
  92. n = limit;
  93. while (i < n) {
  94. int l = n - i < 16 ? n - i : 16;
  95. int b;
  96. (*print)(cmd, "0x%.4lux ", i);
  97. for (b = 0; b < l; b += 2) {
  98. (*print)(cmd, " %.2ux", p[i + b]);
  99. if (b < l - 1)
  100. (*print)(cmd, "%.2ux", p[i + b + 1]);
  101. else
  102. (*print)(cmd, " ");
  103. }
  104. while (b < 16) {
  105. (*print)(cmd, " ");
  106. b += 2;
  107. }
  108. (*print)(cmd, " ");
  109. for (b = 0; b < l; b++)
  110. if (p[i + b] >= ' ' && p[i + b] <= '~')
  111. (*print)(cmd, "%c", p[i + b]);
  112. else
  113. (*print)(cmd, ".");
  114. (*print)(cmd, "\n");
  115. i += l;
  116. }
  117. if (saven > limit)
  118. (*print)(cmd, "0x%.4ux ...\n0x%.4ux\n", limit, saven);
  119. }