Skip to content

Commit

Permalink
Merge pull request markparticle#14 from markparticle/dev_mark
Browse files Browse the repository at this point in the history
Dev mark
  • Loading branch information
markparticle authored Jun 29, 2020
2 parents 2eb7bd8 + b3b1326 commit fc15471
Show file tree
Hide file tree
Showing 15 changed files with 116 additions and 109 deletions.
22 changes: 11 additions & 11 deletions code/buffer/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
#include "buffer.h"

Buffer::Buffer() : buffer_(INIT_BUFF_SIZE), readPos_(0), writePos_(0) {}
Buffer::Buffer(int initBuffSize) : buffer_(initBuffSize), readPos_(0), writePos_(0) {}

size_t Buffer::ReadableBytes() const {
return writePos_ - readPos_;
Expand All @@ -22,10 +22,6 @@ const char* Buffer::Peek() const {
return BeginPtr_() + readPos_;
}

char* Buffer::Peek(){
return BeginPtr_() + readPos_;
}

void Buffer::Retrieve(size_t len) {
assert(len <= ReadableBytes());
readPos_ += len;
Expand All @@ -37,6 +33,7 @@ void Buffer::RetrieveUntil(const char* end) {
}

void Buffer::RetrieveAll() {
bzero(&buffer_[0], buffer_.size());
readPos_ = 0;
writePos_ = 0;
}
Expand All @@ -62,16 +59,19 @@ void Buffer::HasWritten(size_t len) {
void Buffer::Append(const std::string& str) {
Append(str.data(), str.length());
}
void Buffer::Append(const char* data, size_t len) {
EnsureWriteable(len);
std::copy(data, data + len, BeginWrite());
HasWritten(len);
}

void Buffer::Append(const void* data, size_t len) {
assert(data);
Append(static_cast<const char*>(data), len);
}

void Buffer::Append(const char* str, size_t len) {
assert(str);
EnsureWriteable(len);
std::copy(str, str + len, BeginWrite());
HasWritten(len);
}

void Buffer::Append(const Buffer& buff) {
Append(buff.Peek(), buff.ReadableBytes());
}
Expand All @@ -84,7 +84,7 @@ void Buffer::EnsureWriteable(size_t len) {
}

ssize_t Buffer::ReadFd(int fd, int* saveErrno) {
char buff[MAX_BUFF_SIZE];
char buff[65535];
struct iovec iov[2];
const size_t writable = WritableBytes();
/* 分散读, 保证数据全部读完 */
Expand Down
10 changes: 2 additions & 8 deletions code/buffer/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@
#include <vector> //readv
#include <atomic>
#include <assert.h>


class Buffer {
public:
Buffer();
Buffer(int initBuffSize = 1024);
~Buffer() = default;

size_t WritableBytes() const;
size_t ReadableBytes() const ;
size_t PrependableBytes() const;

const char* Peek() const;
char* Peek();
void EnsureWriteable(size_t len);
void HasWritten(size_t len);

Expand All @@ -39,17 +36,14 @@ class Buffer {
char* BeginWrite();

void Append(const std::string& str);
void Append(const char* data, size_t len);
void Append(const char* str, size_t len);
void Append(const void* data, size_t len);
void Append(const Buffer& buff);

ssize_t ReadFd(int fd, int* Errno);
ssize_t WriteFd(int fd, int* Errno);

private:
const size_t MAX_BUFF_SIZE = 65535;
const size_t INIT_BUFF_SIZE = 512;

char* BeginPtr_();
const char* BeginPtr_() const;
void MakeSpace_(size_t len);
Expand Down
6 changes: 1 addition & 5 deletions code/http/httpconn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ void HttpConn::Close() {
}
}

bool HttpConn::IsClose() const {
return isClose_;
};

int HttpConn::GetFd() const {
return fd_;
};
Expand Down Expand Up @@ -116,7 +112,7 @@ void HttpConn::process() {
LOG_DEBUG("Parse code: %d, %s", response_.Code(), request_.path().c_str());
response_.MakeResponse(writeBuff_);

iov_[0].iov_base = writeBuff_.Peek();
iov_[0].iov_base = const_cast<char*>(writeBuff_.Peek());
iov_[0].iov_len = writeBuff_.ReadableBytes();
iovCnt_ = 1;

Expand Down
1 change: 0 additions & 1 deletion code/http/httpconn.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class HttpConn {
ssize_t write(int* saveErrno);

void Close();
bool IsClose() const;

int GetFd() const;
int GetPort() const;
Expand Down
9 changes: 5 additions & 4 deletions code/http/httprequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ bool HttpRequest::IsKeepAlive() const {
}

bool HttpRequest::parse(Buffer& buff) {
const char* CRLF = "\r\n";
const char CRLF[] = "\r\n";
if(buff.ReadableBytes() <= 0) {
return false;
}
while(buff.ReadableBytes() && state_ != FINISH) {
char* lineEnd = search(buff.Peek(), buff.BeginWrite(), CRLF, CRLF + 2);
const char* lineEnd = search(buff.Peek(), buff.BeginWriteConst(), CRLF, CRLF + 2);
std::string line(buff.Peek(), lineEnd);
switch(state_)
{
Expand Down Expand Up @@ -192,7 +192,7 @@ bool HttpRequest::UserVerify(const string &name, const string &pwd, bool isLogin

if(!isLogin) { flag = true; }
/* 查询用户及密码 */
snprintf(order, 128, "SELECT username, password FROM user WHERE username='%s' LIMIT 1", name.c_str());
snprintf(order, 256, "SELECT username, password FROM user WHERE username='%s' LIMIT 1", name.c_str());
LOG_DEBUG("%s", order);

if(mysql_query(sql, order)) {
Expand Down Expand Up @@ -224,7 +224,8 @@ bool HttpRequest::UserVerify(const string &name, const string &pwd, bool isLogin
/* 注册行为 且 用户名未被使用*/
if(!isLogin && flag == true) {
LOG_DEBUG("regirster!");
snprintf(order, 128,"INSERT INTO user(username, password) VALUES('%s','%s')", name.c_str(), pwd.c_str());
bzero(order, 256);
snprintf(order, 256,"INSERT INTO user(username, password) VALUES('%s','%s')", name.c_str(), pwd.c_str());
LOG_DEBUG( "%s", order);
if(mysql_query(sql, order)) {
LOG_DEBUG( "Insert error!");
Expand Down
10 changes: 5 additions & 5 deletions code/http/httpresponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ void HttpResponse::MakeResponse(Buffer& buff) {
else if(!(mmFileStat_.st_mode & S_IROTH)) {
code_ = 403;
}
else if(code_ <= 200) {
code_ = 200;
else if(code_ == -1) {
code_ = 200;
}
ErrorHtml_();
AddStateLine_(buff);
Expand All @@ -87,7 +87,7 @@ char* HttpResponse::File() {
return mmFile_;
}

int HttpResponse::FileLen() const {
size_t HttpResponse::FileLen() const {
return mmFileStat_.st_size;
}

Expand All @@ -114,7 +114,7 @@ void HttpResponse::AddHeader_(Buffer& buff) {
buff.Append("Connection: ");
if(isKeepAlive_) {
buff.Append("Keep-Alive\r\n");
buff.Append("Keep-Alive: timeout=3000\r\n");
buff.Append("Keep-Alive: timeout=10000\r\n");
} else{
buff.Append("close\r\n");
}
Expand Down Expand Up @@ -174,7 +174,7 @@ void HttpResponse::ErrorContent(Buffer& buff, string message)
}
body += to_string(code_) + " : " + status + "\n";
body += "<p>" + message + "</p>";
body += "<hr><em>TinyWebServer server</em></body></html>";
body += "<hr><em>TinyWebServer</em></body></html>";

buff.Append("Content-length: " + to_string(body.size()) + "\r\n\r\n");
buff.Append(body);
Expand Down
2 changes: 1 addition & 1 deletion code/http/httpresponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class HttpResponse {
void MakeResponse(Buffer& buff);
void UnmapFile();
char* File();
int FileLen() const;
size_t FileLen() const;
void ErrorContent(Buffer& buff, std::string message);
int Code() const { return code_; }

Expand Down
110 changes: 58 additions & 52 deletions code/log/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Log::Log() {
isAsync_ = false;
writeThread_ = nullptr;
deque_ = nullptr;

toDay_ = 0;
fp_ = nullptr;
}
Expand All @@ -31,7 +30,8 @@ Log::~Log() {
}
}

int Log::GetLevel() const {
int Log::GetLevel() {
lock_guard<mutex> locker(mtx_);
return level_;
}

Expand All @@ -56,7 +56,7 @@ void Log::init(int level = 1, const char* path, const char* suffix,
} else {
isAsync_ = false;
}
buff_.RetrieveAll();

lineCount_ = 0;

time_t timer = time(nullptr);
Expand All @@ -68,83 +68,89 @@ void Log::init(int level = 1, const char* path, const char* suffix,
snprintf(fileName, LOG_NAME_LEN - 1, "%s/%04d_%02d_%02d%s",
path_, t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, suffix_);
toDay_ = t.tm_mday;

if(fp_) { fclose(fp_); }
fp_ = fopen(fileName, "a");
if(fp_ == nullptr) {
mkdir(path_, 0777);

{
lock_guard<mutex> locker(mtx_);
buff_.RetrieveAll();
if(fp_) {
fflush(fp_);
fclose(fp_);
}

fp_ = fopen(fileName, "a");
}
assert(fp_ != nullptr);
if(fp_ == nullptr) {
mkdir(path_, 0777);
fp_ = fopen(fileName, "a");
}
assert(fp_ != nullptr);
}

}

void Log::write(int level, const char *format, ...) {
if(isAsync_) { deque_->flush(); }

struct timeval now = {0, 0};
gettimeofday(&now, nullptr);
time_t tSec = now.tv_sec;
struct tm *sysTime = localtime(&tSec);
struct tm t = *sysTime;

va_list vaList;

/* 日志日期 日志行数 */
if (toDay_ != t.tm_mday || (lineCount_ && (lineCount_ % MAX_LINES == 0)))
{
unique_lock<mutex> locker(mtx_);
locker.unlock();
/* 日志日期 日志行数 */
if (toDay_ != t.tm_mday || (lineCount_ && (lineCount_ % MAX_LINES == 0)))
char newFile[LOG_NAME_LEN];
char tail[36] = {0};
snprintf(tail, 36, "%04d_%02d_%02d", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday);

if (toDay_ != t.tm_mday)
{
char newFile[LOG_NAME_LEN];
char tail[16] = {0};
snprintf(tail, 20, "%04d_%02d_%02d", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday);

if (toDay_ != t.tm_mday)
{
snprintf(newFile, LOG_NAME_LEN - 1, "%s/%s%s", path_, tail, suffix_);
toDay_ = t.tm_mday;
lineCount_ = 0;
}
else {
snprintf(newFile, LOG_NAME_LEN - 1, "%s/%s-%d%s",
path_, tail, (lineCount_ / MAX_LINES), suffix_);
}

locker.lock();
fflush(fp_);
fclose(fp_);
fp_ = fopen(newFile, "a");
assert(fp_ != nullptr);
snprintf(newFile, LOG_NAME_LEN - 72, "%s/%s%s", path_, tail, suffix_);
toDay_ = t.tm_mday;
lineCount_ = 0;
}
lineCount_++;
else {
snprintf(newFile, LOG_NAME_LEN - 72, "%s/%s-%d%s", path_, tail, (lineCount_ / MAX_LINES), suffix_);
}

locker.lock();
fflush(fp_);
fclose(fp_);
fp_ = fopen(newFile, "a");
assert(fp_ != nullptr);
}

va_list vaList;
va_start(vaList, format);
{
lock_guard<mutex> locker(mtx_);
unique_lock<mutex> locker(mtx_);
lineCount_++;
int n = snprintf(buff_.BeginWrite(), 128, "%d-%02d-%02d %02d:%02d:%02d.%06ld ",
t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
t.tm_hour, t.tm_min, t.tm_sec, now.tv_usec);
t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
t.tm_hour, t.tm_min, t.tm_sec, now.tv_usec);

buff_.HasWritten(n);
AppendLogLevel_();

AppendLogLevelTitle_(level);

va_start(vaList, format);
int m = vsnprintf(buff_.BeginWrite(), buff_.WritableBytes(), format, vaList);
va_end(vaList);

buff_.HasWritten(m);
buff_.Append("\n\0", 2);
}

if(isAsync_ || (deque_ && deque_->full())) {
deque_->push_back(buff_.RetrieveAllToStr());
}
else {
lock_guard<mutex> locker(mtx_);
fputs(buff_.Peek(), fp_);
if(isAsync_ && deque_ && !deque_->full()) {
deque_->push_back(buff_.RetrieveAllToStr());
} else {
fputs(buff_.Peek(), fp_);
}
buff_.RetrieveAll();

}
va_end(vaList);
}

void Log::AppendLogLevel_() {
switch(level_) {
void Log::AppendLogLevelTitle_(int level) {
switch(level) {
case 0:
buff_.Append("[debug]: ", 9);
break;
Expand Down
Loading

0 comments on commit fc15471

Please sign in to comment.