Include VS solution
And refactor code: tab, code style. and end of lines. Update the libcpuid lib.
This commit is contained in:
parent
98c151b190
commit
86f0d9d944
106 changed files with 12665 additions and 6894 deletions
|
@ -21,134 +21,98 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#ifdef WIN32
|
||||
# include <winsock2.h>
|
||||
# include <windows.h>
|
||||
#endif
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "log/ConsoleLog.h"
|
||||
#include "log/Log.h"
|
||||
#include "Options.h"
|
||||
|
||||
|
||||
ConsoleLog::ConsoleLog(bool colors) :
|
||||
m_colors(colors),
|
||||
m_stream(nullptr)
|
||||
m_colors(colors)
|
||||
{
|
||||
if (uv_tty_init(uv_default_loop(), &m_tty, 1, 0) < 0) {
|
||||
Options::i()->setColors(false);
|
||||
m_colors = false;
|
||||
return;
|
||||
}
|
||||
|
||||
uv_tty_set_mode(&m_tty, UV_TTY_MODE_NORMAL);
|
||||
m_uvBuf.base = m_buf;
|
||||
m_stream = reinterpret_cast<uv_stream_t*>(&m_tty);
|
||||
|
||||
# ifdef WIN32
|
||||
HANDLE handle = GetStdHandle(STD_INPUT_HANDLE);
|
||||
if (handle != INVALID_HANDLE_VALUE) {
|
||||
DWORD mode = 0;
|
||||
if (GetConsoleMode(handle, &mode)) {
|
||||
mode &= ~ENABLE_QUICK_EDIT_MODE;
|
||||
SetConsoleMode(handle, mode | ENABLE_EXTENDED_FLAGS);
|
||||
}
|
||||
}
|
||||
# endif
|
||||
}
|
||||
|
||||
|
||||
void ConsoleLog::message(int level, const char* fmt, va_list args)
|
||||
void ConsoleLog::message(Level level, const std::string & text)
|
||||
{
|
||||
time_t now = time(nullptr);
|
||||
tm stime;
|
||||
if(!isWritable())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
# ifdef _WIN32
|
||||
localtime_s(&stime, &now);
|
||||
# else
|
||||
localtime_r(&now, &stime);
|
||||
# endif
|
||||
//
|
||||
//
|
||||
time_t now = time(nullptr);
|
||||
tm stime;
|
||||
|
||||
const char* color = nullptr;
|
||||
if (m_colors) {
|
||||
switch (level) {
|
||||
case Log::ERR:
|
||||
color = Log::kCL_RED;
|
||||
break;
|
||||
#ifdef _WIN32
|
||||
localtime_s(&stime, &now);
|
||||
#else
|
||||
localtime_r(&now, &stime);
|
||||
#endif
|
||||
|
||||
case Log::WARNING:
|
||||
color = Log::kCL_YELLOW;
|
||||
break;
|
||||
char buf[25];
|
||||
int size = snprintf(buf, sizeof(buf), "[%d-%02d-%02d %02d:%02d:%02d] ",
|
||||
stime.tm_year + 1900,
|
||||
stime.tm_mon + 1,
|
||||
stime.tm_mday,
|
||||
stime.tm_hour,
|
||||
stime.tm_min,
|
||||
stime.tm_sec);
|
||||
|
||||
case Log::NOTICE:
|
||||
color = Log::kCL_WHITE;
|
||||
break;
|
||||
//
|
||||
//
|
||||
std::string colorIni, colorEnd;
|
||||
if(m_colors)
|
||||
{
|
||||
colorEnd = Log::CL_N();
|
||||
switch(level)
|
||||
{
|
||||
case ILogBackend::ERR:
|
||||
colorIni = Log::CL_RED();
|
||||
break;
|
||||
|
||||
case Log::DEBUG:
|
||||
color = Log::kCL_GRAY;
|
||||
break;
|
||||
case ILogBackend::WARNING:
|
||||
colorIni = Log::CL_YELLOW();
|
||||
break;
|
||||
|
||||
default:
|
||||
color = "";
|
||||
break;
|
||||
}
|
||||
}
|
||||
case ILogBackend::NOTICE:
|
||||
colorIni = Log::CL_WHITE();
|
||||
break;
|
||||
|
||||
snprintf(m_fmt, sizeof(m_fmt) - 1, "[%d-%02d-%02d %02d:%02d:%02d]%s %s%s\n",
|
||||
stime.tm_year + 1900,
|
||||
stime.tm_mon + 1,
|
||||
stime.tm_mday,
|
||||
stime.tm_hour,
|
||||
stime.tm_min,
|
||||
stime.tm_sec,
|
||||
m_colors ? color : "",
|
||||
fmt,
|
||||
m_colors ? Log::kCL_N : ""
|
||||
);
|
||||
case ILogBackend::DEBUG:
|
||||
colorIni = Log::CL_GRAY();
|
||||
break;
|
||||
|
||||
print(args);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
print(std::string(buf, size) + colorIni + text + colorEnd);
|
||||
}
|
||||
|
||||
|
||||
void ConsoleLog::text(const char* fmt, va_list args)
|
||||
void ConsoleLog::text(const std::string & txt)
|
||||
{
|
||||
snprintf(m_fmt, sizeof(m_fmt) - 1, "%s%s\n", fmt, m_colors ? Log::kCL_N : "");
|
||||
if(!isWritable())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
print(args);
|
||||
print(txt);
|
||||
}
|
||||
|
||||
|
||||
bool ConsoleLog::isWritable() const
|
||||
{
|
||||
if (!m_stream || uv_is_writable(m_stream) != 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const uv_handle_type type = uv_guess_handle(1);
|
||||
return type == UV_TTY || type == UV_NAMED_PIPE;
|
||||
return std::cout.good();
|
||||
}
|
||||
|
||||
|
||||
void ConsoleLog::print(va_list args)
|
||||
void ConsoleLog::print(const std::string & txt)
|
||||
{
|
||||
m_uvBuf.len = vsnprintf(m_buf, sizeof(m_buf) - 1, m_fmt, args);
|
||||
if (m_uvBuf.len <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isWritable()) {
|
||||
fputs(m_buf, stdout);
|
||||
fflush(stdout);
|
||||
}
|
||||
else {
|
||||
uv_try_write(m_stream, &m_uvBuf, 1);
|
||||
}
|
||||
std::cout << txt << std::endl;
|
||||
std::cout.flush();
|
||||
}
|
||||
|
|
|
@ -34,21 +34,16 @@
|
|||
class ConsoleLog : public ILogBackend
|
||||
{
|
||||
public:
|
||||
ConsoleLog(bool colors);
|
||||
ConsoleLog(bool colors);
|
||||
|
||||
void message(int level, const char *fmt, va_list args) override;
|
||||
void text(const char *fmt, va_list args) override;
|
||||
void message(Level level, const std::string & txt) override;
|
||||
void text(const std::string & txt) override;
|
||||
|
||||
private:
|
||||
bool isWritable() const;
|
||||
void print(va_list args);
|
||||
bool isWritable() const;
|
||||
void print(const std::string & txt);
|
||||
|
||||
bool m_colors;
|
||||
char m_buf[512];
|
||||
char m_fmt[256];
|
||||
uv_buf_t m_uvBuf;
|
||||
uv_stream_t *m_stream;
|
||||
uv_tty_t m_tty;
|
||||
bool m_colors;
|
||||
};
|
||||
|
||||
#endif /* __CONSOLELOG_H__ */
|
||||
|
|
|
@ -28,70 +28,68 @@
|
|||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
#include "log/Log.h"
|
||||
#include "log/FileLog.h"
|
||||
|
||||
|
||||
FileLog::FileLog(const char *fileName)
|
||||
FileLog::FileLog(const std::string & fileName)
|
||||
: m_file_name(fileName)
|
||||
{
|
||||
uv_fs_t req;
|
||||
m_file = uv_fs_open(uv_default_loop(), &req, fileName, O_CREAT | O_APPEND | O_WRONLY, 0644, nullptr);
|
||||
uv_fs_req_cleanup(&req);
|
||||
}
|
||||
|
||||
|
||||
void FileLog::message(int level, const char* fmt, va_list args)
|
||||
void FileLog::message(Level level, const std::string & txt)
|
||||
{
|
||||
if (m_file < 0) {
|
||||
return;
|
||||
}
|
||||
if(!isWritable())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
time_t now = time(nullptr);
|
||||
tm stime;
|
||||
//
|
||||
//
|
||||
time_t now = time(nullptr);
|
||||
tm stime;
|
||||
|
||||
# ifdef _WIN32
|
||||
localtime_s(&stime, &now);
|
||||
# else
|
||||
localtime_r(&now, &stime);
|
||||
# endif
|
||||
#ifdef _WIN32
|
||||
localtime_s(&stime, &now);
|
||||
#else
|
||||
localtime_r(&now, &stime);
|
||||
#endif
|
||||
|
||||
char *buf = new char[512];
|
||||
int size = snprintf(buf, 23, "[%d-%02d-%02d %02d:%02d:%02d] ",
|
||||
stime.tm_year + 1900,
|
||||
stime.tm_mon + 1,
|
||||
stime.tm_mday,
|
||||
stime.tm_hour,
|
||||
stime.tm_min,
|
||||
stime.tm_sec);
|
||||
char buf[25];
|
||||
int size = snprintf(buf, sizeof(buf), "[%d-%02d-%02d %02d:%02d:%02d] ",
|
||||
stime.tm_year + 1900,
|
||||
stime.tm_mon + 1,
|
||||
stime.tm_mday,
|
||||
stime.tm_hour,
|
||||
stime.tm_min,
|
||||
stime.tm_sec);
|
||||
|
||||
size = vsnprintf(buf + size, 512 - size - 1, fmt, args) + size;
|
||||
buf[size] = '\n';
|
||||
|
||||
write(buf, size + 1);
|
||||
//
|
||||
//
|
||||
write(std::string(buf, size) + txt);
|
||||
}
|
||||
|
||||
|
||||
void FileLog::text(const char* fmt, va_list args)
|
||||
void FileLog::text(const std::string & txt)
|
||||
{
|
||||
message(0, fmt, args);
|
||||
if(!isWritable())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
write(txt);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void FileLog::onWrite(uv_fs_t *req)
|
||||
bool FileLog::isWritable() const
|
||||
{
|
||||
delete [] static_cast<char *>(req->data);
|
||||
|
||||
uv_fs_req_cleanup(req);
|
||||
delete req;
|
||||
return (m_file_name != "") && std::ofstream(m_file_name, std::ios_base::app).good();
|
||||
}
|
||||
|
||||
|
||||
void FileLog::write(char *data, size_t size)
|
||||
void FileLog::write(const std::string & txt)
|
||||
{
|
||||
uv_buf_t buf = uv_buf_init(data, (unsigned int) size);
|
||||
uv_fs_t *req = new uv_fs_t;
|
||||
req->data = buf.base;
|
||||
std::ofstream outfile;
|
||||
|
||||
uv_fs_write(uv_default_loop(), req, m_file, &buf, 1, 0, FileLog::onWrite);
|
||||
outfile.open(m_file_name, std::ios_base::app);
|
||||
outfile << txt << std::endl;
|
||||
}
|
||||
|
|
|
@ -24,27 +24,21 @@
|
|||
#ifndef __FILELOG_H__
|
||||
#define __FILELOG_H__
|
||||
|
||||
|
||||
#include <uv.h>
|
||||
|
||||
|
||||
#include "interfaces/ILogBackend.h"
|
||||
|
||||
|
||||
class FileLog : public ILogBackend
|
||||
{
|
||||
public:
|
||||
FileLog(const char *fileName);
|
||||
FileLog(const std::string & fileName);
|
||||
|
||||
void message(int level, const char* fmt, va_list args) override;
|
||||
void text(const char* fmt, va_list args) override;
|
||||
void message(Level level, const std::string & txt) override;
|
||||
void text(const std::string & txt) override;
|
||||
|
||||
private:
|
||||
static void onWrite(uv_fs_t *req);
|
||||
bool isWritable() const;
|
||||
void write(const std::string & txt);
|
||||
|
||||
void write(char *data, size_t size);
|
||||
|
||||
int m_file;
|
||||
const std::string m_file_name;
|
||||
};
|
||||
|
||||
#endif /* __FILELOG_H__ */
|
||||
|
|
|
@ -28,47 +28,35 @@
|
|||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
|
||||
#include "interfaces/ILogBackend.h"
|
||||
#include "log/Log.h"
|
||||
|
||||
Log* Log::m_self = nullptr;
|
||||
|
||||
Log *Log::m_self = nullptr;
|
||||
|
||||
|
||||
void Log::message(Log::Level level, const char* fmt, ...)
|
||||
void Log::message(ILogBackend::Level level, const std::string & text)
|
||||
{
|
||||
va_list args;
|
||||
va_list copy;
|
||||
va_start(args, fmt);
|
||||
|
||||
for (ILogBackend *backend : m_backends) {
|
||||
va_copy(copy, args);
|
||||
backend->message(level, fmt, copy);
|
||||
va_end(copy);
|
||||
}
|
||||
for(size_t i = 0; i < m_backends.size(); ++i)
|
||||
{
|
||||
auto backend = m_backends[i];
|
||||
backend->message(level, text);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Log::text(const char* fmt, ...)
|
||||
void Log::text(const std::string & text)
|
||||
{
|
||||
va_list args;
|
||||
va_list copy;
|
||||
va_start(args, fmt);
|
||||
for(size_t i = 0; i < m_backends.size(); ++i)
|
||||
{
|
||||
auto backend = m_backends[i];
|
||||
backend->text(text);
|
||||
}
|
||||
|
||||
for (ILogBackend *backend : m_backends) {
|
||||
va_copy(copy, args);
|
||||
backend->text(fmt, copy);
|
||||
va_end(copy);
|
||||
}
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
|
||||
Log::~Log()
|
||||
{
|
||||
for (auto backend : m_backends) {
|
||||
delete backend;
|
||||
}
|
||||
for(size_t i = 0; i < m_backends.size(); ++i)
|
||||
{
|
||||
auto backend = m_backends[i];
|
||||
delete backend;
|
||||
}
|
||||
}
|
||||
|
|
114
src/log/Log.h
114
src/log/Log.h
|
@ -24,70 +24,102 @@
|
|||
#ifndef __LOG_H__
|
||||
#define __LOG_H__
|
||||
|
||||
|
||||
#include <uv.h>
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
class ILogBackend;
|
||||
|
||||
#include "interfaces/ILogBackend.h"
|
||||
|
||||
class Log
|
||||
{
|
||||
public:
|
||||
enum Level {
|
||||
ERR,
|
||||
WARNING,
|
||||
NOTICE,
|
||||
INFO,
|
||||
DEBUG
|
||||
};
|
||||
|
||||
constexpr static const char* kCL_N = "\x1B[0m";
|
||||
constexpr static const char* kCL_RED = "\x1B[31m";
|
||||
constexpr static const char* kCL_YELLOW = "\x1B[33m";
|
||||
constexpr static const char* kCL_WHITE = "\x1B[01;37m";
|
||||
static const std::string & CL_N()
|
||||
{
|
||||
static const std::string kCL_N = "\x1B[0m";
|
||||
return kCL_N;
|
||||
}
|
||||
static const std::string & CL_RED()
|
||||
{
|
||||
static const std::string kCL_RED = "\x1B[31m";
|
||||
return kCL_RED;
|
||||
}
|
||||
static const std::string & CL_YELLOW()
|
||||
{
|
||||
static const std::string kCL_YELLOW = "\x1B[33m";
|
||||
return kCL_YELLOW;
|
||||
}
|
||||
static const std::string & CL_WHITE()
|
||||
{
|
||||
static const std::string kCL_WHITE = "\x1B[01;37m";
|
||||
return kCL_WHITE;
|
||||
}
|
||||
static const std::string & CL_GRAY()
|
||||
{
|
||||
|
||||
# ifdef WIN32
|
||||
constexpr static const char* kCL_GRAY = "\x1B[01;30m";
|
||||
# else
|
||||
constexpr static const char* kCL_GRAY = "\x1B[90m";
|
||||
# endif
|
||||
#ifdef WIN32
|
||||
static const std::string kCL_GRAY = "\x1B[01;30m";
|
||||
#else
|
||||
static const std::string kCL_GRAY = "\x1B[90m";
|
||||
#endif
|
||||
return kCL_GRAY;
|
||||
}
|
||||
|
||||
static inline Log* i() { return m_self; }
|
||||
static inline void add(ILogBackend *backend) { i()->m_backends.push_back(backend); }
|
||||
static inline void init() { if (!m_self) { m_self = new Log();} }
|
||||
static inline void release() { delete m_self; }
|
||||
static inline Log* i()
|
||||
{
|
||||
return m_self;
|
||||
}
|
||||
static inline void add(ILogBackend* backend)
|
||||
{
|
||||
i()->m_backends.push_back(backend);
|
||||
}
|
||||
static inline void init()
|
||||
{
|
||||
if(!m_self)
|
||||
{
|
||||
m_self = new Log();
|
||||
}
|
||||
}
|
||||
static inline void release()
|
||||
{
|
||||
delete m_self;
|
||||
}
|
||||
|
||||
void message(Level level, const char* fmt, ...);
|
||||
void text(const char* fmt, ...);
|
||||
void message(ILogBackend::Level level, const std::string & text);
|
||||
void text(const std::string & text);
|
||||
|
||||
static inline std::string TO_STRING(const std::basic_ostream<char> & i)
|
||||
{
|
||||
const std::stringstream & stream = static_cast<const std::stringstream &>(i);
|
||||
return stream.str();
|
||||
}
|
||||
private:
|
||||
inline Log() {}
|
||||
~Log();
|
||||
inline Log() {}
|
||||
~Log();
|
||||
|
||||
static Log *m_self;
|
||||
std::vector<ILogBackend*> m_backends;
|
||||
static Log* m_self;
|
||||
std::vector<ILogBackend*> m_backends;
|
||||
};
|
||||
|
||||
|
||||
#define LOG_ERR(x, ...) Log::i()->message(Log::ERR, x, ##__VA_ARGS__)
|
||||
#define LOG_WARN(x, ...) Log::i()->message(Log::WARNING, x, ##__VA_ARGS__)
|
||||
#define LOG_NOTICE(x, ...) Log::i()->message(Log::NOTICE, x, ##__VA_ARGS__)
|
||||
#define LOG_INFO(x, ...) Log::i()->message(Log::INFO, x, ##__VA_ARGS__)
|
||||
#define PRINT_MSG(x) Log::i()->text(Log::TO_STRING(std::stringstream() << x))
|
||||
|
||||
#define LOG_ERR(x) Log::i()->message(ILogBackend::ERR, Log::TO_STRING(std::stringstream() << x))
|
||||
#define LOG_WARN(x) Log::i()->message(ILogBackend::WARNING, Log::TO_STRING(std::stringstream() << x))
|
||||
#define LOG_NOTICE(x) Log::i()->message(ILogBackend::NOTICE, Log::TO_STRING(std::stringstream() << x))
|
||||
#define LOG_INFO(x) Log::i()->message(ILogBackend::INFO, Log::TO_STRING(std::stringstream() << x))
|
||||
|
||||
#ifdef APP_DEBUG
|
||||
# define LOG_DEBUG(x, ...) Log::i()->message(Log::DEBUG, x, ##__VA_ARGS__)
|
||||
#define LOG_DEBUG(x) Log::i()->message(ILogBackend::DEBUG, Log::TO_STRING(std::stringstream() << x))
|
||||
#else
|
||||
# define LOG_DEBUG(x, ...)
|
||||
#define LOG_DEBUG(x)
|
||||
#endif
|
||||
|
||||
#if defined(APP_DEBUG) || defined(APP_DEVEL)
|
||||
# define LOG_DEBUG_ERR(x, ...) Log::i()->message(Log::ERR, x, ##__VA_ARGS__)
|
||||
# define LOG_DEBUG_WARN(x, ...) Log::i()->message(Log::WARNING, x, ##__VA_ARGS__)
|
||||
#define LOG_DEBUG_ERR(x) Log::i()->message(ILogBackend::ERR, Log::TO_STRING(std::stringstream() << x))
|
||||
#define LOG_DEBUG_WARN(x) Log::i()->message(ILogBackend::WARNING, Log::TO_STRING(std::stringstream() << x))
|
||||
#else
|
||||
# define LOG_DEBUG_ERR(x, ...)
|
||||
# define LOG_DEBUG_WARN(x, ...)
|
||||
#define LOG_DEBUG_ERR(x)
|
||||
#define LOG_DEBUG_WARN(x)
|
||||
#endif
|
||||
|
||||
#endif /* __LOG_H__ */
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_SYSLOG_H
|
||||
|
||||
#include <syslog.h>
|
||||
|
||||
|
@ -31,17 +32,18 @@
|
|||
|
||||
SysLog::SysLog()
|
||||
{
|
||||
openlog(APP_ID, LOG_PID, LOG_USER);
|
||||
openlog(APP_ID, LOG_PID, LOG_USER);
|
||||
}
|
||||
|
||||
|
||||
void SysLog::message(int level, const char *fmt, va_list args)
|
||||
void SysLog::text(const std::string & txt)
|
||||
{
|
||||
vsyslog(level, fmt, args);
|
||||
message(INFO, txt);
|
||||
}
|
||||
|
||||
|
||||
void SysLog::text(const char *fmt, va_list args)
|
||||
void SysLog::message(Level level, const std::string & txt)
|
||||
{
|
||||
message(LOG_INFO, fmt, args);
|
||||
syslog(level == INFO ? LOG_INFO : LOG_NOTICE, "%s", txt);
|
||||
}
|
||||
|
||||
#endif
|
|
@ -31,10 +31,10 @@
|
|||
class SysLog : public ILogBackend
|
||||
{
|
||||
public:
|
||||
SysLog();
|
||||
SysLog();
|
||||
|
||||
void message(int level, const char *fmt, va_list args) override;
|
||||
void text(const char *fmt, va_list args) override;
|
||||
void message(Level level, const std::string & txt) override;
|
||||
void text(const std::string & txt) override;
|
||||
};
|
||||
|
||||
#endif /* __SYSLOG_BACKEND_H__ */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue