Skip to content

Commit

Permalink
fix a couple of issues on log re-opening
Browse files Browse the repository at this point in the history
The stat call was not working well enough on windows, it seems to be lagging in
the size values (may of been specific to certain setups). With fstat it seems to
be working better, more in-line with the written data.

Fixed the log cycling on windows, and alignment for the EOL differences.

karl
  • Loading branch information
karlheyes committed May 24, 2023
1 parent fbfe09d commit 60e2b58
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/log/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,17 @@ static int _log_open (log_run_t *lr)
if (file_recheck)
{
_unlock_q (id);
int r = -1;
struct stat st;
if (stat (loglist [id].filename, &st) < 0)
if (loglist [id].logfile)
{
if (access (loglist [id].filename, F_OK) == 0) // in case the log has been moved externally
r = fstat (fileno (loglist [id].logfile), &st);
}
else
r = stat (loglist [id].filename, &st);

if (r < 0)
{
reopen = 1;
archive = 0;
Expand Down Expand Up @@ -221,21 +230,23 @@ static int _log_open (log_run_t *lr)
if (exists)
{
#ifdef _WIN32
fclose (oldf); // windows does not allow for renames while open.
loglist [id].logfile = oldf = NULL;
remove (new_name);
#endif
rename (loglist [id].filename, new_name);
exists = 0;
}
}
snprintf (new_name, sizeof new_name, "%s", loglist [id].filename);
FILE *f = fopen (new_name, "a");
FILE *f = fopen (new_name, "a+t");
if (f == NULL)
{
if (loglist [id].logfile != stderr)
loglist [id] . logfile = stderr;
break;
}
loglist [id].logfile = f;
setvbuf (loglist [id] . logfile, NULL, IO_BUFFER_TYPE, 0);
if (exists == 0)
lr->fsize = 0;
if (loglist [id] . duration)
Expand Down Expand Up @@ -885,9 +896,15 @@ static int do_log_run (int log_id)
_log_expand_preline (next, preline, sizeof preline);

int len = fprintf (loglist [log_id].logfile, "%s%s\n", preline, _entry_line_start (flags, next));
fflush (loglist [log_id].logfile);

if (len > 0)
{
#ifdef _WIN32
len++; // the \n translates to \r\n, but the return does not account for it
#endif
lr.fsize += len;
}
loop = 0;
count++;
_lock_q (log_id);
Expand Down

0 comments on commit 60e2b58

Please sign in to comment.