| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of the Falcon Player (FPP) and is Copyright (C) | ||
| 3 | * 2013-2022 by the Falcon Player Developers. | ||
| 4 | * | ||
| 5 | * The Falcon Player (FPP) is free software, and is covered under | ||
| 6 | * multiple Open Source licenses. Please see the included 'LICENSES' | ||
| 7 | * file for descriptions of what files are covered by each license. | ||
| 8 | * | ||
| 9 | * This source file is covered under the LGPL v2.1 as described in the | ||
| 10 | * included LICENSE.LGPL file. | ||
| 11 | */ | ||
| 12 | |||
| 13 | #include "fpp-pch.h" | ||
| 14 | #include <cstring> | ||
| 15 | |||
| 16 | #include <algorithm> | ||
| 17 | #include <atomic> | ||
| 18 | #include <fcntl.h> | ||
| 19 | #include <sys/stat.h> | ||
| 20 | #include <sys/syscall.h> | ||
| 21 | #include <sys/time.h> | ||
| 22 | #include <sys/utsname.h> | ||
| 23 | #include <pwd.h> | ||
| 24 | #include <sys/types.h> | ||
| 25 | #include <chrono> | ||
| 26 | #include <mutex> | ||
| 27 | #include <sstream> | ||
| 28 | #include <stdarg.h> | ||
| 29 | #include <stdbool.h> | ||
| 30 | #include <time.h> | ||
| 31 | #include <unistd.h> | ||
| 32 | |||
| 33 | #include "fppversion.h" | ||
| 34 | #include "log.h" | ||
| 35 | |||
| 36 | // int logLevel = LOG_INFO; | ||
| 37 | // int logMask = VB_MOST; | ||
| 38 | |||
| 39 | // Create Global Instance | ||
| 40 | FPPLogger FPPLogger::INSTANCE = FPPLogger(); | ||
| 41 | |||
| 42 | char logFileName[1024] = ""; | ||
| 43 | bool logToStdOut = true; | ||
| 44 | |||
| 45 | // Whether a line already written to the log file should be kept off stdout. | ||
| 46 | // Opt-in, and only fppd opts in: it is the one FPP binary whose stdout is a | ||
| 47 | // firehose into journald (it runs as `fppd -f` under systemd), so its stdout | ||
| 48 | // copy is pure duplication of what fppd.log already has. The small services -- | ||
| 49 | // fppoled and friends -- log a handful of lines that users and support look for | ||
| 50 | // with `journalctl -u fppoled`, so they stay on both. See _LogWrite(). | ||
| 51 | static bool suppressDuplicateStdOut = false; | ||
| 52 | |||
| 53 | // --------------------------------------------------------------------------- | ||
| 54 | // Crash-time log ring | ||
| 55 | // | ||
| 56 | // The most useful line for diagnosing a crash is routinely one nobody kept: the | ||
| 57 | // trigger is logged at LogDebug, production runs at info, so it is filtered out | ||
| 58 | // before it is ever formatted. This ring formats and retains those lines in | ||
| 59 | // memory without writing them anywhere, and the crash handler dumps it into the | ||
| 60 | // report -- debug-level detail at info-level cost on disk. | ||
| 61 | // | ||
| 62 | // Constraints that shaped it: | ||
| 63 | // * No locking. The normal log path takes logFileLock, and a crash handler | ||
| 64 | // that blocks on a mutex the faulting thread already holds hangs the | ||
| 65 | // process -- exactly the case (heap corruption inside free()) that most | ||
| 66 | // needs the trace. A slot may be torn between a writer and the dump; a | ||
| 67 | // smeared line in a crash report is a fine price for never deadlocking. | ||
| 68 | // * No allocation, for the same reason: a fixed array of fixed slots, so the | ||
| 69 | // dump is a bounded series of write() calls and is async-signal-safe. | ||
| 70 | // * LOG_EXCESSIVE is excluded. It fires per frame in the output path, and | ||
| 71 | // formatting it unconditionally would be a real cost on a single-core BBB; | ||
| 72 | // LOG_DEBUG and coarser covers the state changes worth reconstructing. | ||
| 73 | // --------------------------------------------------------------------------- | ||
| 74 | static constexpr size_t kCrashRingSlots = 256; | ||
| 75 | static constexpr size_t kCrashRingSlotSize = 240; | ||
| 76 | static char s_crashRing[kCrashRingSlots][kCrashRingSlotSize]; | ||
| 77 | static std::atomic<uint64_t> s_crashRingSeq{ 0 }; | ||
| 78 | |||
| 79 | // Set once the crash handler starts, so its own output -- the gdb stack, the | ||
| 80 | // "Crash handler called" line -- stops evicting the pre-crash history that is | ||
| 81 | // the entire reason the ring exists. That output is already captured in the | ||
| 82 | // stack file; duplicating it here would only cost us the lines we want. | ||
| 83 | static std::atomic<bool> s_crashRingFrozen{ false }; | ||
| 84 | |||
| 85 | 31 | void CrashLogRingFreeze() { | |
| 86 | 31 | s_crashRingFrozen.store(true, std::memory_order_relaxed); | |
| 87 | 31 | } | |
| 88 | |||
| 89 | // Zero-init statics, so this is safe from any point in startup with no | ||
| 90 | // dependency on static initialization order. | ||
| 91 | 2457556 | static void CrashLogRingPush(const char* line, size_t len) { | |
| 92 |
2/2✓ Branch 0 taken 105 times.
✓ Branch 1 taken 2457451 times.
|
2457556 | if (s_crashRingFrozen.load(std::memory_order_relaxed)) { |
| 93 | 105 | return; | |
| 94 | } | ||
| 95 | 2457451 | uint64_t seq = s_crashRingSeq.fetch_add(1, std::memory_order_relaxed); | |
| 96 | 2457451 | char* slot = s_crashRing[seq % kCrashRingSlots]; | |
| 97 |
2/2✓ Branch 0 taken 25270 times.
✓ Branch 1 taken 2432181 times.
|
2457451 | if (len >= kCrashRingSlotSize) { |
| 98 | 25270 | len = kCrashRingSlotSize - 1; | |
| 99 | } | ||
| 100 | 2457451 | memcpy(slot, line, len); | |
| 101 | 2457451 | slot[len] = '\0'; | |
| 102 | } | ||
| 103 | |||
| 104 | // Two independent filters, both needed: | ||
| 105 | // * facility opt-out excludes the per-frame data facilities outright (see | ||
| 106 | // FPPLoggerInstance::crashRingCapture -- HexDump emits at LogInfo, so a | ||
| 107 | // level test alone would not have caught them); | ||
| 108 | // * LOG_EXCESSIVE is dropped everywhere, since it is the per-frame level by | ||
| 109 | // convention in every facility that has one. | ||
| 110 | 7690199 | bool CrashLogRingWillCapture(int level, FPPLoggerInstance& facility) { | |
| 111 |
4/4✓ Branch 0 taken 6436404 times.
✓ Branch 1 taken 1253795 times.
✓ Branch 2 taken 2457556 times.
✓ Branch 3 taken 3978848 times.
|
7690199 | return facility.crashRingCapture && level <= LOG_DEBUG; |
| 112 | } | ||
| 113 | |||
| 114 | // Async-signal-safe: only write(), no allocation, no locks. | ||
| 115 | 32 | void CrashLogRingDump(int fd) { | |
| 116 | 32 | uint64_t seq = s_crashRingSeq.load(std::memory_order_relaxed); | |
| 117 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | uint64_t count = seq < kCrashRingSlots ? seq : kCrashRingSlots; |
| 118 | static const char hdr[] = "=== recent log lines (ring buffer, oldest first) ===\n"; | ||
| 119 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | (void)!write(fd, hdr, sizeof(hdr) - 1); |
| 120 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | if (count == 0) { |
| 121 | static const char none[] = "(empty)\n"; | ||
| 122 | ✗ | (void)!write(fd, none, sizeof(none) - 1); | |
| 123 | ✗ | return; | |
| 124 | } | ||
| 125 |
2/2✓ Branch 0 taken 3729 times.
✓ Branch 1 taken 32 times.
|
3761 | for (uint64_t i = seq - count; i < seq; i++) { |
| 126 | 3729 | const char* slot = s_crashRing[i % kCrashRingSlots]; | |
| 127 | 3729 | size_t len = strnlen(slot, kCrashRingSlotSize); | |
| 128 |
1/2✓ Branch 0 taken 3729 times.
✗ Branch 1 not taken.
|
3729 | if (len) { |
| 129 |
1/2✓ Branch 0 taken 3729 times.
✗ Branch 1 not taken.
|
3729 | (void)!write(fd, slot, len); |
| 130 | } | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | // True when stdout is an interactive terminal (a person ran `fppd -f` in a | ||
| 135 | // shell) rather than a pipe to journald (how systemd runs `fppd -f`). Used to | ||
| 136 | // decide whether the stdout copy of a line already written to the log file is | ||
| 137 | // a useful on-screen echo or a pure journald duplicate. Cached: fppd's stdout | ||
| 138 | // is not reassigned after startup, and isatty() is a syscall per log line. | ||
| 139 | 415063 | static bool stdoutIsInteractive() { | |
| 140 |
3/4✓ Branch 0 taken 2646 times.
✓ Branch 1 taken 412417 times.
✓ Branch 2 taken 2646 times.
✗ Branch 3 not taken.
|
415063 | static const bool interactive = isatty(fileno(stdout)) != 0; |
| 141 | 415063 | return interactive; | |
| 142 | } | ||
| 143 | |||
| 144 | // Program name for the syslog-style program field. fppd.log is a merged, | ||
| 145 | // sequential record -- fppd itself, the fppd_start/stop/restart scripts, and | ||
| 146 | // operation breadcrumbs all append to it -- so every line has to say who wrote | ||
| 147 | // it. The [facility] field cannot carry this: it is a subsystem *within* fppd | ||
| 148 | // (General/ChannelOut/Playlist/...), not a program. | ||
| 149 | 2497579 | static const char* logProgramName() { | |
| 150 | #ifdef PLATFORM_OSX | ||
| 151 | return getprogname(); | ||
| 152 | #else | ||
| 153 | 2497579 | return program_invocation_short_name; | |
| 154 | #endif | ||
| 155 | } | ||
| 156 | // char logLevelStr[16]; | ||
| 157 | // char logMaskStr[1024]; | ||
| 158 | |||
| 159 | // Persistent handle for the log file so each log line is a single write() | ||
| 160 | // instead of a full fopen/write/fclose cycle. On slow SD cards the per-line | ||
| 161 | // open/close metadata traffic was enough that a card-internal stall could | ||
| 162 | // block the (real-time) channel output thread mid-log and drop frames. | ||
| 163 | // | ||
| 164 | // logrotate rotates fppd.log by rename (no copytruncate and no signal to | ||
| 165 | // fppd), so we periodically stat() the path and reopen when the inode under | ||
| 166 | // our name changes or the file disappears. | ||
| 167 | // | ||
| 168 | // A recursive_timed_mutex guards the handle: recursive because the crash | ||
| 169 | // handler calls LogErr and may re-enter on the same thread; timed so that if | ||
| 170 | // another thread is wedged inside a write (e.g. stalled storage) a crashing | ||
| 171 | // thread gives up and falls back to stderr instead of hanging the process. | ||
| 172 | // Owner the log file should end up with, resolved once. Looked up rather than | ||
| 173 | // hardcoded to a uid, and returns false where there is no such user (macOS runs | ||
| 174 | // as the developer's own account, not fpp) so the chown is simply skipped there. | ||
| 175 | 2621 | static bool logFileOwner(uid_t& uid, gid_t& gid) { | |
| 176 | static bool resolved = false; | ||
| 177 | static bool haveOwner = false; | ||
| 178 | static uid_t cachedUid = 0; | ||
| 179 | static gid_t cachedGid = 0; | ||
| 180 |
2/2✓ Branch 0 taken 2620 times.
✓ Branch 1 taken 1 times.
|
2621 | if (!resolved) { |
| 181 | 2620 | resolved = true; | |
| 182 | 2620 | struct passwd* pw = getpwnam("fpp"); | |
| 183 |
2/2✓ Branch 0 taken 295 times.
✓ Branch 1 taken 2325 times.
|
2620 | if (pw) { |
| 184 | 295 | cachedUid = pw->pw_uid; | |
| 185 | 295 | cachedGid = pw->pw_gid; | |
| 186 | 295 | haveOwner = true; | |
| 187 | } | ||
| 188 | } | ||
| 189 | 2621 | uid = cachedUid; | |
| 190 | 2621 | gid = cachedGid; | |
| 191 | 2621 | return haveOwner; | |
| 192 | } | ||
| 193 | |||
| 194 | static std::recursive_timed_mutex logFileLock; | ||
| 195 | static FILE* persistentLogFile = nullptr; | ||
| 196 | static ino_t persistentLogIno = 0; | ||
| 197 | static dev_t persistentLogDev = 0; | ||
| 198 | static time_t lastRotationCheck = 0; | ||
| 199 | |||
| 200 | // Must be called with logFileLock held. | ||
| 201 | 2753 | static void closePersistentLogFile() { | |
| 202 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2752 times.
|
2753 | if (persistentLogFile) { |
| 203 | 1 | fclose(persistentLogFile); | |
| 204 | 1 | persistentLogFile = nullptr; | |
| 205 | 1 | persistentLogIno = 0; | |
| 206 | 1 | persistentLogDev = 0; | |
| 207 | 1 | lastRotationCheck = 0; | |
| 208 | } | ||
| 209 | 2753 | } | |
| 210 | |||
| 211 | // Must be called with logFileLock held. Returns the persistent handle for | ||
| 212 | // logFileName, (re)opening it as needed, or nullptr if it cannot be opened. | ||
| 213 | 415511 | static FILE* getPersistentLogFile(time_t now) { | |
| 214 |
4/4✓ Branch 0 taken 412639 times.
✓ Branch 1 taken 2872 times.
✓ Branch 2 taken 6131 times.
✓ Branch 3 taken 406508 times.
|
415511 | if (persistentLogFile && now != lastRotationCheck) { |
| 215 | // At most once a second, check whether the file we have open is still | ||
| 216 | // the one at logFileName (logrotate renames it, a user may delete it). | ||
| 217 | 6131 | lastRotationCheck = now; | |
| 218 | struct stat st; | ||
| 219 | 6131 | if ((stat(logFileName, &st) != 0) || | |
| 220 |
6/8✓ Branch 0 taken 6130 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 6130 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6130 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 6130 times.
|
6131 | (st.st_ino != persistentLogIno) || (st.st_dev != persistentLogDev)) { |
| 221 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | closePersistentLogFile(); |
| 222 | } | ||
| 223 | } | ||
| 224 |
2/2✓ Branch 0 taken 2873 times.
✓ Branch 1 taken 412638 times.
|
415511 | if (!persistentLogFile) { |
| 225 | 2873 | bool existed = (access(logFileName, F_OK) == 0); | |
| 226 | 2873 | persistentLogFile = fopen(logFileName, "a"); | |
| 227 |
2/2✓ Branch 0 taken 2664 times.
✓ Branch 1 taken 209 times.
|
2873 | if (persistentLogFile) { |
| 228 | // Line buffered: each log line still hits the kernel immediately | ||
| 229 | // (matching the old open/write/close behavior) but as one write(). | ||
| 230 | 2664 | setvbuf(persistentLogFile, nullptr, _IOLBF, 8192); | |
| 231 |
2/2✓ Branch 0 taken 2621 times.
✓ Branch 1 taken 43 times.
|
2664 | if (!existed) { |
| 232 | // We just created the log, and fppd runs as root (fppd.service | ||
| 233 | // sets no User=), so it would be root:root 0644 in an fpp-owned | ||
| 234 | // directory -- locking out every writer that is not root. That | ||
| 235 | // matters because fppd is no longer the only one: the | ||
| 236 | // fppd_start/stop/restart scripts append here, and so do the PHP | ||
| 237 | // breadcrumbs (backup, mp3gain) which run as Apache/fpp. Their | ||
| 238 | // writes are best-effort and error-suppressed, so they would fail | ||
| 239 | // SILENTLY. | ||
| 240 | // | ||
| 241 | // logrotate's `create 0664 fpp fpp` normally gets here first, but | ||
| 242 | // it cannot cover every case: resetConfig.php deletes logs/*, and | ||
| 243 | // a fresh install writes here before FPPINIT's setFileOwnership | ||
| 244 | // runs. Whoever creates the file has to set the ownership, so do | ||
| 245 | // it here too. Best-effort: as non-root the chown simply fails, | ||
| 246 | // which is the case where we are already the right owner. | ||
| 247 | uid_t uid; | ||
| 248 | gid_t gid; | ||
| 249 |
6/8✓ Branch 0 taken 2621 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 295 times.
✓ Branch 3 taken 2326 times.
✓ Branch 4 taken 295 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 295 times.
✓ Branch 7 taken 2326 times.
|
2621 | if (logFileOwner(uid, gid) && fchown(fileno(persistentLogFile), uid, gid) == 0) { |
| 250 | 295 | fchmod(fileno(persistentLogFile), 0664); | |
| 251 | } | ||
| 252 | } | ||
| 253 | struct stat st; | ||
| 254 |
1/2✓ Branch 0 taken 2664 times.
✗ Branch 1 not taken.
|
2664 | if (fstat(fileno(persistentLogFile), &st) == 0) { |
| 255 | 2664 | persistentLogIno = st.st_ino; | |
| 256 | 2664 | persistentLogDev = st.st_dev; | |
| 257 | } | ||
| 258 | 2664 | lastRotationCheck = now; | |
| 259 | } | ||
| 260 | } | ||
| 261 | 415511 | return persistentLogFile; | |
| 262 | } | ||
| 263 | |||
| 264 | 7038 | void FPPLogger::Init() { | |
| 265 |
2/2✓ Branch 0 taken 2653 times.
✓ Branch 1 taken 4385 times.
|
7038 | if (all.empty()) { |
| 266 |
1/2✓ Branch 0 taken 2653 times.
✗ Branch 1 not taken.
|
2653 | all.push_back(&(FPPLogger::General)); |
| 267 |
1/2✓ Branch 0 taken 2653 times.
✗ Branch 1 not taken.
|
2653 | all.push_back(&(FPPLogger::ChannelOut)); |
| 268 |
1/2✓ Branch 0 taken 2653 times.
✗ Branch 1 not taken.
|
2653 | all.push_back(&(FPPLogger::ChannelData)); |
| 269 |
1/2✓ Branch 0 taken 2653 times.
✗ Branch 1 not taken.
|
2653 | all.push_back(&(FPPLogger::Command)); |
| 270 |
1/2✓ Branch 0 taken 2653 times.
✗ Branch 1 not taken.
|
2653 | all.push_back(&(FPPLogger::E131Bridge)); |
| 271 |
1/2✓ Branch 0 taken 2653 times.
✗ Branch 1 not taken.
|
2653 | all.push_back(&(FPPLogger::Effect)); |
| 272 |
1/2✓ Branch 0 taken 2653 times.
✗ Branch 1 not taken.
|
2653 | all.push_back(&(FPPLogger::MediaOut)); |
| 273 |
1/2✓ Branch 0 taken 2653 times.
✗ Branch 1 not taken.
|
2653 | all.push_back(&(FPPLogger::Playlist)); |
| 274 |
1/2✓ Branch 0 taken 2653 times.
✗ Branch 1 not taken.
|
2653 | all.push_back(&(FPPLogger::Schedule)); |
| 275 |
1/2✓ Branch 0 taken 2653 times.
✗ Branch 1 not taken.
|
2653 | all.push_back(&(FPPLogger::Sequence)); |
| 276 |
1/2✓ Branch 0 taken 2653 times.
✗ Branch 1 not taken.
|
2653 | all.push_back(&(FPPLogger::Settings)); |
| 277 |
1/2✓ Branch 0 taken 2653 times.
✗ Branch 1 not taken.
|
2653 | all.push_back(&(FPPLogger::Control)); |
| 278 |
1/2✓ Branch 0 taken 2653 times.
✗ Branch 1 not taken.
|
2653 | all.push_back(&(FPPLogger::Sync)); |
| 279 |
1/2✓ Branch 0 taken 2653 times.
✗ Branch 1 not taken.
|
2653 | all.push_back(&(FPPLogger::Plugin)); |
| 280 |
1/2✓ Branch 0 taken 2653 times.
✗ Branch 1 not taken.
|
2653 | all.push_back(&(FPPLogger::GPIO)); |
| 281 |
1/2✓ Branch 0 taken 2653 times.
✗ Branch 1 not taken.
|
2653 | all.push_back(&(FPPLogger::HTTP)); |
| 282 | } | ||
| 283 | 7038 | } | |
| 284 | |||
| 285 | 1316 | bool FPPLogger::SetLevel(const char* name, const char* level) { | |
| 286 | 1316 | LogLevel newLevel = LOG_ERR; | |
| 287 | 1316 | bool found = false; | |
| 288 |
3/4✓ Branch 0 taken 1314 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1314 times.
|
1316 | if (level == nullptr || name == nullptr) { |
| 289 | 2 | LogErr(VB_SETTING, "Log level (%s) and name (%s) must be specified\n", level, name); | |
| 290 | 2 | return false; | |
| 291 | } | ||
| 292 | |||
| 293 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1314 times.
|
1314 | if (strcmp(level, "error") == 0) { |
| 294 | ✗ | newLevel = LOG_ERR; | |
| 295 | ✗ | found = true; | |
| 296 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1312 times.
|
1314 | } else if (strcmp(level, "warn") == 0) { |
| 297 | 2 | newLevel = LOG_WARN; | |
| 298 | 2 | found = true; | |
| 299 |
2/2✓ Branch 0 taken 325 times.
✓ Branch 1 taken 987 times.
|
1312 | } else if (strcmp(level, "info") == 0) { |
| 300 | 325 | newLevel = LOG_INFO; | |
| 301 | 325 | found = true; | |
| 302 |
2/2✓ Branch 0 taken 610 times.
✓ Branch 1 taken 377 times.
|
987 | } else if (strcmp(level, "debug") == 0) { |
| 303 | 610 | newLevel = LOG_DEBUG; | |
| 304 | 610 | found = true; | |
| 305 |
2/2✓ Branch 0 taken 374 times.
✓ Branch 1 taken 3 times.
|
377 | } else if (strcmp(level, "excess") == 0) { |
| 306 | 374 | newLevel = LOG_EXCESSIVE; | |
| 307 | 374 | found = true; | |
| 308 | } | ||
| 309 | |||
| 310 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1311 times.
|
1314 | if (!found) { |
| 311 | 3 | LogErr(VB_SETTING, "Invalid name for LogLevel: %s\n", level); | |
| 312 | 3 | return false; | |
| 313 | } else { | ||
| 314 |
2/4✓ Branch 0 taken 1311 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1311 times.
✗ Branch 3 not taken.
|
2622 | return SetLevel(std::string(name), newLevel); |
| 315 | } | ||
| 316 | } | ||
| 317 | |||
| 318 | 1311 | bool FPPLogger::SetLevel(std::string name, LogLevel level) { | |
| 319 | 1311 | bool madeChange = false; | |
| 320 |
1/2✓ Branch 0 taken 1311 times.
✗ Branch 1 not taken.
|
1311 | std::string prefix("LogLevel_"); |
| 321 |
3/4✓ Branch 0 taken 1311 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1051 times.
✓ Branch 3 taken 260 times.
|
1311 | if (name.compare(0, prefix.size(), prefix) == 0) { |
| 322 |
1/2✓ Branch 0 taken 1051 times.
✗ Branch 1 not taken.
|
1051 | name = name.substr(prefix.size()); |
| 323 | } | ||
| 324 | |||
| 325 | 1311 | std::vector<FPPLoggerInstance*>::iterator it; | |
| 326 |
1/2✓ Branch 0 taken 1311 times.
✗ Branch 1 not taken.
|
1311 | Init(); /* Just incase */ |
| 327 | |||
| 328 | // "All" sets every logger (fpp --log-level LEVEL). | ||
| 329 |
2/2✓ Branch 0 taken 20976 times.
✓ Branch 1 taken 1311 times.
|
22287 | for (it = all.begin(); it != all.end(); it++) { |
| 330 | 20976 | FPPLoggerInstance* ptr = *it; | |
| 331 |
7/8✓ Branch 0 taken 19667 times.
✓ Branch 1 taken 1309 times.
✓ Branch 2 taken 19667 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✓ Branch 5 taken 19651 times.
✓ Branch 6 taken 1325 times.
✓ Branch 7 taken 19651 times.
|
20976 | if (name == ptr->name || name == "All") { |
| 332 |
1/2✓ Branch 0 taken 1325 times.
✗ Branch 1 not taken.
|
1325 | SetFacilityLevel(*ptr, level); |
| 333 | 1325 | madeChange = true; | |
| 334 | } | ||
| 335 | } | ||
| 336 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1310 times.
|
1311 | if (!madeChange) { |
| 337 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogWarn(VB_SETTING, "Invalid name for LoggerInstance: %s\n", name.c_str()); |
| 338 | } | ||
| 339 | |||
| 340 | 1311 | return madeChange; | |
| 341 | 1311 | } | |
| 342 | |||
| 343 | 3017 | int FPPLogger::MinimumLogLevel() { | |
| 344 | 3017 | int rc = LOG_ERR; | |
| 345 |
1/2✓ Branch 0 taken 3017 times.
✗ Branch 1 not taken.
|
3017 | Init(); /* Just incase */ |
| 346 | 3017 | std::vector<FPPLoggerInstance*>::iterator it; | |
| 347 | |||
| 348 |
2/2✓ Branch 0 taken 48272 times.
✓ Branch 1 taken 3017 times.
|
51289 | for (it = all.begin(); it != all.end(); it++) { |
| 349 |
1/2✓ Branch 0 taken 48272 times.
✗ Branch 1 not taken.
|
48272 | int level = FacilityLevel(**it); |
| 350 |
2/2✓ Branch 0 taken 3180 times.
✓ Branch 1 taken 45092 times.
|
48272 | if (level > rc) { |
| 351 | 3180 | rc = level; | |
| 352 | } | ||
| 353 | } | ||
| 354 | |||
| 355 | 3017 | return rc; | |
| 356 | } | ||
| 357 | |||
| 358 | 2637 | std::string FPPLogger::GetLogLevelString() { | |
| 359 |
1/2✓ Branch 0 taken 2637 times.
✗ Branch 1 not taken.
|
2637 | std::stringstream rc; |
| 360 | 2637 | LogLevel currentLevel = LOG_ERR; | |
| 361 | 2637 | bool firstLevel = true; | |
| 362 | 2637 | bool firstLogger = true; | |
| 363 | 2637 | std::vector<FPPLoggerInstance*>::iterator it; | |
| 364 | |||
| 365 |
2/2✓ Branch 0 taken 42192 times.
✓ Branch 1 taken 2637 times.
|
44829 | for (it = all.begin(); it != all.end(); it++) { |
| 366 |
1/2✓ Branch 0 taken 42192 times.
✗ Branch 1 not taken.
|
42192 | LogLevel level = FacilityLevel(**it); |
| 367 |
3/4✓ Branch 0 taken 39236 times.
✓ Branch 1 taken 2956 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 39236 times.
|
42192 | if ((currentLevel != level) || firstLevel) { |
| 368 |
2/2✓ Branch 0 taken 319 times.
✓ Branch 1 taken 2637 times.
|
2956 | if (!firstLevel) { |
| 369 |
1/2✓ Branch 0 taken 319 times.
✗ Branch 1 not taken.
|
319 | rc << ";"; |
| 370 | } | ||
| 371 | 2956 | firstLevel = false; | |
| 372 | 2956 | firstLogger = true; | |
| 373 |
3/6✓ Branch 0 taken 2956 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2956 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2956 times.
✗ Branch 5 not taken.
|
2956 | rc << LogLevelToString(level) << ":"; |
| 374 | 2956 | currentLevel = level; | |
| 375 | } | ||
| 376 |
2/2✓ Branch 0 taken 2956 times.
✓ Branch 1 taken 39236 times.
|
42192 | if (firstLogger) { |
| 377 | 2956 | firstLogger = false; | |
| 378 | } else { | ||
| 379 |
1/2✓ Branch 0 taken 39236 times.
✗ Branch 1 not taken.
|
39236 | rc << ","; |
| 380 | } | ||
| 381 |
1/2✓ Branch 0 taken 42192 times.
✗ Branch 1 not taken.
|
42192 | rc << (*it)->name; |
| 382 | } | ||
| 383 |
1/2✓ Branch 0 taken 2637 times.
✗ Branch 1 not taken.
|
5274 | return rc.str(); |
| 384 | 2637 | } | |
| 385 | |||
| 386 | 58 | void FPPLogger::SetAllLevel(LogLevel level) { | |
| 387 | 58 | std::vector<FPPLoggerInstance*>::iterator it; | |
| 388 |
1/2✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
|
58 | Init(); /* Just incase */ |
| 389 | |||
| 390 |
2/2✓ Branch 0 taken 928 times.
✓ Branch 1 taken 58 times.
|
986 | for (it = all.begin(); it != all.end(); it++) { |
| 391 |
1/2✓ Branch 0 taken 928 times.
✗ Branch 1 not taken.
|
928 | SetFacilityLevel(**it, level); |
| 392 | } | ||
| 393 | 58 | } | |
| 394 | |||
| 395 | 1207391 | std::string TruncateForLog(const std::string& s, size_t maxLen) { | |
| 396 | // Escape embedded newlines first (on the ORIGINAL string, not just the | ||
| 397 | // truncated slice) - formatLogLines() below re-prefixes every physical | ||
| 398 | // line it's given (deliberately, for attribution in this merged log | ||
| 399 | // file), so an unescaped \n in otherwise-unbounded data (a Variable's | ||
| 400 | // value, raw command output, etc.) turns one logical log entry into | ||
| 401 | // several prefixed-but-uncorrelated-looking lines. A short multi-line | ||
| 402 | // value is just as confusing as a long one, so this isn't gated behind | ||
| 403 | // the length check below. | ||
| 404 | // Only the part that survives truncation is escaped, so logging a large | ||
| 405 | // body costs maxLen rather than a copy of the whole thing. | ||
| 406 | 1207391 | std::string escaped; | |
| 407 |
1/2✓ Branch 0 taken 1207391 times.
✗ Branch 1 not taken.
|
1207391 | escaped.reserve(std::min(s.size(), maxLen + 1)); |
| 408 |
2/2✓ Branch 0 taken 44954842 times.
✓ Branch 1 taken 1192918 times.
|
46147760 | for (char c : s) { |
| 409 |
2/2✓ Branch 0 taken 14473 times.
✓ Branch 1 taken 44940369 times.
|
44954842 | if (escaped.size() > maxLen) { |
| 410 | 14473 | break; | |
| 411 | } | ||
| 412 |
2/2✓ Branch 0 taken 107565 times.
✓ Branch 1 taken 44832804 times.
|
44940369 | if (c == '\n') { |
| 413 |
1/2✓ Branch 0 taken 107565 times.
✗ Branch 1 not taken.
|
107565 | escaped += "\\n"; |
| 414 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44832804 times.
|
44832804 | } else if (c == '\r') { |
| 415 | ✗ | escaped += "\\r"; | |
| 416 | } else { | ||
| 417 |
1/2✓ Branch 0 taken 44832804 times.
✗ Branch 1 not taken.
|
44832804 | escaped += c; |
| 418 | } | ||
| 419 | } | ||
| 420 |
2/2✓ Branch 0 taken 1192918 times.
✓ Branch 1 taken 14473 times.
|
1207391 | if (escaped.size() <= maxLen) { |
| 421 | 1192918 | return escaped; | |
| 422 | } | ||
| 423 |
5/10✓ Branch 0 taken 14473 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14473 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14473 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 14473 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 14121 times.
✗ Branch 9 not taken.
|
14473 | return escaped.substr(0, maxLen) + "...(" + std::to_string(s.size()) + " bytes total)"; |
| 424 | 1207391 | } | |
| 425 | |||
| 426 | 8369858 | bool WillLog(int level, FPPLoggerInstance& facility) { | |
| 427 | // Don't log if we're not concerned about anything at this level | ||
| 428 |
2/2✓ Branch 0 taken 7921423 times.
✓ Branch 1 taken 448435 times.
|
8369858 | if (FacilityLevel(facility) < level) |
| 429 | 7921423 | return false; | |
| 430 | |||
| 431 | 448435 | return true; | |
| 432 | } | ||
| 433 | |||
| 434 | // atomic_ref rather than an atomic member: FPPLoggerInstance's layout is | ||
| 435 | // compiled into plugins (see log.h). | ||
| 436 | 8461586 | LogLevel FacilityLevel(FPPLoggerInstance& facility) { | |
| 437 | 8461586 | return std::atomic_ref<LogLevel>(facility.level).load(std::memory_order_relaxed); | |
| 438 | } | ||
| 439 | 2253 | void SetFacilityLevel(FPPLoggerInstance& facility, LogLevel level) { | |
| 440 | 2253 | std::atomic_ref<LogLevel>(facility.level).store(level, std::memory_order_relaxed); | |
| 441 | 2253 | } | |
| 442 | |||
| 443 | 137 | void _LogWrite(const char* file, int line, int level, FPPLoggerInstance& facility, const std::string& str, ...) { | |
| 444 | // Same gate as the char* overload -- a ring-only line still has to reach it. | ||
| 445 |
2/6✗ Branch 0 not taken.
✓ Branch 1 taken 137 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 137 times.
|
137 | if (!WillLog(level, facility) && !CrashLogRingWillCapture(level, facility)) |
| 446 | ✗ | return; | |
| 447 | // The string is the message, not a format: callers build it from | ||
| 448 | // sequence names, config values and exception text, and a '%' there | ||
| 449 | // used to be read as a conversion with no argument behind it. | ||
| 450 | 137 | _LogWrite(file, line, level, facility, "%s", str.c_str()); | |
| 451 | } | ||
| 452 | |||
| 453 | // Append msg to out, applying prefix to EVERY physical line. | ||
| 454 | // | ||
| 455 | // The prefix used to be snprintf'd together with the caller's format string and | ||
| 456 | // handed to vfprintf, which applied it once per LogX() call rather than per | ||
| 457 | // line: any message containing \n produced several physical lines with only the | ||
| 458 | // first one identifying itself. That was survivable while fppd owned this file | ||
| 459 | // outright, but fppd.log is now a merged, sequential record -- fppd, the | ||
| 460 | // fppd_start/stop/restart scripts and the operation breadcrumbs all append to it | ||
| 461 | // -- and an unlabelled line there is an unattributable line. | ||
| 462 | // | ||
| 463 | // A message that does not end in \n gets one; previously it left the line open | ||
| 464 | // and the next log call concatenated onto it. | ||
| 465 | 2497579 | static void formatLogLines(std::string& out, const char* prefix, const char* msg) { | |
| 466 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2497579 times.
|
2497579 | if (!*msg) { |
| 467 | ✗ | out.append(prefix); | |
| 468 | ✗ | out.append("\n"); | |
| 469 | ✗ | return; | |
| 470 | } | ||
| 471 | 2497579 | const char* p = msg; | |
| 472 |
2/2✓ Branch 0 taken 2514566 times.
✓ Branch 1 taken 2489581 times.
|
5004147 | while (*p) { |
| 473 | 2514566 | const char* nl = strchr(p, '\n'); | |
| 474 |
2/2✓ Branch 0 taken 2506568 times.
✓ Branch 1 taken 7998 times.
|
2514566 | size_t len = nl ? (size_t)(nl - p) : strlen(p); |
| 475 | 2514566 | out.append(prefix); | |
| 476 | 2514566 | out.append(p, len); | |
| 477 | 2514566 | out.append("\n"); | |
| 478 |
2/2✓ Branch 0 taken 7998 times.
✓ Branch 1 taken 2506568 times.
|
2514566 | if (!nl) { |
| 479 | 7998 | break; | |
| 480 | } | ||
| 481 | 2506568 | p = nl + 1; | |
| 482 | } | ||
| 483 | } | ||
| 484 | |||
| 485 | // __FILE__ is whatever path the compiler was handed. A local make passes | ||
| 486 | // paths relative to src/ ("non-gpl/BBShiftString/BBShiftString.cpp"), but a | ||
| 487 | // distributed compile (nocc) passes an absolute path rooted in the remote | ||
| 488 | // cache, e.g. "/var/cache/nocc/cpp/clients/<id>/opt/fpp/src/...", which buries | ||
| 489 | // the part that matters. Trim everything up to and including "/fpp/src/" so | ||
| 490 | // both builds log the same name. Paths without it (plugins) are untouched. | ||
| 491 | 2497579 | static const char* logSourceFile(const char* file) { | |
| 492 | static constexpr char marker[] = "/fpp/src/"; | ||
| 493 | 2497579 | const char* s = strstr(file, marker); | |
| 494 |
2/2✓ Branch 0 taken 37577 times.
✓ Branch 1 taken 2460002 times.
|
2497579 | return s ? s + sizeof(marker) - 1 : file; |
| 495 | } | ||
| 496 | |||
| 497 | 7690199 | void _LogWrite(const char* file, int line, int level, FPPLoggerInstance& facility, const char* format, ...) { | |
| 498 | // A line is formatted if it is either being logged normally OR retained in | ||
| 499 | // the crash ring -- the ring is the whole point of formatting a line the | ||
| 500 | // configured level would discard. | ||
| 501 |
1/2✓ Branch 0 taken 7690199 times.
✗ Branch 1 not taken.
|
7690199 | const bool willLog = WillLog(level, facility); |
| 502 |
1/2✓ Branch 0 taken 7690199 times.
✗ Branch 1 not taken.
|
7690199 | const bool willRing = CrashLogRingWillCapture(level, facility); |
| 503 |
4/4✓ Branch 0 taken 7265832 times.
✓ Branch 1 taken 424367 times.
✓ Branch 2 taken 5192620 times.
✓ Branch 3 taken 2073212 times.
|
7690199 | if (!willLog && !willRing) |
| 504 | 7266041 | return; | |
| 505 | |||
| 506 | va_list arg; | ||
| 507 | |||
| 508 | struct timeval tv; | ||
| 509 | 2497579 | gettimeofday(&tv, nullptr); | |
| 510 | 2497579 | int ms = tv.tv_usec / 1000; | |
| 511 | |||
| 512 | uint64_t tid; | ||
| 513 | #ifdef PLATFORM_OSX | ||
| 514 | pthread_threadid_np(NULL, &tid); | ||
| 515 | #else | ||
| 516 | 2497579 | tid = gettid(); | |
| 517 | #endif | ||
| 518 | |||
| 519 | // The "YYYY-MM-DD HH:MM:SS" field changes once per second, but building it | ||
| 520 | // cost a localtime_r plus seven width-qualified integer conversions on | ||
| 521 | // EVERY line -- together the largest single component of a log call. That | ||
| 522 | // is invisible on a desktop and not on the small ARM boards: measured on an | ||
| 523 | // AM335x, localtime_r ~5us and the full prefix snprintf ~12us, against | ||
| 524 | // ~1.7us to render the caller's actual message. | ||
| 525 | // | ||
| 526 | // So cache it, keyed on the second it describes. thread_local rather than | ||
| 527 | // shared: this is on every log call from every thread, and a lock or atomic | ||
| 528 | // here would cost more than it saves, while a torn shared buffer would | ||
| 529 | // corrupt timestamps. Per-thread cost is one time_t and 20 bytes. | ||
| 530 | // | ||
| 531 | // A timezone change (DST) is picked up on the next second boundary, since | ||
| 532 | // the key is the second itself. | ||
| 533 | thread_local time_t cachedSec = (time_t)-1; | ||
| 534 | thread_local char cachedDateTime[20]; // "YYYY-MM-DD HH:MM:SS" | ||
| 535 |
2/2✓ Branch 0 taken 41115 times.
✓ Branch 1 taken 2456464 times.
|
2497579 | if (tv.tv_sec != cachedSec) { |
| 536 | struct tm tm; | ||
| 537 | 41115 | localtime_r(&tv.tv_sec, &tm); | |
| 538 | 41115 | snprintf(cachedDateTime, sizeof(cachedDateTime), | |
| 539 | "%4d-%.2d-%.2d %.2d:%.2d:%.2d", | ||
| 540 | 41115 | 1900 + tm.tm_year, tm.tm_mon + 1, tm.tm_mday, | |
| 541 | tm.tm_hour, tm.tm_min, tm.tm_sec); | ||
| 542 | 41115 | cachedSec = tv.tv_sec; | |
| 543 | } | ||
| 544 | |||
| 545 | // The remaining numeric fields are emitted directly rather than through | ||
| 546 | // snprintf; integer conversion is the expensive part of a format string, | ||
| 547 | // and milliseconds are always exactly three digits here. | ||
| 548 | char prefix[512]; | ||
| 549 | 2497579 | char* p = prefix; | |
| 550 | 2497579 | memcpy(p, cachedDateTime, 19); | |
| 551 | 2497579 | p += 19; | |
| 552 | 2497579 | *p++ = '.'; | |
| 553 | 2497579 | *p++ = (char)('0' + (ms / 100) % 10); | |
| 554 | 2497579 | *p++ = (char)('0' + (ms / 10) % 10); | |
| 555 | 2497579 | *p++ = (char)('0' + ms % 10); | |
| 556 | 2497579 | *p++ = ' '; | |
| 557 | 2497579 | snprintf(p, sizeof(prefix) - (p - prefix), "%s(%llu) [%s] %s:%d: ", | |
| 558 | logProgramName(), tid, facility.name.c_str(), logSourceFile(file), line); | ||
| 559 | |||
| 560 | // Render the caller's message first so it can be split on newlines. The | ||
| 561 | // stack buffer covers essentially every real log line; the heap path exists | ||
| 562 | // so a long one (a dumped config blob) is not silently truncated. | ||
| 563 | char stackBuf[1024]; | ||
| 564 | 2497579 | va_start(arg, format); | |
| 565 | 2497579 | int msgLen = vsnprintf(stackBuf, sizeof(stackBuf), format, arg); | |
| 566 | 2497579 | va_end(arg); | |
| 567 | |||
| 568 | 2497579 | const char* msg = stackBuf; | |
| 569 | 2497579 | std::string heapBuf; | |
| 570 |
2/2✓ Branch 0 taken 413 times.
✓ Branch 1 taken 2497166 times.
|
2497579 | if (msgLen >= (int)sizeof(stackBuf)) { |
| 571 |
1/2✓ Branch 0 taken 413 times.
✗ Branch 1 not taken.
|
413 | heapBuf.resize(msgLen + 1); |
| 572 | 413 | va_start(arg, format); | |
| 573 | 413 | vsnprintf(&heapBuf[0], msgLen + 1, format, arg); | |
| 574 | 413 | va_end(arg); | |
| 575 | 413 | msg = heapBuf.c_str(); | |
| 576 | } | ||
| 577 | |||
| 578 | 2497579 | std::string out; | |
| 579 |
1/2✓ Branch 0 taken 2497579 times.
✗ Branch 1 not taken.
|
2497579 | out.reserve((msgLen > 0 ? (size_t)msgLen : 0) + 128); |
| 580 |
1/2✓ Branch 0 taken 2497579 times.
✗ Branch 1 not taken.
|
2497579 | formatLogLines(out, prefix, msg); |
| 581 | |||
| 582 |
2/2✓ Branch 0 taken 2457556 times.
✓ Branch 1 taken 40023 times.
|
2497579 | if (willRing) { |
| 583 | 2457556 | CrashLogRingPush(out.data(), out.size()); | |
| 584 | } | ||
| 585 |
2/2✓ Branch 0 taken 2073212 times.
✓ Branch 1 taken 424367 times.
|
2497579 | if (!willLog) { |
| 586 | // Retained for the crash report only; the configured level says this | ||
| 587 | // line does not belong in the log file or on stdout. | ||
| 588 | 2073212 | return; | |
| 589 | } | ||
| 590 | |||
| 591 | // One fwrite per log call: it keeps the whole (possibly multi-line) message | ||
| 592 | // contiguous, which matters now that shell writers append to this same file. | ||
| 593 | 424367 | bool wroteLogFile = false; | |
| 594 |
2/2✓ Branch 0 taken 421654 times.
✓ Branch 1 taken 2713 times.
|
424367 | if (logFileName[0]) { |
| 595 |
2/2✓ Branch 0 taken 6143 times.
✓ Branch 1 taken 415511 times.
|
421654 | if (!strcmp(logFileName, "stderr")) { |
| 596 |
1/2✓ Branch 0 taken 6143 times.
✗ Branch 1 not taken.
|
6143 | fwrite(out.data(), 1, out.size(), stderr); |
| 597 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 415511 times.
|
415511 | } else if (!strcmp(logFileName, "stdout")) { |
| 598 | ✗ | fwrite(out.data(), 1, out.size(), stdout); | |
| 599 | } else { | ||
| 600 | 415511 | std::unique_lock<std::recursive_timed_mutex> lock(logFileLock, std::defer_lock); | |
| 601 |
2/4✓ Branch 0 taken 415511 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 415511 times.
✗ Branch 3 not taken.
|
415511 | if (lock.try_lock_for(std::chrono::seconds(2))) { |
| 602 |
1/2✓ Branch 0 taken 415511 times.
✗ Branch 1 not taken.
|
415511 | FILE* logFile = getPersistentLogFile(tv.tv_sec); |
| 603 |
2/2✓ Branch 0 taken 415302 times.
✓ Branch 1 taken 209 times.
|
415511 | if (logFile) { |
| 604 |
1/2✓ Branch 0 taken 415302 times.
✗ Branch 1 not taken.
|
415302 | fwrite(out.data(), 1, out.size(), logFile); |
| 605 | 415302 | wroteLogFile = true; | |
| 606 | } else { | ||
| 607 |
1/2✓ Branch 0 taken 209 times.
✗ Branch 1 not taken.
|
209 | fprintf(stderr, "Error: Unable to open log file for writing!\n"); |
| 608 |
1/2✓ Branch 0 taken 209 times.
✗ Branch 1 not taken.
|
209 | fwrite(out.data(), 1, out.size(), stderr); |
| 609 | 209 | return; | |
| 610 | } | ||
| 611 | } else { | ||
| 612 | // Could not get the log file lock (holder likely wedged on | ||
| 613 | // stalled storage); don't hang here - emit to stderr instead. | ||
| 614 | ✗ | fwrite(out.data(), 1, out.size(), stderr); | |
| 615 | ✗ | return; | |
| 616 | } | ||
| 617 |
2/2✓ Branch 0 taken 415302 times.
✓ Branch 1 taken 209 times.
|
415511 | } |
| 618 | } | ||
| 619 | // For fppd, once the line is in a real log file a second copy on stdout is a | ||
| 620 | // pure duplicate: it runs as `fppd -f` under systemd, so logToStdOut stays | ||
| 621 | // set and journald captured every line a second time -- doubling the volume | ||
| 622 | // for a copy that does not outlive the boot, while fppd.log is what the | ||
| 623 | // Support Zip ships and what users are asked for. Suppress it at the source, | ||
| 624 | // here, for the callers that ask (SetSuppressDuplicateStdOut). | ||
| 625 | // | ||
| 626 | // Do NOT instead mute the stream in fppd.service: fppd's stdout is inherited | ||
| 627 | // by every child it forks (cape detection, plugin callbacks), so muting it | ||
| 628 | // there discards their output too. That was tried, and reverted. | ||
| 629 | // | ||
| 630 | // Every other binary that links the logger keeps writing both places. They | ||
| 631 | // are low volume, and `journalctl -u fppoled` / `systemctl status` is where | ||
| 632 | // a service's output is looked for -- that is what suppressing this | ||
| 633 | // globally, rather than per-caller, silently took away. | ||
| 634 | // | ||
| 635 | // `fppd -l stdout` still logs to stdout, and nothing suppresses stdout when | ||
| 636 | // no real log file is in use (early startup, before SetLogFile). | ||
| 637 | // | ||
| 638 | // The duplicate-suppression only applies when stdout is a pipe to journald. | ||
| 639 | // When a person runs `fppd -f` in a terminal, stdout is a TTY and the copy | ||
| 640 | // is a genuine on-screen echo of the log file, not a journald duplicate, so | ||
| 641 | // keep echoing every line -- that is the interactive foreground behavior. | ||
| 642 |
5/6✓ Branch 0 taken 424158 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 417816 times.
✓ Branch 3 taken 6342 times.
✓ Branch 4 taken 2783 times.
✓ Branch 5 taken 421375 times.
|
841974 | if (strcmp(logFileName, "stdout") && logToStdOut && |
| 643 |
6/6✓ Branch 0 taken 415103 times.
✓ Branch 1 taken 2713 times.
✓ Branch 2 taken 415063 times.
✓ Branch 3 taken 40 times.
✓ Branch 4 taken 30 times.
✓ Branch 5 taken 415033 times.
|
417816 | (!wroteLogFile || !suppressDuplicateStdOut || stdoutIsInteractive())) { |
| 644 |
1/2✓ Branch 0 taken 2783 times.
✗ Branch 1 not taken.
|
2783 | fwrite(out.data(), 1, out.size(), stdout); |
| 645 | } | ||
| 646 |
4/4✓ Branch 0 taken 424158 times.
✓ Branch 1 taken 2073421 times.
✓ Branch 2 taken 424158 times.
✓ Branch 3 taken 2073421 times.
|
4571000 | } |
| 647 | |||
| 648 | 2652 | void SetSuppressDuplicateStdOut(bool suppress) { | |
| 649 | 2652 | suppressDuplicateStdOut = suppress; | |
| 650 | 2652 | } | |
| 651 | |||
| 652 | 2752 | void SetLogFile(const char* filename, bool toStdOut) { | |
| 653 |
1/2✓ Branch 0 taken 2752 times.
✗ Branch 1 not taken.
|
2752 | std::unique_lock<std::recursive_timed_mutex> lock(logFileLock); |
| 654 |
1/2✓ Branch 0 taken 2752 times.
✗ Branch 1 not taken.
|
2752 | closePersistentLogFile(); |
| 655 | 2752 | snprintf(logFileName, sizeof(logFileName), "%s", filename); | |
| 656 | 2752 | logToStdOut = toStdOut; | |
| 657 | 2752 | } | |
| 658 | |||
| 659 | 4220 | std::string LogLevelToString(LogLevel level) { | |
| 660 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4214 times.
|
4220 | if (level == LOG_WARN) { |
| 661 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
12 | return "warn"; |
| 662 |
2/2✓ Branch 0 taken 698 times.
✓ Branch 1 taken 3516 times.
|
4214 | } else if (level == LOG_DEBUG) { |
| 663 |
1/2✓ Branch 0 taken 698 times.
✗ Branch 1 not taken.
|
1396 | return "debug"; |
| 664 |
2/2✓ Branch 0 taken 3130 times.
✓ Branch 1 taken 386 times.
|
3516 | } else if (level == LOG_INFO) { |
| 665 |
1/2✓ Branch 0 taken 3130 times.
✗ Branch 1 not taken.
|
6260 | return "info"; |
| 666 |
1/2✓ Branch 0 taken 386 times.
✗ Branch 1 not taken.
|
386 | } else if (level == LOG_EXCESSIVE) { |
| 667 |
1/2✓ Branch 0 taken 386 times.
✗ Branch 1 not taken.
|
772 | return "excess"; |
| 668 | ✗ | } else if (level == LOG_ERR) { | |
| 669 | ✗ | return "error"; | |
| 670 | } else { | ||
| 671 | ✗ | return "unknown"; | |
| 672 | } | ||
| 673 | } | ||
| 674 | |||
| 675 | /* | ||
| 676 | * Parse a string like debug:schedule,player;excess:mqtt for log level | ||
| 677 | */ | ||
| 678 | 265 | bool SetLogLevelComplex(std::string& input) { | |
| 679 |
1/2✓ Branch 0 taken 265 times.
✗ Branch 1 not taken.
|
265 | LogDebug(VB_SETTING, "Attempting to parse Log Levels from %s\n", input.c_str()); |
| 680 | |||
| 681 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 265 times.
|
265 | if (input.length() == 0) { |
| 682 | ✗ | LogWarn(VB_SETTING, "Ignoring Empty String sent to SetLogLevelComplex"); | |
| 683 | ✗ | return false; | |
| 684 | } | ||
| 685 | // Check if simple method | ||
| 686 |
2/2✓ Branch 0 taken 245 times.
✓ Branch 1 taken 20 times.
|
265 | if (input.find(':') == std::string::npos) { |
| 687 |
1/2✓ Branch 0 taken 245 times.
✗ Branch 1 not taken.
|
245 | LogDebug(VB_SETTING, "Using simple loging for input \"%s\" as no colon found.\n", input.c_str()); |
| 688 |
1/2✓ Branch 0 taken 245 times.
✗ Branch 1 not taken.
|
245 | return SetLogLevel(input.c_str()); |
| 689 | } | ||
| 690 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
|
20 | if (input.find(';') == std::string::npos) { |
| 691 |
1/2✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
18 | input = input + ';'; |
| 692 | } | ||
| 693 | |||
| 694 | 20 | bool madeChange = false; | |
| 695 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | std::istringstream ss(input); |
| 696 | 20 | std::string token1; | |
| 697 | |||
| 698 |
4/6✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 23 times.
✓ Branch 5 taken 20 times.
|
43 | while (std::getline(ss, token1, ';')) { |
| 699 | // token1 should be debug:scheduler,player | ||
| 700 | |||
| 701 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | LogDebug(VB_SETTING, "Outer Prase is %s\n", token1.c_str()); |
| 702 | 23 | size_t pos = token1.find(':'); | |
| 703 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | if (pos != std::string::npos) { |
| 704 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | std::string level = token1.substr(0, pos); |
| 705 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | std::string loggers = token1.substr(pos + 1); |
| 706 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | std::istringstream st(loggers); |
| 707 | 23 | std::string logger; | |
| 708 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 17 times.
|
23 | if (loggers.find(',') == std::string::npos) { |
| 709 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | loggers = loggers + ","; |
| 710 | } | ||
| 711 |
4/6✓ Branch 0 taken 270 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 270 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 247 times.
✓ Branch 5 taken 23 times.
|
270 | while (std::getline(st, logger, ',')) { |
| 712 |
1/2✓ Branch 0 taken 247 times.
✗ Branch 1 not taken.
|
247 | LogDebug(VB_SETTING, "Attempting to set %s to %s \n", logger.c_str(), level.c_str()); |
| 713 |
4/6✓ Branch 0 taken 247 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 246 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
|
247 | madeChange = FPPLogger::INSTANCE.SetLevel(logger.c_str(), level.c_str()) || madeChange; |
| 714 | } | ||
| 715 | 23 | } else { | |
| 716 | ✗ | LogErr(VB_SETTING, "Invalid Log Level Token: %s\n", token1.c_str()); | |
| 717 | } | ||
| 718 | } | ||
| 719 | |||
| 720 | 20 | return madeChange; | |
| 721 | 20 | } | |
| 722 | 17 | bool SetLogLevelComplex(const char* input) { | |
| 723 |
1/2✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
|
17 | std::string s = input; |
| 724 |
1/2✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
|
34 | return SetLogLevelComplex(s); |
| 725 | 17 | } | |
| 726 | |||
| 727 | 246 | bool SetLogLevel(const char* newLevel) { | |
| 728 | 246 | LogErr(VB_SETTING, "Using Legacy Global Logging to set all to same Log Level of %s\n", newLevel); | |
| 729 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 246 times.
|
246 | if (!strcmp(newLevel, "warn")) { |
| 730 | ✗ | FPPLogger::INSTANCE.SetAllLevel(LOG_WARN); | |
| 731 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 217 times.
|
246 | } else if (!strcmp(newLevel, "debug")) { |
| 732 | 29 | FPPLogger::INSTANCE.SetAllLevel(LOG_DEBUG); | |
| 733 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 211 times.
|
217 | } else if (!strcmp(newLevel, "info")) { |
| 734 | 6 | FPPLogger::INSTANCE.SetAllLevel(LOG_INFO); | |
| 735 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 188 times.
|
211 | } else if (!strcmp(newLevel, "excess")) { |
| 736 | 23 | FPPLogger::INSTANCE.SetAllLevel(LOG_EXCESSIVE); | |
| 737 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 188 times.
|
188 | } else if (!strcmp(newLevel, "error")) { |
| 738 | ✗ | FPPLogger::INSTANCE.SetAllLevel(LOG_ERR); | |
| 739 | } else { | ||
| 740 | 188 | LogErr(VB_SETTING, "Unknown Log Level: %s\n", newLevel); | |
| 741 | 188 | return false; | |
| 742 | } | ||
| 743 | |||
| 744 | 58 | return true; | |
| 745 | } | ||
| 746 | |||
| 747 | 2652 | int loggingToFile(void) { | |
| 748 |
1/2✓ Branch 0 taken 2652 times.
✗ Branch 1 not taken.
|
2652 | if ((logFileName[0]) && |
| 749 |
1/2✓ Branch 0 taken 2652 times.
✗ Branch 1 not taken.
|
2652 | (strcmp(logFileName, "stderr")) && |
| 750 |
1/2✓ Branch 0 taken 2652 times.
✗ Branch 1 not taken.
|
2652 | (strcmp(logFileName, "stdout"))) |
| 751 | 2652 | return 1; | |
| 752 | |||
| 753 | ✗ | return 0; | |
| 754 | } | ||
| 755 | |||
| 756 | 2652 | void logVersionInfo(void) { | |
| 757 |
1/2✓ Branch 0 taken 2652 times.
✗ Branch 1 not taken.
|
2652 | LogErr(VB_ALL, "=========================================\n"); |
| 758 |
2/4✓ Branch 0 taken 2652 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2652 times.
✗ Branch 3 not taken.
|
2652 | LogErr(VB_ALL, "FPP %s\n", getFPPVersion()); |
| 759 |
2/4✓ Branch 0 taken 2652 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2652 times.
✗ Branch 3 not taken.
|
2652 | LogErr(VB_ALL, "Branch: %s\n", getFPPBranch()); |
| 760 |
1/2✓ Branch 0 taken 2652 times.
✗ Branch 1 not taken.
|
2652 | int o = open("/proc/device-tree/model", O_RDONLY); |
| 761 |
2/2✓ Branch 0 taken 296 times.
✓ Branch 1 taken 2356 times.
|
2652 | if (o >= 0) { |
| 762 | // A short read continues at buf + c; reading back into buf replaced | ||
| 763 | // the start of the model with its tail. | ||
| 764 | char buf[256]; | ||
| 765 | 296 | int c = 0; | |
| 766 |
1/2✓ Branch 0 taken 592 times.
✗ Branch 1 not taken.
|
592 | while (c < 255) { |
| 767 |
1/2✓ Branch 0 taken 592 times.
✗ Branch 1 not taken.
|
592 | ssize_t i = read(o, buf + c, 255 - c); |
| 768 |
2/2✓ Branch 0 taken 296 times.
✓ Branch 1 taken 296 times.
|
592 | if (i <= 0) { |
| 769 | 296 | break; | |
| 770 | } | ||
| 771 | 296 | c += i; | |
| 772 | } | ||
| 773 |
1/2✓ Branch 0 taken 296 times.
✗ Branch 1 not taken.
|
296 | if (c > 0) { |
| 774 | 296 | buf[c] = 0; | |
| 775 |
1/2✓ Branch 0 taken 296 times.
✗ Branch 1 not taken.
|
296 | LogErr(VB_ALL, "Model: %s\n", buf); |
| 776 | } | ||
| 777 |
1/2✓ Branch 0 taken 296 times.
✗ Branch 1 not taken.
|
296 | close(o); |
| 778 | } | ||
| 779 | // Kernel matters for triage far more often than it looks: the Pi's V4L2 | ||
| 780 | // decoder wedge (issues #2695/#2727) and several DRM/KMS bugs are fixed or | ||
| 781 | // broken per kernel, and asking a user for `uname -a` days after the fact | ||
| 782 | // is unreliable -- the box may have been updated since. Log it with the | ||
| 783 | // rest of the version banner so every fppd.log answers it on its own. | ||
| 784 | struct utsname un; | ||
| 785 |
1/2✓ Branch 0 taken 2652 times.
✗ Branch 1 not taken.
|
2652 | if (uname(&un) == 0) { |
| 786 |
1/2✓ Branch 0 taken 2652 times.
✗ Branch 1 not taken.
|
2652 | LogErr(VB_ALL, "Kernel: %s %s (%s)\n", un.sysname, un.release, un.machine); |
| 787 | } | ||
| 788 |
1/2✓ Branch 0 taken 2652 times.
✗ Branch 1 not taken.
|
2652 | LogErr(VB_ALL, "=========================================\n"); |
| 789 | 2652 | } | |
| 790 |