Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3e00496

Browse files
committed
Refactor some duplicate code to set up formatted_log_time and
formatted_start_time.
1 parent e4fb8ff commit 3e00496

File tree

1 file changed

+62
-72
lines changed

1 file changed

+62
-72
lines changed

src/backend/utils/error/elog.c

+62-72
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
*
4343
*
4444
* 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 $
4646
*
4747
*-------------------------------------------------------------------------
4848
*/
@@ -146,6 +146,8 @@ static void append_with_tabs(StringInfo buf, const char *str);
146146
static bool is_log_level_output(int elevel, int log_min_level);
147147
static void write_pipe_chunks(char *data, int len, int dest);
148148
static void write_csvlog(ErrorData *edata);
149+
static void setup_formatted_log_time(void);
150+
static void setup_formatted_start_time(void);
149151

150152
/*
151153
* errstart --- begin an error-reporting cycle
@@ -1481,6 +1483,60 @@ write_eventlog(int level, const char *line)
14811483
}
14821484
#endif /* WIN32 */
14831485

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+
14841540
/*
14851541
* Format tag info for log lines; append to the provided buffer.
14861542
*/
@@ -1561,34 +1617,8 @@ log_line_prefix(StringInfo buf)
15611617
appendStringInfo(buf, "%ld", log_line_number);
15621618
break;
15631619
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);
15921622
break;
15931623
case 't':
15941624
{
@@ -1606,16 +1636,7 @@ log_line_prefix(StringInfo buf)
16061636
break;
16071637
case 's':
16081638
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();
16191640
appendStringInfoString(buf, formatted_start_time);
16201641
break;
16211642
case 'i':
@@ -1731,32 +1752,8 @@ write_csvlog(ErrorData *edata)
17311752
* to put same timestamp in both syslog and csvlog messages.
17321753
*/
17331754
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();
17551756

1756-
/* 'paste' milliseconds into place... */
1757-
sprintf(msbuf, ".%03d", (int) (tv.tv_usec / 1000));
1758-
strncpy(formatted_log_time + 19, msbuf, 4);
1759-
}
17601757
appendStringInfoString(&buf, formatted_log_time);
17611758
appendStringInfoChar(&buf, ',');
17621759

@@ -1813,14 +1810,7 @@ write_csvlog(ErrorData *edata)
18131810

18141811
/* session start timestamp */
18151812
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();
18241814
appendStringInfoString(&buf, formatted_start_time);
18251815
appendStringInfoChar(&buf, ',');
18261816

0 commit comments

Comments
 (0)