xerus
a general purpose tensor library
namedLogger.cpp
Go to the documentation of this file.
1 // Xerus - A General Purpose Tensor Library
2 // Copyright (C) 2014-2017 Benjamin Huber and Sebastian Wolf.
3 //
4 // Xerus is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published
6 // by the Free Software Foundation, either version 3 of the License,
7 // or (at your option) any later version.
8 //
9 // Xerus 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 Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with Xerus. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // For further information on Xerus visit https://libXerus.org
18 // or contact us at contact@libXerus.org.
19 
25 #include <time.h>
26 #include <iomanip>
27 #include <chrono>
28 #include <fstream>
29 
30 #ifdef XERUS_LOG_BUFFER
31  #include <fstream>
32 #endif
33 
34 #include <xerus/misc/namedLogger.h>
36 
37 namespace xerus { namespace misc { namespace internal {
38  std::mutex namedLoggerMutex;
39  std::string logFilePrefix;
40  bool silenced = false;
41  std::chrono::system_clock::time_point programStartTime;
42  static void __attribute__((constructor)) initTime() {
43  programStartTime = std::chrono::system_clock::now();
44  }
45 
46  void log_timestamp(std::ostream &_out) {
47  #ifdef XERUS_LOG_ABSOLUTE_TIME
48  //NOTE must not use std::put_time as it was not defined before GCC 5.0
49  std::time_t xerus_err_t=std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
50  std::tm *xerus_err_ltm=std::localtime(&xerus_err_t);
51  _out << std::right << (1900+xerus_err_ltm->tm_year)
52  << '-' << std::setw(2) << std::setfill('0') << (xerus_err_ltm->tm_mon +1)
53  << '-' << std::setw(2) << std::setfill('0') << xerus_err_ltm->tm_mday
54  << ' ' << std::setw(2) << std::setfill('0') << xerus_err_ltm->tm_hour
55  << ':' << std::setw(2) << std::setfill('0') << xerus_err_ltm->tm_min
56  << ':' << std::setw(2) << std::setfill('0') << xerus_err_ltm->tm_sec << ' ' << std::left;
57  #else
58  std::chrono::system_clock::time_point xerus_err_t = std::chrono::system_clock::now();
59  auto xerus_err_timediff = std::chrono::duration_cast<std::chrono::milliseconds>(xerus_err_t - xerus::misc::internal::programStartTime).count();
60  _out << std::right << '+' << std::setw(2) << std::setfill('0') << (xerus_err_timediff/3600000)
61  << ':' << std::setw(2) << std::setfill('0') << ((xerus_err_timediff/60000)%60)
62  << ':' << std::setw(2) << std::setfill('0') << ((xerus_err_timediff/1000)%60)
63  << ',' << std::setw(3) << std::setfill('0') << (xerus_err_timediff%1000) << ' ' << std::left;
64  #endif
65  }
66 
67  void log_timestamp(std::ostream &_out, const char* _file, int _line, const char* _lvl) {
68  log_timestamp(_out);
69  _out << std::setfill(' ') << std::setw(20) << std::left << xerus::misc::explode(_file, '/').back() << ':' \
70  << std::right << std::setfill(' ') << std::setw(4) <<_line << " : " \
71  << std::setfill(' ') << std::setw(12) << std::left \
72  << std::string(_lvl) << ": ";
73  }
74 
75  void log_timestamp(std::ostream &_out, const char* _lvl) {
76  log_timestamp(_out);
77  _out << std::setfill(' ') << std::setw(12) << std::left \
78  << std::string(_lvl) << ": ";
79  }
80 
81  std::ostream &get_fileStream() {
82  static std::ofstream fileStream;
83  if (!fileStream || !fileStream.is_open()) {
84  fileStream.close();
85  fileStream.open("error.log", std::ofstream::app | std::ofstream::out);
86  }
87  return fileStream;
88  }
89 
90  namespace buffer {
91  std::stringstream current;
92  std::stringstream old;
93 
94  void checkSwitch() {
95  if (current.str().size() > 1024*1024) {
96  #if defined(__GNUC__) && __GNUC__ < 5
97  old.str(current.str());
98  current.str(std::string());
99  current.clear();
100  #else
101  old = std::move(current);
102  current = std::stringstream();
103  #endif
104  }
105  }
106 
107  void dump_log(std::string _comment) {
108  std::string name = std::string("errors/") + logFilePrefix + std::to_string(std::time(nullptr)) + ".txt";
109  std::ofstream out(name, std::ofstream::out);
110  out << "Error: " << _comment << std::endl << std::endl;
111 
112  // Get callstack
113  out << "-------------------------------------------------------------------------------" << std::endl
114  << " Callstack : " << std::endl
115  << "-------------------------------------------------------------------------------" << std::endl
116  << get_call_stack() << std::endl;
117 
118  // Output namedLogger
119  old.flush();
120  current.flush();
121  out << "-------------------------------------------------------------------------------" << std::endl
122  << " last " << (current.str().size() + old.str().size()) << " bytes of log:" << std::endl
123  << "-------------------------------------------------------------------------------" << std::endl
124  << old.str() << current.str() << "horst" << std::endl;
125  out.close();
126  }
127  } // namespace buffer
128 } // namespace internal
129 } // namespace misc
130 } // namespace xerus
std::string logFilePrefix
Definition: namedLogger.cpp:39
void dump_log(std::string _comment)
std::ostream & get_fileStream()
Definition: namedLogger.cpp:81
The main namespace of xerus.
Definition: basic.h:37
std::string get_call_stack()
Returns a string representation of the current call-stack (excluding the function itself)...
Definition: callStack.cpp:166
static void __attribute__((constructor)) initTime()
Definition: namedLogger.cpp:42
Header file for all logging macros and log-buffer functionality.
std::mutex namedLoggerMutex
Definition: namedLogger.cpp:38
Header file for some elementary string manipulation routines.
static XERUS_force_inline std::string to_string(const bool obj)
Definition: stringFromTo.h:41
void log_timestamp(std::ostream &_out, const char *_file, int _line, const char *_lvl)
Definition: namedLogger.cpp:67
std::chrono::system_clock::time_point programStartTime
Definition: namedLogger.cpp:41
std::vector< std::string > explode(const std::string &_string, const char _delim)
: Explodes a string at positions indicated by _delim.