Skip to content

Commit 6f6755e

Browse files
Add files via upload
1 parent 19be58d commit 6f6755e

File tree

6 files changed

+580
-0
lines changed

6 files changed

+580
-0
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
3+
all:
4+
#g++ -o thread_workers_co -g -std=c++11 -DUSING_GLOG test.cpp thread_workers_co.cpp -lpthread -levent -lglog -I./libco/include -L./libco/lib -lcolib -ldl # with glog
5+
g++ -o thread_workers_co -g -std=c++11 test.cpp thread_workers_co.cpp -lpthread -levent -I./libco/include -L./libco/lib -lcolib -ldl

logging.h

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
2+
#pragma once
3+
4+
#ifdef BOOST_LOG_DYN_LINK
5+
6+
#include <boost/date_time/posix_time/posix_time_types.hpp>
7+
#include <boost/log/trivial.hpp>
8+
#include <boost/log/expressions.hpp>
9+
#include <boost/log/sources/severity_logger.hpp>
10+
#include <boost/log/sources/record_ostream.hpp>
11+
#include <boost/log/utility/setup/file.hpp>
12+
#include <boost/log/utility/setup/console.hpp>
13+
#include <boost/log/utility/setup/common_attributes.hpp>
14+
#include <boost/log/support/date_time.hpp>
15+
#include <boost/log/core/record.hpp>
16+
17+
namespace logging = boost::log;
18+
namespace src = boost::log::sources;
19+
namespace expr = boost::log::expressions;
20+
namespace keywords = boost::log::keywords;
21+
namespace sinks = boost::log::sinks;
22+
namespace attributes = boost::log::attributes;
23+
24+
#define FILE_T (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
25+
#define BLT BOOST_LOG_TRIVIAL
26+
#define LOG_DEBUG BLT(debug)<<" ("<<FILE_T<<" +"<<__LINE__<<") "<<__FUNCTION__<<" "
27+
#define LOG_INFO BLT(info)<<" ("<<FILE_T<<" +"<<__LINE__<<") "<<__FUNCTION__<<" "
28+
#define LOG_WARN BLT(warning)<<" ("<<FILE_T<<" +"<<__LINE__<<") "<<__FUNCTION__<<" "
29+
#define LOG_ERROR BLT(error)<<" ("<<FILE_T<<" +"<<__LINE__<<") "<<__FUNCTION__<<" "
30+
#define LOG_FATAL BLT(fatal)<<" ("<<FILE_T<<" +"<<__LINE__<<") "<<__FUNCTION__<<" "
31+
32+
static void logging_init(const std::string& logfile, bool debug) {
33+
34+
logging::add_file_log(
35+
keywords::file_name = logfile + "-%Y_%m_%d.log",
36+
keywords::open_mode = (std::ios::out | std::ios::app),
37+
keywords::time_based_rotation=sinks::file::rotation_at_time_point(0,0,0),
38+
keywords::format =(expr::stream
39+
<< "["<<expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f")<<"] "
40+
<< "<" << logging::trivial::severity<< ">"<<expr::smessage
41+
),
42+
keywords::auto_flush = true
43+
);
44+
45+
if(debug) {
46+
//logging::add_console_log(std::clog, keywords::format = "%TimeStamp%: %_%");
47+
logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::trace);
48+
} else {
49+
logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::info);
50+
}
51+
52+
logging::add_common_attributes();
53+
}
54+
55+
static void logging_destroy() {
56+
}
57+
#elif USING_GLOG
58+
59+
#include <iostream>
60+
#include <glog/logging.h>
61+
62+
static void logging_init(const std::string& _logdir, bool debug) {
63+
std::string logdir{_logdir};
64+
if(logdir.empty() || logdir == "/")
65+
logdir = "./log";
66+
67+
FLAGS_log_dir = logdir;
68+
FLAGS_logtostderr = false;
69+
FLAGS_stderrthreshold = 4; // google::FATAL;
70+
//FLAGS_minloglevel = 1;
71+
if(debug)
72+
FLAGS_logbufsecs = 0; // Set log output speed(s)
73+
else
74+
FLAGS_logbufsecs = 30; // Set log output speed(s)
75+
76+
FLAGS_max_log_size = 1024; // M
77+
FLAGS_stop_logging_if_full_disk = true; // If disk is full
78+
79+
google::InitGoogleLogging("log");
80+
google::SetLogDestination(google::GLOG_INFO, std::string(logdir + "/INFO").data());
81+
google::SetLogDestination(google::GLOG_WARNING, std::string(logdir + "/WARN").data());
82+
google::SetLogDestination(google::GLOG_ERROR, std::string(logdir + "/ERROR").data());
83+
84+
google::SetLogFilenameExtension(".glog.");
85+
}
86+
87+
static void logging_destroy() {
88+
google::ShutdownGoogleLogging();
89+
}
90+
91+
#define LOG_DEBUG DLOG(INFO)
92+
#define LOG_INFO LOG(INFO)
93+
#define LOG_WARN LOG(WARNING)
94+
#define LOG_ERROR LOG(ERROR)
95+
#define LOG_FATAL LOG(FATAL)
96+
97+
#else
98+
99+
#include <iostream>
100+
101+
#include <stdlib.h>
102+
#include <time.h>
103+
104+
enum log_rank_t {
105+
DEBUG,
106+
INFO,
107+
WARNING,
108+
ERROR,
109+
FATAL
110+
};
111+
static const char* LOGNAME[] = {
112+
"DEBUG",
113+
"INFO",
114+
"WARN",
115+
"ERROR",
116+
"FATAL"
117+
};
118+
119+
class Logger {
120+
public:
121+
Logger(log_rank_t log_rank) : m_log_rank(log_rank) {};
122+
~Logger() {}
123+
std::ostream& start(log_rank_t log_rank,
124+
const std::string& file,
125+
const int32_t line,
126+
const std::string& function) {
127+
time_t tm = time(NULL);
128+
char time_str[128] = {0};
129+
//char* pts = ctime_r(&tm, time_str);
130+
struct tm t40;
131+
struct tm* t4 = localtime_r(&tm, &t40);
132+
strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", t4);
133+
return stream(log_rank) <<std::endl
134+
<< "["<<time_str<<"] "
135+
<< LOGNAME[log_rank]
136+
<< " " <<file
137+
<< " +" << line
138+
<< " (" <<function << ") "
139+
<< std::flush;
140+
}
141+
private:
142+
static std::ostream& stream(log_rank_t log_rank) {
143+
return std::cerr;
144+
}
145+
log_rank_t m_log_rank;
146+
};
147+
148+
#define LOG(log_rank) \
149+
Logger(log_rank).start(log_rank, __FILE__, __LINE__, __FUNCTION__)
150+
151+
#define LOG_DEBUG LOG(DEBUG)
152+
#define LOG_INFO LOG(INFO)
153+
#define LOG_WARN LOG(WARN)
154+
#define LOG_WARNING LOG(WARNING)
155+
#define LOG_ERROR LOG(ERROR)
156+
#define LOG_FATAL LOG(FATAL)
157+
158+
static void logging_init(const std::string& logdir, bool debug) {
159+
}
160+
161+
static void logging_destroy() {
162+
}
163+
164+
#endif

task.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
#pragma once
3+
4+
#include <iostream>
5+
6+
#include "co_routine.h"
7+
#include "logging.h"
8+
9+
struct Task { // request
10+
public:
11+
Task() : i_(0) {}
12+
Task(int i) : i_(i) {}
13+
int set(int i) { i_ = i; }
14+
int get() { return i_; }
15+
16+
const std::string id() {
17+
return std::to_string(i_);
18+
}
19+
20+
void run() {
21+
co_enable_hook_sys();
22+
// TODO:
23+
struct pollfd pf = { 0 };
24+
pf.fd = -1;
25+
poll(&pf, 1, 20);
26+
LOG_DEBUG<<"Task::run val:"<<get();
27+
28+
}
29+
private:
30+
int i_;
31+
};
32+

test.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
#include <unistd.h>
3+
4+
#include "thread_workers_co.h"
5+
#include "task.h"
6+
#include "logging.h"
7+
8+
int main(int argc, char** arv) {
9+
10+
logging_init("./log/", true);
11+
12+
utils::ThreadWorkers thread_workers;
13+
thread_workers.init(4, 100, 128);
14+
15+
for(int i=0; i<10000000; ++i) {
16+
Task* task = new Task(i);
17+
thread_workers.addTask(task);
18+
}
19+
20+
sleep(10000);
21+
22+
thread_workers.destroy();
23+
}

0 commit comments

Comments
 (0)