Fray Engine
Loading...
Searching...
No Matches
log_store.hpp
Go to the documentation of this file.
1/* log_store.hpp */
2
3#ifndef _FRAY_LOG_STORE_HPP_
4#define _FRAY_LOG_STORE_HPP_
5
6#include <algorithm>
7#include <atomic>
8#include <cctype>
9#include <chrono>
10#include <cstdint>
11#include <cstdio>
12#include <cstdlib>
13#include <filesystem>
14#include <map>
15#include <memory>
16#include <mutex>
17#include <new>
18#include <optional>
19#include <stdexcept>
20#include <string>
21#include <vector>
22
23#include <sqlite3.h>
24
25#include "uid.hpp"
26
27namespace fray
28{
29 namespace logs
30 {
35 struct Entry
36 {
38 std::string uid;
42 std::uint64_t arrivedUs = 0;
46 int process = 0;
50 std::string level;
54 std::uint32_t thread = 0;
58 std::string project;
62 std::string text;
64 int kind = 0;
66 int destination = 0;
68 std::uint64_t operation = 0;
70 std::string refUid;
72 bool consoleVisible = true;
73 };
74
78 struct Query
79 {
83 std::uint64_t beforeUs = 0;
87 std::uint32_t limit = 100;
91 std::vector<int> processes;
95 std::vector<std::string> levels;
99 bool filterProject = false;
103 std::string project;
107 std::string contains;
109 bool consoleOnly = false;
110 };
111
115 struct Page
116 {
120 std::vector<Entry> entries;
124 bool exhausted = true;
125 };
126
132 {
133 private:
137 struct Database
138 {
142 sqlite3* db = nullptr;
144 std::filesystem::path path;
146 std::mutex mutex;
151 bool close()
152 {
153 std::lock_guard<std::mutex> lock(mutex);
154 if (!db)
155 return true;
156 const int result = sqlite3_close(db);
157 if (result != SQLITE_OK)
158 {
159 std::fprintf(stderr, "Cannot close log database: %s\n", sqlite3_errmsg(db));
160 return false;
161 }
162 db = nullptr;
163 return true;
164 }
165
169 {
170 close();
171 }
172 };
173
176 struct Folder
177 {
181 std::filesystem::path path;
185 std::shared_ptr<Database> session;
186 };
187
191 std::mutex _mutex;
195 std::filesystem::path _appFolder;
199 std::map<std::string, Folder> _projects;
203 std::map<std::string, std::vector<Entry>> _opening;
207 std::map<std::string, std::string> _resolvedOpening;
211 std::shared_ptr<Database> _app;
215 std::atomic<std::uint64_t> _last{0};
216
221 static std::uint64_t nowUs()
222 {
223 using namespace std::chrono;
224 return static_cast<std::uint64_t>(
225 duration_cast<microseconds>(system_clock::now().time_since_epoch()).count());
226 }
227
233 std::uint64_t nextStamp(
234 std::uint64_t observedUs = 0)
235 {
236 auto previous = _last.load(std::memory_order_relaxed);
237 const auto observed = observedUs ? observedUs : nowUs();
238 for (;;)
239 {
240 const auto next = std::max(previous + 1, observed);
241 if (_last.compare_exchange_weak(previous, next, std::memory_order_relaxed))
242 return next;
243 }
244 }
245
252 static std::shared_ptr<Database> openFile(
253 const std::filesystem::path& file)
254 {
255 auto database = std::make_shared<Database>();
256 database->path = file;
257 if (sqlite3_open(file.string().c_str(), &database->db) != SQLITE_OK)
258 return nullptr;
259 // Some SQLite builds (Apple's) keep the -wal and -shm files after the last connection closes.
260 int persist = 0;
261 sqlite3_file_control(database->db, "main", SQLITE_FCNTL_PERSIST_WAL, &persist);
262 sqlite3_exec(database->db, "PRAGMA journal_mode=WAL;PRAGMA synchronous=NORMAL;", nullptr, nullptr, nullptr);
263 const char* schema =
264 "CREATE TABLE IF NOT EXISTS log("
265 "arrived_us INTEGER PRIMARY KEY, uid TEXT NOT NULL UNIQUE, process INTEGER NOT NULL, level TEXT NOT NULL,"
266 "thread INTEGER NOT NULL, project TEXT NOT NULL, text TEXT NOT NULL,"
267 "kind INTEGER NOT NULL DEFAULT 0, destination INTEGER NOT NULL DEFAULT 0,"
268 "operation INTEGER NOT NULL DEFAULT 0, ref_uid TEXT NOT NULL DEFAULT '',"
269 "console_visible INTEGER NOT NULL DEFAULT 1);";
270 if (sqlite3_exec(database->db, schema, nullptr, nullptr, nullptr) != SQLITE_OK)
271 return nullptr;
272 bool hasVisibility = false;
273 if (!hasConsoleVisibility(database->db, hasVisibility))
274 return nullptr;
275 if (!hasVisibility && sqlite3_exec(database->db,
276 "ALTER TABLE log ADD COLUMN console_visible INTEGER NOT NULL DEFAULT 1;",
277 nullptr, nullptr, nullptr) != SQLITE_OK)
278 return nullptr;
279 return database;
280 }
281
289 sqlite3* db,
290 bool& present)
291 {
292 sqlite3_stmt* statement = nullptr;
293 if (sqlite3_prepare_v2(db,
294 "SELECT 1 FROM pragma_table_info('log') WHERE name='console_visible';",
295 -1, &statement, nullptr) != SQLITE_OK)
296 {
297 std::fprintf(stderr, "Cannot inspect log schema: %s\n", sqlite3_errmsg(db));
298 return false;
299 }
300 const int result = sqlite3_step(statement);
301 present = result == SQLITE_ROW;
302 if (result != SQLITE_ROW && result != SQLITE_DONE)
303 std::fprintf(stderr, "Cannot inspect log schema: %s\n", sqlite3_errmsg(db));
304 sqlite3_finalize(statement);
305 return result == SQLITE_ROW || result == SQLITE_DONE;
306 }
307
314 static std::shared_ptr<Database> create(
315 const std::filesystem::path& folder,
316 std::uint64_t stamp)
317 {
318 std::error_code error;
319 std::filesystem::create_directories(folder, error);
320 if (error)
321 return nullptr;
322 return openFile(folder / ("session-" + std::to_string(stamp) + ".sqlite"));
323 }
324
331 static void insert(
332 Database& database,
333 const Entry& entry)
334 {
335 std::lock_guard<std::mutex> lock(database.mutex);
336 if (!database.db)
337 {
338 std::fprintf(stderr, "Cannot write log entry: session is closed.\n");
339 return;
340 }
341 sqlite3_stmt* statement = nullptr;
342 if (sqlite3_prepare_v2(database.db,
343 "INSERT INTO log(arrived_us,uid,process,level,thread,project,text,kind,destination,operation,ref_uid,console_visible) "
344 "VALUES(?,?,?,?,?,?,?,?,?,?,?,?);",
345 -1, &statement, nullptr) != SQLITE_OK)
346 {
347 std::fprintf(stderr, "Cannot prepare log entry: %s\n", sqlite3_errmsg(database.db));
348 return;
349 }
350 sqlite3_bind_int64(statement, 1, static_cast<sqlite3_int64>(entry.arrivedUs));
351 sqlite3_bind_text(statement, 2, entry.uid.c_str(), -1, SQLITE_TRANSIENT);
352 sqlite3_bind_int(statement, 3, entry.process);
353 sqlite3_bind_text(statement, 4, entry.level.c_str(), -1, SQLITE_TRANSIENT);
354 sqlite3_bind_int64(statement, 5, entry.thread);
355 sqlite3_bind_text(statement, 6, entry.project.c_str(), -1, SQLITE_TRANSIENT);
356 sqlite3_bind_text(statement, 7, entry.text.c_str(), -1, SQLITE_TRANSIENT);
357 sqlite3_bind_int(statement, 8, entry.kind);
358 sqlite3_bind_int(statement, 9, entry.destination);
359 sqlite3_bind_int64(statement, 10, static_cast<sqlite3_int64>(entry.operation));
360 sqlite3_bind_text(statement, 11, entry.refUid.c_str(), -1, SQLITE_TRANSIENT);
361 sqlite3_bind_int(statement, 12, entry.consoleVisible ? 1 : 0);
362 if (sqlite3_step(statement) != SQLITE_DONE)
363 std::fprintf(stderr, "Cannot write log entry: %s\n", sqlite3_errmsg(database.db));
364 sqlite3_finalize(statement);
365 }
366
372 static std::string escape(
373 const std::string& text)
374 {
375 std::string out;
376 for (const char c : text)
377 {
378 if (c == '\\' || c == '%' || c == '_')
379 out += '\\';
380 out += c;
381 }
382 return out;
383 }
384
393 static void select(
394 sqlite3* db,
395 const Query& query,
396 std::size_t count,
397 std::vector<Entry>& out)
398 {
399 bool hasVisibility = false;
400 if (!hasConsoleVisibility(db, hasVisibility))
401 return;
402 std::string sql = "SELECT arrived_us,uid,process,level,thread,project,text,kind,destination,operation,ref_uid,";
403 sql += hasVisibility ? "console_visible" : "1";
404 sql += " FROM log WHERE 1=1";
405 if (query.consoleOnly && hasVisibility)
406 sql += " AND console_visible = 1";
407 if (query.beforeUs)
408 sql += " AND arrived_us < ?";
409 if (!query.processes.empty())
410 {
411 sql += " AND process IN (";
412 for (std::size_t i = 0; i < query.processes.size(); ++i)
413 sql += i ? ",?" : "?";
414 sql += ")";
415 }
416 if (!query.levels.empty())
417 {
418 sql += " AND level IN (";
419 for (std::size_t i = 0; i < query.levels.size(); ++i)
420 sql += i ? ",?" : "?";
421 sql += ")";
422 }
423 if (query.filterProject)
424 sql += " AND project = ?";
425 if (!query.contains.empty())
426 sql += " AND text LIKE ? ESCAPE '\\'";
427 sql += " ORDER BY arrived_us DESC LIMIT ?;";
428
429 sqlite3_stmt* statement = nullptr;
430 if (sqlite3_prepare_v2(db, sql.c_str(), -1, &statement, nullptr) != SQLITE_OK)
431 {
432 std::fprintf(stderr, "Cannot read log entries: %s\n", sqlite3_errmsg(db));
433 return;
434 }
435 int index = 1;
436 if (query.beforeUs)
437 sqlite3_bind_int64(statement, index++, static_cast<sqlite3_int64>(query.beforeUs));
438 for (const int process : query.processes)
439 sqlite3_bind_int(statement, index++, process);
440 for (const auto& level : query.levels)
441 sqlite3_bind_text(statement, index++, level.c_str(), -1, SQLITE_TRANSIENT);
442 if (query.filterProject)
443 sqlite3_bind_text(statement, index++, query.project.c_str(), -1, SQLITE_TRANSIENT);
444 if (!query.contains.empty())
445 {
446 const std::string pattern = "%" + escape(query.contains) + "%";
447 sqlite3_bind_text(statement, index++, pattern.c_str(), -1, SQLITE_TRANSIENT);
448 }
449 sqlite3_bind_int64(statement, index, static_cast<sqlite3_int64>(count));
450 int result;
451 while ((result = sqlite3_step(statement)) == SQLITE_ROW)
452 {
453 Entry entry;
454 entry.arrivedUs = static_cast<std::uint64_t>(sqlite3_column_int64(statement, 0));
455 entry.uid = reinterpret_cast<const char*>(sqlite3_column_text(statement, 1));
456 entry.process = sqlite3_column_int(statement, 2);
457 entry.level = reinterpret_cast<const char*>(sqlite3_column_text(statement, 3));
458 entry.thread = static_cast<std::uint32_t>(sqlite3_column_int64(statement, 4));
459 entry.project = reinterpret_cast<const char*>(sqlite3_column_text(statement, 5));
460 entry.text = reinterpret_cast<const char*>(sqlite3_column_text(statement, 6));
461 entry.kind = sqlite3_column_int(statement, 7);
462 entry.destination = sqlite3_column_int(statement, 8);
463 entry.operation = static_cast<std::uint64_t>(sqlite3_column_int64(statement, 9));
464 entry.refUid = reinterpret_cast<const char*>(sqlite3_column_text(statement, 10));
465 entry.consoleVisible = sqlite3_column_int(statement, 11) != 0;
466 out.push_back(std::move(entry));
467 }
468 if (result != SQLITE_DONE)
469 std::fprintf(stderr, "Cannot read log entries: %s\n", sqlite3_errmsg(db));
470 sqlite3_finalize(statement);
471 }
472
482 const Folder& folder,
483 const Query& query,
484 std::size_t count,
485 std::vector<Entry>& out)
486 {
487 std::error_code error;
488 for (std::filesystem::directory_iterator it(folder.path, error), end; !error && it != end; it.increment(error))
489 {
490 const auto& path = it->path();
491 const auto name = path.filename().string();
492 if (name.rfind("session-", 0) != 0 || path.extension() != ".sqlite")
493 continue;
494 std::shared_ptr<Database> live = folder.session;
495 if (live)
496 {
497 std::lock_guard<std::mutex> lock(live->mutex);
498 if (live->db && sqlite3_db_filename(live->db, "main") &&
499 std::filesystem::path(sqlite3_db_filename(live->db, "main")).filename() == path.filename())
500 {
501 select(live->db, query, count, out);
502 continue;
503 }
504 }
505 // A session that ended without a clean close (crash, kill) still has its rows in a -wal file,
506 // which an immutable open would ignore. Open those normally so SQLite recovers and checkpoints them.
507 std::error_code walError;
508 const auto walSize = std::filesystem::file_size(path.string() + "-wal", walError);
509 if (!walError && walSize > 0)
510 {
511 Database recovered;
512 if (sqlite3_open_v2(path.string().c_str(), &recovered.db, SQLITE_OPEN_READWRITE, nullptr) == SQLITE_OK)
513 select(recovered.db, query, count, out);
514 continue;
515 }
516 // A cleanly closed session is never written again, so open it immutable: no -wal or -shm is needed or created.
517 std::string uri = "file:";
518 for (const unsigned char c : path.string())
519 {
520 static const char* hex = "0123456789ABCDEF";
521 if (std::isalnum(c) || c == '/' || c == '-' || c == '_' || c == '.')
522 uri.push_back(static_cast<char>(c));
523 else
524 {
525 uri.push_back('%');
526 uri.push_back(hex[c >> 4]);
527 uri.push_back(hex[c & 15]);
528 }
529 }
530 uri += "?immutable=1";
531 Database closed;
532 if (sqlite3_open_v2(uri.c_str(), &closed.db, SQLITE_OPEN_READONLY | SQLITE_OPEN_URI, nullptr) == SQLITE_OK)
533 select(closed.db, query, count, out);
534 }
535 }
536
537 public:
544 const std::filesystem::path& folder)
545 {
546 std::lock_guard<std::mutex> lock(_mutex);
547 _appFolder = folder;
548 _app.reset();
549 }
550
556 static bool isOpeningTag(
557 const std::string& project)
558 {
559 return project.rfind("opening:", 0) == 0;
560 }
561
567 const std::string& openingTag)
568 {
569 std::lock_guard<std::mutex> lock(_mutex);
570 _resolvedOpening.erase(openingTag);
571 }
572
581 const std::string& id,
582 const std::filesystem::path& workingFolder,
583 const std::string& openingTag = std::string())
584 {
585 Folder folder;
586 folder.path = workingFolder / "logs";
587 folder.session = create(folder.path, nextStamp());
588 if (!folder.session)
589 std::fprintf(stderr, "Cannot open project log session in %s; using application log.\n",
590 folder.path.string().c_str());
591 std::vector<Entry> buffered;
592 {
593 std::lock_guard<std::mutex> lock(_mutex);
594 const auto held = _opening.find(openingTag);
595 if (held != _opening.end())
596 {
597 buffered = std::move(held->second);
598 _opening.erase(held);
599 }
600 _projects[id] = folder;
601 if (!openingTag.empty())
602 _resolvedOpening[openingTag] = id;
603 }
604 for (auto& entry : buffered)
605 {
606 entry.project = id;
607 if (folder.session)
608 insert(*folder.session, entry);
609 else
610 persistApplication(entry);
611 }
612 }
613
620 const std::string& openingTag)
621 {
622 {
623 std::vector<Entry> buffered;
624 {
625 std::lock_guard<std::mutex> lock(_mutex);
626 _resolvedOpening[openingTag] = "";
627 const auto held = _opening.find(openingTag);
628 if (held == _opening.end())
629 return;
630 buffered = std::move(held->second);
631 _opening.erase(held);
632 }
633 for (auto& entry : buffered)
634 {
635 entry.project.clear();
636 persist(entry);
637 }
638 }
639 }
640
647 const std::string& id)
648 {
649 std::shared_ptr<Database> session;
650 {
651 std::lock_guard<std::mutex> lock(_mutex);
652 const auto found = _projects.find(id);
653 if (found != _projects.end())
654 session = found->second.session;
655 }
656 return !session || session->close();
657 }
658
667 const std::string& id,
668 const std::filesystem::path& workingFolder)
669 {
670 std::shared_ptr<Database> session;
671 {
672 std::lock_guard<std::mutex> lock(_mutex);
673 const auto found = _projects.find(id);
674 if (found == _projects.end() || !found->second.session)
675 return true;
676 session = found->second.session;
677 }
678 if (!session->close())
679 return false;
680 const auto folder = workingFolder / "logs";
681 const auto file = folder / session->path.filename();
682 // Open and close once so the last connection removes the files, then keep writing.
683 openFile(file).reset();
684 auto reopened = openFile(file);
685 if (!reopened)
686 std::fprintf(stderr, "Cannot reopen project log session in %s.\n", folder.string().c_str());
687 const bool success = static_cast<bool>(reopened);
688 std::lock_guard<std::mutex> lock(_mutex);
689 const auto found = _projects.find(id);
690 if (found != _projects.end() && found->second.session == session)
691 {
692 found->second.path = folder;
693 found->second.session = std::move(reopened);
694 }
695 return success;
696 }
697
705 const std::string& id)
706 {
707 Folder folder;
708 {
709 std::lock_guard<std::mutex> lock(_mutex);
710 const auto found = _projects.find(id);
711 if (found == _projects.end())
712 return true;
713 folder = std::move(found->second);
714 _projects.erase(found);
715 }
716 if (folder.session && !folder.session->close())
717 {
718 std::lock_guard<std::mutex> lock(_mutex);
719 _projects.emplace(id, std::move(folder));
720 return false;
721 }
722 return true;
723 }
724
731 std::optional<Entry> prepare(
732 Entry entry)
733 {
734 if (entry.uid.empty())
735 {
736 try
737 {
739 }
740 catch (const std::runtime_error& error)
741 {
742 std::fprintf(stderr, "Cannot identify log entry: %s\n", error.what());
743 return std::nullopt;
744 }
745 catch (const std::bad_alloc& error)
746 {
747 std::fprintf(stderr, "Cannot identify log entry: %s\n", error.what());
748 return std::nullopt;
749 }
750 }
751 if (!identity::valid(entry.uid))
752 {
753 std::fprintf(stderr, "Cannot identify log entry: invalid UID.\n");
754 return std::nullopt;
755 }
756 entry.arrivedUs = nextStamp(entry.arrivedUs);
757 return entry;
758 }
759
766 const Entry& original)
767 {
768 Entry entry = original;
769 std::shared_ptr<Database> database;
770 {
771 std::lock_guard<std::mutex> lock(_mutex);
772 if (isOpeningTag(entry.project))
773 {
774 const auto resolved = _resolvedOpening.find(entry.project);
775 if (resolved != _resolvedOpening.end())
776 entry.project = resolved->second;
777 }
778 const auto project = _projects.find(entry.project);
779 if (isOpeningTag(entry.project) && project == _projects.end())
780 {
781 _opening[entry.project].push_back(entry);
782 return;
783 }
784 if (!entry.project.empty() && project != _projects.end() && project->second.session)
785 database = project->second.session;
786 else
787 {
788 if (!_app && !_appFolder.empty())
790 database = _app;
791 if (!database)
792 std::fprintf(stderr, "Cannot open application log session in %s.\n", _appFolder.string().c_str());
793 }
794 }
795 if (database)
796 insert(*database, entry);
797 }
798
804 const Entry& original)
805 {
806 Entry entry = original;
807 std::shared_ptr<Database> database;
808 {
809 std::lock_guard<std::mutex> lock(_mutex);
810 if (isOpeningTag(entry.project))
811 {
812 const auto resolved = _resolvedOpening.find(entry.project);
813 if (resolved != _resolvedOpening.end())
814 entry.project = resolved->second;
815 }
816 if (!_app && !_appFolder.empty())
818 database = _app;
819 if (!database)
820 std::fprintf(stderr, "Cannot open application log session in %s.\n", _appFolder.string().c_str());
821 }
822 if (database)
823 insert(*database, entry);
824 }
825
831 std::optional<Entry> write(
832 Entry entry)
833 {
834 auto prepared = prepare(std::move(entry));
835 if (prepared)
836 persist(*prepared);
837 return prepared;
838 }
839
847 const Query& query)
848 {
849 const std::size_t limit = std::max<std::uint32_t>(query.limit, 1);
850 std::vector<Entry> found;
851 std::vector<Folder> folders;
852 {
853 std::lock_guard<std::mutex> lock(_mutex);
854 if (!_appFolder.empty())
855 folders.push_back(Folder{_appFolder, _app});
856 for (const auto& project : _projects)
857 folders.push_back(project.second);
858 }
859 for (const auto& folder : folders)
860 selectFolder(folder, query, limit + 1, found);
861 std::sort(found.begin(), found.end(),
862 [](const Entry& a, const Entry& b) { return a.arrivedUs > b.arrivedUs; });
863 Page result;
864 result.exhausted = found.size() <= limit;
865 if (found.size() > limit)
866 found.resize(limit);
867 std::reverse(found.begin(), found.end());
868 result.entries = std::move(found);
869 return result;
870 }
871 };
872 }
873}
874
875#endif
Persist and query application and project logs across session SQLite files. Project logs use their wo...
std::map< std::string, Folder > _projects
Currently open project folders indexed by project id.
void setApplicationFolder(const std::filesystem::path &folder)
Set the destination folder for application and fallback entries. Locks _mutex and closes the current ...
static std::shared_ptr< Database > create(const std::filesystem::path &folder, std::uint64_t stamp)
Create a session database in a log folder.
std::optional< Entry > write(Entry entry)
Identify, stamp, and persist an entry synchronously.
std::map< std::string, std::vector< Entry > > _opening
Lines buffered before a project opens, keyed by opening tag.
void beginOpening(const std::string &openingTag)
Starts a new opening tag, clearing any previous resolution of the same tag.
void persist(const Entry &original)
Persist an already stamped entry in its project, opening buffer, or application session....
std::atomic< std::uint64_t > _last
Last allocated arrival timestamp or session stamp.
void discardOpening(const std::string &openingTag)
Move a failed project opening's buffered lines into the application log. Clears the project tag and d...
bool suspendProject(const std::string &id)
Close a project's live session before its folder is renamed.
Page page(const Query &query)
Query the newest matching entries across the application and open projects. Takes a snapshot of regis...
std::uint64_t nextStamp(std::uint64_t observedUs=0)
Allocates a timestamp without waiting for SQLite or project operations.
static std::string escape(const std::string &text)
Escape SQLite LIKE wildcard and escape characters.
std::map< std::string, std::string > _resolvedOpening
Resolved opening tags; an empty ID sends late entries to the application log.
void persistApplication(const Entry &original)
Persist an entry only in the application session while retaining its project identity.
static std::shared_ptr< Database > openFile(const std::filesystem::path &file)
Open or create an SQLite log database and ensure its schema exists. Configures WAL and normal synchro...
void openProject(const std::string &id, const std::filesystem::path &workingFolder, const std::string &openingTag=std::string())
Open a new project log session and flush buffered opening lines. Creates <workingFolder>/logs and a s...
void selectFolder(const Folder &folder, const Query &query, std::size_t count, std::vector< Entry > &out)
Append matching entries from session files in a folder. Uses the live connection for its current file...
std::shared_ptr< Database > _app
Shared owner of the current application session connection.
bool moveProject(const std::string &id, const std::filesystem::path &workingFolder)
Reopen a project session after its working folder moves. Closes the old-path connection and reopens t...
std::mutex _mutex
Protects folder registrations, opening buffers, and session references.
static bool isOpeningTag(const std::string &project)
Check whether a project identifier is an opening tag.
static void insert(Database &database, const Entry &entry)
Insert an entry into an open session database. SQLite prepare or write failures are reported directly...
static std::uint64_t nowUs()
Read the system clock in Unix-epoch microseconds.
static void select(sqlite3 *db, const Query &query, std::size_t count, std::vector< Entry > &out)
Append the newest matching rows from one database, newest first. SQL preparation or stepping failures...
std::filesystem::path _appFolder
Application log session directory.
static bool hasConsoleVisibility(sqlite3 *db, bool &present)
Detects whether a session has the Console visibility column.
std::optional< Entry > prepare(Entry entry)
Identify and stamp an entry without accessing SQLite. UID generation failures and invalid supplied UI...
bool closeProject(const std::string &id)
Forget a project folder and release its session connection. Waits for in-flight operations on this co...
std::string hex(const Uid &uid)
Formats a routing UID as lowercase hexadecimal.
Definition uid.hpp:38
Uid generate()
Generates a random, globally unique identifier.
Definition uid.hpp:24
bool valid(const std::string &uid)
Validates the canonical text form of an assigned UID.
Definition uid.hpp:59
One log line from a process. This store instance assigns arrivedUs as an increasing microsecond times...
Definition log_store.hpp:36
std::string project
Project identifier; empty denotes the application.
Definition log_store.hpp:58
std::uint64_t operation
Command id of a command or receipt; zero for lines.
Definition log_store.hpp:68
std::string text
Log message text.
Definition log_store.hpp:62
std::uint64_t arrivedUs
Store-assigned arrival timestamp in microseconds since the Unix epoch.
Definition log_store.hpp:42
int destination
Destination process of a command or receipt; zero for lines.
Definition log_store.hpp:66
bool consoleVisible
Whether this entry may be delivered to the Console or loading window.
Definition log_store.hpp:72
std::string level
Log severity label.
Definition log_store.hpp:50
int process
Source process identifier.
Definition log_store.hpp:46
int kind
What the row records: 0 line, 1 command, 2 receipt, 3 notification, 4 session start (LogEntryKind).
Definition log_store.hpp:64
std::string refUid
For a receipt, the UID of the command it confirms; empty otherwise.
Definition log_store.hpp:70
std::string uid
Globally unique lowercase hexadecimal entry identity.
Definition log_store.hpp:38
std::uint32_t thread
Source thread identifier.
Definition log_store.hpp:54
Own an SQLite connection and close it on destruction.
std::mutex mutex
Serializes use and explicit closure of this connection.
sqlite3 * db
Owned SQLite handle, or null before opening.
std::filesystem::path path
Path used when this connection was opened.
bool close()
Closes the connection after any in-flight use completes.
~Database()
Close the owned SQLite connection if present.
Associate a log folder with its live session database.
std::filesystem::path path
Directory containing session database files.
std::shared_ptr< Database > session
Shared owner of the current session connection, if available.
A page of matching log entries and its exhaustion status.
std::vector< Entry > entries
Matching entries ordered oldest first.
bool exhausted
True when no additional matching entries were found beyond this page.
Filters and cursor for a page of log entries.
Definition log_store.hpp:79
std::string project
Project identifier to match when filterProject is true; empty selects application entries.
std::uint32_t limit
Maximum results requested; page enforces a minimum of one.
Definition log_store.hpp:87
std::uint64_t beforeUs
Exclusive arrival cursor; zero selects the newest entries.
Definition log_store.hpp:83
std::vector< std::string > levels
Allowed severity labels; empty disables this filter.
Definition log_store.hpp:95
bool filterProject
Whether to filter by the exact project identifier.
Definition log_store.hpp:99
std::string contains
Literal SQLite LIKE substring filter for message text; empty disables it.
bool consoleOnly
Exclude entries intended only for the persisted log.
std::vector< int > processes
Allowed process identifiers; empty disables this filter.
Definition log_store.hpp:91