@@ -36,9 +36,17 @@ void RawWrite(const char *buffer) {
3636
3737void ReportFile::ReopenIfNecessary () {
3838 mu->CheckLocked ();
39- if (fd == kStdoutFd || fd == kStderrFd ) return ;
40-
4139 uptr pid = internal_getpid ();
40+ if (fallbackToStderrActive && fd_pid != pid) {
41+ // If fallbackToStderrActive is set then we fellback to stderr. If this is a
42+ // new process, mark fd as invalid so we attempt to open again.
43+ CHECK_EQ (fd, kStderrFd );
44+ fd = kInvalidFd ;
45+ fallbackToStderrActive = false ;
46+ }
47+ if (fd == kStdoutFd || fd == kStderrFd )
48+ return ;
49+
4250 // If in tracer, use the parent's file.
4351 if (pid == stoptheworld_tracer_pid)
4452 pid = stoptheworld_tracer_ppid;
@@ -48,8 +56,7 @@ void ReportFile::ReopenIfNecessary() {
4856 // process, close it now.
4957 if (fd_pid == pid)
5058 return ;
51- else
52- CloseFile (fd);
59+ CloseFile (fd);
5360 }
5461
5562 const char *exe_name = GetProcessName ();
@@ -65,18 +72,24 @@ void ReportFile::ReopenIfNecessary() {
6572 error_t err;
6673 fd = OpenFile (full_path, WrOnly, &err);
6774 if (fd == kInvalidFd ) {
68- const char *ErrorMsgPrefix = " ERROR: Can't open file: " ;
75+ bool fallback = common_flags ()->log_fallback_to_stderr ;
76+ const char *ErrorMsgPrefix =
77+ fallback ? " WARNING: Can't open file, falling back to stderr: "
78+ : " ERROR: Can't open file: " ;
6979 WriteToFile (kStderrFd , ErrorMsgPrefix, internal_strlen (ErrorMsgPrefix));
7080 WriteToFile (kStderrFd , full_path, internal_strlen (full_path));
7181 char errmsg[100 ];
7282 internal_snprintf (errmsg, sizeof (errmsg), " (reason: %d)\n " , err);
7383 WriteToFile (kStderrFd , errmsg, internal_strlen (errmsg));
74- Die ();
84+ if (!fallback)
85+ Die ();
86+ fallbackToStderrActive = true ;
87+ fd = kStderrFd ;
7588 }
7689 fd_pid = pid;
7790}
7891
79- static void RecursiveCreateParentDirs (char *path) {
92+ static void RecursiveCreateParentDirs (char *path, fd_t &fd ) {
8093 if (path[0 ] == ' \0 ' )
8194 return ;
8295 for (int i = 1 ; path[i] != ' \0 ' ; ++i) {
@@ -85,12 +98,19 @@ static void RecursiveCreateParentDirs(char *path) {
8598 continue ;
8699 path[i] = ' \0 ' ;
87100 if (!DirExists (path) && !CreateDir (path)) {
88- const char *ErrorMsgPrefix = " ERROR: Can't create directory: " ;
101+ bool fallback = common_flags ()->log_fallback_to_stderr ;
102+ const char *ErrorMsgPrefix =
103+ fallback ? " WARNING: Can't create directory, falling back to stderr: "
104+ : " ERROR: Can't create directory: " ;
89105 WriteToFile (kStderrFd , ErrorMsgPrefix, internal_strlen (ErrorMsgPrefix));
90106 WriteToFile (kStderrFd , path, internal_strlen (path));
91107 const char *ErrorMsgSuffix = " \n " ;
92108 WriteToFile (kStderrFd , ErrorMsgSuffix, internal_strlen (ErrorMsgSuffix));
93- Die ();
109+ if (!fallback)
110+ Die ();
111+ path[i] = save;
112+ fd = kStderrFd ;
113+ return ;
94114 }
95115 path[i] = save;
96116 }
@@ -164,12 +184,17 @@ void ReportFile::SetReportPath(const char *path) {
164184 if (path) {
165185 uptr len = internal_strlen (path);
166186 if (len > sizeof (path_prefix) - 100 ) {
167- const char *message = " ERROR: Path is too long: " ;
187+ bool fallback = common_flags ()->log_fallback_to_stderr ;
188+ const char *message =
189+ fallback ? " WARNING: Path is too long, falling back to stderr: "
190+ : " ERROR: Path is too long: " ;
168191 WriteToFile (kStderrFd , message, internal_strlen (message));
169192 WriteToFile (kStderrFd , path, 8 );
170193 message = " ...\n " ;
171194 WriteToFile (kStderrFd , message, internal_strlen (message));
172- Die ();
195+ if (!fallback)
196+ Die ();
197+ path = " stderr" ;
173198 }
174199 }
175200
@@ -183,7 +208,7 @@ void ReportFile::SetReportPath(const char *path) {
183208 fd = kStdoutFd ;
184209 } else {
185210 ParseAndSetPath (path, path_prefix, kMaxPathLength );
186- RecursiveCreateParentDirs (path_prefix);
211+ RecursiveCreateParentDirs (path_prefix, fd );
187212 }
188213}
189214
0 commit comments