clog.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. /* clog - log console */
  10. #include <u.h>
  11. #include <libc.h>
  12. #include <bio.h>
  13. char *argv0;
  14. int
  15. openlog(char *name)
  16. {
  17. int fd;
  18. fd = open(name, OWRITE);
  19. if(fd < 0)
  20. fd = create(name, OWRITE, DMAPPEND|0666);
  21. if(fd < 0){
  22. fprint(2, "%s: can't open %s: %r\n", argv0, name);
  23. return -1;
  24. }
  25. seek(fd, 0, 2);
  26. return fd;
  27. }
  28. void
  29. main(int argc, char **argv)
  30. {
  31. Biobuf in;
  32. int fd;
  33. char *p, *t;
  34. char buf[Bsize];
  35. argv0 = argv[0];
  36. if(argc < 3){
  37. fprint(2, "usage: %s console logfile \n", argv0);
  38. exits("usage");
  39. }
  40. fd = open(argv[1], OREAD);
  41. if(fd < 0){
  42. fprint(2, "%s: can't open %s: %r\n", argv0, argv[1]);
  43. exits("open");
  44. }
  45. Binit(&in, fd, OREAD);
  46. fd = openlog(argv[2]);
  47. for(;;){
  48. if(p = Brdline(&in, '\n')){
  49. p[Blinelen(&in)-1] = 0;
  50. t = ctime(time(0));
  51. t[19] = 0;
  52. while(fprint(fd, "%s: %s\n", t, p) < 0) {
  53. close(fd);
  54. sleep(500);
  55. fd = openlog(argv[2]);
  56. }
  57. } else if(Blinelen(&in) == 0) /* true eof or error */
  58. break;
  59. /* discard partial buffer? perhaps due to very long line */
  60. else if (Bread(&in, buf, sizeof buf) < 0)
  61. break;
  62. }
  63. exits(0);
  64. }