Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / editors / sced / scedwin / py / Logger.py
blobd319c8f2dbcf4d39b3c9d64a07880ed82989e74d
1 # -*- coding: utf-8 -*-
2 # sced (SuperCollider mode for gedit)
4 # Copyright 2012 Jakob Leben
5 # Copyright 2009 Artem Popov and other contributors (see AUTHORS)
7 # sced is free software:
8 # you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 import gobject
23 import pango
24 import gtk
25 import pipe_util
27 class Logger:
28 def __init__(self, pipe, log_view):
29 self.__pipe = pipe
30 self.__log_view = log_view
32 tag_table = log_view.buffer.get_tag_table()
33 self.__tag = gtk.TextTag()
35 self.__good_tag = gobject.new(gtk.TextTag,
36 weight = pango.WEIGHT_BOLD,
37 foreground = "darkgreen",
38 paragraph_background = "lightgreen")
40 self.__bad_tag = gobject.new(gtk.TextTag,
41 weight = pango.WEIGHT_BOLD,
42 foreground = "darkred",
43 paragraph_background = "pink")
45 # for warnings, etc.
46 self.__ugly_tag = gobject.new(gtk.TextTag,
47 #weight = pango.WEIGHT_BOLD,
48 foreground = "red")
50 tag_table.add(self.__tag)
51 tag_table.add(self.__good_tag)
52 tag_table.add(self.__bad_tag)
53 tag_table.add(self.__ugly_tag)
55 pipe_util.start_reader(pipe)
56 self.__reader_task = gobject.timeout_add(20, self.__reader_tick)
58 def __reader_tick(self):
59 res = pipe_util.get_buffer()
60 if(res is False):
61 self.stop()
62 return False
63 if(res is not None):
64 self.__append_to_buffer(unicode(res, 'mac_latin2'))
65 return True
67 def __append_to_buffer(self, text):
68 # FIXME: coloring the text is broken, so don't try to do it
70 buffer = self.__log_view.buffer
72 buffer.insert(buffer.get_end_iter(), text)
74 buffer.place_cursor(buffer.get_end_iter())
75 self.__log_view.view.scroll_mark_onscreen(buffer.get_insert())
77 return
79 if text.startswith("ERROR"):
80 tags = self.__bad_tag
82 elif text.startswith("WARNING") or text.startswith("FAILURE"):
83 tags = self.__ugly_tag
85 elif text.startswith("StartUp done."):
86 tags = self.__good_tag
87 else:
88 tags = self.__tag
90 buffer.insert_with_tags(buffer.get_end_iter(), text.rstrip(), tags)
91 buffer.insert(buffer.get_end_iter(), "\n")
93 buffer.place_cursor(buffer.get_end_iter())
94 self.__log_view.view.scroll_mark_onscreen(buffer.get_insert())
96 # only required for thread-based implementation
97 # return False
99 def stop(self):
100 if not self.__pipe.closed:
101 pipe_util.stop_reader()
102 self.__pipe.close()
103 gobject.source_remove(self.__reader_task)