Debian Codegraph @

lib/logger.c

raw symbols SCIP
1 /*
2 Copyright (C) 2009-2017 Brazil
3 Copyright (C) 2018-2024 Sutou Kouhei <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "grn_logger.h"
21 #include "grn_ctx.h"
22 #include "grn_ctx_impl.h"
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <sys/stat.h>
27
28 #ifdef WIN32
29 # include <share.h>
30 #else /* WIN32 */
31 # include <sys/file.h>
32 #endif /* WIN32 */
33
34 static const char *log_level_names[] = {
35 "none",
36 "emergency",
37 "alert",
38 "critical",
39 "error",
40 "warning",
41 "notice",
42 "info",
43 "debug",
44 "dump"
45 };
46
47 #define GRN_LOG_LAST GRN_LOG_DUMP
48
49 typedef enum {
50 GRN_FLAGS_OPERATOR_ADD,
51 GRN_FLAGS_OPERATOR_REMOVE,
52 GRN_FLAGS_OPERATOR_REPLACE,
53 } grn_flags_operator;
54
55 #define INITIAL_LOGGER { \
56 GRN_LOG_DEFAULT_LEVEL, \
57 GRN_LOG_DEFAULT, \
58 NULL, \
59 NULL, \
60 NULL, \
61 NULL \
62 }
63
64 static grn_logger current_logger = INITIAL_LOGGER;
65
66 const char *
67 grn_log_level_to_string(grn_log_level level)
68 {
69 if (level <= GRN_LOG_LAST) {
70 return log_level_names[level];
71 } else {
72 return "unknown";
73 }
74 }
75
76 bool
77 grn_log_level_parse(const char *string, grn_log_level *level)
78 {
79 if (strcmp(string, " ") == 0 ||
80 grn_strcasecmp(string, "none") == 0) {
81 *level = GRN_LOG_NONE;
82 return true;
83 } else if (strcmp(string, "E") == 0 ||
84 grn_strcasecmp(string, "emerg") == 0 ||
85 grn_strcasecmp(string, "emergency") == 0) {
86 *level = GRN_LOG_EMERG;
87 return true;
88 } else if (strcmp(string, "A") == 0 ||
89 grn_strcasecmp(string, "alert") == 0) {
90 *level = GRN_LOG_ALERT;
91 return true;
92 } else if (strcmp(string, "C") == 0 ||
93 grn_strcasecmp(string, "crit") == 0 ||
94 grn_strcasecmp(string, "critical") == 0) {
95 *level = GRN_LOG_CRIT;
96 return true;
97 } else if (strcmp(string, "e") == 0 ||
98 grn_strcasecmp(string, "error") == 0) {
99 *level = GRN_LOG_ERROR;
100 return true;
101 } else if (strcmp(string, "w") == 0 ||
102 grn_strcasecmp(string, "warn") == 0 ||
103 grn_strcasecmp(string, "warning") == 0) {
104 *level = GRN_LOG_WARNING;
105 return true;
106 } else if (strcmp(string, "n") == 0 ||
107 grn_strcasecmp(string, "notice") == 0) {
108 *level = GRN_LOG_NOTICE;
109 return true;
110 } else if (strcmp(string, "i") == 0 ||
111 grn_strcasecmp(string, "info") == 0) {
112 *level = GRN_LOG_INFO;
113 return true;
114 } else if (strcmp(string, "d") == 0 ||
115 grn_strcasecmp(string, "debug") == 0) {
116 *level = GRN_LOG_DEBUG;
117 return true;
118 } else if (strcmp(string, "-") == 0 ||
119 grn_strcasecmp(string, "dump") == 0) {
120 *level = GRN_LOG_DUMP;
121 return true;
122 }
123 return false;
124 }
125
126 bool
127 grn_log_flags_parse(const char *string,
128 int string_size,
129 int *flags)
130 {
131 const char *string_end;
132
133 *flags = GRN_LOG_DEFAULT;
134
135 if (!string) {
136 return true;
137 }
138
139 if (string_size < 0) {
140 string_size = (int)strlen(string);
141 }
142
143 string_end = string + string_size;
144
145 while (string < string_end) {
146 grn_flags_operator operator = GRN_FLAGS_OPERATOR_REPLACE;
147
148 if (*string == '|' || *string == ' ') {
149 string += 1;
150 continue;
151 }
152
153 if (*string == '+') {
154 operator = GRN_FLAGS_OPERATOR_ADD;
155 string++;
156 } else if (*string == '-') {
157 operator = GRN_FLAGS_OPERATOR_REMOVE;
158 string++;
159 }
160
161 #define CHECK_FLAG(name) \
162 if (((size_t)(string_end - string) >= (sizeof(#name) - 1)) && \
163 (grn_strncasecmp(string, #name, sizeof(#name) - 1) == 0) && \
164 (((size_t)(string_end - string) == (sizeof(#name) - 1)) || \
165 (string[sizeof(#name) - 1] == '|') || \
166 (string[sizeof(#name) - 1] == ' ') || \
167 (string[sizeof(#name) - 1] == '+') || \
168 (string[sizeof(#name) - 1] == '-'))) { \
169 if (operator == GRN_FLAGS_OPERATOR_ADD) { \
170 *flags |= GRN_LOG_ ## name; \
171 } else if (operator == GRN_FLAGS_OPERATOR_REMOVE) { \
172 *flags &= ~GRN_LOG_ ## name; \
173 } else { \
174 *flags = GRN_LOG_ ## name; \
175 } \
176 string += sizeof(#name) - 1; \
177 continue; \
178 }
179
180 CHECK_FLAG(NONE);
181 CHECK_FLAG(TIME);
182 CHECK_FLAG(TITLE);
183 CHECK_FLAG(MESSAGE);
184 CHECK_FLAG(LOCATION);
185 CHECK_FLAG(PID);
186 CHECK_FLAG(PROCESS_ID);
187 CHECK_FLAG(THREAD_ID);
188 CHECK_FLAG(CTX_ID);
189 CHECK_FLAG(CONTEXT_ID);
190 CHECK_FLAG(ALL);
191 CHECK_FLAG(DEFAULT);
192
193 #undef CHECK_FLAG
194
195 return false;
196 }
197
198 return true;
199 }
200
201 typedef struct {
202 char *path;
203 FILE *file;
204 grn_critical_section lock;
205 off_t size;
206 off_t rotate_threshold_size;
207 bool need_flock;
208 } grn_logger_output;
209
210 static void
211 grn_logger_output_init(grn_logger_output *output)
212 {
213 CRITICAL_SECTION_INIT(output->lock);
214 }
215
216 static void
217 grn_logger_output_close(grn_ctx *ctx, grn_logger_output *output)
218 {
219 CRITICAL_SECTION_ENTER(output->lock);
220 if (output->file) {
221 if (output->file != stdout &&
222 output->file != stderr) {
223 fclose(output->file);
224 }
225 output->file = NULL;
226 }
227 CRITICAL_SECTION_LEAVE(output->lock);
228 }
229
230 static void
231 grn_logger_output_fin(grn_ctx *ctx, grn_logger_output *output)
232 {
233 grn_logger_output_close(ctx, output);
234 if (output->path) {
235 free(output->path);
236 output->path = NULL;
237 }
238 CRITICAL_SECTION_FIN(output->lock);
239 }
240
241 static bool
242 grn_logger_output_start(grn_ctx *ctx,
243 grn_logger_output *output,
244 bool need_flock)
245 {
246 if (!output->path) {
247 return false;
248 }
249
250 CRITICAL_SECTION_ENTER(output->lock);
251
252 if (!output->file) {
253 output->size = 0;
254 if (strcmp(output->path, "-") == 0) {
255 output->file = stdout;
256 } else if (strcmp(output->path, "+") == 0) {
257 output->file = stderr;
258 } else {
259 output->file = grn_fopen(output->path, "a");
260 if (output->file) {
261 struct stat stat;
262 if (fstat(grn_fileno(output->file), &stat) != -1) {
263 output->size = stat.st_size;
264 }
265 }
266 }
267 }
268
269 if (!output->file) {
270 CRITICAL_SECTION_LEAVE(output->lock);
271 return false;
272 }
273
274 output->need_flock = need_flock;
275 if (output->need_flock) {
276 if (output->file == stdout ||
277 output->file == stderr) {
278 output->need_flock = false;
279 #ifndef _WIN32
280 } else if (flock(fileno(output->file), LOCK_EX) == -1) {
281 output->need_flock = false;
282 #endif
283 }
284 }
285
286 return true;
287 }
288
289 static void
290 grn_logger_output_rotate(grn_ctx *ctx, grn_logger_output *output)
291 {
292 char rotated_path[PATH_MAX];
293 grn_timeval now;
294 struct tm tm_buffer;
295 struct tm *tm;
296
297 grn_timeval_now(ctx, &now);
298 tm = grn_timeval2tm(ctx, &now, &tm_buffer);
299 grn_snprintf(rotated_path, PATH_MAX, PATH_MAX,
300 "%s.%04d-%02d-%02d-%02d-%02d-%02d-%06d",
301 output->path,
302 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
303 tm->tm_hour, tm->tm_min, tm->tm_sec,
304 (int)(GRN_TIME_NSEC_TO_USEC(now.tv_nsec)));
305 rename(output->path, rotated_path);
306 }
307
308 static void
309 grn_logger_output_end(grn_ctx *ctx,
310 grn_logger_output *output,
311 int written)
312 {
313 if (written > 0) {
314 output->size += written;
315 if (output->file != stdout &&
316 output->file != stderr &&
317 (output->rotate_threshold_size > 0 &&
318 output->size >= output->rotate_threshold_size)) {
319 #ifndef _WIN32
320 if (output->need_flock) {
321 flock(fileno(output->file), LOCK_UN);
322 }
323 #endif
324 fclose(output->file);
325 output->file = NULL;
326 grn_logger_output_rotate(ctx, output);
327 } else {
328 fflush(output->file);
329 }
330 }
331 #ifndef _WIN32
332 if (output->need_flock && output->file) {
333 flock(fileno(output->file), LOCK_UN);
334 }
335 #endif
336
337 CRITICAL_SECTION_LEAVE(output->lock);
338 }
339
340 static bool logger_inited = false;
341 static grn_logger_output default_logger_output = {0};
342
343 static void
344 default_logger_log(grn_ctx *ctx, grn_log_level level,
345 const char *timestamp, const char *title,
346 const char *message, const char *location, void *user_data)
347 {
348 if (!grn_logger_output_start(ctx,
349 &default_logger_output,
350 (current_logger.flags & GRN_LOG_PID))) {
351 return;
352 }
353
354 const char slev[] = " EACewnid-";
355 char label = *(slev + level);
356 int written = 0;
357 if (location && *location) {
358 if (title && *title) {
359 written = fprintf(default_logger_output.file, "%s|%c|%s: %s %s\n",
360 timestamp, label, location, title, message);
361 } else {
362 written = fprintf(default_logger_output.file, "%s|%c|%s: %s\n",
363 timestamp, label, location, message);
364 }
365 } else {
366 written = fprintf(default_logger_output.file, "%s|%c|%s %s\n",
367 timestamp, label, title, message);
368 }
369
370 grn_logger_output_end(ctx, &default_logger_output, written);
371 }
372
373 static void
374 default_logger_reopen(grn_ctx *ctx, void *user_data)
375 {
376 GRN_LOG(ctx, GRN_LOG_DEBUG, "log will be closed.");
377 grn_logger_output_close(ctx, &default_logger_output);
378 GRN_LOG(ctx, GRN_LOG_DEBUG, "log opened.");
379 }
380
381 static void
382 default_logger_fin(grn_ctx *ctx, void *user_data)
383 {
384 if (!logger_inited) {
385 return;
386 }
387 grn_logger_output_close(ctx, &default_logger_output);
388 }
389
390 static grn_logger default_logger = {
391 GRN_LOG_DEFAULT_LEVEL,
392 GRN_LOG_DEFAULT,
393 NULL,
394 default_logger_log,
395 default_logger_reopen,
396 default_logger_fin
397 };
398
399 void
400 grn_default_logger_set_max_level(grn_log_level max_level)
401 {
402 default_logger.max_level = max_level;
403 if (current_logger.log == default_logger_log) {
404 current_logger.max_level = max_level;
405 }
406 }
407
408 grn_log_level
409 grn_default_logger_get_max_level(void)
410 {
411 return default_logger.max_level;
412 }
413
414 void
415 grn_default_logger_set_flags(int flags)
416 {
417 default_logger.flags = flags;
418 if (current_logger.log == default_logger_log) {
419 current_logger.flags = flags;
420 }
421 }
422
423 int
424 grn_default_logger_get_flags(void)
425 {
426 return default_logger.flags;
427 }
428
429 void
430 grn_default_logger_set_path(const char *path)
431 {
432 if (logger_inited) {
433 CRITICAL_SECTION_ENTER(default_logger_output.lock);
434 }
435
436 if (default_logger_output.path) {
437 free(default_logger_output.path);
438 }
439
440 if (path) {
441 default_logger_output.path = grn_strdup_raw(path);
442 } else {
443 default_logger_output.path = NULL;
444 }
445
446 if (logger_inited) {
447 CRITICAL_SECTION_LEAVE(default_logger_output.lock);
448 }
449 }
450
451 const char *
452 grn_default_logger_get_path(void)
453 {
454 return default_logger_output.path;
455 }
456
457 void
458 grn_default_logger_set_rotate_threshold_size(off_t threshold)
459 {
460 default_logger_output.rotate_threshold_size = threshold;
461 }
462
463 off_t
464 grn_default_logger_get_rotate_threshold_size(void)
465 {
466 return default_logger_output.rotate_threshold_size;
467 }
468
469 void
470 grn_logger_reopen(grn_ctx *ctx)
471 {
472 if (current_logger.reopen) {
473 current_logger.reopen(ctx, current_logger.user_data);
474 }
475 }
476
477 static void
478 current_logger_fin(grn_ctx *ctx)
479 {
480 if (current_logger.fin) {
481 current_logger.fin(ctx, current_logger.user_data);
482 }
483 {
484 grn_logger initial_logger = INITIAL_LOGGER;
485 current_logger = initial_logger;
486 }
487 }
488
489 static void
490 logger_info_func_wrapper(grn_ctx *ctx, grn_log_level level,
491 const char *timestamp, const char *title,
492 const char *message, const char *location,
493 void *user_data)
494 {
495 grn_logger_info *info = user_data;
496 info->func(level, timestamp, title, message, location, info->func_arg);
497 }
498
499 /* Deprecated since 2.1.2. */
500 grn_rc
501 grn_logger_info_set(grn_ctx *ctx, const grn_logger_info *info)
502 {
503 if (info) {
504 grn_logger logger;
505
506 memset(&logger, 0, sizeof(grn_logger));
507 logger.max_level = info->max_level;
508 logger.flags = info->flags;
509 if (info->func) {
510 logger.log = logger_info_func_wrapper;
511 logger.user_data = (grn_logger_info *)info;
512 } else {
513 logger.log = default_logger_log;
514 logger.reopen = default_logger_reopen;
515 logger.fin = default_logger_fin;
516 }
517 return grn_logger_set(ctx, &logger);
518 } else {
519 return grn_logger_set(ctx, NULL);
520 }
521 }
522
523 grn_rc
524 grn_logger_set(grn_ctx *ctx, const grn_logger *logger)
525 {
526 current_logger_fin(ctx);
527 if (logger) {
528 current_logger = *logger;
529 } else {
530 current_logger = default_logger;
531 }
532 return GRN_SUCCESS;
533 }
534
535 bool
536 grn_logger_is_default_logger(grn_ctx *ctx)
537 {
538 return current_logger.log == default_logger.log;
539 }
540
541 void
542 grn_logger_set_max_level(grn_ctx *ctx, grn_log_level max_level)
543 {
544 current_logger.max_level = max_level;
545 }
546
547 grn_log_level
548 grn_logger_get_max_level(grn_ctx *ctx)
549 {
550 return current_logger.max_level;
551 }
552
553 bool
554 grn_logger_pass(grn_ctx *ctx, grn_log_level level)
555 {
556 return level <= current_logger.max_level;
557 }
558
559 #define TBUFSIZE GRN_TIMEVAL_STR_SIZE
560 #define MBUFSIZE 0x1000
561 #define LBUFSIZE 0x400
562
563 void
564 grn_logger_put(grn_ctx *ctx,
565 grn_log_level level,
566 const char *file,
567 int line,
568 const char *func,
569 const char *fmt,
570 ...)
571 {
572 va_list ap;
573 va_start(ap, fmt);
574 grn_logger_putv(ctx, level, file, line, func, fmt, ap);
575 va_end(ap);
576 }
577
578 void
579 grn_logger_putv(grn_ctx *ctx,
580 grn_log_level level,
581 const char *file,
582 int line,
583 const char *func,
584 const char *fmt,
585 va_list ap)
586 {
587 if (!ctx) {
588 ctx = &grn_gctx;
589 }
590
591 if (level <= current_logger.max_level && current_logger.log) {
592 char tbuf[TBUFSIZE];
593 char mbuf[MBUFSIZE];
594 char lbuf[LBUFSIZE];
595 tbuf[0] = '\0';
596 if (current_logger.flags & GRN_LOG_TIME) {
597 grn_timeval tv;
598 grn_timeval_now(ctx, &tv);
599 grn_timeval2str(ctx, &tv, tbuf, TBUFSIZE);
600 }
601 if (current_logger.flags & GRN_LOG_MESSAGE) {
602 grn_vsnprintf(mbuf, MBUFSIZE, fmt, ap);
603 } else {
604 mbuf[0] = '\0';
605 }
606 {
607 char *lbuf_current = lbuf;
608 size_t lbuf_rest_size = LBUFSIZE;
609 lbuf[0] = '\0';
610 lbuf_current = lbuf;
611 if ((current_logger.flags & GRN_LOG_PID) ||
612 /* For backward compatibility. GRN_LOG_LOCATION implies GRN_LOG_PID. */
613 (current_logger.flags & GRN_LOG_LOCATION)) {
614 grn_snprintf(lbuf_current, lbuf_rest_size, lbuf_rest_size,
615 "%d", grn_getpid());
616 const size_t lbuf_size = strlen(lbuf_current);
617 lbuf_current += lbuf_size;
618 lbuf_rest_size -= lbuf_size;
619 }
620 if (current_logger.flags & GRN_LOG_THREAD_ID) {
621 const char *prefix = "";
622 if (lbuf_current != lbuf) {
623 prefix = "|";
624 }
625 #ifdef HAVE_PTHREAD_H
626 grn_snprintf(lbuf_current, lbuf_rest_size, lbuf_rest_size,
627 "%s%08lx", prefix, (uintptr_t)(pthread_self()));
628 #elif defined(WIN32) /* HAVE_PTHREAD_H */
629 grn_snprintf(lbuf_current, lbuf_rest_size, lbuf_rest_size,
630 "%s%08ld", prefix, GetCurrentThreadId());
631 #endif /* HAVE_PTHREAD_H */
632 const size_t lbuf_size = strlen(lbuf_current);
633 lbuf_current += lbuf_size;
634 lbuf_rest_size -= lbuf_size;
635 }
636 if (current_logger.flags & GRN_LOG_CONTEXT_ID) {
637 const char *prefix = "";
638 if (lbuf_current != lbuf) {
639 prefix = "|";
640 }
641 grn_snprintf(lbuf_current, lbuf_rest_size, lbuf_rest_size,
642 "%s%p", prefix, ctx);
643 const size_t lbuf_size = strlen(lbuf_current);
644 lbuf_current += lbuf_size;
645 lbuf_rest_size -= lbuf_size;
646 }
647 if (current_logger.flags & GRN_LOG_LOCATION) {
648 const char *prefix = "";
649 if (lbuf_current != lbuf) {
650 prefix = " ";
651 }
652 grn_snprintf(lbuf_current, lbuf_rest_size, lbuf_rest_size,
653 "%s%s:%d %s()", prefix, file, line, func);
654 const size_t lbuf_size = strlen(lbuf_current);
655 lbuf_current += lbuf_size;
656 lbuf_rest_size -= lbuf_size;
657 }
658 }
659
660 if (mbuf[0] == '\0') {
661 current_logger.log(ctx, level, tbuf, "", mbuf, lbuf,
662 current_logger.user_data);
663 } else {
664 const char *mbuf_line_start = mbuf;
665 const char *mbuf_end = mbuf + strlen(mbuf);
666 int mbuf_char_length = 0;
667 for (char *mbuf_current = mbuf;
668 mbuf_current < mbuf_end;
669 mbuf_current += mbuf_char_length) {
670 mbuf_char_length = grn_charlen(ctx, mbuf_current, mbuf_end);
671 if (mbuf_char_length == 0) {
672 break;
673 } else if (mbuf_char_length == 1 && mbuf_current[0] == '\n') {
674 mbuf_current[0] = '\0';
675 current_logger.log(ctx, level, tbuf, "", mbuf_line_start, lbuf,
676 current_logger.user_data);
677 mbuf_line_start = mbuf_current + 1;
678 }
679 }
680 if (mbuf_line_start < mbuf_end) {
681 current_logger.log(ctx, level, tbuf, "", mbuf_line_start, lbuf,
682 current_logger.user_data);
683 }
684 }
685 }
686 }
687
688 void
689 grn_logger_init(void)
690 {
691 grn_logger_output_init(&default_logger_output);
692 if (!current_logger.log) {
693 current_logger = default_logger;
694 }
695
696 logger_inited = true;
697 }
698
699 void
700 grn_logger_fin(grn_ctx *ctx)
701 {
702 current_logger_fin(ctx);
703 grn_logger_output_fin(ctx, &default_logger_output);
704 logger_inited = false;
705 }
706
707
708 static bool query_logger_inited = false;
709 static grn_logger_output default_query_logger_output = {0};
710
711 bool
712 grn_query_log_flags_parse(const char *string,
713 int string_size,
714 unsigned int *flags)
715 {
716 const char *string_end;
717
718 *flags = GRN_QUERY_LOG_NONE;
719
720 if (!string) {
721 return true;
722 }
723
724 if (string_size < 0) {
725 string_size = (int)strlen(string);
726 }
727
728 string_end = string + string_size;
729
730 while (string < string_end) {
731 if (*string == '|' || *string == ' ') {
732 string += 1;
733 continue;
734 }
735
736 #define CHECK_FLAG(name) \
737 if (((size_t)(string_end - string) >= (sizeof(#name) - 1)) && \
738 (memcmp(string, #name, sizeof(#name) - 1) == 0) && \
739 (((size_t)(string_end - string) == (sizeof(#name) - 1)) || \
740 (string[sizeof(#name) - 1] == '|') || \
741 (string[sizeof(#name) - 1] == ' '))) { \
742 *flags |= GRN_QUERY_LOG_ ## name; \
743 string += sizeof(#name) - 1; \
744 continue; \
745 }
746
747 CHECK_FLAG(NONE);
748 CHECK_FLAG(COMMAND);
749 CHECK_FLAG(RESULT_CODE);
750 CHECK_FLAG(DESTINATION);
751 CHECK_FLAG(CACHE);
752 CHECK_FLAG(SIZE);
753 CHECK_FLAG(SCORE);
754 CHECK_FLAG(ALL);
755 CHECK_FLAG(DEFAULT);
756
757 #undef CHECK_FLAG
758
759 return false;
760 }
761
762 return true;
763 }
764
765 static void
766 default_query_logger_log(grn_ctx *ctx, unsigned int flag,
767 const char *timestamp, const char *info,
768 const char *message, void *user_data)
769 {
770 if (!grn_logger_output_start(ctx,
771 &default_query_logger_output,
772 (current_logger.flags & GRN_LOG_PID))) {
773 return;
774 }
775 int written = fprintf(default_query_logger_output.file, "%s|%s%s\n",
776 timestamp, info, message);
777 grn_logger_output_end(ctx, &default_query_logger_output, written);
778 }
779
780 static void
781 default_query_logger_close(grn_ctx *ctx, void *user_data)
782 {
783 GRN_QUERY_LOG(ctx, GRN_QUERY_LOG_DESTINATION, " ",
784 "query log will be closed: <%s>",
785 default_query_logger_output.path);
786 grn_logger_output_close(ctx, &default_query_logger_output);
787 }
788
789 static void
790 default_query_logger_reopen(grn_ctx *ctx, void *user_data)
791 {
792 default_query_logger_close(ctx, user_data);
793 if (default_query_logger_output.path) {
794 GRN_QUERY_LOG(ctx, GRN_QUERY_LOG_DESTINATION, " ",
795 "query log is opened: <%s>",
796 default_query_logger_output.path);
797 }
798 }
799
800 static void
801 default_query_logger_fin(grn_ctx *ctx, void *user_data)
802 {
803 default_query_logger_close(ctx, user_data);
804 }
805
806 static grn_query_logger default_query_logger = {
807 GRN_QUERY_LOG_DEFAULT,
808 NULL,
809 default_query_logger_log,
810 default_query_logger_reopen,
811 default_query_logger_fin
812 };
813
814 #define INITIAL_QUERY_LOGGER { \
815 GRN_QUERY_LOG_DEFAULT, \
816 NULL, \
817 NULL, \
818 NULL, \
819 NULL \
820 }
821
822 static grn_query_logger current_query_logger = INITIAL_QUERY_LOGGER;
823
824 void
825 grn_default_query_logger_set_flags(unsigned int flags)
826 {
827 default_query_logger.flags = flags;
828 if (current_query_logger.log == default_query_logger_log) {
829 current_query_logger.flags = flags;
830 }
831 }
832
833 unsigned int
834 grn_default_query_logger_get_flags(void)
835 {
836 return default_query_logger.flags;
837 }
838
839 void
840 grn_default_query_logger_set_path(const char *path)
841 {
842 if (query_logger_inited) {
843 CRITICAL_SECTION_ENTER(default_query_logger_output.lock);
844 }
845
846 if (default_query_logger_output.path) {
847 free(default_query_logger_output.path);
848 }
849
850 if (path) {
851 default_query_logger_output.path = grn_strdup_raw(path);
852 } else {
853 default_query_logger_output.path = NULL;
854 }
855
856 if (query_logger_inited) {
857 CRITICAL_SECTION_LEAVE(default_query_logger_output.lock);
858 }
859 }
860
861 const char *
862 grn_default_query_logger_get_path(void)
863 {
864 return default_query_logger_output.path;
865 }
866
867 void
868 grn_default_query_logger_set_rotate_threshold_size(off_t threshold)
869 {
870 default_query_logger_output.rotate_threshold_size = threshold;
871 }
872
873 off_t
874 grn_default_query_logger_get_rotate_threshold_size(void)
875 {
876 return default_query_logger_output.rotate_threshold_size;
877 }
878
879 void
880 grn_query_logger_reopen(grn_ctx *ctx)
881 {
882 if (current_query_logger.reopen) {
883 current_query_logger.reopen(ctx, current_query_logger.user_data);
884 }
885 }
886
887 static void
888 current_query_logger_fin(grn_ctx *ctx)
889 {
890 if (current_query_logger.fin) {
891 current_query_logger.fin(ctx, current_query_logger.user_data);
892 }
893 {
894 grn_query_logger initial_query_logger = INITIAL_QUERY_LOGGER;
895 current_query_logger = initial_query_logger;
896 }
897 }
898
899 grn_rc
900 grn_query_logger_set(grn_ctx *ctx, const grn_query_logger *logger)
901 {
902 current_query_logger_fin(ctx);
903 if (logger) {
904 current_query_logger = *logger;
905 } else {
906 current_query_logger = default_query_logger;
907 }
908 return GRN_SUCCESS;
909 }
910
911 void
912 grn_query_logger_set_flags(grn_ctx *ctx, unsigned int flags)
913 {
914 current_query_logger.flags = flags;
915 }
916
917 void
918 grn_query_logger_add_flags(grn_ctx *ctx, unsigned int flags)
919 {
920 current_query_logger.flags |= flags;
921 }
922
923 void
924 grn_query_logger_remove_flags(grn_ctx *ctx, unsigned int flags)
925 {
926 current_query_logger.flags &= ~flags;
927 }
928
929 unsigned int
930 grn_query_logger_get_flags(grn_ctx *ctx)
931 {
932 return current_query_logger.flags;
933 }
934
935 bool
936 grn_query_logger_pass(grn_ctx *ctx, unsigned int flag)
937 {
938 return current_query_logger.flags & flag;
939 }
940
941 #define TIMESTAMP_BUFFER_SIZE TBUFSIZE
942 /* 8+a(%p) + 1(|) + 1(mark) + 15(elapsed time) = 25+a */
943 #define INFO_BUFFER_SIZE 40
944
945 void
946 grn_query_logger_put(grn_ctx *ctx, unsigned int flag, const char *mark,
947 const char *format, ...)
948 {
949 char timestamp[TIMESTAMP_BUFFER_SIZE];
950 char info[INFO_BUFFER_SIZE];
951 grn_obj *message = &ctx->impl->query_log_buf;
952
953 if (!current_query_logger.log) {
954 return;
955 }
956
957 {
958 grn_timeval tv;
959 timestamp[0] = '\0';
960 grn_timeval_now(ctx, &tv);
961 grn_timeval2str(ctx, &tv, timestamp, TIMESTAMP_BUFFER_SIZE);
962 }
963
964 grn_ctx *target_ctx = ctx;
965 while (target_ctx->impl->parent) {
966 target_ctx = target_ctx->impl->parent;
967 }
968 if (flag & (GRN_QUERY_LOG_COMMAND | GRN_QUERY_LOG_DESTINATION)) {
969 grn_snprintf(info, INFO_BUFFER_SIZE, INFO_BUFFER_SIZE,
970 "%p|%s", target_ctx, mark);
971 info[INFO_BUFFER_SIZE - 1] = '\0';
972 } else {
973 grn_timeval tv;
974 uint64_t elapsed_time;
975 grn_timeval_now(ctx, &tv);
976 elapsed_time =
977 (uint64_t)(tv.tv_sec - target_ctx->impl->tv.tv_sec) *
978 GRN_TIME_NSEC_PER_SEC +
979 (uint64_t)(tv.tv_nsec - target_ctx->impl->tv.tv_nsec);
980
981 grn_snprintf(info, INFO_BUFFER_SIZE, INFO_BUFFER_SIZE,
982 "%p|%s%015" GRN_FMT_INT64U " ", target_ctx, mark, elapsed_time);
983 info[INFO_BUFFER_SIZE - 1] = '\0';
984 }
985
986 {
987 va_list args;
988
989 va_start(args, format);
990 GRN_BULK_REWIND(message);
991 grn_text_printfv(ctx, message, format, args);
992 va_end(args);
993 GRN_TEXT_PUTC(ctx, message, '\0');
994 }
995
996 current_query_logger.log(ctx, flag, timestamp, info, GRN_TEXT_VALUE(message),
997 current_query_logger.user_data);
998 }
999
1000 void
1001 grn_query_logger_init(void)
1002 {
1003 current_query_logger = default_query_logger;
1004 grn_logger_output_init(&default_query_logger_output);
1005 query_logger_inited = true;
1006 }
1007
1008 void
1009 grn_query_logger_fin(grn_ctx *ctx)
1010 {
1011 current_query_logger_fin(ctx);
1012 grn_logger_output_fin(ctx, &default_query_logger_output);
1013 query_logger_inited = false;
1014 }
1015
1016 void
1017 grn_log_reopen(grn_ctx *ctx)
1018 {
1019 grn_logger_reopen(ctx);
1020 grn_query_logger_reopen(ctx);
1021 }