-
Notifications
You must be signed in to change notification settings - Fork 46
/
logger.hpp
162 lines (126 loc) · 3.63 KB
/
logger.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//
// http_logger.hpp
// ~~~~~~~~~~~~~~~
//
// Copyright (c) 2011 Jack (jack.wgm@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// * $Id: http_logger.hpp 67 2011-07-19 08:21:07Z jack $
//
#ifndef __LOGGER_H__
#define __LOGGER_H__
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
#ifdef _MSC_VER
#pragma warning(push, 1)
#endif
#include <iostream>
#include <boost/filesystem/fstream.hpp>
#include <boost/filesystem/convenience.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
namespace portmap_logger {
namespace fs = boost::filesystem;
typedef boost::posix_time::ptime ptime;
typedef boost::posix_time::time_duration time_duration;
#ifdef WIN32
inline int snprintf(char* buf, int len, char const* fmt, ...);
#endif // WIN32
char const* time_now_string();
inline ptime time_now_hires();
ptime const& time_now();
inline int total_milliseconds(time_duration td);
std::string log_time();
class logger
{
public:
logger(fs::path const& logpath, fs::path const& filename, int instance, bool append = true)
{
try
{
char log_name[1024];
snprintf(log_name, sizeof(log_name), "logs%d", instance);
fs::path dir(fs::complete(logpath / log_name));
if (!fs::exists(dir)) fs::create_directories(dir);
m_file.open((dir / filename).string().c_str(), std::ios_base::out | (append ? std::ios_base::app : std::ios_base::out));
*this << "\n\n\n*** starting log ***\n\n\n";
}
catch (std::exception& e)
{
std::cerr << "failed to create log '" << filename.string() << "': " << e.what() << std::endl;
}
}
template <class T>
logger& operator<<(T const& v)
{
std::cout << v;
std::cout.flush();
m_file << v;
m_file.flush();
std::ostringstream oss;
oss << v;
m_last_log += oss.str();
return *this;
}
std::string last_log() {
return m_last_log;
}
void clear_last_log() {
m_last_log = "";
}
private:
std::string m_last_log;
std::ofstream m_file;
};
namespace aux {
extern ptime g_current_time;
extern boost::mutex g_logger_mutex;
extern boost::shared_ptr<logger> g_logger;
}
boost::shared_ptr<logger> logger_();
#define logs (*portmap_logger::logger_())
#ifdef LOGGER_THREAD_SAFE
#define locks() boost::mutex::scoped_lock lock(aux::g_logger_mutex)
#else
#define locks() do {} while (0)
#endif // LOGGER_THREAD_SAFE
#if defined(WIN32) && defined(LOGGER_DEBUG_VIEW)
#define debug_view() do { OutputDebugStringA(logs.last_log().c_str()); logs.clear_last_log(); } while (0)
#else
#define debug_view() do { } while (0)
#endif // WIN32 && LOGGER_DEBUG_VIEW
#ifdef LOGGER_OUTPUT_LOG
#define log_debug(message) do { \
locks(); \
logs << time_now_string() << "[DEBUG]: " << message; \
debug_view(); \
} while (0)
#define log_info(message) do { \
locks(); \
logs << time_now_string() << "[INFO]: " << message; \
debug_view(); \
} while (0)
#define log_warning(message) do { \
locks(); \
logs << time_now_string() << "[WARNING]: " << message; \
debug_view(); \
} while (0)
#define log_error(message) do { \
locks(); \
logs << time_now_string() << "[ERROR]: " << message; \
debug_view(); \
} while (0)
#else
#define log_debug(message) do { } while (0)
#define log_info(message) do { } while (0)
#define log_warning(message) do { } while (0)
#define log_error(message) do { } while (0)
#endif // LOGGER_OUTPUT_LOG
} // namespace portmap_logger
using namespace portmap_logger;
#endif // __LOGGER_H__