这个时候关于log的,不影响核心逻辑的理解:
- /* Log a fixed message without printf-alike capabilities, in a way that is
- * safe to call from a signal handler.
- *
- * We actually use this only for signals that are not fatal from the point
- * of view of Redis. Signals that are going to kill the server anyway and
- * where we need printf-alike features are served by serverLog(). */
- void serverLogFromHandler(int level, const char *msg) {
- int fd;
- int log_to_stdout = server.logfile[0] == '\0';
- char buf[64];
-
- if ((level&0xff) < server.verbosity || (log_to_stdout && server.daemonize))
- return;
- fd = log_to_stdout ? STDOUT_FILENO :
- open(server.logfile, O_APPEND|O_CREAT|O_WRONLY, 0644);
- if (fd == -1) return;
- 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);
- }
-
- /* Return the UNIX time in microseconds */
- long long ustime(void) {
- struct timeval tv;
- long long ust;
-
- gettimeofday(&tv, NULL);
- ust = ((long long)tv.tv_sec)*1000000;
- ust += tv.tv_usec;
- return ust;
- }
-
- /* Return the UNIX time in milliseconds */
- mstime_t mstime(void) {
- return ustime()/1000;
- }
-
- /* After an RDB dump or AOF rewrite we exit from children using _exit() instead of
- * exit(), because the latter may interact with the same file objects used by
- * the parent process. However if we are testing the coverage normal exit() is
- * used in order to obtain the right coverage information. */
- void exitFromChild(int retcode) {
- #ifdef COVERAGE_TEST
- exit(retcode);
- #else
- _exit(retcode);
- #endif
- }
-
- /*====================== Hash table type implementation ==================== */
-
- /* This is a hash table type that uses the SDS dynamic strings library as
- * keys and redis objects as values (objects can hold SDS strings,
- * lists, sets). */
-
- void dictVanillaFree(void *privdata, void *val)
- {
- DICT_NOTUSED(privdata);
- zfree(val);
- }
-
- void dictListDestructor(void *privdata, void *val)
- {
- DICT_NOTUSED(privdata);
- listRelease((list*)val);
- }
-
- int dictSdsKeyCompare(void *privdata, const void *key1,
- const void *key2)
- {
- int l1,l2;
- DICT_NOTUSED(privdata);
-
- l1 = sdslen((sds)key1);
- l2 = sdslen((sds)key2);
- if (l1 != l2) return 0;
- return memcmp(key1, key2, l1) == 0;
- }
-
- /* A case insensitive version used for the command lookup table and other
- * places where case insensitive non binary-safe comparison is needed. */
- int dictSdsKeyCaseCompare(void *privdata, const void *key1,
- const void *key2)
- {
- DICT_NOTUSED(privdata);
-
- return strcasecmp(key1, key2) == 0;
- }
-
- void dictObjectDestructor(void *privdata, void *val)
- {
- DICT_NOTUSED(privdata);
-
- if (val == NULL) return; /* Lazy freeing will set value to NULL. */
- decrRefCount(val);
- }
-
- void dictSdsDestructor(void *privdata, void *val)
- {
- DICT_NOTUSED(privdata);
-
- sdsfree(val);
- }
-
- int dictObjKeyCompare(void *privdata, const void *key1,
- const void *key2)
- {
- const robj *o1 = key1, *o2 = key2;
- return dictSdsKeyCompare(privdata,o1->ptr,o2->ptr);
- }
-
- uint64_t dictObjHash(const void *key) {
- const robj *o = key;
- return dictGenHashFunction(o->ptr, sdslen((sds)o->ptr));
- }
-
- uint64_t dictSdsHash(const void *key) {
- return dictGenHashFunction((unsigned char*)key, sdslen((char*)key));
- }
-
- uint64_t dictSdsCaseHash(const void *key) {
- return dictGenCaseHashFunction((unsigned char*)key, sdslen((char*)key));
- }
-
- int dictEncObjKeyCompare(void *privdata, const void *key1,
- const void *key2)
- {
- robj *o1 = (robj*) key1, *o2 = (robj*) key2;
- int cmp;
-
- if (o1->encoding == OBJ_ENCODING_INT &&
- o2->encoding == OBJ_ENCODING_INT)
- return o1->ptr == o2->ptr;
-
- /* Due to OBJ_STATIC_REFCOUNT, we avoid calling getDecodedObject() without
- * good reasons, because it would incrRefCount() the object, which
- * is invalid. So we check to make sure dictFind() works with static
- * objects as well. */
- if (o1->refcount != OBJ_STATIC_REFCOUNT) o1 = getDecodedObject(o1);
- if (o2->refcount != OBJ_STATIC_REFCOUNT) o2 = getDecodedObject(o2);
- cmp = dictSdsKeyCompare(privdata,o1->ptr,o2->ptr);
- if (o1->refcount != OBJ_STATIC_REFCOUNT) decrRefCount(o1);
- if (o2->refcount != OBJ_STATIC_REFCOUNT) decrRefCount(o2);
- return cmp;
- }