LogStore.cpp
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* ArmarX is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License version 2 as
6* published by the Free Software Foundation.
7*
8* ArmarX is distributed in the hope that it will be useful, but
9* WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*
13* You should have received a copy of the GNU General Public License
14* along with this program. If not, see <http://www.gnu.org/licenses/>.
15*
16* @package ArmarX::
17* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
18* GNU General Public License
19*/
20
21#include "LogStore.h"
22
23#include <IceUtil/Time.h>
24
25namespace armarx
26{
27 namespace
28 {
29 // Format the time column (0) value, guarding the substr against a missing space
30 // (which would otherwise throw).
31 QString
32 formatTime(Ice::Long timeMicroSeconds)
33 {
34 IceUtil::Time time = IceUtil::Time::microSeconds(timeMicroSeconds);
35 std::string timeStr = time.toDateTime();
36 const auto spacePos = timeStr.find(' ');
37 if (spacePos != std::string::npos)
38 {
39 timeStr = timeStr.substr(spacePos);
40 }
41 return QString::fromStdString(timeStr);
42 }
43
44 // Build the message column (4) display string (truncated to a maximum number of
45 // lines) and report how many display lines it spans.
46 QString
47 buildDisplayMessage(const LogMessage& entry, int& lineCount)
48 {
49 QString whatStr = entry.backtrace.empty()
50 ? QString::fromStdString(entry.what)
51 : QString::fromStdString(entry.what + "\nBacktrace:\n...");
52
53 constexpr int maxLinesToShow = 50;
54 int lines = 0;
55 int pos = 0;
56 for (int i = 0; i < whatStr.length(); i++)
57 {
58 if (whatStr.at(i) == '\n')
59 {
60 lines++;
61 }
62 if (lines == maxLinesToShow)
63 {
64 break;
65 }
66 pos++;
67 }
68 if (lines >= maxLinesToShow)
69 {
70 whatStr.truncate(pos);
71 whatStr += "\n...";
72 }
73
74 lineCount = whatStr.count('\n') + 1;
75 return whatStr;
76 }
77 } // namespace
78
79 LogStore::LogStore(std::size_t maxEntries) : maxEntries(maxEntries)
80 {
81 }
82
83 LogSeq
84 LogStore::append(const LogMessage& message)
85 {
86 StoredLogEntry entry;
87 entry.message = message;
88 entry.timeString = formatTime(message.time);
89 entry.displayMessage = buildDisplayMessage(message, entry.lineCount);
90
91 const LogSeq seq = endSeq();
92 entries.push_back(std::move(entry));
93 return seq;
94 }
95
96 bool
98 {
99 if (maxEntries == 0 || entries.size() <= maxEntries)
100 {
101 return false;
102 }
103
104 const std::size_t removeCount = entries.size() - maxEntries;
105 entries.erase(entries.begin(), entries.begin() + removeCount);
106 first += removeCount;
107 return true;
108 }
109
110 LogSeq
112 {
113 return first;
114 }
115
116 LogSeq
118 {
119 return first + entries.size();
120 }
121
122 bool
124 {
125 return seq >= first && seq < endSeq();
126 }
127
128 const StoredLogEntry&
130 {
131 return entries[seq - first];
132 }
133
134 std::size_t
136 {
137 return entries.size();
138 }
139
140 void
142 {
143 // Keep counting sequence numbers up rather than resetting to 0, so that any
144 // reference still held to a discarded entry stays invalid instead of silently
145 // aliasing a future one.
146 first = endSeq();
147 entries.clear();
148 }
149} // namespace armarx
LogSeq append(const LogMessage &message)
Append message, deriving its display data, and return its sequence number.
Definition LogStore.cpp:84
LogSeq firstSeq() const
Sequence number of the oldest retained entry.
Definition LogStore.cpp:111
bool trim()
Drop the oldest entries until the bound is met.
Definition LogStore.cpp:97
LogSeq endSeq() const
One past the sequence number of the newest retained entry.
Definition LogStore.cpp:117
const StoredLogEntry & at(LogSeq seq) const
Only valid if contains(seq).
Definition LogStore.cpp:129
bool contains(LogSeq seq) const
Definition LogStore.cpp:123
void clear()
Drop everything.
Definition LogStore.cpp:141
LogStore(std::size_t maxEntries)
Definition LogStore.cpp:79
std::size_t size() const
Definition LogStore.cpp:135
This file offers overloads of toIce() and fromIce() functions for STL container types.
quint64 LogSeq
Identifies a stored log entry for as long as it is retained.
Definition LogStore.h:37
A retained log line: the message plus the display data derived from it.
Definition LogStore.h:43
QString displayMessage
truncated message column (4)
Definition LogStore.h:46
int lineCount
number of display lines in displayMessage
Definition LogStore.h:47
QString timeString
formatted time column (0)
Definition LogStore.h:45