Ruby GTK3移行後のメインリポジトリ
修訂 | d32eb159f46727745121eca8f1701fc9a10ea70c (tree) |
---|---|
時間 | 2015-05-31 23:11:28 |
作者 | Shyouzou Sugitani <shy@user...> |
Commiter | Shyouzou Sugitani |
assorted fixes(30)
@@ -3,30 +3,48 @@ require "logger" | ||
3 | 3 | module Logging |
4 | 4 | |
5 | 5 | class Logging |
6 | - @@logger = Logger.new(STDOUT) | |
6 | + @@logger = [] | |
7 | + @@logger << Logger.new(STDOUT) | |
8 | + @@level = Logger::WARN | |
7 | 9 | |
8 | 10 | def initialize |
9 | 11 | ##@@logger.level = Logger::WARN |
10 | 12 | end |
11 | 13 | |
12 | 14 | def self.set_level(level) |
13 | - @@logger.level = level | |
15 | + @@logger.each do |logger| | |
16 | + logger.level = level | |
17 | + end | |
18 | + @@level = level | |
19 | + end | |
20 | + | |
21 | + def self.add_logger(logger) | |
22 | + @@logger << logger | |
23 | + logger.level = @@level | |
14 | 24 | end |
15 | 25 | |
16 | 26 | def self.info(message) |
17 | - @@logger.info(message) | |
27 | + @@logger.each do |logger| | |
28 | + logger.info(message) | |
29 | + end | |
18 | 30 | end |
19 | 31 | |
20 | 32 | def self.error(message) |
21 | - @@logger.error(message) | |
33 | + @@logger.each do |logger| | |
34 | + logger.error(message) | |
35 | + end | |
22 | 36 | end |
23 | 37 | |
24 | 38 | def self.warning(message) |
25 | - @@logger.warn(message) | |
39 | + @@logger.each do |logger| | |
40 | + logger.warn(message) | |
41 | + end | |
26 | 42 | end |
27 | 43 | |
28 | 44 | def self.debug(message) |
29 | - @@logger.debug(message) | |
45 | + @@logger.each do |logger| | |
46 | + logger.debug(message) | |
47 | + end | |
30 | 48 | end |
31 | 49 | end |
32 | 50 | end |
@@ -36,18 +36,21 @@ require "ninix/logging" | ||
36 | 36 | |
37 | 37 | module Ninix_Main |
38 | 38 | |
39 | - def self.handleException(exception_type, value, tb) | |
40 | - #logger.error('Uncaught exception', | |
41 | - # exc_info=(exception_type, value, tb)) | |
39 | + def self.handleException(exception) | |
40 | + message = 'Uncaught exception (' + exception.class.to_s + ")\n" + exception.backtrace.join("\n") | |
41 | + Logging::Logging.error(message) | |
42 | 42 | response_id = 1 |
43 | 43 | dialog = Gtk::MessageDialog.new( |
44 | - nil, 0, Gtk.MessageType.ERROR, Gtk.ButtonsType.NONE, | |
45 | - _('A ninix-aya error has been detected.')) | |
46 | - dialog.set_title(_('Bug Detected')) | |
47 | - dialog.set_position(Gtk.WindowPosition.CENTER) | |
48 | - dialog.set_gravity(Gdk.Gravity.CENTER) | |
49 | - button = dialog.add_button(_('Show Details'), response_id) | |
50 | - dialog.add_button("_Close", Gtk.ResponseType.CLOSE) | |
44 | + nil, 0, Gtk::MessageType::ERROR, Gtk::MessageDialog::ButtonsType::NONE, | |
45 | + # _('A ninix-aya error has been detected.')) | |
46 | + 'A ninix-aya error has been detected.') | |
47 | + # dialog.set_title(_('Bug Detected')) | |
48 | + dialog.set_title('Bug Detected') | |
49 | + dialog.set_window_position(Gtk::Window::Position::CENTER) | |
50 | + dialog.gravity = Gdk::Window::Gravity::CENTER | |
51 | + # button = dialog.add_button(_('Show Details'), response_id) | |
52 | + button = dialog.add_button('Show Details', response_id) | |
53 | + dialog.add_button("_Close", Gtk::ResponseType::CLOSE) | |
51 | 54 | textview = Gtk::TextView.new |
52 | 55 | textview.set_editable(false) |
53 | 56 | left, top, scrn_w, scrn_h = Pix.get_workarea() |
@@ -57,22 +60,21 @@ module Ninix_Main | ||
57 | 60 | textview.show() |
58 | 61 | sw = Gtk::ScrolledWindow.new |
59 | 62 | sw.show() |
60 | - sw.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) | |
63 | + sw.set_policy(Gtk::PolicyType::AUTOMATIC, Gtk::PolicyType::AUTOMATIC) | |
61 | 64 | sw.add(textview) |
62 | 65 | frame = Gtk::Frame.new |
63 | - frame.set_shadow_type(Gtk.ShadowType.IN) | |
66 | + frame.set_shadow_type(Gtk::ShadowType::IN) | |
64 | 67 | frame.add(sw) |
65 | 68 | frame.set_border_width(7) |
66 | - content_area = dialog.get_content_area() | |
69 | + frame.set_size_request(480, 320) # XXX | |
70 | + content_area = dialog.content_area | |
67 | 71 | content_area.add(frame) |
68 | - stringio = io.StringIO() | |
69 | - traceback.print_exception(exception_type, value, tb, nil, stringio) | |
70 | - textbuffer = textview.get_buffer() | |
71 | - textbuffer.set_text(stringio.getvalue()) | |
72 | + textbuffer = textview.buffer | |
73 | + textbuffer.set_text(message) | |
72 | 74 | while true |
73 | 75 | if dialog.run() == response_id |
74 | 76 | frame.show() |
75 | - button.set_sensitive(0) | |
77 | + button.set_sensitive(false) | |
76 | 78 | else # close button |
77 | 79 | break |
78 | 80 | end |
@@ -81,12 +83,10 @@ module Ninix_Main | ||
81 | 83 | raise SystemExit |
82 | 84 | end |
83 | 85 | |
84 | -# sys.excepthook = handleException | |
85 | - | |
86 | 86 | def self.main(option) |
87 | 87 | # parse command line arguments |
88 | 88 | if option[:logfile] |
89 | - #logger.addHandler(logging.FileHandler(args.logfile)) | |
89 | + Logging::Logging.add_logger(Logger.new(option[:logfile])) | |
90 | 90 | end |
91 | 91 | # TCP 7743:伺か(未使用)(IANA Registered Port for SSTP) |
92 | 92 | # UDP 7743:伺か(未使用)(IANA Registered Port for SSTP) |
@@ -132,9 +132,7 @@ module Ninix_Main | ||
132 | 132 | raise SystemExit('ninix-aya is already running') |
133 | 133 | end |
134 | 134 | # start |
135 | - Logging::Logging.info('loading...') | |
136 | 135 | app = Application.new(f, :sstp_port => sstp_port) |
137 | - Logging::Logging.info('done.') | |
138 | 136 | app.run(abend) |
139 | 137 | f.truncate(0) |
140 | 138 | begin |
@@ -273,16 +271,21 @@ module Ninix_Main | ||
273 | 271 | socket = sstp_server.accept_nonblock |
274 | 272 | rescue |
275 | 273 | next |
276 | - #rescue # except socket.error as e: | |
277 | - #code, message = e.args | |
278 | - #logging.error('socket.error: {0} ({1:d})'.format(message, code)) | |
279 | - #rescue # except ValueError: # may happen when ninix is terminated | |
280 | - #return | |
281 | 274 | end |
282 | - handler = SSTP::SSTPRequestHandler.new(sstp_server, socket) | |
283 | - buffer = socket.gets | |
284 | - handler.handle(buffer) | |
285 | - socket.close | |
275 | + begin | |
276 | + handler = SSTP::SSTPRequestHandler.new(sstp_server, socket) | |
277 | + buffer = socket.gets | |
278 | + handler.handle(buffer) | |
279 | + socket.close | |
280 | + rescue SocketError => e | |
281 | + Logging::Logging.error( | |
282 | + 'socket.error: ' + e.message) | |
283 | + rescue SystemCallError => e | |
284 | + Logging::Logging.error( | |
285 | + 'socket.error: ' + e.message + ' (' + e.errno.to_s + ')') | |
286 | + rescue # may happen when ninix is terminated | |
287 | + return | |
288 | + end | |
286 | 289 | end |
287 | 290 | end |
288 | 291 |
@@ -303,10 +306,9 @@ module Ninix_Main | ||
303 | 306 | for port in @sstp_port |
304 | 307 | begin |
305 | 308 | server = SSTP::SSTPServer.new(port) |
306 | - rescue # except socket.error as e: | |
307 | - #code, message = e.args | |
308 | - #logging.warning( | |
309 | - # 'Port {0:d}: {1} (ignored)'.format(port, message)) | |
309 | + rescue SystemCallError => e | |
310 | + Logging::Logging.warning( | |
311 | + 'Port ' + port.to_s + ': ' + e.message + ' (ignored)') | |
310 | 312 | next |
311 | 313 | end |
312 | 314 | server.set_responsible(self) |
@@ -387,6 +389,7 @@ module Ninix_Main | ||
387 | 389 | @loaded = false |
388 | 390 | @confirmed = false |
389 | 391 | @console = Console.new(self) |
392 | + Logging::Logging.info('loading...') | |
390 | 393 | # create preference dialog |
391 | 394 | @prefs = Prefs::PreferenceDialog.new |
392 | 395 | @prefs.set_responsible(self) |
@@ -425,6 +428,7 @@ module Ninix_Main | ||
425 | 428 | @nekoninni = Home.search_nekoninni() |
426 | 429 | @katochan = Home.search_katochan() |
427 | 430 | @kinoko = Home.search_kinoko() |
431 | + Logging::Logging.info('done.') | |
428 | 432 | end |
429 | 433 | |
430 | 434 | def edit_preferences(*arglist) |
@@ -945,14 +949,12 @@ module Ninix_Main | ||
945 | 949 | |
946 | 950 | def run(abend) |
947 | 951 | @abend = abend |
948 | -# if os.name == "nt" | |
949 | -# # The SIGTERM signal is not generated under Windows NT. | |
950 | -# import win32api | |
951 | -# win32api.SetConsoleCtrlHandler(self.exit_handler, true) | |
952 | -# else | |
953 | -# import signal | |
954 | -# signal.signal(signal.SIGTERM, self.exit_handler) | |
955 | -# end | |
952 | + if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/ | |
953 | + # The SIGTERM signal is not generated under Windows NT. | |
954 | + #win32api.SetConsoleCtrlHandler(exit_handler, true) | |
955 | + else | |
956 | + Signal.trap(:TERM) {|signo| exit_handler(signo) } | |
957 | + end | |
956 | 958 | @timeout_id = GLib::Timeout.add(100) { do_idle_tasks } # 100[ms] |
957 | 959 | Gtk.main() |
958 | 960 | end |
@@ -1248,10 +1250,10 @@ module Ninix_Main | ||
1248 | 1250 | def add_balloon(balloon_dir) |
1249 | 1251 | if @balloons.include?(balloon_dir) |
1250 | 1252 | exists = true |
1251 | - #logging.warning('INSTALLED BALLOON CHANGED: {0}'.format(balloon_dir)) | |
1253 | + Logging::Logging.warning('INSTALLED BALLOON CHANGED: ' + balloon_dir) | |
1252 | 1254 | else |
1253 | 1255 | exists = false |
1254 | - #logging.info('NEW BALLOON INSTALLED: {0}'.format(balloon_dir)) | |
1256 | + Logging::Logging.info('NEW BALLOON INSTALLED: ' + balloon_dir) | |
1255 | 1257 | end |
1256 | 1258 | balloon_conf = Home.search_balloons(:target => [balloon_dir]) |
1257 | 1259 | if not balloon_conf.empty? |
@@ -1282,16 +1284,16 @@ module Ninix_Main | ||
1282 | 1284 | begin |
1283 | 1285 | File.delete(File.join(prefix, filename)) |
1284 | 1286 | rescue |
1285 | - #logging.error( | |
1286 | - # '*** REMOVE FAILED *** : {0}'.format(filename)) | |
1287 | + Logging::Logging.error( | |
1288 | + '*** REMOVE FAILED *** : ' + filename) | |
1287 | 1289 | end |
1288 | 1290 | end |
1289 | 1291 | else # dir |
1290 | 1292 | begin |
1291 | 1293 | FileUtils.remove_entry_secure(File.join(prefix, filename)) |
1292 | 1294 | rescue |
1293 | - #logging.error( | |
1294 | - # '*** REMOVE FAILED *** : {0}'.format(filename)) | |
1295 | + Logging::Logging.error( | |
1296 | + '*** REMOVE FAILED *** : ' + filename) | |
1295 | 1297 | end |
1296 | 1298 | end |
1297 | 1299 | } |
@@ -1351,7 +1353,7 @@ module Ninix_Main | ||
1351 | 1353 | end |
1352 | 1354 | end |
1353 | 1355 | rescue IOError => e |
1354 | - #logging.error('cannot read {0}'.format(path)) | |
1356 | + Logging::Logging.error('cannot read ' + path) | |
1355 | 1357 | end |
1356 | 1358 | end |
1357 | 1359 | ai_list = [] |
@@ -1400,6 +1402,7 @@ module Ninix_Main | ||
1400 | 1402 | |
1401 | 1403 | class Console |
1402 | 1404 | include GetText |
1405 | + attr_writer :level | |
1403 | 1406 | |
1404 | 1407 | def initialize(app) |
1405 | 1408 | @app = app |
@@ -1407,9 +1410,8 @@ module Ninix_Main | ||
1407 | 1410 | @dialog.signal_connect('delete_event') do |w, e| |
1408 | 1411 | #return true # XXX |
1409 | 1412 | end |
1410 | - #@log_handler = logging.handlers.BufferingHandler(0) | |
1411 | - #@log_handler.shouldFlush = self.shouldFlush | |
1412 | - #logging.getLogger().addHandler(self.log_handler) | |
1413 | + @level = Logger::WARN # XXX | |
1414 | + Logging::Logging.add_logger(self) | |
1413 | 1415 | @sw = Gtk::ScrolledWindow.new |
1414 | 1416 | @sw.set_policy(Gtk::PolicyType::NEVER, Gtk::PolicyType::ALWAYS) |
1415 | 1417 | @sw.show() |
@@ -1427,9 +1429,9 @@ module Ninix_Main | ||
1427 | 1429 | @tag_debug = @tb.create_tag(nil, 'foreground' => 'yellow') |
1428 | 1430 | @tag_notset = @tb.create_tag(nil, 'foreground' => 'blue') |
1429 | 1431 | # DnD data types |
1430 | - #dnd_targets = [['text/uri-list', 0, 0]] | |
1431 | - #@tv.drag_dest_set(Gtk::Drag::DestDefaults::ALL, dnd_targets, | |
1432 | - # Gdk::DragContext::Action::COPY) | |
1432 | + dnd_targets = [['text/uri-list', 0, 0]] | |
1433 | + @tv.drag_dest_set(Gtk::Drag::DestDefaults::ALL, dnd_targets, | |
1434 | + Gdk::DragContext::Action::COPY) | |
1433 | 1435 | @tv.drag_dest_set_target_list(nil) # important |
1434 | 1436 | @tv.drag_dest_add_uri_targets() |
1435 | 1437 | @tv.signal_connect('drag_data_received') do |widget, context, x, y, data, info, time| |
@@ -1465,48 +1467,80 @@ module Ninix_Main | ||
1465 | 1467 | @opened = false |
1466 | 1468 | end |
1467 | 1469 | |
1468 | - def shouldFlush(record) | |
1469 | - it = @tb.get_end_iter() | |
1470 | - if record.levelno >= 50 | |
1471 | - tag = @tag_critical | |
1472 | - elsif record.levelno >= 40 | |
1473 | - tag = @tag_error | |
1474 | - elsif record.levelno >= 30 | |
1475 | - tag = @tag_warning | |
1476 | - elsif record.levelno >= 20 | |
1477 | - tag = @tag_info | |
1478 | - elsif record.levelno >= 10 | |
1479 | - tag = @tag_debug | |
1480 | - else | |
1481 | - tag = @tag_notset | |
1482 | - end | |
1483 | - @tb.insert_with_tags( | |
1484 | - it, | |
1485 | - [record.levelname, ':', record.getMessage(), '\n'].join(''), | |
1486 | - tag) | |
1487 | - it = @tb.get_end_iter() | |
1470 | + def message_with_tag(message, tag) | |
1471 | + it = @tb.end_iter | |
1472 | + @tb.insert(it, [Logger::SEV_LABEL[@level], ':', message, "\n"].join(""), tag) | |
1473 | + it = @tb.end_iter | |
1488 | 1474 | # scroll_to_iter may not have the desired effect. |
1489 | 1475 | mark = @tb.create_mark("end", it, false) |
1490 | 1476 | @tv.scroll_to_mark(mark, 0.0, false, 0.5, 0.5) |
1491 | 1477 | end |
1492 | 1478 | |
1479 | + def info(message) | |
1480 | + if @level > Logger::INFO | |
1481 | + return | |
1482 | + end | |
1483 | + tag = @tag_info | |
1484 | + message_with_tag(message, tag) | |
1485 | + end | |
1486 | + | |
1487 | + def debug(message) | |
1488 | + if @level > Logger::DEBUG | |
1489 | + return | |
1490 | + end | |
1491 | + tag = @tag_debug | |
1492 | + message_with_tag(message, tag) | |
1493 | + end | |
1494 | + | |
1495 | + def fatal(message) | |
1496 | + if @level > Logger::FATAL | |
1497 | + return | |
1498 | + end | |
1499 | + tag = @tag_critical | |
1500 | + message_with_tag(message, tag) | |
1501 | + end | |
1502 | + | |
1503 | + def error(message) | |
1504 | + if @level > Logger::ERROR | |
1505 | + return | |
1506 | + end | |
1507 | + tag = @tag_error | |
1508 | + message_with_tag(message, tag) | |
1509 | + end | |
1510 | + | |
1511 | + def warn(message) | |
1512 | + if @level > Logger::WARN | |
1513 | + return | |
1514 | + end | |
1515 | + tag = @tag_warning | |
1516 | + message_with_tag(message, tag) | |
1517 | + end | |
1518 | + | |
1519 | + def unknown(message) | |
1520 | + if @level > Logger::UNKNOWN | |
1521 | + return | |
1522 | + end | |
1523 | + tag = @tag_notset | |
1524 | + message_with_tag(message, tag) | |
1525 | + end | |
1526 | + | |
1493 | 1527 | def update |
1494 | 1528 | ghosts, balloons = @app.search_ghosts() # XXX |
1495 | 1529 | if ghosts > 0 and balloons > 0 |
1496 | 1530 | @dialog.set_title(_('Console')) |
1497 | - #logging.info('Ghosts: {0:d}'.format(ghosts)) | |
1498 | - #logging.info('Balloons: {0:d}'.format(balloons)) | |
1531 | + Logging::Logging.info('Ghosts: ' + ghosts.to_s) | |
1532 | + Logging::Logging.info('Balloons: ' + balloons.to_s) | |
1499 | 1533 | else |
1500 | 1534 | @dialog.set_title(_('Nanntokashitekudasai.')) |
1501 | 1535 | if ghosts > 0 |
1502 | - #logging.info('Ghosts: {0:d}'.format(ghosts)) | |
1536 | + Logging::Logging.info('Ghosts: ' + ghosts.to_s) | |
1503 | 1537 | else |
1504 | - #logging.warning('Ghosts: {0:d}'.format(ghosts)) | |
1538 | + Logging::Logging.warning('Ghosts: ' + ghosts.to_s) | |
1505 | 1539 | end |
1506 | 1540 | if balloons > 0 |
1507 | - #logging.info('Balloons: {0:d}'.format(balloons)) | |
1541 | + Logging::Logging.info('Balloons: ' + balloons.to_s) | |
1508 | 1542 | else |
1509 | - #logging.warning('Balloons: {0:d}'.format(balloons)) | |
1543 | + Logging::Logging.warning('Balloons: ' + balloons.to_s) | |
1510 | 1544 | end |
1511 | 1545 | end |
1512 | 1546 | end |
@@ -1763,4 +1797,8 @@ opt.on('--debug', 'debug') {|v| option[:debug] = v} | ||
1763 | 1797 | opt.on('--logfile logfile_name', 'logfile name') {|v| option[:logfile] = v} |
1764 | 1798 | opt.parse!(ARGV) |
1765 | 1799 | |
1766 | -Ninix_Main.main(option) | |
1800 | +begin | |
1801 | + Ninix_Main.main(option) | |
1802 | +rescue => e # should never rescue Exception | |
1803 | + Ninix_Main.handleException(e) | |
1804 | +end |