-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtplog.go
459 lines (368 loc) · 13.3 KB
/
tplog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/**
* @brief TPLOG - text logging and debuging API provided by Enduro/X
*
* @file tplog.go
*/
/* -----------------------------------------------------------------------------
* Enduro/X Middleware Platform for Distributed Transaction Processing
* Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
* Copyright (C) 2017-2019, Mavimax, Ltd. All Rights Reserved.
* This software is released under one of the following licenses:
* LGPL or Mavimax's license for commercial use.
* See LICENSE file for full text.
*
* C (as designed by Dennis Ritchie and later authors) language code is licensed
* under Enduro/X Modified GNU Affero General Public License, version 3.
* See LICENSE_C file for full text.
* -----------------------------------------------------------------------------
* LGPL license:
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License, version 3 as published
* by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License, version 3
* for more details.
*
* You should have received a copy of the Lesser General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* -----------------------------------------------------------------------------
* A commercial use license is available from Mavimax, Ltd
* -----------------------------------------------------------------------------
*/
package atmi
/*
#cgo pkg-config: atmisrvinteg
#include <ndebug.h>
#include <ondebug.h>
#include <xatmi.h>
#include <oatmi.h>
#include <string.h>
#include <stdlib.h>
#include <ubf.h>
#include <oubf.h>
#include <userlog.h>
*/
import "C"
import (
"fmt"
"runtime"
"unsafe"
)
const (
DETAIL_MODE = "detailed" //Slower, not for production !
)
////////////////////////////////////////////////////////////////////////////////
// Logging sub-system tplog*
////////////////////////////////////////////////////////////////////////////////
//Print the byte array buffer to Enduro/X logger (see tplogdump(3) manpage)
//@param lev Logging level (see LOG_* constants)
//@param comment Title of the buffer dump
//@param ptr Pointer to buffer for dump
//@param dumplen Length of the bytes to dump
//@return atmiError (in case if invalid length we have for ptr and dumplen)
func (ac *ATMICtx) TpLogDump(lev int, comment string, ptr []byte, dumplen int) ATMIError {
//NOTE: Checking level here - assume C call in cheaper than
//String formatting and memory copy in Go!
if lev <= int(C.debug_get_tp_level()) {
c_comment := C.CString(comment)
defer C.free(unsafe.Pointer(c_comment))
l1 := len(ptr)
/* Check the buffer sizes (both must be equal or larger then len) */
if l1 < dumplen {
return NewCustomATMIError(TPEINVAL,
fmt.Sprintf("ptr len is %d but must be >= %d (len param)",
l1, dumplen))
}
c_ptr := C.malloc(C.size_t(l1))
defer C.free(c_ptr)
//Copy stuff to C memory (ptr1)
for i := 0; i < l1; i++ {
*(*C.char)(unsafe.Pointer(uintptr(c_ptr) + uintptr(i))) = C.char(ptr[i])
}
C.Otplogdump(&ac.c_ctx, C.int(lev), c_comment, c_ptr, C.int(dumplen))
}
ac.nop()
return nil
}
//Function compares to byte array buffers and prints the differences to Enduro/X logger
//(see tplogdumpdiff(3) manpage)
//@param lev Logging level (see LOG_* constants)
//@param comment Title of the buffer diff
//@param ptr1 Pointer to buffer1 for compare
//@param ptr2 Pointer to buffer2 for compare
//@param difflen Length of the bytes to compare
//@return atmiError (in case if invalid length we have for ptr1/ptr2 and difflen)
func (ac *ATMICtx) TpLogDumpDiff(lev int, comment string, ptr1 []byte, ptr2 []byte, difflen int) ATMIError {
if lev <= int(C.debug_get_tp_level()) {
c_comment := C.CString(comment)
defer C.free(unsafe.Pointer(c_comment))
l1 := len(ptr1)
l2 := len(ptr2)
/* Check the buffer sizes (both must be equal or larger then len) */
if l1 < difflen {
return NewCustomATMIError(TPEINVAL,
fmt.Sprintf("ptr1 len is %d but must be >= %d (len param)",
l1, difflen))
}
if l2 < difflen {
return NewCustomATMIError(TPEINVAL,
fmt.Sprintf("ptr2 len is %d but must be >= %d (len param)",
l2, difflen))
}
c_ptr1 := C.malloc(C.size_t(l1))
defer C.free(c_ptr1)
//Copy stuff to C memory (ptr1)
for i := 0; i < l1; i++ {
*(*C.char)(unsafe.Pointer(uintptr(c_ptr1) + uintptr(i))) = C.char(ptr1[i])
}
c_ptr2 := C.malloc(C.size_t(l2))
defer C.free(c_ptr2)
//Copy stuff to C memory (ptr1)
for i := 0; i < l2; i++ {
*(*C.char)(unsafe.Pointer(uintptr(c_ptr2) + uintptr(i))) = C.char(ptr2[i])
}
C.Otplogdumpdiff(&ac.c_ctx, C.int(lev), c_comment, c_ptr1, c_ptr2, C.int(difflen))
}
ac.nop()
return nil
}
//Log the message to Enduro/X loggers (see tplog(3) manpage)
//This version does not check the debug level (kind of internal one)
//@param lev Logging level
//@param a arguemnts for sprintf
//@param format Format string for loggers
func (ac *ATMICtx) tpLog(lev int, format string, a ...interface{}) {
msg := fmt.Sprintf(format, a...)
c_msg := C.CString(msg)
defer C.free(unsafe.Pointer(c_msg))
if ac.TpLogGetIflags() != DETAIL_MODE {
C.Otplog(&ac.c_ctx, C.int(lev), c_msg)
} else {
//Get the stack and give file name and line
_, file, line, _ := runtime.Caller(2)
c_file := C.CString(file)
defer C.free(unsafe.Pointer(c_file))
C.Otplogex(&ac.c_ctx, C.int(lev), c_file, C.long(line), c_msg)
}
ac.nop()
}
//Log the message to Enduro/X loggers (see tplog(3) manpage)
//@param lev Logging level
//@param a arguemnts for sprintf
//@param format Format string for loggers
func (ac *ATMICtx) TpLog(lev int, format string, a ...interface{}) {
if lev <= int(C.debug_get_tp_level()) {
msg := fmt.Sprintf(format, a...)
c_msg := C.CString(msg)
defer C.free(unsafe.Pointer(c_msg))
C.Otplog(&ac.c_ctx, C.int(lev), c_msg)
}
ac.nop()
}
//Log the message to Enduro/X loggers (see tplog(3) manpage), internal ndrx only
//@param lev Logging level
//@param a arguemnts for sprintf
//@param format Format string for loggers
func (ac *ATMICtx) ndrxLog(lev int, format string, a ...interface{}) {
if lev <= int(C.debug_get_ndrx_level()) {
msg := fmt.Sprintf(format, a...)
c_msg := C.CString(msg)
defer C.free(unsafe.Pointer(c_msg))
C.Ondrxlog(&ac.c_ctx, C.int(lev), c_msg)
}
ac.nop()
}
//Log the message to Enduro/X loggers (see tplog(3) manpage), internal ubf only
//@param lev Logging level
//@param a arguemnts for sprintf
//@param format Format string for loggers
func (ac *ATMICtx) ubfLog(lev int, format string, a ...interface{}) {
if lev <= int(C.debug_get_ubf_level()) {
msg := fmt.Sprintf(format, a...)
c_msg := C.CString(msg)
defer C.free(unsafe.Pointer(c_msg))
C.Oubflog(&ac.c_ctx, C.int(lev), c_msg)
}
ac.nop()
}
//Log the message to Enduro/X loggers (see tplog(3) manpage)
//Debug level wrapper
//@param a arguemnts for sprintf
//@param format Format string for loggers
func (ac *ATMICtx) TpLogDebug(format string, a ...interface{}) {
if LOG_DEBUG <= int(C.debug_get_tp_level()) {
ac.tpLog(LOG_DEBUG, format, a...)
}
}
//Log the message to Enduro/X loggers (see tplog(3) manpage)
//Info level wrapper
//@param a arguemnts for sprintf
//@param format Format string for loggers
func (ac *ATMICtx) TpLogInfo(format string, a ...interface{}) {
if LOG_INFO <= int(C.debug_get_tp_level()) {
ac.tpLog(LOG_INFO, format, a...)
}
}
//Log the message to Enduro/X loggers (see tplog(3) manpage)
//Warning level wrapper
//@param a arguemnts for sprintf
//@param format Format string for loggers
func (ac *ATMICtx) TpLogWarn(format string, a ...interface{}) {
if LOG_WARN <= int(C.debug_get_tp_level()) {
ac.tpLog(LOG_WARN, format, a...)
}
}
//Log the message to Enduro/X loggers (see tplog(3) manpage)
//Error level wrapper
//@param a arguemnts for sprintf
//@param format Format string for loggers
func (ac *ATMICtx) TpLogError(format string, a ...interface{}) {
if LOG_WARN <= int(C.debug_get_tp_level()) {
ac.tpLog(LOG_ERROR, format, a...)
}
}
//Log the message to Enduro/X loggers (see tplog(3) manpage)
//Fatal/Always level wrapper
//@param a arguemnts for sprintf
//@param format Format string for loggers
func (ac *ATMICtx) TpLogAlways(format string, a ...interface{}) {
if LOG_ALWAYS <= int(C.debug_get_tp_level()) {
ac.tpLog(LOG_ALWAYS, format, a...)
}
}
//Log the message to Enduro/X loggers (see tplog(3) manpage)
//Fatal/Always level wrapper
//@param a arguemnts for sprintf
//@param format Format string for loggers
func (ac *ATMICtx) TpLogFatal(format string, a ...interface{}) {
if LOG_ALWAYS <= int(C.debug_get_tp_level()) {
ac.tpLog(LOG_ALWAYS, format, a...)
}
}
//Return request logging file (if there is one currenlty in use)
// (see tploggetreqfile(3) manpage)
//@return Status (request logger open or not), full path to request file
func (ac *ATMICtx) TpLogGetReqFile() (bool, string) {
var status bool
var reqfile string
c_reqfile := C.malloc(C.PATH_MAX)
c_reqfile_ptr := (*C.char)(unsafe.Pointer(c_reqfile))
defer C.free(c_reqfile)
//Bug #825 fix
if 1 == C.Otploggetreqfile(&ac.c_ctx, c_reqfile_ptr, C.PATH_MAX) {
status = true
reqfile = C.GoString(c_reqfile_ptr)
} else {
status = false
}
return status, reqfile
}
//Configure Enduro/X logger (see tplogconfig(3) manpage)
//@param logger is bitwise 'ored' (see LOG_FACILITY_*)
//@param lev is optional (if not set: -1), log level to be assigned to facilites
//@param debug_string optional Enduro/X debug string (see ndrxdebug.conf(5) manpage)
//@param new_file optional (if not set - empty string) logging output file, overrides debug_string file tag
//@return NSTDError - standard library error
func (ac *ATMICtx) TpLogConfig(logger int, lev int, debug_string string, module string, new_file string) NSTDError {
var err NSTDError
c_debug_string := C.CString(debug_string)
defer C.free(unsafe.Pointer(c_debug_string))
c_module := C.CString(module)
defer C.free(unsafe.Pointer(c_module))
c_new_file := C.CString(new_file)
defer C.free(unsafe.Pointer(c_new_file))
if SUCCEED != C.Otplogconfig(&ac.c_ctx, C.int(logger), C.int(lev), c_debug_string,
c_module, c_new_file) {
err = ac.NewNstdError()
}
return err
}
//Close request logger (see tplogclosereqfile(3) manpage)
func (ac *ATMICtx) TpLogCloseReqFile() {
C.Otplogclosereqfile(&ac.c_ctx)
}
//Close request logger (see tplogclosethread(3) manpage)
func (ac *ATMICtx) TpLogCloseThread() {
C.Otplogclosethread(&ac.c_ctx)
}
//Set request logging file, direct version (see tplogsetreqfile_direct(3) manpage)
//Which does operate with thread local storage
//If fails to open request logging file, it will
//automatically fall-back to stderr.
//@param filename Set file name to perform logging to
func (ac *ATMICtx) TpLogSetReqFileDirect(filename string) {
c_filename := C.CString(filename)
defer C.free(unsafe.Pointer(c_filename))
C.Otplogsetreqfile_direct(&ac.c_ctx, c_filename)
ac.nop()
}
//Set request file to log to (see tplogsetreqfile(3) manpage)
//@param data pointer to XATMI buffer (must be UBF, others will cause error), optional
//@param filename field name to set (this goes to UBF buffer too, if set), optional
//@param filesvc XATMI service name to call for requesting the new request file name, optional
//@return ATMI error
func (ac *ATMICtx) TpLogSetReqFile(data TypedBuffer, filename string, filesvc string) ATMIError {
var err ATMIError
c_filename := C.CString(filename)
defer C.free(unsafe.Pointer(c_filename))
c_filesvc := C.CString(filesvc)
defer C.free(unsafe.Pointer(c_filesvc))
buf := data.GetBuf()
if SUCCEED != C.Otplogsetreqfile(&ac.c_ctx, &buf.C_ptr, c_filename, c_filesvc) {
err = ac.NewATMIError()
}
ac.nop()
return err
}
//Get the request file name from UBF buffer (see tploggetbufreqfile(3) manpage)
//@param data XATMI buffer (must be UBF)
//@return file name, ATMI error
func (ac *ATMICtx) TpLogGetBufReqFile(data TypedBuffer) (string, ATMIError) {
var err ATMIError
var reqfile string
c_reqfile := C.malloc(C.PATH_MAX)
c_reqfile_ptr := (*C.char)(unsafe.Pointer(c_reqfile))
defer C.free(c_reqfile)
buf := data.GetBuf()
if SUCCEED != C.Otploggetbufreqfile(&ac.c_ctx, buf.C_ptr, c_reqfile_ptr, C.PATH_MAX) {
err = ac.NewATMIError()
} else {
reqfile = C.GoString(c_reqfile_ptr)
}
ac.nop()
return reqfile, err
}
//Delete request file from UBF buffer (see tplogdelbufreqfile(3) manpage)
//@param data XATMI buffer, must be UBF type
//@return ATMI error
func (ac *ATMICtx) TpLogDelBufReqFile(data TypedBuffer) ATMIError {
var err ATMIError
buf := data.GetBuf()
if SUCCEED != C.Otplogdelbufreqfile(&ac.c_ctx, buf.C_ptr) {
err = ac.NewATMIError()
}
buf.nop()
return err
}
//Return integration flags
//Well we will run it in cached mode...
func (ac *ATMICtx) TpLogGetIflags() string {
return C.GoString(C.tploggetiflags())
}
//Do the user logging. This prints the message to ULOG. Suitable for system wide
//critical message notifications
//@param format format string
//@param a list of data fields for format string
func (ac *ATMICtx) UserLog(format string, a ...interface{}) {
msg := fmt.Sprintf(format, a...)
c_msg := C.CString(msg)
defer C.free(unsafe.Pointer(c_msg))
C.userlog_const(c_msg)
}
/* vim: set ts=4 sw=4 et smartindent: */