1
0
ithewei 6 жил өмнө
parent
commit
c37606493e
4 өөрчлөгдсөн 61 нэмэгдсэн , 15 устгасан
  1. 1 1
      base/hlog.c
  2. 37 0
      base/hversion.c
  3. 16 1
      base/hversion.h
  4. 7 13
      utils/hmain.cpp

+ 1 - 1
base/hlog.c

@@ -258,7 +258,7 @@ int logger_print(logger_t* logger, int level, const char* fmt, ...) {
     hmutex_lock(&logger->mutex_);
     char* buf = logger->buf;
     int bufsize = logger->bufsize;
-    int len = snprintf(buf, bufsize, "%s[%04d-%02d-%02d %02d:%02d:%02d.%03d][%s]: ",
+    int len = snprintf(buf, bufsize, "%s[%04d-%02d-%02d %02d:%02d:%02d.%03d][%s] ",
         pcolor,
         year, month, day, hour, min, sec, ms, plevel);
 

+ 37 - 0
base/hversion.c

@@ -9,3 +9,40 @@ const char* get_compile_version() {
         H_VERSION_MAJOR, dt.year%100, dt.month, dt.day);
     return s_version;
 }
+
+int version_atoi(const char* str) {
+    int hex = 0;
+
+    // trim v1.2.3.4
+    const char* pv = strchr(str, 'v');
+    const char* pdot = pv ? pv+1 : str;
+
+    while (1) {
+        hex = (hex << 8) | atoi(pdot);
+        pdot = strchr(pdot, '.');
+        if (pdot == NULL)   break;
+        ++pdot;
+    }
+
+    return hex;
+}
+
+void version_itoa(int num, char* str) {
+    char* ch = (char*)&num;
+    sprintf(str, "%d.%d.%d.%d", ch[3], ch[2], ch[1], ch[0]);
+
+    // trim 0.1.2.3
+    const char* p = str;
+    while (1) {
+        if (p[0] == '0' && p[1] == '.') {
+            p += 2;
+        }
+        else {
+            break;
+        }
+    }
+
+    if (p != str) {
+        strcpy(str, p);
+    }
+}

+ 16 - 1
base/hversion.h

@@ -1,6 +1,10 @@
 #ifndef HW_VERSION_H_
 #define HW_VERSION_H_
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 #include "hdef.h"
 
 #define H_VERSION_MAJOR   1
@@ -15,10 +19,21 @@
 
 #define H_VERSION_NUMBER    (H_VERSION_MAJOR << 24) | (H_VERSION_MINOR << 16) | (H_VERSION_MICRO << 8) | H_VERSION_PATCH
 
+
 static inline const char* get_static_version() {
     return H_VERSION_STRING;
 }
 
-EXTERN_C const char* get_compile_version();
+const char* get_compile_version();
+
+// 1.2.3.4 => 0x01020304
+int version_atoi(const char* str);
+
+// 0x01020304 => 1.2.3.4
+void version_itoa(int hex, char* str);
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
 
 #endif  // HW_VERSION_H_

+ 7 - 13
utils/hmain.cpp

@@ -283,47 +283,41 @@ const char* get_env(const char* key) {
 void setproctitle(const char* title) {
     //printf("proctitle=%s\n", title);
     int len = g_main_ctx.arg_len + g_main_ctx.env_len;
-    if (strlen(title) >= len)   return;
-    memset(g_main_ctx.os_argv[0], 0, len);
-    strcpy(g_main_ctx.os_argv[0], title);
+    strncpy(g_main_ctx.os_argv[0], title, len-1);
 }
 #endif
 
 int create_pidfile() {
     FILE* fp = fopen(g_main_ctx.pidfile, "w");
     if (fp == NULL) {
-        printf("fopen [%s] error: %d\n", g_main_ctx.pidfile, errno);
-        return -10;
+        hloge("fopen('%s') error: %d\n", g_main_ctx.pidfile, errno);
+        return -1;
     }
 
     char pid[16] = {0};
     snprintf(pid, sizeof(pid), "%d\n", g_main_ctx.pid);
     fwrite(pid, 1, strlen(pid), fp);
     fclose(fp);
-    hlogi("create_pidfile [%s] pid=%d", g_main_ctx.pidfile, g_main_ctx.pid);
+    hlogi("create_pidfile('%s') pid=%d", g_main_ctx.pidfile, g_main_ctx.pid);
     atexit(delete_pidfile);
     return 0;
 }
 
 void delete_pidfile() {
+    hlogi("delete_pidfile('%s') pid=%d", g_main_ctx.pidfile, g_main_ctx.pid);
     remove(g_main_ctx.pidfile);
-    hlogi("delete_pidfile [%s]", g_main_ctx.pidfile);
 }
 
 pid_t getpid_from_pidfile() {
     FILE* fp = fopen(g_main_ctx.pidfile, "r");
     if (fp == NULL) {
-        //printf("fopen [%s] error: %d\n", g_conf_ctx.pidfile, errno);
+        hloge("fopen('%s') error: %d\n", g_main_ctx.pidfile, errno);
         return -1;
     }
     char pid[64];
     int readbytes = fread(pid, 1, sizeof(pid), fp);
     fclose(fp);
-    if (readbytes <= 0) {
-        //printf("fread [%s] bytes=%d\n", g_main_ctx.pidfile, readbytes);
-        return -1;
-    }
-    return atoi(pid);
+    return readbytes <= 0 ? -1 : atoi(pid);
 }
 
 static procedure_t s_reload_fn = NULL;