Browse Source

format_tm: Refactor BTF_TIME/BTF_USEC into BTF_LRTIME (low res), BTF_TIME, and BTF_HRTIME (high res)

Luke Dashjr 12 years ago
parent
commit
654479ded3
3 changed files with 17 additions and 12 deletions
  1. 1 1
      logging.c
  2. 11 7
      util.c
  3. 5 4
      util.h

+ 1 - 1
logging.c

@@ -65,7 +65,7 @@ void _applog(int prio, const char *str)
 			struct timeval tv;
 			bfg_init_time();
 			bfg_gettimeofday(&tv);
-			format_timestamp(datetime, BTF_DATE | BTF_TIME | BTF_USEC | BTF_BRACKETS, &tv);
+			format_timestamp(datetime, BTF_DATE | BTF_HRTIME | BTF_BRACKETS, &tv);
 		}
 		else
 			get_now_datestamp(datetime);

+ 11 - 7
util.c

@@ -1252,19 +1252,23 @@ int format_tm(char * const buf, const int fmt, const struct tm * const tm, const
 		        tm->tm_year + 1900,
 		        tm->tm_mon + 1,
 		        tm->tm_mday);
-		if (fmt & BTF_TIME)
+		if (fmt & 3 /* any time */)
 			(s++)[0] = ' ';
 	}
-	if (fmt & BTF_TIME)
+	if (fmt & 3 /* any time */)
 	{
 		s +=
-		sprintf(s, "%02d:%02d:%02d",
+		sprintf(s, "%02d:%02d",
 		        tm->tm_hour,
-		        tm->tm_min,
-		        tm->tm_sec);
-		if (fmt & BTF_USEC)
+		        tm->tm_min);
+		if (fmt & 1 /* with seconds */)
+		{
 			s +=
-			sprintf(s, ".%06ld", usecs);
+			sprintf(s, ":%02d", tm->tm_sec);
+			if ((fmt & BTF_HRTIME) == BTF_HRTIME)
+				s +=
+				sprintf(s, ".%06ld", usecs);
+		}
 	}
 	if (fmt & BTF_BRACKETS)
 		s +=

+ 5 - 4
util.h

@@ -301,10 +301,11 @@ extern void bfg_gettimeofday(struct timeval *);
 #endif
 
 enum timestamp_format {
-	BTF_DATE = 1,
-	BTF_TIME = 2,
-	BTF_USEC = 4,
-	BTF_BRACKETS = 8,
+	BTF_LRTIME = 2,  // low resolution time (eg, HH:MM)
+	BTF_TIME   = 1,  // eg, HH:MM:SS
+	BTF_HRTIME = 3,  // high resolution time (eg, HH:MM:SS.MICROS)
+	BTF_DATE   = 8,
+	BTF_BRACKETS = 0x10,
 };
 
 extern int format_tm(char * const buf, const int fmt, const struct tm * const, const long usecs);