My Project
print_handler.cpp
1 /*
2  * uuid-log - Microcontroller logging framework
3  * Copyright 2022 Simon Arlott
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <uuid/log.h>
20 
21 #include <Arduino.h>
22 
23 #include <memory>
24 #if UUID_LOG_THREAD_SAFE
25 # include <mutex>
26 #endif
27 
28 namespace uuid {
29 
30 namespace log {
31 
32 PrintHandler::PrintHandler(::Print &print) : print_(print) {
33 }
34 
36 #if UUID_LOG_THREAD_SAFE
37  std::lock_guard<std::mutex> lock{mutex_};
38 #endif
39 
40  return maximum_log_messages_;
41 }
42 
44 #if UUID_LOG_THREAD_SAFE
45  std::lock_guard<std::mutex> lock{mutex_};
46 #endif
47 
48  maximum_log_messages_ = std::max((size_t)1, count);
49 
50  while (log_messages_.size() > maximum_log_messages_) {
51  log_messages_.pop_front();
52  }
53 }
54 
55 void PrintHandler::loop(size_t count) {
56 #if UUID_LOG_THREAD_SAFE
57  std::unique_lock<std::mutex> lock{mutex_};
58 #endif
59 
60  count = std::max((size_t)1, count);
61 
62  while (!log_messages_.empty()) {
63  auto message = log_messages_.front();
64 
65  log_messages_.pop_front();
66 #if UUID_LOG_THREAD_SAFE
67  lock.unlock();
68 #endif
69 
70  print_.print(uuid::log::format_timestamp_ms(message->uptime_ms, 3).c_str());
71  print_.print(' ');
72  print_.print(uuid::log::format_level_char(message->level));
73  print_.print(F(" ["));
74  print_.print(message->name);
75  print_.print(F("] "));
76  print_.println(message->text.c_str());
77 
78  count--;
79  if (count == 0) {
80  break;
81  }
82 
83  ::yield();
84 
85 #if UUID_LOG_THREAD_SAFE
86  lock.lock();
87 #endif
88  }
89 }
90 
91 void PrintHandler::operator<<(std::shared_ptr<Message> message) {
92 #if UUID_LOG_THREAD_SAFE
93  std::lock_guard<std::mutex> lock{mutex_};
94 #endif
95 
96  if (log_messages_.size() >= maximum_log_messages_) {
97  log_messages_.pop_front();
98  }
99 
100  log_messages_.emplace_back(std::move(message));
101 }
102 
103 } // namespace log
104 
105 } // namespace uuid
uuid::log::PrintHandler::PrintHandler
PrintHandler(Print &print)
Create a new Print log handler.
Definition: print_handler.cpp:32
uuid::log::PrintHandler::loop
void loop(size_t count=SIZE_MAX)
Dispatch queued log messages.
Definition: print_handler.cpp:55
uuid::log::PrintHandler::maximum_log_messages_
size_t maximum_log_messages_
Definition: log.h:847
uuid::log::PrintHandler::print_
Print & print_
Definition: log.h:843
uuid::log::PrintHandler::maximum_log_messages
size_t maximum_log_messages() const
Get the maximum number of queued log messages.
Definition: print_handler.cpp:35
uuid
Common utilities.
Definition: get_uptime_ms.cpp:28
uuid::log::format_timestamp_ms
std::string format_timestamp_ms(uint64_t timestamp_ms, unsigned int days_width)
Format a system uptime timestamp as a string.
Definition: format_timestamp_ms.cpp:31
uuid::log::format_level_char
char format_level_char(Level level)
Format a log level as a single character.
Definition: format_level_char.cpp:25
uuid::log::PrintHandler::log_messages_
std::list< std::shared_ptr< Message > > log_messages_
Definition: log.h:848
uuid::log::PrintHandler::mutex_
std::mutex mutex_
Definition: log.h:845
uuid::log::PrintHandler::operator<<
void operator<<(std::shared_ptr< Message > message) override
Add a new log message.
Definition: print_handler.cpp:91