AsyncFw 1.2
Async Framework is c++ runtime with timers, poll notifiers, sockets, coroutines, etc.
 
Loading...
Searching...
No Matches
Log.h
1/*
2Copyright (c) 2026 Alexandr Kuzmuk
3
4This file is part of the AsyncFw project. Licensed under the MIT License.
5See {Link: LICENSE file https://mit-license.org} in the project root for full license information.
6*/
7
8#pragma once
9
10#include <queue>
11
12#include "../core/LogStream.h"
13#include "Rrd.h"
14#include "Instance.h"
15
16#define _messages_ 8
17
18namespace AsyncFw {
21 friend class Log;
22
23public:
24 using Message = LogStream::Message;
25
26 virtual ~AbstractLog() = 0;
27 void append(uint8_t, const std::string &, const std::string &, const std::string & = {});
28 void setExtendOut(bool b);
29 void setColorOut(bool b);
30 void setNotesOut(bool b);
31 void setHideDuplicates(bool b);
32 void setLevel(int i);
33 void setConsoleLevel(int i);
34 void setFilter(const std::vector<std::string> &f);
35
36protected:
37 virtual void output(const Message &message);
38 virtual void save() = 0;
39 void append(const Message &m);
40 void flush();
41 void finality();
42 uint8_t level = LogStream::Trace;
43 uint8_t consoleLevel = LogStream::Trace;
44 int queueLimit = 128;
45 Thread *thread_;
46
47private:
48 struct LastMessage {
49 Message message;
50 int count = 1;
51 int timerId = -1;
52 bool marked = false;
53 };
54 void timerTask(int);
55 void startTimer(int *, int);
56 void stopTimer(int *);
57 void process(const Message &);
58 uint8_t flags = LOG_STREAM_CONSOLE_EXTEND
59#ifndef _WIN32
60 | LOG_STREAM_CONSOLE_COLOR
61#endif
62 ;
63 std::queue<Message> messages;
64 LastMessage lastMessages[_messages_];
65 bool hideDuplicates = false;
66 std::vector<std::string> filter;
67};
68
71class Log : public Rrd, public AbstractLog {
72public:
73 using Message = LogStream::Message;
74 using MessageType = LogStream::MessageType;
75 using Color = LogStream::Color;
76
77 using AbstractLog::append;
78
79 inline static Log *instance() { return instance_.value; }
80
81 Log(int = 0, const std::string & = {});
82 ~Log() override;
83
84 void finality();
85
86 void output(const Message &m) override;
87
88 void setAutoSave(bool b) { autoSave = (b) ? 100 : -1; }
89 Rrd::Item rrdItemFromMessage(const Message &m) const;
90 Message messageFromRrdItem(const Rrd::Item &_v) const;
91 Message readMessage(uint32_t index) { return messageFromRrdItem(readFromArray(index)); }
92 void writeMessage(uint32_t index, const Message &message) { writeToArray(index, rrdItemFromMessage(message)); }
93
94 void save() override {
95 if (!Rrd::readOnly) Rrd::save();
96 }
97
98protected:
99 using AbstractLog::thread_;
100 int autoSave;
101 int timerIdAutosave = -1;
102
103private:
104 static class Instance : public AsyncFw::Instance<Log> {
105 using AsyncFw::Instance<Log>::Instance;
106 void created() override;
107 } instance_;
108 static void lsAppend(const Message &m, uint8_t t);
110};
111} // namespace AsyncFw
The AbstractLog class provides the base functionality for logger.
Definition Log.h:20
The FunctionConnectionGuard class.
Definition FunctionConnector.h:144
The Instance class.
Definition Instance.h:40
AsyncFw::Thread thread with sockets.
Definition Thread.h:16