00001
00002
00003 #ifndef AITOOLS_UTIL_LOGGING_HPP
00004 #define AITOOLS_UTIL_LOGGING_HPP
00005
00006 #include <ctime>
00007 #include <string>
00008 #include <cassert>
00009 #include <iostream>
00010 #include <iterator>
00011 #include <cstdarg>
00012
00016 namespace aitools {
00017 namespace util {
00018
00019 inline std::ostream&
00020 print_timestamp_to(std::ostream& os)
00021 {
00022 if (os)
00023 {
00024 const time_t rawtime(std::time(NULL));
00025 struct tm* now(localtime(&rawtime));
00026 char timestr[64];
00027 strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", now);
00028 os << timestr;
00029 }
00030 return os;
00031 }
00032
00033 inline void
00034 log_debug(const char* file, size_t line, const std::string& msg)
00035 {
00036 assert(print_timestamp_to(std::clog)
00037 << " DEBUG " << file << ':' << line
00038 << ' ' << msg << std::endl);
00039 }
00040
00041 template <typename T>
00042 inline void
00043 log_debug(const char* file, size_t line, const std::string& msg, const T& val)
00044 {
00045 assert(print_timestamp_to(std::clog)
00046 << " DEBUG " << file << ':' << line
00047 << ' ' << msg << " : " << val << std::endl);
00048 }
00049
00050 template <typename Iter>
00051 inline void
00052 log_debug(const char* file, size_t line,
00053 const std::string& msg, Iter first, Iter last)
00054 {
00055 assert(print_timestamp_to(std::clog)
00056 << " DEBUG " << file << ':' << line
00057 << ' ' << msg << '\n');
00058 assert((std::copy(first, last, std::ostream_iterator<
00059 typename Iter::value_type>(std::clog, "\n")), true));
00060 }
00061
00062 inline void
00063 vlog_debug(const std::string& format, va_list args)
00064 {
00065 assert(print_timestamp_to(std::cerr));
00066 assert(std::cerr << " DEBUG ");
00067 assert(std::vfprintf(stderr, format.c_str(), args));
00068 assert(std::fprintf(stderr, "\n"));
00069 }
00070
00071 inline void
00072 logf_debug(const std::string& format, ...)
00073 {
00074 va_list args;
00075 va_start(args, format);
00076 vlog_debug(format, args);
00077 va_end(args);
00078 }
00079
00080 inline void
00081 log_error(const std::string& message)
00082 {
00083 print_timestamp_to(std::cerr);
00084 std::cerr << " ERROR " << message << std::endl;
00085 }
00086
00087 template <typename T>
00088 inline void
00089 log_error(const std::string& message, const T& value)
00090 {
00091 print_timestamp_to(std::cerr);
00092 std::cerr << " ERROR " << message << " : " << value << std::endl;
00093 }
00094
00095 inline void
00096 log_info(const std::string& message)
00097 {
00098 print_timestamp_to(std::cout);
00099 std::cout << " INFO " << message << std::endl;
00100 }
00101
00102 template <typename T>
00103 inline void
00104 log_info(const std::string& message, const T& value)
00105 {
00106 print_timestamp_to(std::cout);
00107 std::cout << " INFO " << message << " : " << value << std::endl;
00108 }
00109
00110 }
00111
00112 using util::print_timestamp_to;
00113 using util::log_debug;
00114 using util::vlog_debug;
00115 using util::logf_debug;
00116 using util::log_error;
00117 using util::log_info;
00118
00119 }
00120
00121 #endif // AITOOLS_UTIL_LOGGING_HPP