|
| 1 | +/* Copyright (c) 2019, Codership Oy. All rights reserved. |
| 2 | + * |
| 3 | + * This program is free software; you can redistribute it and/or modify |
| 4 | + * it under the terms of the GNU General Public License as published by |
| 5 | + * the Free Software Foundation; version 2 of the License. |
| 6 | + * |
| 7 | + * This program is distributed in the hope that it will be useful, |
| 8 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + * GNU General Public License for more details. |
| 11 | + * |
| 12 | + * You should have received a copy of the GNU General Public License |
| 13 | + * along with this program; if not, write to the Free Software |
| 14 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 15 | + */ |
| 16 | + |
| 17 | +#include "log.h" |
| 18 | + |
| 19 | +#include <stdio.h> // fprintf(), fflush() |
| 20 | +#include <sys/time.h> // gettimeofday() |
| 21 | +#include <time.h> // localtime_r() |
| 22 | +#include <stdarg.h> // va_start(), va_end() |
| 23 | + |
| 24 | +wsrep_log_level_t node_log_max_level = WSREP_LOG_INFO; |
| 25 | + |
| 26 | +static const char* log_level_str[WSREP_LOG_DEBUG + 2] = |
| 27 | +{ |
| 28 | + "FATAL: ", |
| 29 | + "ERROR: ", |
| 30 | + " WARN: ", |
| 31 | + " INFO: ", |
| 32 | + "DEBUG: ", |
| 33 | + "XXXXX: " |
| 34 | +}; |
| 35 | + |
| 36 | +static inline void |
| 37 | +log_timestamp_and_log(const char* const prefix, // source of msg |
| 38 | + int const severity, |
| 39 | + const char* const msg) |
| 40 | +{ |
| 41 | + struct tm date; |
| 42 | + struct timeval time; |
| 43 | + |
| 44 | + gettimeofday(&time, NULL); |
| 45 | + localtime_r (&time.tv_sec, &date); |
| 46 | + |
| 47 | + FILE* log_file = stderr; |
| 48 | + fprintf(log_file, |
| 49 | + "%04d-%02d-%02d %02d:%02d:%02d.%03d " /* timestamp fmt */ |
| 50 | + "[%s] %s%s\n", /* [prefix] severity msg */ |
| 51 | + date.tm_year + 1900, date.tm_mon + 1, date.tm_mday, |
| 52 | + date.tm_hour, date.tm_min, date.tm_sec, |
| 53 | + (int)time.tv_usec / 1000, |
| 54 | + prefix, log_level_str[severity], msg |
| 55 | + ); |
| 56 | + |
| 57 | + fflush (log_file); |
| 58 | +} |
| 59 | + |
| 60 | +void |
| 61 | +node_log_cb(wsrep_log_level_t const severity, const char* const msg) |
| 62 | +{ |
| 63 | + /* REPLICATION: let provider log messages be prefixed with 'wsrep'*/ |
| 64 | + log_timestamp_and_log("wsrep", severity, msg); |
| 65 | +} |
| 66 | + |
| 67 | +void |
| 68 | +node_log(wsrep_log_level_t const severity, |
| 69 | + const char* const file, |
| 70 | + const char* const function, |
| 71 | + int const line, |
| 72 | + ...) |
| 73 | +{ |
| 74 | + va_list ap; |
| 75 | + |
| 76 | + char string[2048]; |
| 77 | + int max_string = sizeof(string); |
| 78 | + char* str = string; |
| 79 | + |
| 80 | + /* provide file:func():line info only if debug logging is on */ |
| 81 | + if (NODE_DO_LOG_DEBUG) { |
| 82 | + int const len = snprintf(str, (size_t)max_string, "%s:%s():%d: ", |
| 83 | + file, function, line); |
| 84 | + str += len; |
| 85 | + max_string -= len; |
| 86 | + } |
| 87 | + |
| 88 | + va_start(ap, line); |
| 89 | + { |
| 90 | + const char* format = va_arg (ap, const char*); |
| 91 | + |
| 92 | + if (max_string > 0 && NULL != format) { |
| 93 | + vsnprintf (str, (size_t)max_string, format, ap); |
| 94 | + } |
| 95 | + } |
| 96 | + va_end(ap); |
| 97 | + |
| 98 | + /* actual logging */ |
| 99 | + log_timestamp_and_log(" node", severity, string); |
| 100 | +} |
0 commit comments