Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitea4ff53

Browse files
committed
Logs are written to log files
1 parent62e4e90 commitea4ff53

File tree

3 files changed

+47
-25
lines changed

3 files changed

+47
-25
lines changed

‎pg_probackup.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ main(int argc, char *argv[])
228228

229229
join_path_components(arclog_path,backup_path,"wal");
230230

231+
log_filename="pg_probackup-%Y-%m-%d_%H%M%S.log";
232+
join_path_components(log_path,backup_path,"log");
233+
231234
/* setup exclusion list for file search */
232235
if (!backup_logs)
233236
{

‎utils/logger.c‎

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
*-------------------------------------------------------------------------
88
*/
99

10-
#include"postgres_fe.h"
11-
#include"pgtime.h"
12-
1310
#include<errno.h>
1411
#include<pthread.h>
1512
#include<stdio.h>
1613
#include<string.h>
14+
#include<sys/stat.h>
1715
#include<time.h>
1816

1917
#include"logger.h"
@@ -25,6 +23,7 @@ intlog_level = INFO;
2523
char*log_filename=NULL;
2624
char*error_log_filename=NULL;
2725
char*log_directory=NULL;
26+
charlog_path[MAXPGPATH]="";
2827

2928
intlog_rotation_size=0;
3029
intlog_rotation_age=0;
@@ -47,7 +46,7 @@ static void elog_internal(int elevel, const char *fmt, va_list args)
4746
/* Functions to work with log files */
4847
staticvoidopen_logfile(void);
4948
staticvoidrelease_logfile(void);
50-
staticchar*logfile_getname(pg_time_ttimestamp);
49+
staticchar*logfile_getname(time_ttimestamp);
5150
staticFILE*logfile_open(constchar*filename,constchar*mode);
5251

5352
/* Static variables */
@@ -60,43 +59,47 @@ static bool logging_to_file = false;
6059

6160
staticpthread_mutex_tlog_file_mutex=PTHREAD_MUTEX_INITIALIZER;
6261

63-
/*
64-
* Logs to stderr or to log file and exit if ERROR or FATAL.
65-
*
66-
* Actual implementation for elog() and pg_log().
67-
*/
6862
staticvoid
69-
elog_internal(intelevel,constchar*fmt,va_listargs)
63+
write_elevel(FILE*stream,intelevel)
7064
{
71-
boolwrote_to_file= false;
72-
7365
switch (elevel)
7466
{
7567
caseLOG:
76-
fputs("LOG: ",stderr);
68+
fputs("LOG: ",stream);
7769
break;
7870
caseINFO:
79-
fputs("INFO: ",stderr);
71+
fputs("INFO: ",stream);
8072
break;
8173
caseNOTICE:
82-
fputs("NOTICE: ",stderr);
74+
fputs("NOTICE: ",stream);
8375
break;
8476
caseWARNING:
85-
fputs("WARNING: ",stderr);
77+
fputs("WARNING: ",stream);
8678
break;
8779
caseERROR:
88-
fputs("ERROR: ",stderr);
80+
fputs("ERROR: ",stream);
8981
break;
9082
caseFATAL:
91-
fputs("FATAL: ",stderr);
83+
fputs("FATAL: ",stream);
9284
break;
9385
casePANIC:
94-
fputs("PANIC: ",stderr);
86+
fputs("PANIC: ",stream);
9587
break;
9688
default:
9789
elog(ERROR,"invalid logging level: %d",elevel);
9890
break;
9991
}
92+
}
93+
94+
/*
95+
* Logs to stderr or to log file and exit if ERROR or FATAL.
96+
*
97+
* Actual implementation for elog() and pg_log().
98+
*/
99+
staticvoid
100+
elog_internal(intelevel,constchar*fmt,va_listargs)
101+
{
102+
boolwrote_to_file= false;
100103

101104
pthread_mutex_lock(&log_file_mutex);
102105

@@ -112,6 +115,8 @@ elog_internal(int elevel, const char *fmt, va_list args)
112115
if (log_file==NULL)
113116
open_logfile();
114117

118+
write_elevel(log_file,elevel);
119+
115120
vfprintf(log_file,fmt,args);
116121
fputc('\n',log_file);
117122
fflush(log_file);
@@ -127,6 +132,8 @@ elog_internal(int elevel, const char *fmt, va_list args)
127132
if (!wrote_to_file||
128133
elevel >=ERROR)
129134
{
135+
write_elevel(stderr,elevel);
136+
130137
vfprintf(stderr,fmt,args);
131138
fputc('\n',stderr);
132139
fflush(stderr);
@@ -203,20 +210,24 @@ pg_log(eLogType type, const char *fmt, ...)
203210
* Result is palloc'd.
204211
*/
205212
staticchar*
206-
logfile_getname(pg_time_ttimestamp)
213+
logfile_getname(time_ttimestamp)
207214
{
208215
char*filename;
209216
size_tlen;
217+
structtm*tm=localtime(&timestamp);
218+
219+
if (log_path[0]=='\0')
220+
elog(ERROR,"logging path is not set");
210221

211222
filename= (char*)palloc(MAXPGPATH);
212223

213-
snprintf(filename,MAXPGPATH,"%s/",log_directory);
224+
snprintf(filename,MAXPGPATH,"%s/",log_path);
214225

215226
len=strlen(filename);
216227

217-
/*treat pgaudit.log_filename as a strftime pattern */
218-
pg_strftime(filename+len,MAXPGPATH-len,log_filename,
219-
pg_localtime(&timestamp,log_timezone));
228+
/*Treatlog_filename as a strftime pattern */
229+
if (strftime(filename+len,MAXPGPATH-len,log_filename,tm) <=0)
230+
elog(ERROR,"strftime(%s) failed: %s",log_filename,strerror(errno));
220231

221232
returnfilename;
222233
}
@@ -229,6 +240,11 @@ logfile_open(const char *filename, const char *mode)
229240
{
230241
FILE*fh;
231242

243+
/*
244+
* Create log directory if not present; ignore errors
245+
*/
246+
mkdir(log_path,S_IRWXU);
247+
232248
fh=fopen(filename,mode);
233249

234250
if (fh)
@@ -257,7 +273,7 @@ open_logfile(void)
257273

258274
log_file=logfile_open(filename,"a");
259275

260-
if (last_file_name!=NULL)/* probably shouldn't happen */
276+
if (last_file_name!=NULL)
261277
pfree(last_file_name);
262278

263279
last_file_name=filename;

‎utils/logger.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#ifndefLOGGER_H
1111
#defineLOGGER_H
1212

13+
#include"postgres_fe.h"
14+
1315
/* Log level */
1416
#defineVERBOSE(-5)
1517
#defineLOG(-4)
@@ -27,6 +29,7 @@ extern intlog_level;
2729
externchar*log_filename;
2830
externchar*error_log_filename;
2931
externchar*log_directory;
32+
externcharlog_path[MAXPGPATH];
3033

3134
externintlog_rotation_size;
3235
externintlog_rotation_age;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp