log.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // CTHTTPD - Simple Web Server - GPLv2
  2. // Chris Dorman, 2012-2014 (cddo@riseup.net)
  3. // Help from Nickolai Vurgaft (slipperygate@gmail.com)
  4. #include "log.h"
  5. #include "dep.h"
  6. #include "chttpd.h"
  7. void do_chttpd_log(int type, char *s1, char *s2, int num)
  8. {
  9. // logs the local time of the event
  10. time_t now = time(NULL);
  11. struct tm* tm_info;
  12. char timebuf[30];
  13. memset(timebuf, 0, strlen(timebuf));
  14. tm_info = localtime(&now);
  15. strftime(timebuf, sizeof(timebuf), "%Y/%m/%d, %H:%M:%S", tm_info);
  16. int fd ;
  17. char logbuffer[BUFSIZE*2];
  18. switch (type) {
  19. case ERROR: sprintf(logbuffer,"chttpd - %s : Error: %s %s", timebuf, s1, s2); break;
  20. case SORRY: sprintf(logbuffer, "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<html><head><title>CHTTPD: Error</title>\n</head><body><h2>CHTTPD Error:</h2> %s %s <hr /><address>chttpd</address></body></html>\r\n", s1, s2);
  21. write(num,logbuffer,strlen(logbuffer));
  22. break;
  23. case LOG: sprintf(logbuffer,"chttpd - %s : Info: %s:%s:5d", timebuf, s1, s2); break;
  24. case SEND_ERROR: sprintf(logbuffer,"HTTP/1.0 500 Internal Server Error\r\nContent-Type: text/html\r\n\r\n<html><head><title>CHTTPD: Found error</title></head><body><h2>Index error</h2>%s<hr /><address>chttpd</address></body></html>\r\n", s1);
  25. write(num,logbuffer,strlen(logbuffer));
  26. break;
  27. }
  28. if(type == ERROR || type == LOG) { // Log important data
  29. if((fd = open("server.log", O_CREAT| O_WRONLY | O_APPEND,0644)) >= 0) {
  30. write(fd,logbuffer,strlen(logbuffer));
  31. write(fd,"\n",1);
  32. close(fd);
  33. }
  34. }
  35. if(type == ERROR || type == SORRY || type == SEND_ERROR) exit(3);
  36. }