log.c 1.7 KB

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