]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/journal.c
Fix journalling of output ids
[empserver] / src / lib / subs / journal.c
index 9313135ab453859ff223cbda08ee7d16d00aa31f..e5354402090d2b2b684747006878031409979193 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-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -26,7 +26,7 @@
  *  ---
  *
  *  journal.c: Log a journal of events to a file
- * 
+ *
  *  Known contributors to this file:
  *     Markus Armbruster, 2004-2008
  *     Ron Koenderink, 2008
@@ -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 *, int, char *, char *, int);
 
 static FILE *
 journal_open(void)
@@ -86,7 +89,7 @@ journal_entry(char *fmt, ...)
        time(&now);
        fprintf(journal, "%.24s %10.10s ",
                ctime(&now), empth_name(empth_self()));
-       
+
        va_start(ap, fmt);
        vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
        va_end(ap);
@@ -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,61 @@ journal_logout(void)
     journal_entry("logout %d", player->cnum);
 }
 
+void
+journal_output(struct player *pl, int id, char *output)
+{
+    static char buf[1024];
+    static struct player *bpl;
+    static int bid;
+    char *s, *e;
+
+    if (keep_journal < 2)
+       return;
+
+    if (buf[0] && (pl != bpl && id != bid)) {
+       journal_output_1(bpl, bid, buf, "", -1);
+       buf[0] = 0;
+    }
+
+    for (s = output; (e = strchr(s, '\n')); s = e + 1) {
+       journal_output_1(pl, id, buf, s, (int)(e + 1 - s));
+       buf[0] = 0;
+    }
+    if (strlen(buf) + strlen(s) < 1024) {
+       strcpy(buf + strlen(buf), s);
+       bpl = pl;
+       bid = id;
+    } else {
+       journal_output_1(pl, id, buf, s, -1);
+       buf[0] = 0;
+    }
+}
+
+void
+journal_output_1(struct player *pl, int id,
+                char *buf1, char *buf2, int buf2prec)
+{
+    if (pl && pl->state == PS_PLAYING)
+       journal_entry("output %d %d %s%.*s",
+                     pl->cnum, id, buf1, buf2prec, buf2);
+    else
+       journal_entry("output %p %d %s%.*s",
+                     pl, id, 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)
 {