smblog.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "headers.h"
  2. static QLock logreflock, logprintlock;
  3. static int locked;
  4. void
  5. smbloglock(void)
  6. {
  7. qlock(&logreflock);
  8. if (locked++ == 0)
  9. qlock(&logprintlock);
  10. qunlock(&logreflock);
  11. }
  12. void
  13. smblogunlock(void)
  14. {
  15. qlock(&logreflock);
  16. if (locked && --locked == 0)
  17. qunlock(&logprintlock);
  18. qunlock(&logreflock);
  19. }
  20. static int
  21. smbloglockedvprint(char *fmt, va_list ap)
  22. {
  23. if (smbglobals.log.fd >= 0)
  24. vfprint(smbglobals.log.fd, fmt, ap);
  25. if (smbglobals.log.print)
  26. vfprint(2, fmt, ap);
  27. return 0;
  28. }
  29. int
  30. smblogvprint(int cmd, char *fmt, va_list ap)
  31. {
  32. if (cmd < 0 || smboptable[cmd].debug) {
  33. smbloglock();
  34. smbloglockedvprint(fmt, ap);
  35. smblogunlock();
  36. }
  37. return 0;
  38. }
  39. int
  40. smblogprint(int cmd, char *fmt, ...)
  41. {
  42. if (cmd < 0 || smbtrans2optable[cmd].debug) {
  43. va_list ap;
  44. va_start(ap, fmt);
  45. smblogvprint(cmd, fmt, ap);
  46. va_end(ap);
  47. }
  48. return 0;
  49. }
  50. int
  51. translogprint(int cmd, char *fmt, ...)
  52. {
  53. if (cmd < 0 || smboptable[cmd].debug) {
  54. va_list ap;
  55. va_start(ap, fmt);
  56. smblogvprint(cmd, fmt, ap);
  57. va_end(ap);
  58. }
  59. return 0;
  60. }
  61. int
  62. smblogprintif(int v, char *fmt, ...)
  63. {
  64. if (v) {
  65. va_list ap;
  66. va_start(ap, fmt);
  67. smbloglock();
  68. smbloglockedvprint(fmt, ap);
  69. smblogunlock();
  70. va_end(ap);
  71. }
  72. return 0;
  73. }
  74. void
  75. smblogdata(int cmd, int (*print)(int cmd, char *fmt, ...), void *ap, long n, long limit)
  76. {
  77. uchar *p = ap;
  78. long i;
  79. long saven;
  80. i = 0;
  81. saven = n;
  82. if (saven > limit)
  83. n = limit;
  84. while (i < n) {
  85. int l = n - i < 16 ? n - i : 16;
  86. int b;
  87. (*print)(cmd, "0x%.4lux ", i);
  88. for (b = 0; b < l; b += 2) {
  89. (*print)(cmd, " %.2ux", p[i + b]);
  90. if (b < l - 1)
  91. (*print)(cmd, "%.2ux", p[i + b + 1]);
  92. else
  93. (*print)(cmd, " ");
  94. }
  95. while (b < 16) {
  96. (*print)(cmd, " ");
  97. b += 2;
  98. }
  99. (*print)(cmd, " ");
  100. for (b = 0; b < l; b++)
  101. if (p[i + b] >= ' ' && p[i + b] <= '~')
  102. (*print)(cmd, "%c", p[i + b]);
  103. else
  104. (*print)(cmd, ".");
  105. (*print)(cmd, "\n");
  106. i += l;
  107. }
  108. if (saven > limit)
  109. (*print)(cmd, "0x%.4ux ...\n0x%.4ux\n", limit, saven);
  110. }