فهرست منبع

feat: support %f for us and %q for ms in #format {%t}, also in #log timestamp

dzp 1 سال پیش
والد
کامیت
1cd8327b5c
3فایلهای تغییر یافته به همراه54 افزوده شده و 4 حذف شده
  1. 2 3
      src/log.c
  2. 1 0
      src/tintin.h
  3. 51 1
      src/variable.c

+ 2 - 3
src/log.c

@@ -261,13 +261,12 @@ void logit(struct session *ses, char *txt, FILE *file, int flags)
 	{
 		if (ses->log->stamp_time != gtd->time)
 		{
-			struct tm timeval_tm = *localtime(&gtd->time);
-
 			ses->log->stamp_time = gtd->time;
 
 			substitute(ses, ses->log->stamp_strf, out, SUB_COL|SUB_ESC|SUB_VAR|SUB_FUN);
 
-			strftime(ses->log->stamp_text, 99, out, &timeval_tm);
+			timestring(ses, out);
+			strcpy(ses->log->stamp_text, out);
 		}
 		fputs(ses->log->stamp_text, file);
 	}

+ 1 - 0
src/tintin.h

@@ -3060,6 +3060,7 @@ extern void  charactertonumber(struct session *ses, char *str);
 extern  int  delete_variable(struct session *ses, char *variable);
 extern void  justify_string(struct session *ses, char *in, char *out, int align, int cut);
 extern void  format_string(struct session *ses, char *format, char *arg, char *out);
+extern void timestring(struct session *ses, char *str);
 extern struct listnode *search_variable(struct session *ses, char *variable);
 extern struct listnode *get_variable(struct session *ses, char *variable, char *result);
 extern struct listnode *set_variable(struct session *ses, char *variable, char *format, ...);

+ 51 - 1
src/variable.c

@@ -1222,8 +1222,10 @@ int string_raw_str_len(struct session *ses, char *str, int raw_start, int raw_en
 void timestring(struct session *ses, char *str)
 {
 	char *arg, *arg1, *arg2;
+	char *format, *ptms, *ptus;
 	struct tm timeval_tm;
 	time_t    timeval_t;
+	int       ms, us;
 
 	push_call("timestring(%p,%p)",ses,str);
 
@@ -1238,15 +1240,63 @@ void timestring(struct session *ses, char *str)
 	}
 	arg = get_arg_in_braces(ses, arg, arg2, GET_ALL);
 
+	ptms = strstr(arg1, "%q");
+	ptus = strstr(arg1, "%f");
+
 	if (*arg2)
 	{
-		timeval_t = (time_t) get_number(ses, arg2);
+		unsigned long long time = get_ulong(ses, arg2);
+		if (time >= 1000000000LL * 1000000LL)
+		{
+			us = time % 1000000;
+			ms = us / 1000;
+			time /= 1000000;
+		}
+		else
+		{
+			us = 0;
+			ms = 0;
+		}
+		timeval_t = (time_t) time;
+	}
+	else if (ptms || ptus)
+	{
+		struct timeval now_time;
+		gettimeofday(&now_time, NULL);
+		timeval_t = now_time.tv_sec;
+		us = now_time.tv_usec;
+		ms = us / 1000;
 	}
 	else
 	{
 		timeval_t = gtd->time;
 	}
 
+	if (ptms || ptus)
+	{
+		int prefix;
+		if (ptms)
+		{
+			ptms = strstr(arg1, "%q");
+			format = str_alloc_stack(0);
+			prefix = ptms - arg1;
+			memcpy(format, arg1, prefix);
+			sprintf(format + prefix, "%03d", ms);
+			strcpy(format + prefix + 3, ptms + 2);
+			arg1 = format;
+		}
+		if (ptus)
+		{
+			ptus = strstr(arg1, "%f");
+			format = str_alloc_stack(0);
+			prefix = ptus - arg1;
+			memcpy(format, arg1, prefix);
+			sprintf(format + prefix, "%06d", us);
+			strcpy(format + prefix + 6, ptus + 2);
+			arg1 = format;
+		}
+	}
+
 	timeval_tm = *localtime(&timeval_t);
 
 	strftime(str, BUFFER_SIZE, arg1, &timeval_tm);