forked from ray-project/ray-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Redis logging * Rearrange logging interfaces * Fix test case * Changes to logging interface and test case for logging * Fixes * Fix memory leaks * Add interface method to destroy logger * is_local -> is_direct * Merge fix
- Loading branch information
Showing
8 changed files
with
217 additions
and
34 deletions.
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
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,78 @@ | ||
#include "logging.h" | ||
|
||
#include <hiredis/hiredis.h> | ||
#include <utstring.h> | ||
|
||
#include "state/redis.h" | ||
#include "io.h" | ||
|
||
static const char *log_levels[5] = {"DEBUG", "INFO", "WARN", "ERROR", "FATAL"}; | ||
static const char *log_fmt = | ||
"HMSET log:%s:%s:%s log_level %s event_type %s message %s timestamp %s"; | ||
|
||
struct ray_logger_impl { | ||
/* String that identifies this client type. */ | ||
char *client_type; | ||
/* Suppress all log messages below this level. */ | ||
int log_level; | ||
/* Whether or not we have a direct connection to Redis. */ | ||
int is_direct; | ||
/* Either a db_conn or a socket to a process with a db_conn, | ||
* depending on the is_direct flag. */ | ||
void *conn; | ||
}; | ||
|
||
ray_logger *init_ray_logger(const char *client_type, | ||
int log_level, | ||
int is_direct, | ||
void *conn) { | ||
ray_logger *logger = malloc(sizeof(ray_logger)); | ||
logger->client_type = client_type; | ||
logger->log_level = log_level; | ||
logger->is_direct = is_direct; | ||
logger->conn = conn; | ||
return logger; | ||
} | ||
|
||
void free_ray_logger(ray_logger *logger) { | ||
free(logger); | ||
} | ||
|
||
void ray_log(ray_logger *logger, | ||
int log_level, | ||
const char *event_type, | ||
const char *message) { | ||
if (log_level < logger->log_level) { | ||
return; | ||
} | ||
if (log_level < RAY_DEBUG || log_level > RAY_FATAL) { | ||
return; | ||
} | ||
struct timeval tv; | ||
UT_string *timestamp; | ||
utstring_new(timestamp); | ||
gettimeofday(&tv, NULL); | ||
utstring_printf(timestamp, "%ld.%ld", tv.tv_sec, tv.tv_usec); | ||
|
||
UT_string *origin_id; | ||
utstring_new(origin_id); | ||
if (logger->is_direct) { | ||
db_conn *db = (db_conn *) logger->conn; | ||
utstring_printf(origin_id, "%ld:%s", db->client_id, ""); | ||
redisAsyncCommand(db->context, NULL, NULL, log_fmt, | ||
utstring_body(timestamp), logger->client_type, | ||
utstring_body(origin_id), log_levels[log_level], | ||
event_type, message, utstring_body(timestamp)); | ||
} else { | ||
/* If we don't own a Redis connection, we leave our client | ||
* ID to be filled in by someone else. */ | ||
utstring_printf(origin_id, "%s:%s", "%ld", "%ld"); | ||
int *socket_fd = (int *) logger->conn; | ||
write_formatted_string(*socket_fd, log_fmt, utstring_body(timestamp), | ||
logger->client_type, utstring_body(origin_id), | ||
log_levels[log_level], event_type, message, | ||
utstring_body(timestamp)); | ||
} | ||
utstring_free(origin_id); | ||
utstring_free(timestamp); | ||
} |
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,39 @@ | ||
#ifndef LOGGING_H | ||
#define LOGGING_H | ||
|
||
#define RAY_VERBOSE -1 | ||
#define RAY_DEBUG 0 | ||
#define RAY_INFO 1 | ||
#define RAY_WARNING 2 | ||
#define RAY_ERROR 3 | ||
#define RAY_FATAL 4 | ||
|
||
/* Entity types. */ | ||
#define RAY_FUNCTION "FUNCTION" | ||
#define RAY_OBJECT "OBJECT" | ||
#define RAY_TASK "TASK" | ||
|
||
typedef struct ray_logger_impl ray_logger; | ||
|
||
/* Initialize a Ray logger for the given client type and logging level. If the | ||
* is_direct flag is set, the logger will treat the given connection as a | ||
* direct connection to the log. Otherwise, it will treat it as a socket to | ||
* another process with a connection to the log. | ||
* NOTE: User is responsible for freeing the returned logger. */ | ||
ray_logger *init_ray_logger(const char *client_type, | ||
int log_level, | ||
int is_direct, | ||
void *conn); | ||
|
||
/* Free the logger. This does not free the connection to the log. */ | ||
void free_ray_logger(ray_logger *logger); | ||
|
||
/* Log an event at the given log level with the given event_type. | ||
* NOTE: message cannot contain spaces! JSON format is recommended. | ||
* TODO: Support spaces in messages. */ | ||
void ray_log(ray_logger *logger, | ||
int log_level, | ||
const char *event_type, | ||
const char *message); | ||
|
||
#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
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