log.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <u.h>
  2. #include <libc.h>
  3. #include "httpd.h"
  4. #include "httpsrv.h"
  5. int logall[2];
  6. void
  7. logit(HConnect *c, char *fmt, ...)
  8. {
  9. char buf[4096];
  10. va_list arg;
  11. HSPriv *p;
  12. va_start(arg, fmt);
  13. vseprint(buf, buf+sizeof(buf), fmt, arg);
  14. va_end(arg);
  15. p = nil;
  16. if(c != nil)
  17. p = c->private;
  18. if(p != nil && p->remotesys != nil)
  19. syslog(0, HTTPLOG, "%s %s", p->remotesys, buf);
  20. else
  21. syslog(0, HTTPLOG, "%s", buf);
  22. }
  23. void
  24. writelog(HConnect *c, char *fmt, ...)
  25. {
  26. HSPriv *p;
  27. char buf[HBufSize+500], *bufp, *bufe;
  28. ulong now, today;
  29. int logfd;
  30. va_list arg;
  31. bufe = buf + sizeof(buf);
  32. now = time(nil);
  33. today = now / (24*60*60);
  34. logfd = logall[today & 1];
  35. if(c == nil || logfd <= 0)
  36. return;
  37. p = c->private;
  38. if(c->hstop == c->header || c->hstop[-1] != '\n')
  39. *c->hstop = '\n';
  40. *c->hstop = '\0';
  41. bufp = seprint(buf, bufe, "==========\n");
  42. bufp = seprint(bufp, bufe, "LogTime: %D\n", now);
  43. bufp = seprint(bufp, bufe, "ConnTime: %D\n", c->reqtime);
  44. bufp = seprint(bufp, bufe, "RemoteIP: %s\n", p->remotesys);
  45. bufp = seprint(bufp, bufe, "Port: %s\n", p->remoteserv);
  46. va_start(arg, fmt);
  47. bufp = vseprint(bufp, bufe, fmt, arg);
  48. va_end(arg);
  49. if(c->req.uri != nil && c->req.uri[0] != 0)
  50. bufp = seprint(bufp, bufe, "FinalURI: %s\n", c->req.uri);
  51. bufp = seprint(bufp, bufe, "----------\n%s\n", (char*)c->header);
  52. write(logfd, buf, bufp-buf); /* append-only file */
  53. }