1818 *
1919 *
2020 * IDENTIFICATION
21- * $PostgreSQL: pgsql/src/backend/postmaster/syslogger.c,v 1.52 2009/11/05 20:13:06 petere Exp $
21+ * $PostgreSQL: pgsql/src/backend/postmaster/syslogger.c,v 1.53 2009/11/19 02:45:33 tgl Exp $
2222 *
2323 *-------------------------------------------------------------------------
2424 */
@@ -140,7 +140,7 @@ static void open_csvlogfile(void);
140140static unsignedint __stdcallpipeThread (void * arg );
141141#endif
142142static void logfile_rotate (bool time_based_rotation ,int size_rotation_for );
143- static char * logfile_getname (pg_time_t timestamp ,char * suffix );
143+ static char * logfile_getname (pg_time_t timestamp ,const char * suffix );
144144static void set_next_rotation_time (void );
145145static void sigHupHandler (SIGNAL_ARGS );
146146static void sigUsr1Handler (SIGNAL_ARGS );
@@ -1016,6 +1016,7 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
10161016{
10171017char * filename ;
10181018char * csvfilename = NULL ;
1019+ pg_time_t fntime ;
10191020FILE * fh ;
10201021
10211022rotation_requested = false;
@@ -1026,17 +1027,12 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
10261027 * file name when we don't do the rotation immediately.
10271028 */
10281029if (time_based_rotation )
1029- {
1030- filename = logfile_getname (next_rotation_time ,NULL );
1031- if (csvlogFile != NULL )
1032- csvfilename = logfile_getname (next_rotation_time ,".csv" );
1033- }
1030+ fntime = next_rotation_time ;
10341031else
1035- {
1036- filename = logfile_getname (time (NULL ),NULL );
1037- if (csvlogFile != NULL )
1038- csvfilename = logfile_getname (time (NULL ),".csv" );
1039- }
1032+ fntime = time (NULL );
1033+ filename = logfile_getname (fntime ,NULL );
1034+ if (csvlogFile != NULL )
1035+ csvfilename = logfile_getname (fntime ,".csv" );
10401036
10411037/*
10421038 * Decide whether to overwrite or append. We can overwrite if (a)
@@ -1084,7 +1080,9 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
10841080Log_RotationAge = 0 ;
10851081Log_RotationSize = 0 ;
10861082}
1087- pfree (filename );
1083+
1084+ if (filename )
1085+ pfree (filename );
10881086if (csvfilename )
10891087pfree (csvfilename );
10901088return ;
@@ -1100,8 +1098,10 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
11001098#ifdef WIN32
11011099EnterCriticalSection (& sysfileSection );
11021100#endif
1101+
11031102fclose (syslogFile );
11041103syslogFile = fh ;
1104+
11051105#ifdef WIN32
11061106LeaveCriticalSection (& sysfileSection );
11071107#endif
@@ -1110,6 +1110,7 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
11101110if (last_file_name != NULL )
11111111pfree (last_file_name );
11121112last_file_name = filename ;
1113+ filename = NULL ;
11131114}
11141115
11151116/* Same as above, but for csv file. */
@@ -1146,7 +1147,11 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
11461147Log_RotationAge = 0 ;
11471148Log_RotationSize = 0 ;
11481149}
1149- pfree (csvfilename );
1150+
1151+ if (filename )
1152+ pfree (filename );
1153+ if (csvfilename )
1154+ pfree (csvfilename );
11501155return ;
11511156}
11521157
@@ -1160,8 +1165,10 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
11601165#ifdef WIN32
11611166EnterCriticalSection (& sysfileSection );
11621167#endif
1168+
11631169fclose (csvlogFile );
11641170csvlogFile = fh ;
1171+
11651172#ifdef WIN32
11661173LeaveCriticalSection (& sysfileSection );
11671174#endif
@@ -1170,19 +1177,28 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
11701177if (last_csv_file_name != NULL )
11711178pfree (last_csv_file_name );
11721179last_csv_file_name = csvfilename ;
1180+ csvfilename = NULL ;
11731181}
11741182
1183+ if (filename )
1184+ pfree (filename );
1185+ if (csvfilename )
1186+ pfree (csvfilename );
1187+
11751188set_next_rotation_time ();
11761189}
11771190
11781191
11791192/*
11801193 * construct logfile name using timestamp information
11811194 *
1195+ * If suffix isn't NULL, append it to the name, replacing any ".log"
1196+ * that may be in the pattern.
1197+ *
11821198 * Result is palloc'd.
11831199 */
11841200static char *
1185- logfile_getname (pg_time_t timestamp ,char * suffix )
1201+ logfile_getname (pg_time_t timestamp ,const char * suffix )
11861202{
11871203char * filename ;
11881204int len ;
@@ -1193,7 +1209,7 @@ logfile_getname(pg_time_t timestamp, char *suffix)
11931209
11941210len = strlen (filename );
11951211
1196- /* treatit as a strftime pattern */
1212+ /* treatLog_filename as a strftime pattern */
11971213pg_strftime (filename + len ,MAXPGPATH - len ,Log_filename ,
11981214pg_localtime (& timestamp ,log_timezone ));
11991215
@@ -1202,7 +1218,7 @@ logfile_getname(pg_time_t timestamp, char *suffix)
12021218len = strlen (filename );
12031219if (len > 4 && (strcmp (filename + (len - 4 ),".log" )== 0 ))
12041220len -= 4 ;
1205- strncpy (filename + len ,suffix ,MAXPGPATH - len );
1221+ strlcpy (filename + len ,suffix ,MAXPGPATH - len );
12061222}
12071223
12081224return filename ;