Skip to content

Commit e2ec41f

Browse files
committed
PCBC-410: consolidate LCB and extension logging
Change-Id: Ia116a628efc977072d1c5abf600f1c181a847833 Reviewed-on: http://review.couchbase.org/67006 Tested-by: buildbot <build@couchbase.com> Reviewed-by: Sergey Avseyev <sergey.avseyev@gmail.com>
1 parent 8f74e22 commit e2ec41f

21 files changed

+257
-44
lines changed

.clang-format

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# -*- mode: yaml; -*-
2+
3+
BasedOnStyle: LLVM
4+
IndentWidth: 4
5+
BreakBeforeBraces: Linux

bucket.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
#include "transcoding.h"
2929
#include "opcookie.h"
3030

31+
extern struct pcbc_logger_st pcbc_logger;
32+
#define LOGARGS(instance, lvl) LCB_LOG_##lvl, instance, "pcbc/bucket", __FILE__, __LINE__
33+
3134
zap_class_entry bucket_class;
3235
zend_class_entry *bucket_ce;
3336
char *pcbc_client_string = "PCBC/"PHP_COUCHBASE_VERSION;
@@ -176,6 +179,15 @@ PHP_METHOD(Bucket, __construct)
176179
throw_lcb_exception(err);
177180
RETURN_NULL();
178181
}
182+
pcbc_log(LOGARGS(instance, INFO), "New lcb_t instance has been initialized");
183+
err = lcb_cntl(instance, LCB_CNTL_SET, LCB_CNTL_LOGGER, &pcbc_logger);
184+
if (err != LCB_SUCCESS) {
185+
efree(connkey);
186+
efree(name);
187+
throw_lcb_exception(err);
188+
RETURN_NULL();
189+
}
190+
179191
lcb_cntl(instance, LCB_CNTL_SET, LCB_CNTL_CLIENT_STRING, pcbc_client_string);
180192

181193
lcb_install_callback3(instance, LCB_CALLBACK_GET, get_callback);
@@ -228,12 +240,14 @@ PHP_METHOD(Bucket, __construct)
228240
PCBCG(first_bconn) = conn;
229241
PCBCG(last_bconn) = conn;
230242
}
243+
pcbc_log(LOGARGS(instance, INFO), "lcb_t instance has been connected");
231244
} else {
232245
if (dsn) efree(dsn);
233246
if (name) efree(name);
234247
if (password) efree(password);
235248

236249
data->conn = conn_iter;
250+
pcbc_log(LOGARGS(data->conn->lcb, INFO), "lcb_t instance has been fetched from cache");
237251
}
238252

239253
efree(connkey);

bucket.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ typedef struct bucket_object {
5252

5353
#define PCBC_PHP_THISOBJ() zap_fetch_this(bucket_object)
5454

55-
#define pcbc_assert_number_of_commands(cmd, nscheduled, ntotal) \
55+
#define pcbc_assert_number_of_commands(lcb, cmd, nscheduled, ntotal) \
5656
if (nscheduled != ntotal) { \
57-
php_error_docref(NULL TSRMLS_CC, E_WARNING, \
58-
"Failed to schedule %s commands (%d out of %d sent)", \
59-
cmd, nscheduled, ntotal); \
57+
pcbc_log(LOGARGS(lcb, ERROR), \
58+
"Failed to schedule %s commands (%d out of %d sent)", \
59+
cmd, nscheduled, ntotal); \
6060
}
6161

6262
zval* bop_get_return_doc(zval *return_value, zapval *key, int is_mapped);

cbft.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "transcoding.h"
2828
#include "opcookie.h"
2929

30+
#define LOGARGS(instance, lvl) LCB_LOG_##lvl, instance, "pcbc/cbft", __FILE__, __LINE__
31+
3032
typedef struct {
3133
opcookie_res header;
3234
lcb_U16 rflags;
@@ -40,11 +42,8 @@ static void ftsrow_callback(lcb_t instance, int ignoreme, const lcb_RESPFTS *res
4042

4143
result->header.err = resp->rc;
4244
if (result->header.err == LCB_HTTP_ERROR) {
43-
php_error_docref(NULL TSRMLS_CC, E_WARNING,
44-
"failed to search in index. %d: %.*s",
45-
(int)resp->htresp->htstatus,
46-
(int)resp->nrow,
47-
(char *)resp->row);
45+
pcbc_log(LOGARGS(instance, ERROR), "Failed to search in index. %d: %.*s",
46+
(int)resp->htresp->htstatus, (int)resp->nrow, (char *)resp->row);
4847
}
4948
result->rflags = resp->rflags;
5049
zapval_alloc_stringl(result->row, resp->row, resp->nrow);

config.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ if test "$PHP_COUCHBASE" != "no"; then
3737

3838
PHP_SUBST(COUCHBASE_SHARED_LIBADD)
3939

40-
COUCHBASE_FILES="bucket.c cas.c cluster.c couchbase.c docfrag.c exception.c get.c unlock.c metadoc.c opcookie.c paramparser.c transcoding.c touch.c remove.c subdoc.c store.c n1ql.c http.c counter.c durability.c n1ix_spec.c n1ix_list.c n1ix_create.c n1ix_drop.c cbft.c token.c"
40+
COUCHBASE_FILES="bucket.c cas.c cluster.c couchbase.c docfrag.c exception.c get.c unlock.c metadoc.c opcookie.c paramparser.c transcoding.c touch.c remove.c subdoc.c store.c n1ql.c http.c counter.c durability.c n1ix_spec.c n1ix_list.c n1ix_create.c n1ix_drop.c cbft.c token.c log.c"
4141

4242
if test "$PHP_SYSTEM_FASTLZ" != "no"; then
4343
AC_CHECK_HEADERS([fastlz.h])

config.w32

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ if (PHP_COUCHBASE != "no") {
1313
"exception.c " +
1414
"get.c " +
1515
"http.c " +
16+
"log.c " +
1617
"metadoc.c " +
1718
"n1ix_create.c " +
1819
"n1ix_drop.c " +

couchbase.c

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,53 @@
3434
#include <zlib.h>
3535
#endif
3636

37+
#define LOGARGS(lvl) LCB_LOG_##lvl, NULL, "pcbc/ext", __FILE__, __LINE__
38+
3739
typedef unsigned char uint8_t;
3840
typedef unsigned int uint32_t;
3941

4042
ZEND_DECLARE_MODULE_GLOBALS(couchbase)
4143

44+
extern struct pcbc_logger_st pcbc_logger;
45+
46+
static PHP_INI_MH(OnUpdateLogLevel)
47+
{
48+
const char *str_val =
49+
#if PHP_VERSION_ID >= 70000
50+
ZSTR_VAL(new_value);
51+
#else
52+
new_value;
53+
#endif
54+
if (!new_value) {
55+
pcbc_logger.minlevel = LCB_LOG_WARN;
56+
} else if (!strcmp(new_value, "TRACE") || !strcmp(new_value, "TRAC")) {
57+
pcbc_logger.minlevel = LCB_LOG_TRACE;
58+
} else if (!strcmp(new_value, "DEBUG") || !strcmp(new_value, "DEBG")) {
59+
pcbc_logger.minlevel = LCB_LOG_DEBUG;
60+
} else if (!strcmp(new_value, "INFO")) {
61+
pcbc_logger.minlevel = LCB_LOG_INFO;
62+
} else if (!strcmp(new_value, "WARN")) {
63+
pcbc_logger.minlevel = LCB_LOG_WARN;
64+
} else if (!strcmp(new_value, "ERROR") || !strcmp(new_value, "EROR")) {
65+
pcbc_logger.minlevel = LCB_LOG_ERROR;
66+
} else if (!strcmp(new_value, "FATAL") || !strcmp(new_value, "FATL")) {
67+
pcbc_logger.minlevel = LCB_LOG_FATAL;
68+
} else {
69+
return FAILURE;
70+
}
71+
72+
#if PHP_VERSION_ID >= 70000
73+
return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
74+
#else
75+
return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
76+
#endif
77+
}
78+
79+
PHP_INI_BEGIN()
80+
STD_PHP_INI_ENTRY(PCBC_INIENT_LOG_LEVEL, PCBC_INIDFL_LOG_LEVEL, PHP_INI_ALL,
81+
OnUpdateLogLevel, log_level, zend_couchbase_globals, couchbase_globals)
82+
PHP_INI_END()
83+
4284
#define PCBC_LONG_CONSTANT(key, val) \
4385
REGISTER_LONG_CONSTANT("COUCHBASE_"key, val, CONST_CS | CONST_PERSISTENT)
4486
#define PCBC_REGISTER_CONST(c) \
@@ -55,6 +97,7 @@ static void php_extname_init_globals(zend_couchbase_globals *couchbase_globals)
5597
PHP_MINIT_FUNCTION(couchbase)
5698
{
5799
ZEND_INIT_MODULE_GLOBALS(couchbase, php_extname_init_globals, NULL);
100+
REGISTER_INI_ENTRIES();
58101

59102
couchbase_init_exceptions(INIT_FUNC_ARGS_PASSTHRU);
60103
couchbase_init_metadoc(INIT_FUNC_ARGS_PASSTHRU);
@@ -132,6 +175,7 @@ PHP_MINIT_FUNCTION(couchbase)
132175
PHP_MSHUTDOWN_FUNCTION(couchbase)
133176
{
134177
couchbase_shutdown_bucket(SHUTDOWN_FUNC_ARGS_PASSTHRU);
178+
UNREGISTER_INI_ENTRIES();
135179

136180
return SUCCESS;
137181
}
@@ -151,7 +195,7 @@ PHP_RINIT_FUNCTION(couchbase)
151195
pcbc_stub_data *this_stub = &PCBC_PHP_CODESTR[stub_idx];
152196
int retval = zend_eval_string((char*)this_stub->data, NULL, (char*)this_stub->filename TSRMLS_CC);
153197
if (retval != SUCCESS) {
154-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to inject Couchbase stub: %s.", (char *)this_stub->filename);
198+
pcbc_log(LOGARGS(ERROR), "Failed to inject Couchbase stub: %s.", (char *)this_stub->filename);
155199
return FAILURE;
156200
}
157201
}
@@ -273,6 +317,7 @@ static PHP_MINFO_FUNCTION(couchbase)
273317
php_info_print_table_row(2, "libcouchbase runtime version", buf);
274318
php_info_print_table_row(2, "libcouchbase headers version", LCB_VERSION_STRING " (git: " LCB_VERSION_CHANGESET ")");
275319
php_info_print_table_end();
320+
DISPLAY_INI_ENTRIES();
276321
}
277322

278323
static zend_function_entry couchbase_functions[] = {

couchbase.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <php.h>
3030
#include <zend_exceptions.h>
3131
#include "php_couchbase.h"
32+
#include "log.h"
3233

3334
enum pcbc_constants {
3435
PERSISTTO_ONE = 1,
@@ -51,6 +52,7 @@ ZEND_BEGIN_MODULE_GLOBALS(couchbase)
5152
// Linked list of bucket connections
5253
pcbc_lcb *first_bconn;
5354
pcbc_lcb *last_bconn;
55+
char *log_level;
5456
ZEND_END_MODULE_GLOBALS(couchbase)
5557
ZEND_EXTERN_MODULE_GLOBALS(couchbase)
5658

@@ -66,4 +68,7 @@ void couchbase_init_bucket(INIT_FUNC_ARGS);
6668

6769
void couchbase_shutdown_bucket(SHUTDOWN_FUNC_ARGS);
6870

71+
#define PCBC_INIENT_LOG_LEVEL "couchbase.log_level"
72+
#define PCBC_INIDFL_LOG_LEVEL "WARN"
73+
6974
#endif /* COUCHBASE_H_ */

counter.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include "transcoding.h"
2929
#include "opcookie.h"
3030

31+
#define LOGARGS(instance, lvl) LCB_LOG_##lvl, instance, "pcbc/counter", __FILE__, __LINE__
32+
3133
typedef struct {
3234
opcookie_res header;
3335
zapval key;
@@ -147,7 +149,7 @@ PHP_METHOD(Bucket, counter)
147149
}
148150
nscheduled++;
149151
}
150-
pcbc_assert_number_of_commands("counter", nscheduled, ncmds);
152+
pcbc_assert_number_of_commands(data->conn->lcb, "counter", nscheduled, ncmds);
151153

152154
if (nscheduled) {
153155
lcb_wait(data->conn->lcb);

get.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include "docfrag.h"
2727
#include "transcoding.h"
2828

29+
#define LOGARGS(instance, lvl) LCB_LOG_##lvl, instance, "pcbc/get", __FILE__, __LINE__
30+
2931
typedef struct {
3032
opcookie_res header;
3133
zapval key;
@@ -141,7 +143,7 @@ PHP_METHOD(Bucket, get)
141143

142144
nscheduled++;
143145
}
144-
pcbc_assert_number_of_commands("get", nscheduled, ncmds);
146+
pcbc_assert_number_of_commands(data->conn->lcb, "get", nscheduled, ncmds);
145147

146148
if (nscheduled) {
147149
lcb_wait(data->conn->lcb);
@@ -205,7 +207,7 @@ PHP_METHOD(Bucket, getFromReplica)
205207
}
206208
nscheduled++;
207209
}
208-
pcbc_assert_number_of_commands("get_from_replica", nscheduled, ncmds);
210+
pcbc_assert_number_of_commands(data->conn->lcb, "get_from_replica", nscheduled, ncmds);
209211

210212
if (nscheduled) {
211213
lcb_wait(data->conn->lcb);

log.c

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* Copyright 2016 Couchbase, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <libcouchbase/couchbase.h>
18+
#include <php.h>
19+
20+
#include "zap.h"
21+
#include "log.h"
22+
23+
static const char *level_to_string(int severity)
24+
{
25+
switch (severity) {
26+
case LCB_LOG_TRACE:
27+
return "TRAC";
28+
case LCB_LOG_DEBUG:
29+
return "DEBG";
30+
case LCB_LOG_INFO:
31+
return "INFO";
32+
case LCB_LOG_WARN:
33+
return "WARN";
34+
case LCB_LOG_ERROR:
35+
return "EROR";
36+
case LCB_LOG_FATAL:
37+
return "FATL";
38+
default:
39+
return "";
40+
}
41+
}
42+
43+
static void log_handler(struct lcb_logprocs_st *procs, unsigned int iid,
44+
const char *subsys, int severity, const char *srcfile,
45+
int srcline, const char *fmt, va_list ap)
46+
{
47+
struct pcbc_logger_st *logger = (struct pcbc_logger_st *)procs;
48+
char *buf = NULL;
49+
char *msg = NULL;
50+
TSRMLS_FETCH();
51+
52+
if (severity < logger->minlevel) {
53+
return;
54+
}
55+
56+
vspprintf(&msg, 0, fmt, ap);
57+
spprintf(&buf, 0, "[cb,%s] (%s L:%d I:%d) %s", level_to_string(severity),
58+
subsys, srcline, iid, msg);
59+
efree(msg);
60+
php_log_err(buf TSRMLS_CC);
61+
efree(buf);
62+
}
63+
64+
struct pcbc_logger_st pcbc_logger = {
65+
{0 /* version */, {{log_handler} /* v1 */} /*v*/},
66+
/** Minimum severity */
67+
LCB_LOG_INFO};
68+
69+
void pcbc_log(int severity, lcb_t instance, const char *subsys,
70+
const char *srcfile, int srcline, const char *fmt, ...)
71+
{
72+
va_list ap;
73+
char *msg = NULL;
74+
char *buf = NULL;
75+
TSRMLS_FETCH();
76+
77+
if (severity < pcbc_logger.minlevel) {
78+
return;
79+
}
80+
81+
va_start(ap, fmt);
82+
vspprintf(&msg, 0, fmt, ap);
83+
va_end(ap);
84+
if (instance) {
85+
spprintf(&buf, 0, "[cb,%s] (%s L:%d) %s. I=%p",
86+
level_to_string(severity), subsys, srcline, msg,
87+
(void *)instance);
88+
} else {
89+
spprintf(&buf, 0, "[cb,%s] (%s L:%d) %s", level_to_string(severity),
90+
subsys, srcline, msg);
91+
}
92+
efree(msg);
93+
94+
php_log_err(buf TSRMLS_CC);
95+
efree(buf);
96+
}

log.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright 2016 Couchbase, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef LOG_H_
18+
#define LOG_H_
19+
20+
#include <libcouchbase/couchbase.h>
21+
#include <php.h>
22+
23+
struct pcbc_logger_st {
24+
struct lcb_logprocs_st base;
25+
int minlevel;
26+
};
27+
28+
void pcbc_log(int severity, lcb_t instance, const char *subsys,
29+
const char *srcfile, int srcline, const char *fmt, ...);
30+
31+
#endif // LOG_H_

0 commit comments

Comments
 (0)