-
Notifications
You must be signed in to change notification settings - Fork 703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce log.h to decouple serverLog from server.h. #664
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
#include "log.h" | ||
|
||
#include "util.h" | ||
|
||
#include <fcntl.h> | ||
#include <stdarg.h> | ||
#include <string.h> | ||
|
||
/* We use a private localtime implementation which is fork-safe. The logging | ||
* function of the server may be called from other threads. */ | ||
void nolocks_localtime(struct tm *tmp, time_t t, time_t tz, int dst); | ||
|
||
/* Low level logging from signal handler. Should be used with pre-formatted strings. | ||
See serverLogFromHandler. */ | ||
void valkeyLogRawFromHandler(int level, int daemonize, const char *logfile, const char *msg) { | ||
int fd; | ||
int log_to_stdout = logfile[0] == '\0'; | ||
char buf[64]; | ||
|
||
if (log_to_stdout && daemonize) return; | ||
fd = log_to_stdout ? STDOUT_FILENO : open(logfile, O_APPEND | O_CREAT | O_WRONLY, 0644); | ||
if (fd == -1) return; | ||
if (level & LL_RAW) { | ||
if (write(fd, msg, strlen(msg)) == -1) goto err; | ||
} else { | ||
ll2string(buf, sizeof(buf), getpid()); | ||
if (write(fd, buf, strlen(buf)) == -1) goto err; | ||
if (write(fd, ":signal-handler (", 17) == -1) goto err; | ||
ll2string(buf, sizeof(buf), time(NULL)); | ||
if (write(fd, buf, strlen(buf)) == -1) goto err; | ||
if (write(fd, ") ", 2) == -1) goto err; | ||
if (write(fd, msg, strlen(msg)) == -1) goto err; | ||
if (write(fd, "\n", 1) == -1) goto err; | ||
} | ||
err: | ||
if (!log_to_stdout) close(fd); | ||
} | ||
|
||
/* An async-signal-safe version of serverLog. if LL_RAW is not included in level flags, | ||
* The message format is: <pid>:signal-handler (<time>) <msg> \n | ||
* with LL_RAW flag only the msg is printed (with no new line at the end) | ||
* | ||
* We actually use this only for signals that are not fatal from the point | ||
* of view of the server. Signals that are going to kill the server anyway and | ||
* where we need printf-alike features are served by serverLog(). */ | ||
void valkeyLogFromHandler(int level, int daemonize, const char *logfile, const char *fmt, ...) { | ||
va_list ap; | ||
char msg[LOG_MAX_LEN]; | ||
|
||
va_start(ap, fmt); | ||
vsnprintf_async_signal_safe(msg, sizeof(msg), fmt, ap); | ||
va_end(ap); | ||
|
||
valkeyLogRawFromHandler(level, daemonize, logfile, msg); | ||
} | ||
|
||
/* Low level logging. To use only for very big messages, otherwise | ||
* valkeyLog() is to prefer. */ | ||
void valkeyLogRaw(int level, | ||
int syslog_enabled, | ||
time_t timezone, | ||
int daylight_active, | ||
int sentinel_mode, | ||
pid_t server_pid, | ||
const char *primary_host, | ||
const char *logfile, | ||
const char *msg) { | ||
const int syslogLevelMap[] = {LOG_DEBUG, LOG_INFO, LOG_NOTICE, LOG_WARNING}; | ||
const char *c = ".-*#"; | ||
FILE *fp; | ||
char buf[64]; | ||
int rawmode = (level & LL_RAW); | ||
int log_to_stdout = logfile[0] == '\0'; | ||
|
||
fp = log_to_stdout ? stdout : fopen(logfile, "a"); | ||
if (!fp) return; | ||
|
||
if (rawmode) { | ||
fprintf(fp, "%s", msg); | ||
} else { | ||
int off; | ||
struct timeval tv; | ||
int role_char; | ||
pid_t pid = getpid(); | ||
|
||
gettimeofday(&tv, NULL); | ||
struct tm tm; | ||
nolocks_localtime(&tm, tv.tv_sec, timezone, daylight_active); | ||
off = strftime(buf, sizeof(buf), "%d %b %Y %H:%M:%S.", &tm); | ||
snprintf(buf + off, sizeof(buf) - off, "%03d", (int)tv.tv_usec / 1000); | ||
if (sentinel_mode) { | ||
role_char = 'X'; /* Sentinel. */ | ||
} else if (pid != server_pid) { | ||
role_char = 'C'; /* RDB / AOF writing child. */ | ||
} else { | ||
role_char = (primary_host ? 'S' : 'M'); /* replica or Primary. */ | ||
} | ||
fprintf(fp, "%d:%c %s %c %s\n", (int)getpid(), role_char, buf, c[level], msg); | ||
} | ||
fflush(fp); | ||
|
||
if (!log_to_stdout) fclose(fp); | ||
if (syslog_enabled) syslog(syslogLevelMap[level], "%s", msg); | ||
} | ||
|
||
/* Like valkeyLogRaw() but with printf-alike support. This is the function that | ||
* is used across the code. The raw version is only used in order to dump | ||
* the INFO output on crash. */ | ||
void valkeyLog(int level, | ||
int syslog_enabled, | ||
time_t timezone, | ||
int daylight_active, | ||
int sentinel_mode, | ||
pid_t server_pid, | ||
const char *primary_host, | ||
const char *logfile, | ||
const char *fmt, | ||
...) { | ||
va_list ap; | ||
char msg[LOG_MAX_LEN]; | ||
|
||
va_start(ap, fmt); | ||
vsnprintf(msg, sizeof(msg), fmt, ap); | ||
va_end(ap); | ||
|
||
valkeyLogRaw(level, syslog_enabled, timezone, daylight_active, sentinel_mode, server_pid, primary_host, logfile, | ||
msg); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#ifndef VALKEY_LOG_H | ||
#define VALKEY_LOG_H | ||
|
||
#include <ctype.h> | ||
#include <stddef.h> | ||
#include <stdio.h> | ||
#include <sys/syslog.h> | ||
#include <sys/time.h> | ||
#include <time.h> | ||
#include <unistd.h> | ||
|
||
#define LOG_MAX_LEN 1024 /* Default maximum length of syslog messages.*/ | ||
|
||
/* Log levels */ | ||
#define LL_DEBUG 0 | ||
#define LL_VERBOSE 1 | ||
#define LL_NOTICE 2 | ||
#define LL_WARNING 3 | ||
#define LL_NOTHING 4 | ||
#define LL_RAW (1 << 10) /* Modifier to log without timestamp */ | ||
|
||
/* Use macro for checking log level to avoid evaluating arguments in cases log | ||
* should be ignored due to low level. */ | ||
#define serverLog(level, ...) \ | ||
do { \ | ||
if (((level) & 0xff) < server.verbosity) break; \ | ||
valkeyLog(level, server.syslog_enabled, server.timezone, server.daylight_active, server.sentinel_mode, \ | ||
server.pid, server.primary_host, server.logfile, __VA_ARGS__); \ | ||
} while (0) | ||
|
||
#define serverLogRaw(level, ...) \ | ||
do { \ | ||
if (((level) & 0xff) < server.verbosity) break; \ | ||
valkeyLogRaw(level, server.syslog_enabled, server.timezone, server.daylight_active, server.sentinel_mode, \ | ||
server.pid, server.primary_host, server.logfile, __VA_ARGS__); \ | ||
} while (0) | ||
|
||
#define serverLogFromHandler(level, fmt, ...) \ | ||
do { \ | ||
if (((level) & 0xff) < server.verbosity) break; \ | ||
valkeyLogFromHandler(level, server.daemonize, server.logfile, fmt, __VA_ARGS__); \ | ||
} while (0) | ||
|
||
#define serverLogRawFromHandler(level, msg) \ | ||
do { \ | ||
if (((level) & 0xff) < server.verbosity) break; \ | ||
valkeyLogRawFromHandler(level, server.daemonize, server.logfile, msg); \ | ||
} while (0) | ||
|
||
#define serverDebug(fmt, ...) printf("DEBUG %s:%d > " fmt "\n", __FILE__, __LINE__, __VA_ARGS__) | ||
#define serverDebugMark() printf("-- MARK %s:%d --\n", __FILE__, __LINE__) | ||
|
||
#ifdef __GNUC__ | ||
void valkeyLog(int level, | ||
int syslog_enabled, | ||
time_t timezone, | ||
int daylight_active, | ||
int sentinel_mode, | ||
pid_t pid, | ||
const char *primary_host, | ||
const char *logfile, | ||
const char *fmt, | ||
...) __attribute__((format(printf, 9, 10))); | ||
void valkeyLogFromHandler(int level, int daemonize, const char *logfile, const char *fmt, ...) | ||
__attribute__((format(printf, 4, 5))); | ||
#else | ||
void valkeyLog(int level, | ||
int syslog_enabled, | ||
time_t timezone, | ||
int daylight_active, | ||
int sentinel_mode, | ||
pid_t pid, | ||
const char *primary_host, | ||
const char *logfile, | ||
const char *fmt, | ||
...); | ||
void valkeyLogFromHandler(int level, int daemonize, const char *logfile, const char *fmt, ...); | ||
#endif | ||
void valkeyLogRawFromHandler(int level, int daemonize, const char *logfile, const char *msg); | ||
void valkeyLogRaw(int level, | ||
int syslog_enabled, | ||
time_t timezone, | ||
int daylight_active, | ||
int sentinel_mode, | ||
pid_t pid, | ||
const char *primary_host, | ||
const char *logfile, | ||
const char *msg); | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels a bit like fake decoupling, since you can't import log.h without server.h, since we are accessing the server structure here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the existing log implementation seems relied server and server config too much, its not straightforward to split it from
server.h
. Let's open an issue to discuss the code refactor work.