mavtables  0.2.1
MAVLink router and firewall.
Logger.cpp
Go to the documentation of this file.
1 // MAVLink router and firewall.
2 // Copyright (C) 2018 Michael R. Shannon <mrshannon.aerospace@gmail.com>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 
18 #include <ctime>
19 #include <iomanip>
20 #include <iostream>
21 #include <mutex>
22 #include <string>
23 
24 #include "Logger.hpp"
25 
26 
27 /** Log a message with timestamp (at level 1).
28  *
29  * This will log a message with the current date and time as the timestamp if
30  * the loglevel is set to at least 1.
31  *
32  * \note This is the only method (along with \ref log(unsigned int,
33  * std::string)) that is threadsafe.
34  *
35  * \param message The message to log.
36  */
37 void Logger::log(std::string message)
38 {
39  Logger::log(1, std::move(message));
40 }
41 
42 
43 /** Log a message with timestamp at the given level.
44  *
45  * This will log a message with the current date and time as the timestamp if
46  * the loglevel is at least \p level.
47  *
48  * \note This is the only method (along with \ref log(std::string)) that is
49  * threadsafe.
50  *
51  * \param level The level to log the \p message at. If the logger's level is
52  * lower than this, the message will be discarded. The valid range is 1 to
53  * 65535, a value of 0 will be corrected to 1.
54  * \param message The message to log.
55  * \remarks
56  * Threadsafe (locking).
57  */
58 void Logger::log(unsigned int level, std::string message)
59 {
60  if (level < 1)
61  {
62  level = 1;
63  }
64 
65  if (level_ >= level)
66  {
67  auto t = std::time(nullptr);
68  auto tm = *std::localtime(&t);
69  std::lock_guard<std::mutex> lock(mutex_);
70  std::cout << std::put_time(&tm, "%Y-%m-%d %H:%M:%S") << " "
71  << message << std::endl;
72  }
73 }
74 
75 
76 /** Set the logging level.
77  *
78  * A higher level indicates a higher verbosity of logging. A level of 0 will
79  * completely disable logging.
80  *
81  * \param level The new logging level, valid values are 0 to 65535.
82  */
83 void Logger::level(unsigned int level)
84 {
85  level_ = level;
86 }
87 
88 
89 /** Get the logging level.
90  *
91  * \note It is recommended to check the level before constructing a log message
92  * if the message is expensive to construct.
93  *
94  * \returns The current logging level.
95  */
96 unsigned int Logger::level()
97 {
98  return level_;
99 }
100 
101 
102 unsigned int Logger::level_ = 0;
103 
104 
105 #ifdef __clang__
106  #pragma clang diagnostic push
107  #pragma clang diagnostic ignored "-Wglobal-constructors"
108  #pragma clang diagnostic ignored "-Wexit-time-destructors"
109 #endif
110 
111 std::mutex Logger::mutex_;
112 
113 #ifdef __clang__
114  #pragma clang diagnostic pop
115 #endif
static void log(std::string message)
Definition: Logger.cpp:37
static unsigned int level()
Definition: Logger.cpp:96