Line data Source code
1 : /*
2 : * SPDX-License-Identifier: MPL-2.0
3 : *
4 : * This Source Code Form is subject to the terms of the Mozilla Public
5 : * License, v. 2.0. If a copy of the MPL was not distributed with this
6 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 : *
8 : * Copyright 2024 MonetDB Foundation;
9 : * Copyright August 2008 - 2023 MonetDB B.V.;
10 : * Copyright 1997 - July 2008 CWI.
11 : */
12 :
13 : /*
14 : * GDKtracer exposes routines where an occurring failure should reach the
15 : * client immediately. For that reason, GDKtracer reports those errors
16 : * directly to the stream.
17 : *
18 : */
19 :
20 : #include "monetdb_config.h"
21 : #include "mal.h"
22 : #include "mal_interpreter.h"
23 : #include "mal_exception.h"
24 :
25 : static str
26 0 : TRACERflush_buffer(void *ret)
27 : {
28 0 : (void) ret;
29 0 : GDKtracer_flush_buffer();
30 0 : return MAL_SUCCEED;
31 : }
32 :
33 :
34 : static str
35 116 : TRACERset_component_level(void *ret, const char *const *comp_id, const char *const *lvl_id)
36 : {
37 116 : (void) ret;
38 116 : if (GDKtracer_set_component_level(*comp_id, *lvl_id) != GDK_SUCCEED)
39 0 : throw(MAL, "logging.setcomplevel", ILLEGAL_ARGUMENT);
40 :
41 : return MAL_SUCCEED;
42 : }
43 :
44 :
45 : static str
46 0 : TRACERreset_component_level(void *ret, const char *const *comp_id)
47 : {
48 0 : (void) ret;
49 0 : if (GDKtracer_reset_component_level(*comp_id) != GDK_SUCCEED)
50 0 : throw(MAL, "logging.resetcomplevel", ILLEGAL_ARGUMENT "\n");
51 :
52 : return MAL_SUCCEED;
53 : }
54 :
55 :
56 : static str
57 0 : TRACERset_layer_level(void *ret, const char *const *layer_id, const char *const *lvl_id)
58 : {
59 0 : (void) ret;
60 0 : if (GDKtracer_set_layer_level(*layer_id, *lvl_id) != GDK_SUCCEED)
61 0 : throw(MAL, "logging.setlayerlevel", ILLEGAL_ARGUMENT "\n");
62 :
63 : return MAL_SUCCEED;
64 : }
65 :
66 :
67 : static str
68 0 : TRACERreset_layer_level(void *ret, const char *const *layer_id)
69 : {
70 0 : (void) ret;
71 0 : if (GDKtracer_reset_layer_level(*layer_id) != GDK_SUCCEED)
72 0 : throw(MAL, "logging.resetlayerlevel", ILLEGAL_ARGUMENT "\n");
73 :
74 : return MAL_SUCCEED;
75 : }
76 :
77 :
78 : static str
79 0 : TRACERset_flush_level(void *ret, const char *const *lvl_id)
80 : {
81 0 : (void) ret;
82 0 : if (GDKtracer_set_flush_level(*lvl_id) != GDK_SUCCEED)
83 0 : throw(MAL, "logging.setflushlevel", ILLEGAL_ARGUMENT "\n");
84 :
85 : return MAL_SUCCEED;
86 : }
87 :
88 :
89 : static str
90 0 : TRACERreset_flush_level(void *ret)
91 : {
92 0 : (void) ret;
93 0 : if (GDKtracer_reset_flush_level() != GDK_SUCCEED)
94 0 : throw(MAL, "logging.resetflushlevel", _OPERATION_FAILED "\n");
95 :
96 : return MAL_SUCCEED;
97 : }
98 :
99 :
100 : static str
101 0 : TRACERset_adapter(void *ret, const char *const *adapter_id)
102 : {
103 0 : (void) ret;
104 0 : if (GDKtracer_set_adapter(*adapter_id) != GDK_SUCCEED)
105 0 : throw(MAL, "logging.setadapter", ILLEGAL_ARGUMENT "\n");
106 :
107 : return MAL_SUCCEED;
108 : }
109 :
110 :
111 : static str
112 0 : TRACERreset_adapter(void *ret)
113 : {
114 0 : (void) ret;
115 0 : if (GDKtracer_reset_adapter() != GDK_SUCCEED)
116 0 : throw(MAL, "logging.resetadapter", _OPERATION_FAILED "\n");
117 :
118 : return MAL_SUCCEED;
119 : }
120 :
121 :
122 : static str
123 0 : TRACERcomp_info(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
124 : {
125 0 : (void) cntxt;
126 0 : (void) mb;
127 :
128 0 : BAT *id, *component, *log_level;
129 0 : bat *i = getArgReference_bat(stk, pci, 0);
130 0 : bat *c = getArgReference_bat(stk, pci, 1);
131 0 : bat *l = getArgReference_bat(stk, pci, 2);
132 :
133 0 : id = COLnew(0, TYPE_int, (BUN) COMPONENTS_COUNT, TRANSIENT);
134 0 : component = COLnew(0, TYPE_str, (BUN) COMPONENTS_COUNT, TRANSIENT);
135 0 : log_level = COLnew(0, TYPE_str, (BUN) COMPONENTS_COUNT, TRANSIENT);
136 :
137 0 : if (id == NULL || component == NULL || log_level == NULL) {
138 0 : BBPreclaim(id);
139 0 : BBPreclaim(component);
140 0 : BBPreclaim(log_level);
141 0 : throw(MAL, "logging.compinfo", SQLSTATE(HY013) MAL_MALLOC_FAIL);
142 : }
143 : // Fill the BATs
144 0 : if (GDKtracer_fill_comp_info(id, component, log_level) != GDK_SUCCEED) {
145 0 : BBPunfix(id->batCacheid);
146 0 : BBPunfix(component->batCacheid);
147 0 : BBPunfix(log_level->batCacheid);
148 0 : throw(MAL, "logging.compinfo", GDK_EXCEPTION);
149 : }
150 :
151 0 : *i = id->batCacheid;
152 0 : BBPkeepref(id);
153 0 : *c = component->batCacheid;
154 0 : BBPkeepref(component);
155 0 : *l = log_level->batCacheid;
156 0 : BBPkeepref(log_level);
157 0 : return MAL_SUCCEED;
158 : }
159 :
160 : #include "mel.h"
161 : mel_func tracer_init_funcs[] = {
162 : command("logging", "flush", TRACERflush_buffer, true, "Flush the buffer", args(1,1, arg("",void))),
163 : command("logging", "setcomplevel", TRACERset_component_level, true, "Sets the log level for a specific component", args(1,3, arg("",void),arg("comp",str),arg("lvl",str))),
164 : command("logging", "resetcomplevel", TRACERreset_component_level, true, "Resets the log level for a specific component back to the default", args(1,2, arg("",void),arg("comp",str))),
165 : command("logging", "setlayerlevel", TRACERset_layer_level, true, "Sets the log level for a specific layer", args(1,3, arg("",void),arg("layer",str),arg("lvl",str))),
166 : command("logging", "resetlayerlevel", TRACERreset_layer_level, true, "Resets the log level for a specific layer back to the default", args(1,2, arg("",void),arg("layer",str))),
167 : command("logging", "setflushlevel", TRACERset_flush_level, true, "Sets the flush level", args(1,2, arg("",void),arg("lvl",str))),
168 : command("logging", "resetflushlevel", TRACERreset_flush_level, true, "Resets the flush level back to the default", args(1,1, arg("",void))),
169 : command("logging", "setadapter", TRACERset_adapter, true, "Sets the adapter", args(1,2, arg("",void),arg("adapter",str))),
170 : command("logging", "resetadapter", TRACERreset_adapter, true, "Resets the adapter back to the default", args(1,1, arg("",void))),
171 : pattern("logging", "compinfo", TRACERcomp_info, false, "Returns in the form of a SQL result-set all the components along with their ID\nand the their current logging level being set", args(3,3, batarg("id",int),batarg("component",str),batarg("log_level",str))),
172 : { .imp=NULL }
173 : };
174 : #include "mal_import.h"
175 : #ifdef _MSC_VER
176 : #undef read
177 : #pragma section(".CRT$XCU",read)
178 : #endif
179 323 : LIB_STARTUP_FUNC(init_tracer_mal)
180 323 : { mal_module("tracer", NULL, tracer_init_funcs); }
|