summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xminiircd86
1 files changed, 85 insertions, 1 deletions
diff --git a/miniircd b/miniircd
index c323a93..3458e97 100755
--- a/miniircd
+++ b/miniircd
@@ -28,12 +28,14 @@ import string
import sys
import tempfile
import time
+import logging
+import subprocess
from datetime import datetime
from optparse import OptionParser
+from logging.handlers import RotatingFileHandler
VERSION = "1.1"
-
def create_directory(path):
if not os.path.isdir(path):
os.makedirs(path)
@@ -642,6 +644,10 @@ class Server(object):
self.chroot = options.chroot
self.setuid = options.setuid
self.statedir = options.statedir
+ self.miniircd_log_file = options.miniircd_log_file
+ self.miniircd_log_maxbytes = options.miniircd_log_maxsize * 1024 * 1024
+ self.miniircd_log_count = options.miniircd_log_count
+ self.logger = None
if self.ssl_pem_file:
self.ssl = __import__("ssl")
@@ -666,6 +672,32 @@ class Server(object):
if self.statedir:
create_directory(self.statedir)
+ def make_pidfile(self, filename):
+ if (os.path.isfile(filename)):
+ pid_fo = open(filename, "r")
+ old_pid = pid_fo.readline().strip()
+ pid_fo.close()
+
+ old_pid_is_deprecated = False
+ try:
+ result = subprocess.check_output(["ps", "--pid", old_pid, "-o", "pid=,args="])
+ if (result.find("miniircd") == -1):
+ old_pid_is_deprecated = True
+ except:
+ old_pid_is_deprecated = True
+
+ if old_pid_is_deprecated:
+ self.print_debug("Remove deprecated pidfile '%s'" % filename)
+ os.remove(filename)
+
+ try:
+ fd = os.open(filename, os.O_RDWR | os.O_CREAT | os.O_EXCL, 0644)
+ os.write(fd, "%i\n" % os.getpid())
+ os.close(fd)
+ except:
+ self.print_error("Could not create pidfile '%s'" % filename)
+ sys.exit(1)
+
def daemonize(self):
try:
pid = os.fork()
@@ -715,14 +747,20 @@ class Server(object):
if self.verbose:
print(msg)
sys.stdout.flush()
+ if self.logger:
+ self.logger.info(msg)
def print_debug(self, msg):
if self.debug:
print(msg)
sys.stdout.flush()
+ if self.logger:
+ self.logger.debug(msg)
def print_error(self, msg):
sys.stderr.write("%s\n" % msg)
+ if self.logger:
+ self.logger.error(msg)
def client_changed_nickname(self, client, oldnickname):
if oldnickname:
@@ -770,6 +808,34 @@ class Server(object):
os.setuid(self.setuid[0])
self.print_info("Setting uid:gid to %s:%s"
% (self.setuid[0], self.setuid[1]))
+
+ self.miniircd_log_init()
+ try:
+ self.loop(serversockets)
+ except:
+ if self.logger:
+ self.logger.exception("Fatal exception")
+ raise
+
+ def miniircd_log_init(self):
+ if not self.miniircd_log_file:
+ return
+
+ log_level = logging.INFO
+ if self.debug:
+ log_level = logging.DEBUG
+
+ self.logger = logging.getLogger("miniircd")
+
+ formatter = logging.Formatter("%(asctime)s - %(name)s[%(process)d] - %(levelname)s - %(message)s")
+ fh = RotatingFileHandler(self.miniircd_log_file, maxBytes=self.miniircd_log_maxbytes, backupCount=self.miniircd_log_count)
+ fh.setLevel(log_level)
+ fh.setFormatter(formatter)
+
+ self.logger.setLevel(log_level)
+ self.logger.addHandler(fh)
+
+ def loop(self, serversockets):
last_aliveness_check = time.time()
while True:
(iwtd, owtd, ewtd) = select.select(
@@ -840,6 +906,22 @@ def main(argv):
metavar="X",
help="listen on specific IP address X")
op.add_option(
+ "--make-pidfile",
+ metavar="X",
+ help="make pidfile file X")
+ op.add_option(
+ "--miniircd-log-file",
+ metavar="X",
+ help="store miniircd log in file X")
+ op.add_option(
+ "--miniircd-log-maxsize",
+ metavar="X", default=10, type="int",
+ help="set maximum miniircd log file size to X MiB [default: %default MiB]")
+ op.add_option(
+ "--miniircd-log-count",
+ metavar="X", default=10, type="int",
+ help="set count backup miniircd log file to X [default: %default]")
+ op.add_option(
"--logdir",
metavar="X",
help="store channel log in directory X")
@@ -922,6 +1004,8 @@ def main(argv):
server = Server(options)
if options.daemon:
server.daemonize()
+ if options.make_pidfile:
+ server.make_pidfile(options.make_pidfile)
try:
server.start()
except KeyboardInterrupt: