-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogtest.cc
More file actions
373 lines (357 loc) · 9.81 KB
/
logtest.cc
File metadata and controls
373 lines (357 loc) · 9.81 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include "logging.h"
#include "logstream.h"
#include "timeformatter.h"
#include "logging.h"
#include "fileutility.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <thread>
#include <typeinfo>
#include <chrono>
#include <vector>
#include <time.h>
#include <sstream>
//#include <boost/smart_ptr/shared_ptr.hpp>
//#include <boost/smart_ptr/make_shared_object.hpp>
//#include <boost/phoenix/bind.hpp>
//#include <boost/date_time/posix_time/posix_time.hpp>
//#include <boost/log/core.hpp>
//#include <boost/log/trivial.hpp>
//
//#include <boost/log/expressions.hpp>
//#include <boost/log/attributes.hpp>
//#include <boost/log/sources/basic_logger.hpp>
//#include <boost/log/sources/severity_logger.hpp>
//#include <boost/log/sources/severity_channel_logger.hpp>
//#include <boost/log/support/date_time.hpp>
//#include <boost/log/expressions/formatters/date_time.hpp>
//#include <boost/log/sources/record_ostream.hpp>
//#include <boost/log/sinks/sync_frontend.hpp>
//#include <boost/log/sinks/text_ostream_backend.hpp>
//#include <boost/log/attributes/scoped_attribute.hpp>
//#include <boost/log/utility/value_ref.hpp>
//#include <boost/log/utility/setup/common_attributes.hpp>
//namespace logging = boost::log;
//namespace src = boost::log::sources;
//namespace expr = boost::log::expressions;
//namespace sinks = boost::log::sinks;
//namespace attrs = boost::log::attributes;
//namespace keywords = boost::log::keywords;
// We define our own severity levels
//enum severity_level
//{
// NORMAL,
// NOTIFICATION,
// WARNING,
// ERROR,
// CRITICAL
//};
//
//// The operator puts a human-friendly representation of the severity level to the stream
//std::ostream& operator<< (std::ostream& strm, severity_level level)
//{
// static const char* strings[] =
// {
// "NORMAL",
// "NOTIFICATION",
// "WARNING",
// "ERROR",
// "CRITICAL"
// };
//
// if (static_cast<std::size_t>(level) < sizeof(strings) / sizeof(*strings))
// strm << strings[level];
// else
// strm << static_cast<int>(level);
//
// return strm;
//}
//
////[ example_tutorial_filtering
//BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)
//BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)
//BOOST_LOG_ATTRIBUTE_KEYWORD(tag_attr, "Tag", std::string)
//
//void init()
//{
// // Setup the common formatter for all sinks
// /*logging::formatter fmt = expr::stream
// << std::setw(6) << std::setfill('0') << line_id << std::setfill(' ')
// << ": <" << severity << ">\t"
// << expr::if_(expr::has_attr(tag_attr))
// [
// expr::stream << "[" << tag_attr << "] "
// ]
//
// << expr::smessage << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S");*/
//
// logging::formatter fmt2 = expr::stream
// << std::setw(6) << std::setfill('0') << line_id << std::setfill(' ')
// << ": " << std::setiosflags(std::ios::left) << std::setw(14) << severity << "\t" << std::resetiosflags(std::ios::left)
// << expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%m-%d %H:%M:%S.%f") <<
// expr::attr<attrs::current_process_id::value_type>("ProcessID");
//
// // Initialize sinks
// typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
// boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
//
// sink->locked_backend()->add_stream(
// boost::make_shared< std::ofstream >("full.log"));
//
// sink->set_formatter(fmt2);
//
// logging::core::get()->add_sink(sink);
//
// sink = boost::make_shared< text_sink >();
//
// sink->locked_backend()->add_stream(
// boost::make_shared< std::ofstream >("important.log"));
//
// sink->set_formatter(fmt2);
//
// sink->set_filter(severity >= WARNING || (expr::has_attr(tag_attr) && tag_attr == "IMPORTANT_MESSAGE"));
//
// logging::core::get()->add_sink(sink);
// // Add attributes
// logging::add_common_attributes();
//}
////]
//
//#if 0
//
////[ example_tutorial_filtering_bind
//bool my_filter(logging::value_ref< severity_level, tag::severity > const& level,
// logging::value_ref< std::string, tag::tag_attr > const& tag)
//{
// return level >= warning || tag == "IMPORTANT_MESSAGE";
//}
//
//void init()
//{
// //<-
//
// // Setup the common formatter for all sinks
// logging::formatter fmt = expr::stream
// << std::setw(6) << std::setfill('0') << line_id << std::setfill(' ')
// << ": <" << severity << ">\t"
// << expr::if_(expr::has_attr(tag_attr))
// [
// expr::stream << "[" << tag_attr << "] "
// ]
// << expr::smessage;
//
// // Initialize sinks
// typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
// boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
//
// sink->locked_backend()->add_stream(
// boost::make_shared< std::ofstream >("full.log"));
//
// sink->set_formatter(fmt);
//
// logging::core::get()->add_sink(sink);
//
// sink = boost::make_shared< text_sink >();
//
// sink->locked_backend()->add_stream(
// boost::make_shared< std::ofstream >("important.log"));
//
// sink->set_formatter(fmt);
//
// //->
// // ...
//
// namespace phoenix = boost::phoenix;
// sink->set_filter(phoenix::bind(&my_filter, severity.or_none(), tag_attr.or_none()));
//
// // ...
// //<-
//
// logging::core::get()->add_sink(sink);
//
// // Add attributes
// logging::add_common_attributes();
//
// //->
//}
////]
//
//#endif
//void time_logging();
//void logging_function()
//{
// src::severity_logger< severity_level > slg;
// BOOST_LOG_SEV(slg, NORMAL) << "starting to time nested function";
// time_logging();
// BOOST_LOG_SEV(slg, NORMAL) << "stopping to time nested function";
//
//
//}
//
//void time_logging()
//{
// src::severity_logger<severity_level>slg;
// BOOST_LOG_SEV(slg, NORMAL) << "A regular message";
// BOOST_LOG_SEV(slg, WARNING) << "Something bad is going on but I can handle it";
// BOOST_LOG_SEV(slg, CRITICAL) << "Everything crumbles, shoot me now!";
//
// {
// BOOST_LOG_SCOPED_THREAD_TAG("Tag", "IMPORTANT_MESSAGE");
// BOOST_LOG_SEV(slg, NORMAL) << "An important message";
// }
//}
struct Time_val
{
time_t seconds;
int64_t microseconds;
};
const char* monthName[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
static Logger_nsp::details::LogStream* stream = Logger_nsp::Logger::GetStream();
void LogErrorTest()
{
for (auto it : monthName)
{
LOG_ERROR << "month Name " << it << "\n";
}
}
void LogTraceTest()
{
int i = 19;
LOG_TRACE << "This is a LogTraceTest " <<
"Here variable i.value= " << i << " address is: " << &i << "\n";
}
void LogDebugTest()
{
double debugvariable = 8.99999;
LOG_DEBUG << "This is a LogDebug Test "
<< "double variable's value is: " << debugvariable << "\n";
}
void LogWarningTest()
{
std::vector<int>v;
for (int i = 0; i < 20; i++)
{
v.push_back(i + 19);
LOG_INFO << "v.member: " << (i + 19) << "\n";
}
LOG_INFO << "\n";
int* p = new int(20);
LOG_WARNING << "Here I use new operator to generator a integer " <<
"value is: " << *p << " You should caution memory leaks\n";
}
void LogFatalTest()
{
std::vector<std::string>str;
for (auto it : monthName)
{
str.push_back(it);
}
for (auto t : str)
{
LOG_FATAL << "Month Name " << t << " address " << &t << "\n";
}
}
int Release()
{
delete ::stream;
return 0;
}
int main(int, char*[])
{
/*std::ofstream filestr("test");
const std::string fname = "[Level] [Time] [Thid] [Line] [File] [Function] [Text]";
filestr << fname;
filestr.close();
printf("%-9s %-20s %-7s %-6s %-22s %-16s %s\n", "[Level]", "[Time]", "[ThreadId]", "[Line]", "[File]", "[Function]","[Text]");
std::atomic<short>val = 19;
char buf[10] = { 0 };
Logger_nsp::details::Convert(buf, val.operator short());
auto id = std::this_thread::get_id();
std::stringstream stream;
stream << id;
std::string strid = stream.str();
const char* levelName = "TRACE";
std::string timeStr = Logger_nsp::details::TimeFormatter().GetTimeStr();
unsigned int lineNumber = __LINE__;
const char* fileName = __FILE__;
const char* func = __FUNCTION__;
char temp[120] = { 0 };
size_t len = sprintf_s(temp, sizeof(temp), "[%7s] %s [%-5s] [Line]: %04d [File]: %-20s [Func]: %-14s ",
levelName, timeStr.c_str(),strid.c_str(), lineNumber, fileName, func);
Logger_nsp::details::TimeFormatter fmt;
double db = 3.658965;
stream.clear();
stream.str("");
stream << "this";
auto s = stream.str();*/
LOG_INFO << "Log 测试开始 \n";
for (int i = 0; i <500; i++)
{
LogErrorTest();
double db = 6.589;
LOG_DEBUG << "db is: " << db << "\n";
float fx = 10.6f;
//LOG__ERROR << "this";
//LOG_ERROR << "Caution float can safe convert to double, double can not convert to float safely\n";
LogTraceTest();
LogDebugTest();
LogWarningTest();
LogFatalTest();
printf("Log 次数: %d\n", i);
//Sleep(1000);
}
//Sleep(3000);
for (int j = 0; j < 100; j++)
{
LogFatalTest();
}
LOG_INFO << "Log 测试结束\n";
//LOG_TRACE << "Log trace Test!\n";
//LOG_FATAL << "start logging\n";
//int x = 10;
//LOG_FATAL << "x.address is " << &x << "\n";
//for (int i = 0; i < 100; i++)
//{
// LOG_FATAL << "Temporary variable: " << i << "\n";
//}
//LOG_FATAL << "Finished logging\n";
/*LOG_INFO << "this is just a test\n";
int x = 10;
LOG_INFO << "x.address is " << &x << "\n";
LOG_INFO << "finished logging";*/
/*printf("%s\n", __FUNCTION__);
Logger_nsp::FileUtility::FileUtil file1("newfile");
const char* sen = "this is just a test\n";
for (int i = 0; i < 200; i++)
{
file1.Append(sen, strlen(sen));
}
file1.Flush();*/
/*int z = 10;
LOG_INFO << "this is just a address test " << &z;
std::ostringstream str;
double x = 1.6;
char temp[20] = { 0 };
size_t len = sprintf_s(temp, sizeof(temp), "%.12f", x);
str << temp;
std::string s = str.str();
char* pPath = ::getenv("Path");
const char* p = "1";
PRINT(p);*/
//printf("%s\n", pPath);
//printf("%03s\n", p);
//TimeFormatter fmt;
//for (int i = 0; i < 400; i++)
//{
// fmt.init();
// Sleep(1000);
//}
//return 0;
onexit(Release);
/*int z;
std::cin >> z;*/
}