* src/vmcore/options.c (options_xx): Integrated vmlog options handling.
[cacao.git] / contrib / vmlog / vmlog_cacao.c
1 /* vmlog - high-speed logging for free VMs                  */
2 /* Copyright (C) 2006 Edwin Steiner <edwin.steiner@gmx.net> */
3
4 /* This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 /* vmlog_cacao.c - code to be #included in cacao */
20
21 #include <vmlog_cacao.h>
22 #define VMLOG_HAVE_PTRINT
23 #include <vmlog.h>
24 #include <assert.h>
25
26 /*** global variables ************************************************/
27
28 static vmlog_log *vmlog_global_log = NULL;
29 static java_object_t *vmlog_global_lock = NULL;
30 static vmlog_options *vmlog_cacao_options = NULL;
31
32 /*** locking *********************************************************/
33
34 #define VMLOG_LOCK(vml)    lock_monitor_enter(vmlog_global_lock)
35 #define VMLOG_UNLOCK(vml)  lock_monitor_exit(vmlog_global_lock)
36
37 /*** include the vmlog code ******************************************/
38
39 #include <vmlog.c>
40
41 /*** internal functions **********************************************/
42
43 void vmlog_cacao_init()
44 {
45         if (!vmlog_cacao_options->prefix)
46                 return;
47
48         vmlog_global_log = vmlog_log_new(vmlog_cacao_options->prefix,1);
49
50         if (vmlog_cacao_options->ignoreprefix) {
51                 vmlog_log_load_ignorelist(vmlog_global_log,
52                                 vmlog_cacao_options->ignoreprefix);
53         }
54
55         if (vmlog_cacao_options->stringprefix) {
56                 vmlog_load_stringhash(vmlog_global_log,
57                                 vmlog_cacao_options->stringprefix);
58         }
59
60         vmlog_opt_free(vmlog_cacao_options);
61         vmlog_cacao_options = NULL;
62 }
63
64 void vmlog_cacao_init_lock(void)
65 {
66         vmlog_global_lock = NEW(java_object_t);
67         lock_init_object_lock(vmlog_global_lock);
68 }
69
70 static void vmlog_cacao_do_log(vmlog_log_function fun,
71                                methodinfo *m)
72 {
73         char *name;
74         int namelen;
75
76         assert(m);
77
78         if (!vmlog_global_log)
79                 return;
80
81         name = vmlog_concat4len(m->class->name->text,m->class->name->blength,
82                                 ".",1,
83                                 m->name->text,m->name->blength,
84                                 m->descriptor->text,m->descriptor->blength,
85                                 &namelen);
86
87         fun(vmlog_global_log,(void*) THREADOBJECT,name,namelen);
88
89         VMLOG_FREE_ARRAY(char,namelen+1,name);
90 }
91
92 /*** functions callable from cacao ***********************************/
93
94 void vmlog_cacao_enter_method(methodinfo *m)
95 {
96         vmlog_cacao_do_log(vmlog_log_enter,m);
97 }
98
99 void vmlog_cacao_leave_method(methodinfo *m)
100 {
101         vmlog_cacao_do_log(vmlog_log_leave,m);
102 }
103
104 void vmlog_cacao_unrol_method(methodinfo *m)
105 {
106         vmlog_cacao_do_log(vmlog_log_unrol,m);
107 }
108
109 void vmlog_cacao_rerol_method(methodinfo *m)
110 {
111         vmlog_cacao_do_log(vmlog_log_rerol,m);
112 }
113
114 void vmlog_cacao_unwnd_method(methodinfo *m)
115 {
116         vmlog_cacao_do_log(vmlog_log_unwnd,m);
117 }
118
119 void vmlog_cacao_throw(java_object_t *xptr)
120 {
121         classinfo *c;
122         
123         if (!vmlog_global_log)
124                 return;
125
126         if (xptr) {
127                 c = xptr->vftbl->class;
128                 vmlog_log_throw(vmlog_global_log,(void*) THREADOBJECT,
129                                 c->name->text,c->name->blength);
130         }
131         else {
132                 vmlog_log_throw(vmlog_global_log,(void*) THREADOBJECT,
133                                 "unknown Throwable",strlen("unknown Throwable"));
134         }
135 }
136
137 void vmlog_cacao_catch(java_object_t *xptr)
138 {
139         classinfo *c;
140         
141         if (!vmlog_global_log)
142                 return;
143
144         if (xptr) {
145                 c = xptr->vftbl->class;
146                 vmlog_log_catch(vmlog_global_log,(void*) THREADOBJECT,
147                                 c->name->text,c->name->blength);
148         }
149         else {
150                 vmlog_log_catch(vmlog_global_log,(void*) THREADOBJECT,
151                                 "unknown Throwable",strlen("unknown Throwable"));
152         }
153 }
154
155 void vmlog_cacao_signl(const char *name)
156 {
157         if (!vmlog_global_log)
158                 return;
159
160         vmlog_log_signl(vmlog_global_log,(void*) THREADOBJECT,
161                         name, strlen(name));
162 }
163
164 void vmlog_cacao_init_options(void)
165 {
166         vmlog_cacao_options = vmlog_opt_new();
167 }
168
169 void vmlog_cacao_set_prefix(const char *arg)
170 {
171         vmlog_opt_set_prefix(vmlog_cacao_options, arg);
172 }
173
174 void vmlog_cacao_set_stringprefix(const char *arg)
175 {
176         vmlog_opt_set_stringprefix(vmlog_cacao_options, arg);
177 }
178
179 void vmlog_cacao_set_ignoreprefix(const char *arg)
180 {
181         vmlog_opt_set_ignoreprefix(vmlog_cacao_options, arg);
182 }
183
184 /* vim: noet ts=8 sw=8
185  */
186