Browse Source

Abstract timestamp formatting to format_timestamp/format_time_t function

Luke Dashjr 12 years ago
parent
commit
e48a6826e3
5 changed files with 52 additions and 44 deletions
  1. 1 12
      logging.c
  2. 0 30
      miner.c
  3. 0 2
      miner.h
  4. 38 0
      util.c
  5. 13 0
      util.h

+ 1 - 12
logging.c

@@ -63,20 +63,9 @@ void _applog(int prio, const char *str)
 		if (opt_log_microseconds)
 		if (opt_log_microseconds)
 		{
 		{
 			struct timeval tv;
 			struct timeval tv;
-			struct tm tm;
-			
 			bfg_init_time();
 			bfg_init_time();
 			bfg_gettimeofday(&tv);
 			bfg_gettimeofday(&tv);
-			localtime_r(&tv.tv_sec, &tm);
-			
-			sprintf(datetime, "[%d-%02d-%02d %02d:%02d:%02d.%06ld]",
-				tm.tm_year + 1900,
-				tm.tm_mon + 1,
-				tm.tm_mday,
-				tm.tm_hour,
-				tm.tm_min,
-				tm.tm_sec,
-				(long)tv.tv_usec);
+			format_timestamp(datetime, BTF_DATE | BTF_TIME | BTF_USEC | BTF_BRACKETS, &tv);
 		}
 		}
 		else
 		else
 			get_now_datestamp(datetime);
 			get_now_datestamp(datetime);

+ 0 - 30
miner.c

@@ -416,36 +416,6 @@ static bool should_run(void)
 	return within_range;
 	return within_range;
 }
 }
 
 
-void get_datestamp(char *f, time_t tt)
-{
-	struct tm _tm;
-	struct tm *tm = &_tm;
-	
-	if (tt == INVALID_TIMESTAMP)
-		tt = time(NULL);
-
-	localtime_r(&tt, tm);
-	sprintf(f, "[%d-%02d-%02d %02d:%02d:%02d]",
-		tm->tm_year + 1900,
-		tm->tm_mon + 1,
-		tm->tm_mday,
-		tm->tm_hour,
-		tm->tm_min,
-		tm->tm_sec);
-}
-
-void get_timestamp(char *f, time_t tt)
-{
-	struct tm _tm;
-	struct tm *tm = &_tm;
-
-	localtime_r(&tt, tm);
-	sprintf(f, "[%02d:%02d:%02d]",
-		tm->tm_hour,
-		tm->tm_min,
-		tm->tm_sec);
-}
-
 static void applog_and_exit(const char *fmt, ...) FORMAT_SYNTAX_CHECK(printf, 1, 2);
 static void applog_and_exit(const char *fmt, ...) FORMAT_SYNTAX_CHECK(printf, 1, 2);
 
 
 static char exit_buf[512];
 static char exit_buf[512];

+ 0 - 2
miner.h

@@ -1273,8 +1273,6 @@ struct work {
 	struct work *next;
 	struct work *next;
 };
 };
 
 
-extern void get_datestamp(char *, time_t);
-#define get_now_datestamp(buf)  get_datestamp(buf, INVALID_TIMESTAMP)
 extern void inc_hw_errors(struct thr_info *, const struct work *, const uint32_t bad_nonce);
 extern void inc_hw_errors(struct thr_info *, const struct work *, const uint32_t bad_nonce);
 #define inc_hw_errors_only(thr)  inc_hw_errors(thr, NULL, 0)
 #define inc_hw_errors_only(thr)  inc_hw_errors(thr, NULL, 0)
 enum test_nonce2_result {
 enum test_nonce2_result {

+ 38 - 0
util.c

@@ -1239,6 +1239,44 @@ void bfg_init_time()
 #endif
 #endif
 }
 }
 
 
+int format_timestamp(char * const buf, const int fmt, const struct timeval * const tv)
+{
+	struct tm tm;
+	char *s = buf;
+	time_t tt = tv->tv_sec;
+	
+	localtime_r(&tt, &tm);
+	
+	if (fmt & BTF_BRACKETS)
+		(s++)[0] = '[';
+	if (fmt & BTF_DATE)
+	{
+		s +=
+		sprintf(s, "%d-%02d-%02d",
+		        tm.tm_year + 1900,
+		        tm.tm_mon + 1,
+		        tm.tm_mday);
+		if (fmt & BTF_TIME)
+			(s++)[0] = ' ';
+	}
+	if (fmt & BTF_TIME)
+	{
+		s +=
+		sprintf(s, "%02d:%02d:%02d",
+		        tm.tm_hour,
+		        tm.tm_min,
+		        tm.tm_sec);
+		if (fmt & BTF_USEC)
+			s +=
+			sprintf(s, ".%06ld", (long)tv->tv_usec);
+	}
+	if (fmt & BTF_BRACKETS)
+		s +=
+		sprintf(s, "]");
+	
+	return (s - buf);
+}
+
 void subtime(struct timeval *a, struct timeval *b)
 void subtime(struct timeval *a, struct timeval *b)
 {
 {
 	timersub(a, b, b);
 	timersub(a, b, b);

+ 13 - 0
util.h

@@ -300,6 +300,19 @@ extern void bfg_gettimeofday(struct timeval *);
 #define bfg_gettimeofday(out)  gettimeofday(out, NULL)
 #define bfg_gettimeofday(out)  gettimeofday(out, NULL)
 #endif
 #endif
 
 
+enum timestamp_format {
+	BTF_DATE = 1,
+	BTF_TIME = 2,
+	BTF_USEC = 4,
+	BTF_BRACKETS = 8,
+};
+
+extern int format_timestamp(char * const buf, const int fmt, const struct timeval * const);
+#define format_time_t(buf, fmt, tt)  format_timestamp(buf, fmt, (struct timeval[1]){ { .tv_sec = tt } })
+#define get_datestamp(buf, tt)  format_time_t(buf, BTF_DATE | BTF_TIME | BTF_BRACKETS, tt)
+#define get_now_datestamp(buf)  get_datestamp(buf, time(NULL))
+#define get_timestamp(buf, tt)  format_time_t(buf, BTF_TIME | BTF_BRACKETS, tt)
+
 static inline
 static inline
 void reduce_timeout_to(struct timeval *tvp_timeout, struct timeval *tvp_time)
 void reduce_timeout_to(struct timeval *tvp_timeout, struct timeval *tvp_time)
 {
 {