]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/journal.c
Journal output lines instead of chunks
[empserver] / src / lib / subs / journal.c
index 4f553c51ef2410397cb6dc3fb63e9ad2e169984e..0c739ab02ea21ca244fc3af3ed16b687fd84c123 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -44,7 +44,9 @@
  *     prng NAME SEED
  *     login CNUM HOSTADDR USER
  *     logout CNUM
+ *     command NAME
  *     input INPUT
+ *     output THREAD ID OUTPUT
  *     update ETU
  */
 
@@ -67,6 +69,7 @@ static FILE *journal;
 
 static void journal_entry(char *fmt, ...)
     ATTRIBUTE((format (printf, 1, 2)));
+static void journal_output_1(struct player *, char *, char *, int);
 
 static FILE *
 journal_open(void)
@@ -98,7 +101,8 @@ journal_entry(char *fmt, ...)
                fprintf(journal, "\\%03o", *p);
        }
        fputs("\n", journal);
-       fflush(journal);
+       if (fmt[0] != 'o')      /* FIXME disgusting hack */
+           fflush(journal);
        if (ferror(journal)) {
            logerror("Error writing journal (%s)", strerror(errno));
            clearerr(journal);
@@ -167,12 +171,51 @@ journal_logout(void)
     journal_entry("logout %d", player->cnum);
 }
 
+void
+journal_output(struct player *pl, char *output)
+{
+    static char buf[1024];
+    char *s, *e;
+
+    if (keep_journal < 2)
+       return;
+
+    for (s = output; (e = strchr(s, '\n')); s = e + 1) {
+       journal_output_1(pl, buf, s, (int)(e + 1 - s));
+       buf[0] = 0;
+    }
+    if (strlen(buf) + strlen(s) < 1024)
+       strcpy(buf + strlen(buf), s);
+    else {
+       journal_output_1(pl, buf, s, -1);
+       buf[0] = 0;
+    }
+}
+
+void
+journal_output_1(struct player *pl, char *buf1, char *buf2, int buf2prec)
+{
+    if (pl && pl->state == PS_PLAYING)
+       journal_entry("output %d %s%.*s",
+                     pl->cnum, buf1, buf2prec, buf2);
+    else
+       journal_entry("output %p %s%.*s",
+                     pl, buf1, buf2prec, buf2);
+}
+
 void
 journal_input(char *input)
 {
     journal_entry("input %s", input);
 }
 
+void
+journal_command(char *cmd)
+{
+    char *eptr = strchr(cmd, ' ');
+    journal_entry("command %.*s", eptr ? (int)(eptr - cmd) : -1, cmd);
+}
+
 void
 journal_update(int etu)
 {