Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar/logger.py
blob: d6b6d43dc69fef38a219e596df92e90438fc2b84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""Logging module configuration for Sugar"""
# Copyright (C) 2006, Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import sys
import os
import logging
import traceback
from cStringIO import StringIO

from sugar import env

_log_writer = None

STDOUT_LEVEL = 1000
STDERR_LEVEL = 2000

formatter = logging.Formatter('%(name)s: %(message)s')

class LogWriter:
    def __init__(self, module_id):
        self._module_id = module_id

        logs_dir = _get_logs_dir()
        log_path = os.path.join(logs_dir, module_id + '.log')
        self._log_file = open(log_path, 'w')

    def write_record(self, record):
        self.write(record.levelno, formatter.format(record))

    def write(self, level, msg):
        if level == logging.ERROR:
            level_txt = 'ERROR'
        elif level == logging.WARNING:
            level_txt = 'WARNING'
        elif level == logging.DEBUG:
            level_txt = 'DEBUG'
        elif level == logging.INFO:
            level_txt = 'INFO'
        elif level == STDERR_LEVEL:
            level_txt = 'STDERR'
        elif level == STDOUT_LEVEL:
            level_txt = 'STDOUT'            

        if msg[len(msg) - 1] != '\n':
            msg += "\n"
        fmt = "%s - %s" % (level_txt, msg)
        fmt = fmt.encode("utf8")
        self._log_file.write(fmt)
        self._log_file.flush()

class Handler(logging.Handler):
    def __init__(self, writer):
        logging.Handler.__init__(self)

        self._writer = writer

    def emit(self, record):
        self._writer.write_record(record)

class StdoutCatcher:
    def write(self, txt):
        _log_writer.write(STDOUT_LEVEL, txt)
        sys.__stdout__.write(txt)

class StderrCatcher:
    def write(self, txt):
        _log_writer.write(STDERR_LEVEL, txt)
        sys.__stderr__.write(txt)

def __exception_handler(typ, exc, tb):
    trace = StringIO()
    traceback.print_exception(typ, exc, tb, None, trace)
    print >> sys.stderr, trace.getvalue()

    _log_writer.write(logging.ERROR, trace.getvalue())

def _get_logs_dir():
    logs_dir = os.path.join(env.get_profile_path(), 'logs')
    if not os.path.isdir(logs_dir):
        os.makedirs(logs_dir)
    return logs_dir

def start(module_id):
    log_writer = LogWriter(module_id)

    root_logger = logging.getLogger('')
    root_logger.setLevel(logging.DEBUG)
    root_logger.addHandler(Handler(log_writer))

    sys.stdout = StdoutCatcher()
    sys.stderr = StderrCatcher()

    global _log_writer
    _log_writer = log_writer
    sys.excepthook = __exception_handler

def cleanup():
    logs_dir = _get_logs_dir()
    for f in os.listdir(logs_dir):
        os.remove(os.path.join(logs_dir, f))