--- deliantra/server/common/logger.C 2009/10/12 14:00:57 1.20 +++ deliantra/server/common/logger.C 2010/04/11 17:27:51 1.24 @@ -1,9 +1,7 @@ /* * This file is part of Deliantra, the Roguelike Realtime MMORPG. * - * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team - * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team - * Copyright (©) 1992,2007 Frank Tore Johansen + * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team * * Deliantra is free software: you can redistribute it and/or modify it under * the terms of the Affero GNU General Public License as published by the @@ -43,16 +41,20 @@ }; static SMUTEX(mutex); +static SMUTEX(fdlock); static SCOND(cond); static int logfd = STDERR_FILENO; static int logsync = 1; static std::vector > queue; -void set_logfd (int fd) +int log_setfd (int fd) { - SMUTEX_LOCK (mutex); + SMUTEX_LOCK (fdlock); + int old = logfd; logfd = fd < 0 ? STDERR_FILENO : fd; - SMUTEX_UNLOCK (mutex); + SMUTEX_UNLOCK (fdlock); + + return old; } #define PREFIX_LEN sizeof ("0000-00-00 00:00:00.0000+") - 1 @@ -84,6 +86,9 @@ char *buf = line.buf; + if (logsync != 2) + SMUTEX_LOCK (fdlock); + while (char *end = strchr (buf, '\n')) { iov [1].iov_base = buf; @@ -101,6 +106,9 @@ pfx [PREFIX_LEN - 1] = '+'; } + if (logsync != 2) + SMUTEX_UNLOCK (fdlock); + sfree (line.buf, line.len); } @@ -152,13 +160,15 @@ break; usleep (10000); + SMUTEX_LOCK (fdlock); + SMUTEX_UNLOCK (fdlock); } } static void af_child () { - logsync = 1; + logsync = 2; } static struct logthread : thread @@ -172,7 +182,7 @@ } logthread; void -LOG (int flags, const char *format, ...) +LOG (int flags, const_utf8_string format, ...) { int level = flags & 15; @@ -224,3 +234,25 @@ } } +static int suspended; + +void +log_suspend () +{ + if (!suspended++) + { + LOG (llevDebug, "logging suspended."); + SMUTEX_LOCK (fdlock); + } +} + +void +log_resume () +{ + if (!--suspended) + { + SMUTEX_UNLOCK (fdlock); + LOG (llevDebug, "logging resumed."); + } +} +