File size: 5,403 Bytes
e59bd9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""Rotating structured logging for every LPU component.

Controller-footprint safe: imports only stdlib. JSON-lines on disk keep the
logs machine-parseable for diagnose scripts; a plain console handler is added
for foreground commands.
"""

from __future__ import annotations

import json
import logging
import logging.handlers
import os
import sys
import time
from pathlib import Path
from typing import Optional

_LOGGERS: dict = {}


def _user_state_dir() -> Path:
    """Per-user writable state dir honoring Windows conventions."""
    if os.name == "nt":
        local = os.environ.get("LOCALAPPDATA")
        if local:
            return Path(local) / "LPU"
    return Path.home() / ".lpu_home"


def _writable(path: Path) -> bool:
    try:
        path.mkdir(parents=True, exist_ok=True)
        probe = path / ".write_probe"
        probe.write_text("ok", encoding="utf-8")
        probe.unlink()
        return True
    except OSError:
        return False


def lpu_home() -> Path:
    """Canonical LPU state directory (logs, sessions, lock, reports).

    Resolution order:
      1. LPU_HOME env
      2. runtime.home_dir from lpu_config.yaml (when found next to the package)
      3. <install_root>/.lpu_home when writable (normal install layout)
      4. %LOCALAPPDATA%\\LPU (Windows) or ~/.lpu_home - NEVER the drive root
         or Program Files, so no admin privileges are ever required.
    """
    env = os.environ.get("LPU_HOME")
    if env:
        return Path(env)
    root = Path(__file__).resolve().parent.parent
    try:
        while root.parent != root and not (root / "lpu_config.yaml").exists():
            root = root.parent
        if (root / "lpu_config.yaml").exists():
            import yaml  # controller footprint allows PyYAML

            try:
                with open(root / "lpu_config.yaml", "r", encoding="utf-8") as fh:
                    data = yaml.safe_load(fh) or {}
                cfg_dir = (data.get("runtime") or {}).get("home_dir")
                if cfg_dir:
                    return Path(cfg_dir)
            except Exception:  # noqa: BLE001 - config problems must not kill logging
                pass
            candidate = root / ".lpu_home"
            if _writable(candidate):
                return candidate
            return _user_state_dir()
        # no config found (e.g. pip-installed layout): per-user dir, not root
        return _user_state_dir()
    except Exception:  # noqa: BLE001
        return _user_state_dir()


def default_log_dir() -> Path:
    return lpu_home() / "logs"


def get_logger(
    name: str,
    log_dir: Optional[os.PathLike] = None,
    level: str = "INFO",
    max_mb: float = 10.0,
    backups: int = 5,
    console: bool = True,
) -> logging.Logger:
    """Idempotent logger factory with rotation + JSON-lines file handler."""
    key = (name, str(log_dir))
    if key in _LOGGERS:
        return _LOGGERS[key]

    logger = logging.getLogger(f"lpu.{name}")
    logger.setLevel(getattr(logging, level.upper(), logging.INFO))
    logger.propagate = False

    if not logger.handlers:
        d = Path(log_dir) if log_dir else default_log_dir()
        try:
            d.mkdir(parents=True, exist_ok=True)
            fh = logging.handlers.RotatingFileHandler(
                d / f"{name}.log",
                maxBytes=int(max_mb * 1024 * 1024),
                backupCount=backups,
                encoding="utf-8",
            )
            fh.setFormatter(
                logging.Formatter(
                    "%(asctime)s|%(levelname)s|%(name)s|%(message)s", "%Y-%m-%dT%H:%M:%S"
                )
            )
            logger.addHandler(fh)
        except Exception:  # noqa: BLE001 - read-only FS etc: console only
            pass
        if console:
            ch = logging.StreamHandler(sys.stderr)
            ch.setFormatter(logging.Formatter("[lpu] %(levelname)s %(message)s"))
            logger.addHandler(ch)

    _LOGGERS[key] = logger
    return logger


class JsonRecorder:
    """Append-only JSONL event recorder (metrics, session reports).

    Writes are atomic-per-line and flushed, so the controller can tail the
    file while the target process is still running. Used by monitoring and
    the IPC session channel.
    """

    def __init__(self, path: os.PathLike):
        self.path = Path(path)
        self.path.parent.mkdir(parents=True, exist_ok=True)
        self._fh = open(self.path, "a", encoding="utf-8")

    def write(self, record: dict) -> None:
        record = {"ts": round(time.time(), 3), **record}
        self._fh.write(json.dumps(record, ensure_ascii=False, default=str) + "\n")
        self._fh.flush()

    def close(self) -> None:
        try:
            self._fh.close()
        except Exception:  # noqa: BLE001
            pass

    def __enter__(self) -> "JsonRecorder":
        return self

    def __exit__(self, *exc) -> None:
        self.close()


def read_jsonl(path: os.PathLike):
    """Yield parsed records from a JSONL file, skipping torn tail lines."""
    p = Path(path)
    if not p.exists():
        return
    with open(p, "r", encoding="utf-8") as fh:
        for line in fh:
            line = line.strip()
            if not line:
                continue
            try:
                yield json.loads(line)
            except json.JSONDecodeError:
                continue  # torn write from a live process; skip