42
42
*
43
43
*
44
44
* IDENTIFICATION
45
- * $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.207 2008/10/09 17:24:05 alvherre Exp $
45
+ * $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.208 2008/10/17 22:56:16 alvherre Exp $
46
46
*
47
47
*-------------------------------------------------------------------------
48
48
*/
@@ -146,6 +146,8 @@ static void append_with_tabs(StringInfo buf, const char *str);
146
146
static bool is_log_level_output (int elevel , int log_min_level );
147
147
static void write_pipe_chunks (char * data , int len , int dest );
148
148
static void write_csvlog (ErrorData * edata );
149
+ static void setup_formatted_log_time (void );
150
+ static void setup_formatted_start_time (void );
149
151
150
152
/*
151
153
* errstart --- begin an error-reporting cycle
@@ -1481,6 +1483,60 @@ write_eventlog(int level, const char *line)
1481
1483
}
1482
1484
#endif /* WIN32 */
1483
1485
1486
+ /*
1487
+ * setup formatted_log_time, for consistent times between CSV and regular logs
1488
+ */
1489
+ static void
1490
+ setup_formatted_log_time (void )
1491
+ {
1492
+ struct timeval tv ;
1493
+ pg_time_t stamp_time ;
1494
+ pg_tz * tz ;
1495
+ char msbuf [8 ];
1496
+
1497
+ gettimeofday (& tv , NULL );
1498
+ stamp_time = (pg_time_t ) tv .tv_sec ;
1499
+
1500
+ /*
1501
+ * Normally we print log timestamps in log_timezone, but during startup we
1502
+ * could get here before that's set. If so, fall back to gmt_timezone
1503
+ * (which guc.c ensures is set up before Log_line_prefix can become
1504
+ * nonempty).
1505
+ */
1506
+ tz = log_timezone ? log_timezone : gmt_timezone ;
1507
+
1508
+ pg_strftime (formatted_log_time , FORMATTED_TS_LEN ,
1509
+ /* leave room for milliseconds... */
1510
+ "%Y-%m-%d %H:%M:%S %Z" ,
1511
+ pg_localtime (& stamp_time , tz ));
1512
+
1513
+ /* 'paste' milliseconds into place... */
1514
+ sprintf (msbuf , ".%03d" , (int ) (tv .tv_usec / 1000 ));
1515
+ strncpy (formatted_log_time + 19 , msbuf , 4 );
1516
+ }
1517
+
1518
+ /*
1519
+ * setup formatted_start_time
1520
+ */
1521
+ static void
1522
+ setup_formatted_start_time (void )
1523
+ {
1524
+ pg_time_t stamp_time = (pg_time_t ) MyStartTime ;
1525
+ pg_tz * tz ;
1526
+
1527
+ /*
1528
+ * Normally we print log timestamps in log_timezone, but during startup we
1529
+ * could get here before that's set. If so, fall back to gmt_timezone
1530
+ * (which guc.c ensures is set up before Log_line_prefix can become
1531
+ * nonempty).
1532
+ */
1533
+ tz = log_timezone ? log_timezone : gmt_timezone ;
1534
+
1535
+ pg_strftime (formatted_start_time , FORMATTED_TS_LEN ,
1536
+ "%Y-%m-%d %H:%M:%S %Z" ,
1537
+ pg_localtime (& stamp_time , tz ));
1538
+ }
1539
+
1484
1540
/*
1485
1541
* Format tag info for log lines; append to the provided buffer.
1486
1542
*/
@@ -1561,34 +1617,8 @@ log_line_prefix(StringInfo buf)
1561
1617
appendStringInfo (buf , "%ld" , log_line_number );
1562
1618
break ;
1563
1619
case 'm' :
1564
- {
1565
- struct timeval tv ;
1566
- pg_time_t stamp_time ;
1567
- pg_tz * tz ;
1568
- char msbuf [8 ];
1569
-
1570
- gettimeofday (& tv , NULL );
1571
- stamp_time = (pg_time_t ) tv .tv_sec ;
1572
-
1573
- /*
1574
- * Normally we print log timestamps in log_timezone, but
1575
- * during startup we could get here before that's set. If
1576
- * so, fall back to gmt_timezone (which guc.c ensures is
1577
- * set up before Log_line_prefix can become nonempty).
1578
- */
1579
- tz = log_timezone ? log_timezone : gmt_timezone ;
1580
-
1581
- pg_strftime (formatted_log_time , FORMATTED_TS_LEN ,
1582
- /* leave room for milliseconds... */
1583
- "%Y-%m-%d %H:%M:%S %Z" ,
1584
- pg_localtime (& stamp_time , tz ));
1585
-
1586
- /* 'paste' milliseconds into place... */
1587
- sprintf (msbuf , ".%03d" , (int ) (tv .tv_usec / 1000 ));
1588
- strncpy (formatted_log_time + 19 , msbuf , 4 );
1589
-
1590
- appendStringInfoString (buf , formatted_log_time );
1591
- }
1620
+ setup_formatted_log_time ();
1621
+ appendStringInfoString (buf , formatted_log_time );
1592
1622
break ;
1593
1623
case 't' :
1594
1624
{
@@ -1606,16 +1636,7 @@ log_line_prefix(StringInfo buf)
1606
1636
break ;
1607
1637
case 's' :
1608
1638
if (formatted_start_time [0 ] == '\0' )
1609
- {
1610
- pg_time_t stamp_time = (pg_time_t ) MyStartTime ;
1611
- pg_tz * tz ;
1612
-
1613
- tz = log_timezone ? log_timezone : gmt_timezone ;
1614
-
1615
- pg_strftime (formatted_start_time , FORMATTED_TS_LEN ,
1616
- "%Y-%m-%d %H:%M:%S %Z" ,
1617
- pg_localtime (& stamp_time , tz ));
1618
- }
1639
+ setup_formatted_start_time ();
1619
1640
appendStringInfoString (buf , formatted_start_time );
1620
1641
break ;
1621
1642
case 'i' :
@@ -1731,32 +1752,8 @@ write_csvlog(ErrorData *edata)
1731
1752
* to put same timestamp in both syslog and csvlog messages.
1732
1753
*/
1733
1754
if (formatted_log_time [0 ] == '\0' )
1734
- {
1735
- struct timeval tv ;
1736
- pg_time_t stamp_time ;
1737
- pg_tz * tz ;
1738
- char msbuf [8 ];
1739
-
1740
- gettimeofday (& tv , NULL );
1741
- stamp_time = (pg_time_t ) tv .tv_sec ;
1742
-
1743
- /*
1744
- * Normally we print log timestamps in log_timezone, but during
1745
- * startup we could get here before that's set. If so, fall back to
1746
- * gmt_timezone (which guc.c ensures is set up before Log_line_prefix
1747
- * can become nonempty).
1748
- */
1749
- tz = log_timezone ? log_timezone : gmt_timezone ;
1750
-
1751
- pg_strftime (formatted_log_time , FORMATTED_TS_LEN ,
1752
- /* leave room for milliseconds... */
1753
- "%Y-%m-%d %H:%M:%S %Z" ,
1754
- pg_localtime (& stamp_time , tz ));
1755
+ setup_formatted_log_time ();
1755
1756
1756
- /* 'paste' milliseconds into place... */
1757
- sprintf (msbuf , ".%03d" , (int ) (tv .tv_usec / 1000 ));
1758
- strncpy (formatted_log_time + 19 , msbuf , 4 );
1759
- }
1760
1757
appendStringInfoString (& buf , formatted_log_time );
1761
1758
appendStringInfoChar (& buf , ',' );
1762
1759
@@ -1813,14 +1810,7 @@ write_csvlog(ErrorData *edata)
1813
1810
1814
1811
/* session start timestamp */
1815
1812
if (formatted_start_time [0 ] == '\0' )
1816
- {
1817
- pg_time_t stamp_time = (pg_time_t ) MyStartTime ;
1818
- pg_tz * tz = log_timezone ? log_timezone : gmt_timezone ;
1819
-
1820
- pg_strftime (formatted_start_time , FORMATTED_TS_LEN ,
1821
- "%Y-%m-%d %H:%M:%S %Z" ,
1822
- pg_localtime (& stamp_time , tz ));
1823
- }
1813
+ setup_formatted_start_time ();
1824
1814
appendStringInfoString (& buf , formatted_start_time );
1825
1815
appendStringInfoChar (& buf , ',' );
1826
1816
0 commit comments