Skip to content

Commit

Permalink
Allow disabling logging by setting AUSTIN_NO_LOGGING in the environment
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias Rav committed Jul 4, 2022
1 parent a0c8308 commit 49aa73f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Added support for Python 3.11.

Allowed disabling logging by setting the environment variable
AUSTIN_NO_LOGGING.

Improved permission error reporting on MacOS.

Improved MacOS support.
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,15 @@ where the structure of `[frame]` and the number and type of metrics on each line
depend on the mode.


## Environment variables

Some behaviour of Austin can be configured via environment variables.

| Variable | Effect |
| -------------------- | ------ |
| `AUSTIN_NO_LOGGING=` | Disables all [log messages](#logging). |


## Normal Mode

In normal mode, the `[frame]` part of each emitted sample has the structure
Expand Down Expand Up @@ -522,6 +531,8 @@ statistics. _Bad_ frames are output together with the other frames. In general,
entries for bad frames will not be visible in a flame graph as all tests show
error rates below 1% on average.

Logging can be disabled using [environment variables](#environment-variables).


## Cheat sheet

Expand Down
5 changes: 5 additions & 0 deletions src/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ FILE * logfile = NULL;
#include "austin.h"
#include "logging.h"

int logging = 0;

void
_log_writer(int prio, const char * fmt, va_list ap) {
if (!logging) return;
#ifdef PL_UNIX
vsyslog(prio, fmt, ap);

Expand All @@ -72,6 +74,8 @@ _log_writer(int prio, const char * fmt, va_list ap) {

void
logger_init(void) {
if (getenv("AUSTIN_NO_LOGGING") == NULL) logging = 1;
if (!logging) return;
#ifdef PL_UNIX
setlogmask (LOG_UPTO (LOG_DEBUG));
openlog ("austin", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
Expand Down Expand Up @@ -162,6 +166,7 @@ log_t(const char * fmt, ...) {

void
logger_close(void) {
if (!logging) return;
#ifdef PL_UNIX
closelog();

Expand Down
9 changes: 9 additions & 0 deletions test/test_fork.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,12 @@ def test_qualnames(py, austin):

assert has_pattern(result.stdout, "qualnames.py:Foo.run"), compress(result.stdout)
assert has_pattern(result.stdout, "qualnames.py:Bar.run"), compress(result.stdout)


@allpythons()
def test_no_logging(py):
datafile = tmp_path / "test_no_logging.austin"
env = {"AUSTIN_NO_LOGGING", ""}
script = "import time; time.sleep(.1)"
result = austin("-i", "1ms", "-o", datafile, *python(py), "-c", script, env=env)
assert result.returncode == 0, result.stderr or result.stdout
7 changes: 5 additions & 2 deletions test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from pathlib import Path
from subprocess import PIPE, CompletedProcess, Popen, check_output, run
from test import PYTHON_VERSIONS
from typing import Iterator, TypeVar
from typing import Dict, Iterator, Optional, TypeVar

import pytest

Expand Down Expand Up @@ -130,7 +130,9 @@ def __init__(self, name: str) -> None:

self.ALL.append(self)

def __call__(self, *args: tuple[str], timeout: int = 60) -> CompletedProcess:
def __call__(
self, *args: tuple[str], timeout: int = 60, env: Optional[Dict[str, str]] = None
) -> CompletedProcess:
if not self.path.is_file():
pytest.skip(f"Variant '{self}' not available")

Expand All @@ -140,6 +142,7 @@ def __call__(self, *args: tuple[str], timeout: int = 60) -> CompletedProcess:
timeout=timeout,
text=True,
errors="ignore",
env=None if env is None else {**os.environ, **env},
)

if result.returncode in (-11, 139): # SIGSEGV
Expand Down

0 comments on commit 49aa73f

Please sign in to comment.