[前][次][番号順一覧][スレッド一覧]

ruby-changes:31224

From: ko1 <ko1@a...>
Date: Wed, 16 Oct 2013 17:39:49 +0900 (JST)
Subject: [ruby-changes:31224] ko1:r43303 (trunk): * error.c, internal.h (rb_bug_reporter_add): add a new C-API.

ko1	2013-10-16 17:39:39 +0900 (Wed, 16 Oct 2013)

  New Revision: 43303

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=43303

  Log:
    * error.c, internal.h (rb_bug_reporter_add): add a new C-API.
      rb_bug_reporter_add() allows to register a function which
      is called at rb_bug() called.
    * ext/-test-/bug_reporter/bug_reporter.c: add a test for this C-API.
    * ext/-test-/bug_reporter/extconf.rb: ditto.
    * test/-ext-/bug_reporter/test_bug_reporter.rb: ditto.

  Added directories:
    trunk/ext/-test-/bug_reporter/
    trunk/test/-ext-/bug_reporter/
  Added files:
    trunk/ext/-test-/bug_reporter/bug_reporter.c
    trunk/ext/-test-/bug_reporter/extconf.rb
    trunk/test/-ext-/bug_reporter/test_bug_reporter.rb
  Modified files:
    trunk/ChangeLog
    trunk/error.c
    trunk/internal.h
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 43302)
+++ ChangeLog	(revision 43303)
@@ -1,3 +1,15 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Oct 16 17:37:17 2013  Koichi Sasada  <ko1@a...>
+
+	* error.c, internal.h (rb_bug_reporter_add): add a new C-API.
+	  rb_bug_reporter_add() allows to register a function which
+	  is called at rb_bug() called.
+
+	* ext/-test-/bug_reporter/bug_reporter.c: add a test for this C-API.
+
+	* ext/-test-/bug_reporter/extconf.rb: ditto.
+
+	* test/-ext-/bug_reporter/test_bug_reporter.rb: ditto.
+
 Wed Oct 16 15:14:21 2013  Koichi Sasada  <ko1@a...>
 
 	* NEWS: add a line into NEWS for last commit.
Index: ext/-test-/bug_reporter/bug_reporter.c
===================================================================
--- ext/-test-/bug_reporter/bug_reporter.c	(revision 0)
+++ ext/-test-/bug_reporter/bug_reporter.c	(revision 43303)
@@ -0,0 +1,24 @@ https://github.com/ruby/ruby/blob/trunk/ext/-test-/bug_reporter/bug_reporter.c#L1
+#include <ruby.h>
+#include <stdio.h>
+
+int rb_bug_reporter_add(void (*func)(FILE *, void *), void *data);
+
+static void
+sample_bug_reporter(FILE *out, void *ptr)
+{
+    int n = (int)ptr;
+    fprintf(out, "Sample bug reporter: %d\n", n);
+}
+
+static VALUE
+register_sample_bug_reporter(VALUE self, VALUE obj)
+{
+    rb_bug_reporter_add(sample_bug_reporter, NUM2INT(obj));
+    return Qnil;
+}
+
+void
+Init_bug_reporter(void)
+{
+    rb_define_global_function("register_sample_bug_reporter", register_sample_bug_reporter, 1);
+}
Index: ext/-test-/bug_reporter/extconf.rb
===================================================================
--- ext/-test-/bug_reporter/extconf.rb	(revision 0)
+++ ext/-test-/bug_reporter/extconf.rb	(revision 43303)
@@ -0,0 +1 @@
+create_makefile("-test-/bug_reporter/bug_reporter")
Index: error.c
===================================================================
--- error.c	(revision 43302)
+++ error.c	(revision 43303)
@@ -264,6 +264,29 @@ rb_warn_m(int argc, VALUE *argv, VALUE e https://github.com/ruby/ruby/blob/trunk/error.c#L264
     return Qnil;
 }
 
+#define MAX_BUG_REPORTERS 0x100
+
+static struct bug_reporters {
+    void (*func)(FILE *out, void *data);
+    void *data;
+} bug_reporters[MAX_BUG_REPORTERS];
+
+static int bug_reporters_size;
+
+int
+rb_bug_reporter_add(void (*func)(FILE *, void *), void *data)
+{
+    struct bug_reporters *reporter;
+    if (bug_reporters_size >= MAX_BUG_REPORTERS) {
+	rb_bug("rb_bug_reporter_add: overflow");
+    }
+    reporter = &bug_reporters[bug_reporters_size++];
+    reporter->func = func;
+    reporter->data = data;
+
+    return bug_reporters_size;
+}
+
 static void
 report_bug(const char *file, int line, const char *fmt, va_list args)
 {
@@ -281,9 +304,16 @@ report_bug(const char *file, int line, c https://github.com/ruby/ruby/blob/trunk/error.c#L304
 	snprintf(buf, 256, "\n%s\n\n", ruby_description);
 	fputs(buf, out);
 
-
 	rb_vm_bugreport();
 
+	/* call additional bug reporters */
+	{
+	    int i;
+	    for (i=0; i<bug_reporters_size; i++) {
+		struct bug_reporters *reporter = &bug_reporters[i];
+		(*reporter->func)(out, reporter->data);
+	    }
+	}
 	fprintf(out, REPORTBUG_MSG);
     }
 }
Index: internal.h
===================================================================
--- internal.h	(revision 43302)
+++ internal.h	(revision 43303)
@@ -772,6 +772,9 @@ VALUE rb_big2str_gmp(VALUE x, int base); https://github.com/ruby/ruby/blob/trunk/internal.h#L772
 VALUE rb_str2big_gmp(VALUE arg, int base, int badcheck);
 #endif
 
+/* error.c */
+int rb_bug_reporter_add(void (*func)(FILE *, void *), void *data);
+
 /* file.c */
 #ifdef __APPLE__
 VALUE rb_str_normalize_ospath(const char *ptr, long len);
Index: test/-ext-/bug_reporter/test_bug_reporter.rb
===================================================================
--- test/-ext-/bug_reporter/test_bug_reporter.rb	(revision 0)
+++ test/-ext-/bug_reporter/test_bug_reporter.rb	(revision 43303)
@@ -0,0 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/test/-ext-/bug_reporter/test_bug_reporter.rb#L1
+require 'test/unit'
+require_relative "../../ruby/envutil"
+
+class TestBugReporter < Test::Unit::TestCase
+  def test_bug_reporter_add
+    expected_stderr = /Sample bug reporter: 12345/
+    assert_in_out_err(["--disable-gems", "-r-test-/bug_reporter/bug_reporter", "-e", "register_sample_bug_reporter(12345); Process.kill :SEGV, $$"], "", [], expected_stderr, nil)
+  end
+end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]