]> git.feebdaed.xyz Git - socialize.git/commitdiff
add logger
authorseantywork <seantywork@gmail.com>
Wed, 23 Jul 2025 00:51:24 +0000 (09:51 +0900)
committerseantywork <seantywork@gmail.com>
Wed, 23 Jul 2025 00:51:24 +0000 (09:51 +0900)
README.md
hack/tls.sh
include/socialize/utils.h
src/utils.c

index a75a5d6b077406590575b11718f7e1b21784a329..df186870c96273fdae23fada87f426439f2b2bfa 100644 (file)
--- a/README.md
+++ b/README.md
@@ -1,3 +1,54 @@
 # socialize
 
-I socialize on terminal
\ No newline at end of file
+I socialize on terminal
+
+## develop
+
+```shell
+
+# build
+
+make deps
+
+make dev
+
+```
+
+```shell
+
+# generate certs
+
+./hack/tls.sh
+
+
+```
+
+
+```shell
+# run engine
+
+./engine.out
+```
+
+```shell
+# run client
+
+./cli.out "addr:port" number | cert location
+
+# ex) ./cli.out "127.0.0.1:3001" tls/sub1.crt.pem
+
+```
+
+
+```shell
+# it is also possible to login as admin specified in config.json
+# at endpoint ${addr}:3000
+
+# using command below
+
+# gencert
+#   : generates client cert
+#   - data: client id
+
+
+```
\ No newline at end of file
index 81dbea243ab580939e6679af721a44777164795d..960ad8edd066a5d64429db4b0d2c802a5159db1a 100755 (executable)
@@ -48,6 +48,6 @@ echo "signiing sub2..."
 openssl  x509 -req -extfile <(printf "subjectAltName = DNS:sub2.test") -days 180 -in sub2.csr -CA ca.crt.pem -CAkey ca_priv.pem -CAcreateserial -sha256 -out sub2.crt.pem
 
 
-/bin/cp -Rf *.pem ../tls/
+/bin/cp -Rf *.pem ./tls/
 
 rm -rf *.pem *.srl *.csr
\ No newline at end of file
index ce5b2bfa078ac56ef84f20c2e8234037ac69c2b2..881327cea2316efdb8c8823ad6753a10c2021832 100644 (file)
@@ -26,9 +26,9 @@ void get_current_time_string(char* tstr);
 
 void sleepms(long ms);
 
-void fmt_logln(FILE *fp, char* fmt_out, ...);
+#define fmt_logln(fp, fmt, ...) _fmt_logln(fp,__FILE__, __LINE__, fmt,  ##__VA_ARGS__)
+
+void _fmt_logln(FILE *fp, char* file, int line, char* fmt_out, ...);
 
-// TODO:
-//  logger
 
 #endif
index 07798031655ac2d9a60ad811819797d31ddbf1d9..fa983db046f8cf1c70721f03ebe821ac0ff8088b 100644 (file)
@@ -143,17 +143,15 @@ int get_host_port(char* hostname, int* port, char* addr){
 void get_current_time_string(char* tstr){
 
 
-    struct tm *info;
+    struct tm info;
     
     int millisec;
     char msec_str[5] = {0};
 
     struct timeval tv;
 
-
     gettimeofday(&tv, NULL);
 
-
     millisec = tv.tv_usec / 1000;
 
     if (millisec >= 1000){
@@ -163,18 +161,14 @@ void get_current_time_string(char* tstr){
 
     }
 
+    localtime_r(&tv.tv_sec, &info);
 
-
-
-    info = localtime(&tv.tv_sec);
-
-    strftime(tstr, MAX_TIMESTR_LEN, "%Y-%m-%d %H:%M:%S", info);
+    strftime(tstr, MAX_TIMESTR_LEN, "%Y-%m-%d %H:%M:%S", &info);
 
     sprintf(msec_str, ".%03d", millisec);
 
     strcat(tstr, msec_str);
 
-
 }
 
 void sleepms(long ms){
@@ -192,117 +186,36 @@ void sleepms(long ms){
 
 }
 
-void fmt_logln(FILE *fp, char* fmt_out, ...){
 
-    va_list valist;
+int _log_lock_init = 0;
+pthread_mutex_t _log_lock;
 
-    char log_str[MAX_LOG_TXT_LEN] = {0};
+static inline void _do_log(FILE *fp, char* file, int line, char* fmt_out, va_list args){
 
     char log_fmt_str[MAX_LOG_TXT_LEN] = {0};
-
-    char fmt_arg[MAX_FMT_ARG_LEN] = {0};
-
     char time_str[MAX_TIMESTR_LEN] = {0};
 
-
-
-    int arg_len = 0;
-
-    int str_len = strlen(fmt_out);
-
-    if(str_len < 1){
-        return;
-    }
-
-    for(int i = 0; i < str_len; i++){
-
-        if(fmt_out[i] == '%' && i != 0 && fmt_out[i - 1] != '\\'){
-
-            arg_len += 1;
-
-        }
-
-    }
-
     get_current_time_string(time_str);
 
-    if(arg_len == 0){
-
-        sprintf(log_str, "[ %s ] %s\n", time_str, fmt_out);
-
-    } else {
-
-        char fmt_flag[3] = {0};
-
-        va_start(valist, arg_len);
-
-        int i = 0;
-
-        int fmt_i = 0;
-
-        for(;;){
-            
-            if(fmt_out[i] == '%' && i != 0 && i != (str_len - 1) && fmt_out[i - 1] != '\\'){
-
-                fmt_flag[0] = fmt_out[i];
-
-                i += 1;
-
-                fmt_flag[1] = fmt_out[i];
-
-                fmt_flag[2] = 0;
-
-                memset(fmt_arg, 0, MAX_FMT_ARG_LEN);
-
-                if(fmt_flag[1] == 'd'){
-
-                    int a = va_arg(valist, int);
-
-                    sprintf(fmt_arg, fmt_flag, a);
-
-
-                } else if (fmt_flag[1] == 's') {
-
-                    char* a = va_arg(valist, char*);
-
-                    sprintf(fmt_arg, fmt_flag, a);                    
-
-                }
-
-                strcat(log_fmt_str, fmt_arg);
-                
-                int added = strlen(fmt_arg);
-
-                i += 1;
-                fmt_i += added;
-
-
-            } else {
-
-                log_fmt_str[fmt_i] = fmt_out[i];
-
-                i += 1;
-                fmt_i += 1;
-
-            }
-
-
-            if(i >= str_len){
-                break;
-            }
+    sprintf(log_fmt_str, "[%s][%s:%d] ", time_str, file, line);
+    strcat(log_fmt_str, fmt_out);
+    strcat(log_fmt_str, "\n");
+    vfprintf(fp, log_fmt_str, args);
+    fflush(fp);
 
-        }
+}
 
-        va_end(valist);
+void _fmt_logln(FILE *fp, char* file, int line, char* fmt_out, ...){
 
-        sprintf(log_str, "[ %s ] %s\n", time_str, log_fmt_str);
+    if(!_log_lock_init){
+        pthread_mutex_init(&_log_lock, NULL);
+        _log_lock_init = 1;
     }
 
-
-    fputs(log_str, fp);
-
-    fflush(fp);
-
-
-
+    va_list args;
+    pthread_mutex_lock(&_log_lock);
+    va_start(args, fmt_out);
+    _do_log(fp, file, line, fmt_out, args);
+    va_end(args);
+    pthread_mutex_unlock(&_log_lock);
 }
\ No newline at end of file