Skip to content

Commit

Permalink
chore(win): enrich logs
Browse files Browse the repository at this point in the history
We add more details to the logs generated by Austin on Windows to
include a timestamp and process information. This allows us to collect
the data in tests for helping with investigations.
  • Loading branch information
P403n1x87 committed Sep 6, 2023
1 parent ff51dad commit 02ced64
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
35 changes: 26 additions & 9 deletions src/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

#else
#include <windows.h>
#include <stdio.h>
#include <time.h>

#define LOG_EMERG 0 /* system is unusable */
#define LOG_ALERT 1 /* action must be taken immediately */
Expand All @@ -61,14 +61,31 @@ _log_writer(int prio, const char * fmt, va_list ap) {
vsyslog(prio, fmt, ap);

#else
if (logfile == NULL) {
vfprintf(stderr, fmt, ap); fputc('\n', stderr);
fflush(stderr);
}
else {
vfprintf(logfile, fmt, ap); fputc('\n', logfile);
fflush(logfile);
}
time_t timer;
char buffer[32];
struct tm* tm_info;

timer = time(NULL);
tm_info = localtime(&timer);

strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);

FILE * stream = isvalid(logfile) ? logfile : stderr;

// Timestamp
fputs(buffer, stream);

// Process info
fprintf(stream, " " PROGRAM_NAME "[%d]: ", GetCurrentProcessId());

// Log message
vfprintf(stream, fmt, ap);

// Newline
fputc('\n', stream);

// Flush
fflush(stream);

#endif
}
Expand Down
15 changes: 15 additions & 0 deletions test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from subprocess import Popen
from subprocess import TimeoutExpired
from subprocess import check_output
from tempfile import gettempdir
from test import PYTHON_VERSIONS
from time import sleep
from types import ModuleType
Expand Down Expand Up @@ -149,6 +150,20 @@ def collect_logs(variant: str, pid: int) -> List[str]:
),
f" end of logs for {variant}[{pid}] ".center(80, "="),
]

case "Windows":
with Path(gettempdir()) / "austin.log" as logfile:
needles = (f"{variant}[{pid}]",)
return [
f" logs for {variant}[{pid}] ".center(80, "="),
*(
line.strip()
for line in logfile.readlines()
if any(needle in line for needle in needles)
),
f" end of logs for {variant}[{pid}] ".center(80, "="),
]

case _:
return []

Expand Down

0 comments on commit 02ced64

Please sign in to comment.