summaryrefslogtreecommitdiff
path: root/sys/src/cmd/aux/clog.c
diff options
context:
space:
mode:
authorTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
committerTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
commite5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch)
treed8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/cmd/aux/clog.c
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/cmd/aux/clog.c')
-rwxr-xr-xsys/src/cmd/aux/clog.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/sys/src/cmd/aux/clog.c b/sys/src/cmd/aux/clog.c
new file mode 100755
index 000000000..984f69964
--- /dev/null
+++ b/sys/src/cmd/aux/clog.c
@@ -0,0 +1,64 @@
+/* clog - log console */
+#include <u.h>
+#include <libc.h>
+#include <bio.h>
+
+char *argv0;
+
+int
+openlog(char *name)
+{
+ int fd;
+
+ fd = open(name, OWRITE);
+ if(fd < 0)
+ fd = create(name, OWRITE, DMAPPEND|0666);
+ if(fd < 0){
+ fprint(2, "%s: can't open %s: %r\n", argv0, name);
+ return -1;
+ }
+ seek(fd, 0, 2);
+ return fd;
+}
+
+void
+main(int argc, char **argv)
+{
+ Biobuf in;
+ int fd;
+ char *p, *t;
+ char buf[Bsize];
+
+ argv0 = argv[0];
+ if(argc < 3){
+ fprint(2, "usage: %s console logfile \n", argv0);
+ exits("usage");
+ }
+
+ fd = open(argv[1], OREAD);
+ if(fd < 0){
+ fprint(2, "%s: can't open %s: %r\n", argv0, argv[1]);
+ exits("open");
+ }
+ Binit(&in, fd, OREAD);
+
+ fd = openlog(argv[2]);
+
+ for(;;){
+ if(p = Brdline(&in, '\n')){
+ p[Blinelen(&in)-1] = 0;
+ t = ctime(time(0));
+ t[19] = 0;
+ while(fprint(fd, "%s: %s\n", t, p) < 0) {
+ close(fd);
+ sleep(500);
+ fd = openlog(argv[2]);
+ }
+ } else if(Blinelen(&in) == 0) /* true eof or error */
+ break;
+ /* discard partial buffer? perhaps due to very long line */
+ else if (Bread(&in, buf, sizeof buf) < 0)
+ break;
+ }
+ exits(0);
+}