2016-09-13 18:54:26 -07:00
|
|
|
#ifndef COMMON_H
|
|
|
|
#define COMMON_H
|
|
|
|
|
2016-09-23 22:53:58 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-09-18 18:06:42 -07:00
|
|
|
#include <string.h>
|
2016-09-13 18:54:26 -07:00
|
|
|
#include <errno.h>
|
|
|
|
|
2016-10-03 17:55:57 -07:00
|
|
|
#ifndef RAY_COMMON_DEBUG
|
2016-09-13 18:54:26 -07:00
|
|
|
#define LOG_DEBUG(M, ...)
|
|
|
|
#else
|
|
|
|
#define LOG_DEBUG(M, ...) \
|
|
|
|
fprintf(stderr, "[DEBUG] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define LOG_ERR(M, ...) \
|
|
|
|
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, \
|
|
|
|
errno == 0 ? "None" : strerror(errno), ##__VA_ARGS__)
|
|
|
|
|
|
|
|
#define LOG_INFO(M, ...) \
|
|
|
|
fprintf(stderr, "[INFO] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
|
|
|
|
|
2016-09-17 00:03:10 -07:00
|
|
|
#define CHECK(COND) \
|
|
|
|
do { \
|
|
|
|
if (!(COND)) { \
|
|
|
|
LOG_ERR("Check failure: %s", #COND); \
|
|
|
|
exit(-1); \
|
|
|
|
} \
|
|
|
|
} while (0);
|
|
|
|
|
2016-10-03 17:55:57 -07:00
|
|
|
#define CHECKM(COND, M, ...) \
|
|
|
|
do { \
|
|
|
|
if (!(COND)) { \
|
|
|
|
LOG_ERR("Check failure: %s \n" M, #COND, ##__VA_ARGS__); \
|
|
|
|
exit(-1); \
|
|
|
|
} \
|
|
|
|
} while (0);
|
|
|
|
|
2016-09-13 18:54:26 -07:00
|
|
|
#define UNIQUE_ID_SIZE 20
|
|
|
|
|
2016-09-22 23:15:45 -07:00
|
|
|
/* Cleanup method for running tests with the greatest library.
|
|
|
|
* Runs the test, then clears the Redis database. */
|
2016-09-20 22:40:35 -07:00
|
|
|
#define RUN_REDIS_TEST(context, test) \
|
|
|
|
RUN_TEST(test); \
|
|
|
|
freeReplyObject(redisCommand(context, "FLUSHALL"));
|
|
|
|
|
2016-09-13 18:54:26 -07:00
|
|
|
typedef struct { unsigned char id[UNIQUE_ID_SIZE]; } unique_id;
|
|
|
|
|
2016-09-29 21:12:06 -07:00
|
|
|
extern const unique_id NIL_ID;
|
|
|
|
|
2016-09-18 18:06:42 -07:00
|
|
|
/* Generate a globally unique ID. */
|
|
|
|
unique_id globally_unique_id(void);
|
|
|
|
|
2016-09-13 18:54:26 -07:00
|
|
|
/* Convert a 20 byte sha1 hash to a hexdecimal string. This function assumes
|
|
|
|
* that buffer points to an already allocated char array of size 2 *
|
|
|
|
* UNIQUE_ID_SIZE + 1 */
|
|
|
|
char *sha1_to_hex(const unsigned char *sha1, char *buffer);
|
|
|
|
|
2016-09-20 17:02:56 -07:00
|
|
|
typedef unique_id object_id;
|
|
|
|
|
2016-09-13 18:54:26 -07:00
|
|
|
#endif
|