forked from linyacool/WebServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogging.cpp
More file actions
executable file
·59 lines (50 loc) · 1.31 KB
/
Copy pathLogging.cpp
File metadata and controls
executable file
·59 lines (50 loc) · 1.31 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
// @Author Lin Ya
// @Email [email protected]
#include "Logging.h"
#include "CurrentThread.h"
#include "Thread.h"
#include "AsyncLogging.h"
#include <assert.h>
#include <iostream>
#include <time.h>
#include <sys/time.h>
static pthread_once_t once_control_ = PTHREAD_ONCE_INIT;
static AsyncLogging *AsyncLogger_;
std::string Logger::logFileName_ = "./WebServer.log";
void once_init()
{
AsyncLogger_ = new AsyncLogging(Logger::getLogFileName());
AsyncLogger_->start();
}
void output(const char* msg, int len)
{
pthread_once(&once_control_, once_init);
AsyncLogger_->append(msg, len);
}
Logger::Impl::Impl(const char *fileName, int line)
: stream_(),
line_(line),
basename_(fileName)
{
formatTime();
}
void Logger::Impl::formatTime()
{
struct timeval tv;
time_t time;
char str_t[26] = {0};
gettimeofday (&tv, NULL);
time = tv.tv_sec;
struct tm* p_time = localtime(&time);
strftime(str_t, 26, "%Y-%m-%d %H:%M:%S\n", p_time);
stream_ << str_t;
}
Logger::Logger(const char *fileName, int line)
: impl_(fileName, line)
{ }
Logger::~Logger()
{
impl_.stream_ << " -- " << impl_.basename_ << ':' << impl_.line_ << '\n';
const LogStream::Buffer& buf(stream().buffer());
output(buf.data(), buf.length());
}