log.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "dat.h"
  10. void
  11. logbufproc(Logbuf *lb)
  12. {
  13. char *s;
  14. int n;
  15. Req *r;
  16. while(lb->wait && lb->rp != lb->wp){
  17. r = lb->wait;
  18. lb->wait = r->aux;
  19. if(lb->wait == nil)
  20. lb->waitlast = &lb->wait;
  21. r->aux = nil;
  22. if(r->ifcall.count < 5){
  23. respond(r, "factotum: read request count too short");
  24. continue;
  25. }
  26. s = lb->msg[lb->rp];
  27. lb->msg[lb->rp] = nil;
  28. if(++lb->rp == nelem(lb->msg))
  29. lb->rp = 0;
  30. n = r->ifcall.count;
  31. if(n < strlen(s)+1+1){
  32. memmove(r->ofcall.data, s, n-5);
  33. n -= 5;
  34. r->ofcall.data[n] = '\0';
  35. /* look for first byte of UTF-8 sequence by skipping continuation bytes */
  36. while(n>0 && (r->ofcall.data[--n]&0xC0)==0x80)
  37. ;
  38. strcpy(r->ofcall.data+n, "...\n");
  39. }else{
  40. strcpy(r->ofcall.data, s);
  41. strcat(r->ofcall.data, "\n");
  42. }
  43. r->ofcall.count = strlen(r->ofcall.data);
  44. free(s);
  45. respond(r, nil);
  46. }
  47. }
  48. void
  49. logbufread(Logbuf *lb, Req *r)
  50. {
  51. if(lb->waitlast == nil)
  52. lb->waitlast = &lb->wait;
  53. *(lb->waitlast) = r;
  54. lb->waitlast = (Req **)r->aux;
  55. r->aux = nil;
  56. logbufproc(lb);
  57. }
  58. void
  59. logbufflush(Logbuf *lb, Req *r)
  60. {
  61. Req **l;
  62. for(l=(Req **)lb->wait; *l; l=(Req **)(*l)->aux){
  63. if(*l == r){
  64. *l = r->aux;
  65. r->aux = nil;
  66. if(*l == nil)
  67. lb->waitlast = l;
  68. respond(r, "interrupted");
  69. break;
  70. }
  71. }
  72. }
  73. void
  74. logbufappend(Logbuf *lb, char *buf)
  75. {
  76. if(debug)
  77. fprint(2, "%s\n", buf);
  78. if(lb->msg[lb->wp])
  79. free(lb->msg[lb->wp]);
  80. lb->msg[lb->wp] = estrdup9p(buf);
  81. if(++lb->wp == nelem(lb->msg))
  82. lb->wp = 0;
  83. logbufproc(lb);
  84. }
  85. Logbuf logbuf;
  86. void
  87. logread(Req *r)
  88. {
  89. logbufread(&logbuf, r);
  90. }
  91. void
  92. logflush(Req *r)
  93. {
  94. logbufflush(&logbuf, r);
  95. }
  96. void
  97. flog(char *fmt, ...)
  98. {
  99. char buf[1024];
  100. va_list arg;
  101. va_start(arg, fmt);
  102. vseprint(buf, buf+sizeof buf, fmt, arg);
  103. va_end(arg);
  104. logbufappend(&logbuf, buf);
  105. }