1 /*
2  * Copyright (C) 2018 Rafael Ostertag
3  *
4  * This file is part of YAPET.
5  *
6  * YAPET is free software: you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation, either version 3 of the License, or (at your option) any later
9  * version.
10  *
11  * YAPET is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * YAPET.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  * Additional permission under GNU GPL version 3 section 7
20  *
21  * If you modify this program, or any covered work, by linking or combining it
22  * with the OpenSSL project's OpenSSL library (or a modified version of that
23  * library), containing parts covered by the terms of the OpenSSL or SSLeay
24  * licenses, Rafael Ostertag grants you additional permission to convey the
25  * resulting work.  Corresponding Source for a non-source form of such a
26  * combination shall include the source code for the parts of OpenSSL used as
27  * well as that of the covered work.
28  */
29 
30 #ifndef _LOGGER_H
31 #define _LOGGER_H
32 
33 #ifdef DEBUG_LOG
34 #include <chrono>
35 #include <ctime>
36 #include <fstream>
37 #include <iomanip>
38 #include <string>
39 
40 #include "securearray.hh"
41 
42 namespace yapet {
43 class Logger {
44    public:
Logger(const std::string & filename="/tmp/yapet_debug.log")45     Logger(const std::string& filename = "/tmp/yapet_debug.log")
46         : _file{filename, _file.out | _file.app} {}
47 
48     Logger(const Logger&) = delete;
49     Logger(Logger&&) = delete;
50 
log(const std::string & message)51     void log(const std::string& message) {
52         auto t{std::chrono::system_clock::to_time_t(
53             std::chrono::system_clock::now())};
54 
55         _file << "[" << std::put_time(std::localtime(&t), "%FT%T") << "] "
56               << message << std::endl;
57     }
58 
59    private:
60     std::fstream _file;
61 };
62 
secureArrayToString(const SecureArray & secureArray)63 inline std::string secureArrayToString(const SecureArray& secureArray) {
64     std::string str;
65     for (SecureArray::size_type i = 0; i < secureArray.size(); i++) {
66         str += static_cast<const char>(secureArray[i]);
67     }
68 
69     return str;
70 }
71 
72 extern Logger logger;
73 }  // namespace yapet
74 #define LOG_MESSAGE(x) (yapet::logger.log(x))
75 #else
76 #define LOG_MESSAGE(x)
77 #endif
78 
79 #endif