log.h
1 /* Copyright (C) 2020-2021 IBM Corp.
2  * This program is Licensed under the Apache License, Version 2.0
3  * (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://www.apache.org/licenses/LICENSE-2.0
6  * Unless required by applicable law or agreed to in writing, software
7  * distributed under the License is distributed on an "AS IS" BASIS,
8  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9  * See the License for the specific language governing permissions and
10  * limitations under the License. See accompanying LICENSE file.
11  */
12 
13 #ifndef HELIB_LOG_H
14 #define HELIB_LOG_H
15 
16 #include <iomanip>
17 #include <ctime>
18 #include <ostream>
19 #include <sstream>
20 #include <string>
21 
30 namespace helib {
31 
35 class Logger
36 {
37 private:
38  // A stream pointer to be set for the logs to go to.
39  std::ostream* logStream_p;
40 
41 public:
45  void setLogToStderr();
46 
53  void setLogToFile(const std::string& filepath, bool overwrite = false);
54 
59  Logger() = default;
60 
64  Logger(const Logger& other) = default;
65 
70  Logger(Logger&& other) = default;
71 
75  Logger& operator=(Logger& other) = default;
76 
81  Logger& operator=(Logger&& other) = default;
82 
87  ~Logger();
88 
89  friend inline void Warning(const char* msg);
90 };
91 
95 extern Logger helog;
96 
101 inline const std::string timestamp()
102 {
103  auto t = std::time(nullptr);
104  auto now = *std::localtime(&t);
105  std::ostringstream oss;
106  oss << std::put_time(&now, "[ %T ]");
107  return oss.str();
108 }
109 
114 inline void Warning(const char* msg)
115 {
116  *helog.logStream_p << timestamp() << " WARNING: " << msg << std::endl;
117 }
118 
123 inline void Warning(const std::string& msg) { Warning(msg.c_str()); }
124 
125 // Errors should raise exceptions through throw or assertion functions
126 // in helib/assertions.h
127 
128 // TODO Info Debug
129 // inline void Info(const std::string& msg)
130 // inline void Debug(const std::string& msg)
131 
132 } // namespace helib
133 
134 #endif // ifndef HELIB_LOG_H
Logger class that handles warning printouts.
Definition: log.h:36
void setLogToFile(const std::string &filepath, bool overwrite=false)
Set the logger object to write to specified file.
Definition: log.cpp:50
Logger & operator=(Logger &other)=default
Copy assignment operator, copies a logger object.
Logger(const Logger &other)=default
Copy constructor, creates a copy of a logger object.
Logger(Logger &&other)=default
Move constructor, can be used with std::move but does the same as the copy constructor.
friend void Warning(const char *msg)
Function for logging a warning message.
Definition: log.h:114
Logger()=default
Default constructor creates a logger object that does not point to any target/destination.
~Logger()
Destructor that closes and deletes the log stream object if required i.e. if the log stream is a file...
Definition: log.cpp:36
void setLogToStderr()
Set the logger object to write to stderr.
Definition: log.cpp:42
Logger & operator=(Logger &&other)=default
Move assignment operator, does the same as the copy assignment operator.
Definition: apiAttributes.h:21
Logger helog
Internal global logger.
Definition: log.cpp:30
void Warning(const char *msg)
Function for logging a warning message.
Definition: log.h:114
const std::string timestamp()
Return a time stamp for now.
Definition: log.h:101