This commit is contained in:
2025-09-24 10:53:28 +08:00
commit f8e4df77fb
856 changed files with 140098 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
#ifndef __UT_LOG_HPP__
#define __UT_LOG_HPP__
#include <unitree/common/log/log_initor.hpp>
#endif//__UT_LOG_HPP__

View File

@@ -0,0 +1,44 @@
#ifndef __UT_LOG_BUFFER_HPP__
#define __UT_LOG_BUFFER_HPP__
#include <unitree/common/log/log_decl.hpp>
namespace unitree
{
namespace common
{
class LogBuffer
{
public:
explicit LogBuffer();
virtual ~LogBuffer();
virtual bool Append(const std::string& s);
virtual bool Get(std::string& s);
virtual bool Empty();
protected:
std::string mData;
};
typedef std::shared_ptr<LogBuffer> LogBufferPtr;
class LogBlockBuffer : public LogBuffer
{
public:
explicit LogBlockBuffer();
~LogBlockBuffer();
bool Append(const std::string& s);
bool Get(std::string& s);
bool Empty();
private:
Mutex mLock;
};
typedef std::shared_ptr<LogBlockBuffer> LogBlockBufferPtr;
}
}
#endif//__UT_LOG_BUFFER_HPP__

View File

@@ -0,0 +1,261 @@
#ifndef __UT_LOG_DECL_HPP__
#define __UT_LOG_DECL_HPP__
#include <unitree/common/exception.hpp>
#include <unitree/common/os.hpp>
#include <unitree/common/lock/lock.hpp>
#include <unitree/common/thread/thread.hpp>
#include <unitree/common/thread/recurrent_thread.hpp>
#include <unitree/common/filesystem/file.hpp>
/*
* log buffer size
*/
#define UT_LOG_BUFFER_SIZE 65535 //64K-1
#define UT_LOG_CRT_BUFFER_SIZE 64512 //63K
#define UT_LOG_MAX_BUFFER_SIZE 4194303 //4M-1
/*
* log file size
*/
#define UT_LOG_FILE_SIZE 104857600 //100M
#define UT_LOG_MAX_FILE_SIZE 10737418240 //10G
#define UT_LOG_MIN_FILE_SIZE UT_LOG_MAX_BUFFER_SIZE
/*
* log write interval(micro second)
*/
#define UT_LOG_WRITE_INTER 100000 //100ms
#define UT_LOG_MAX_WRITE_INTER 1000000 //1s
#define UT_LOG_MIN_WRITE_INTER 2000 //2ms
/*
* log file number
*/
#define UT_LOG_FILE_NUMBER 2
#define UT_LOG_MAX_FILE_NUMBER 100
#define UT_LOG_MIN_FILE_NUMBER UT_LOG_FILE_NUMBER
#define UT_LOG_FILE_EXT ".LOG"
//write log macro wrapper
#define __UT_LOG(logger, level, ...)\
do { \
if (logger != NULL) \
{ \
logger->Log(level, __VA_ARGS__); \
} \
} while (0)
#define __UT_CRIT_LOG(logger, key, code, ...) \
do { \
if (logger != NULL) \
{ \
logger->CritLog(UT_LOG_CRIT, key, code, __VA_ARGS__);\
} \
} while (0)
//debug
#define LOG_DEBUG(logger, ...) \
__UT_LOG(logger, UT_LOG_DEBUG, __VA_ARGS__)
//info
#define LOG_INFO(logger, ...) \
__UT_LOG(logger, UT_LOG_INFO, __VA_ARGS__)
//warning
#define LOG_WARNING(logger, ...) \
__UT_LOG(logger, UT_LOG_WARNING, __VA_ARGS__)
//error
#define LOG_ERROR(logger, ...) \
__UT_LOG(logger, UT_LOG_ERROR, __VA_ARGS__)
//fatal
#define LOG_FATAL(logger, ...) \
__UT_LOG(logger, UT_LOG_FATAL, __VA_ARGS__)
//critical log. the 1st args is CRITICAL KEY
#define CRIT_LOG(logger, ...) \
__UT_CRIT_LOG(logger, __VA_ARGS__)
#define CRIT_LOG(logger, ...) \
__UT_CRIT_LOG(logger, __VA_ARGS__)
//write log format macro wrapper
/*
* FMT_DEBUG(logger, ("key1", val1)("key2", val2)("keyn", ""));
*/
#define __UT_LOG_FMT(logger, level, keyvalues) \
do { \
if (logger != NULL) \
{ \
logger->LogFormat(level, unitree::common::LogBuilder() keyvalues); \
} \
} while (0)
#define __UT_CRIT_LOG_FMT(logger, key, code, keyvalues) \
do { \
if (logger != NULL) \
{ \
logger->CritLogFormat(UT_LOG_CRIT, key, code, unitree::common::LogBuilder() keyvalues); \
} \
} while (0)
//debug
#define FMT_DEBUG(logger, keyvalues) \
__UT_LOG_FMT(logger, UT_LOG_DEBUG, keyvalues)
//info
#define FMT_INFO(logger, keyvalues) \
__UT_LOG_FMT(logger, UT_LOG_INFO, keyvalues)
//warning
#define FMT_WARNING(logger, keyvalues) \
__UT_LOG_FMT(logger, UT_LOG_WARNING, keyvalues)
//error
#define FMT_ERROR(logger, keyvalues) \
__UT_LOG_FMT(logger, UT_LOG_ERROR, keyvalues)
//fatal
#define FMT_FATAL(logger, keyvalues) \
__UT_LOG_FMT(logger, UT_LOG_FATAL, keyvalues)
#define CRIT_FMT(logger, critkey, keyvalues) \
__UT_CRIT_LOG_FMT(logger, critkey, keyvalues)
/*
//declare and define log level
#define UT_LOG_DECL_LEVEL(name, level, desc) \
const int32_t name = level; \
const std::string name##_DESC = desc;
#define UT_LOG_DESC_LEVEL(name) name##_DESC
*/
//declare and define log store type
#define UT_LOG_DECL_STORE_TYPE(name, type, desc)\
const int32_t name = type; \
const std::string name##_DESC = desc;
#define UT_LOG_DESC_STORE_TYPE(name) name##_DESC
//define log level
#define UT_LOG_NONE 0
#define UT_LOG_CRIT 1
#define UT_LOG_FATAL 2
#define UT_LOG_ERROR 3
#define UT_LOG_WARNING 4
#define UT_LOG_INFO 5
#define UT_LOG_DEBUG 6
#define UT_LOG_ALL 7
#define UT_LOG_DESC_NONE "NONE"
#define UT_LOG_DESC_CRIT "CRIT"
#define UT_LOG_DESC_FATAL "FATAL"
#define UT_LOG_DESC_ERROR "ERROR"
#define UT_LOG_DESC_WARNING "WARNING"
#define UT_LOG_DESC_INFO "INFO"
#define UT_LOG_DESC_DEBUG "DEBUG"
#define UT_LOG_DESC_ALL "ALL"
//define log store type
#define UT_LOG_STORE_FILE_ASYNC 0
#define UT_LOG_STORE_FILE 1
#define UT_LOG_STORE_STDOUT 2
#define UT_LOG_STORE_STDERR 3
#define UT_LOG_STORE_DESC_FILE_ASYNC "FILEASYNC"
#define UT_LOG_STORE_DESC_ASYNC_FILE "ASYNCFILE"
#define UT_LOG_STORE_DESC_ASYNC "ASYNC"
#define UT_LOG_STORE_DESC_FILE "FILE"
#define UT_LOG_STORE_DESC_STDOUT "STDOUT"
#define UT_LOG_STORE_DESC_STDERR "STDERR"
namespace unitree
{
namespace common
{
static inline int32_t GetLogLevel(const std::string& desc)
{
if (desc == UT_LOG_DESC_NONE) {
return UT_LOG_NONE; }
else if (desc == UT_LOG_DESC_CRIT) {
return UT_LOG_CRIT; }
else if (desc == UT_LOG_DESC_FATAL) {
return UT_LOG_FATAL; }
else if (desc == UT_LOG_DESC_ERROR) {
return UT_LOG_ERROR; }
else if (desc == UT_LOG_DESC_WARNING) {
return UT_LOG_WARNING; }
else if (desc == UT_LOG_DESC_INFO) {
return UT_LOG_INFO; }
else if (desc == UT_LOG_DESC_DEBUG) {
return UT_LOG_DEBUG; }
else if (desc == UT_LOG_DESC_ALL) {
return UT_LOG_ALL; }
UT_THROW(CommonException, std::string("unknown log level desc:") + desc);
}
static inline const char* GetLogLevelDesc(int32_t level)
{
switch (level)
{
case UT_LOG_NONE:
return UT_LOG_DESC_NONE;
case UT_LOG_CRIT:
return UT_LOG_DESC_CRIT;
case UT_LOG_FATAL:
return UT_LOG_DESC_FATAL;
case UT_LOG_ERROR:
return UT_LOG_DESC_ERROR;
case UT_LOG_WARNING:
return UT_LOG_DESC_WARNING;
case UT_LOG_INFO:
return UT_LOG_DESC_INFO;
case UT_LOG_DEBUG:
return UT_LOG_DESC_DEBUG;
case UT_LOG_ALL:
return UT_LOG_DESC_ALL;
}
UT_THROW(CommonException, "unknown log level");
}
static inline int32_t GetLogStoreType(const std::string& desc)
{
if (desc == UT_LOG_STORE_DESC_FILE_ASYNC ||
desc == UT_LOG_STORE_DESC_ASYNC_FILE ||
desc == UT_LOG_STORE_DESC_ASYNC) {
return UT_LOG_STORE_FILE_ASYNC; }
else if (desc == UT_LOG_STORE_DESC_FILE) {
return UT_LOG_STORE_FILE; }
else if (desc == UT_LOG_STORE_DESC_STDOUT){
return UT_LOG_STORE_STDOUT; }
else if (desc == UT_LOG_STORE_DESC_STDERR){
return UT_LOG_STORE_STDERR; }
UT_THROW(CommonException, std::string("unknown log store type desc:") + desc);
}
static inline const char* GetLogStoreTypeDesc(int32_t type)
{
switch (type)
{
case UT_LOG_STORE_FILE_ASYNC:
return UT_LOG_STORE_DESC_FILE_ASYNC;
case UT_LOG_STORE_FILE:
return UT_LOG_STORE_DESC_FILE;
case UT_LOG_STORE_STDOUT:
return UT_LOG_STORE_DESC_STDOUT;
case UT_LOG_STORE_STDERR:
return UT_LOG_STORE_DESC_STDERR;
}
UT_THROW(CommonException, "unknown log store type");
}
}
}
#endif//__UT_LOG_DECL_HPP__

View File

@@ -0,0 +1,44 @@
#ifndef __UT_LOG_INITOR_HPP__
#define __UT_LOG_INITOR_HPP__
#include <unitree/common/log/log_policy.hpp>
#include <unitree/common/log/log_store.hpp>
#include <unitree/common/log/log_logger.hpp>
namespace unitree
{
namespace common
{
class LogInitor
{
public:
explicit LogInitor();
~LogInitor();
void Init(const std::string& configFileName, bool stdoutDefault = false);
Logger* GetLogger(const std::string& tag);
void Final();
void ParseConf(Any json);
void SetStdoutPolicy();
void InitLogger();
private:
std::set<std::string> mStoreNames;
std::vector<LogPolicyPtr> mPolicis;
std::vector<LogStorePolicyPtr> mStorePolicis;
std::map<std::string, LoggerPtr> mLoggerMap;
};
using LogInitorPtr = std::shared_ptr<LogInitor>;
void LogInit(const std::string& configFileName = "", bool stdoutDefault = false);
void LogFinal();
Logger* GetLogger(const std::string& tag);
}
}
#endif//__UT_LOG_INITOR_HPP__

View File

@@ -0,0 +1,44 @@
#ifndef __UT_LOG_FILE_KEEPER_H__
#define __UT_LOG_FILE_KEEPER_H__
#include <unitree/common/log/log_policy.hpp>
namespace unitree
{
namespace common
{
class LogKeeper
{
public:
LogKeeper(LogStorePolicyPtr storePolicyPtr);
~LogKeeper();
LogStorePolicyPtr GetStorePolicy() const;
bool Append(const std::string& s, bool rotate);
private:
void Rotate();
void AppendFile(const std::string& s);
void OpenFile();
void CloseFile();
void CheckFileSize();
std::string MakeRegexExpress();
private:
volatile int64_t mFileSize;
std::string mFileName;
std::string mDirectory;
FilePtr mFilePtr;
LogStorePolicyPtr mStorePolicyPtr;
};
typedef std::shared_ptr<LogKeeper> LogKeeperPtr;
}
}
#endif//__UT_LOG_FILE_KEEPER_H__

View File

@@ -0,0 +1,139 @@
#ifndef __UT_LOGGER_HPP__
#define __UT_LOGGER_HPP__
#include <unitree/common/log/log_store.hpp>
namespace unitree
{
namespace common
{
class LogBuilder
{
public:
LogBuilder()
{}
template<typename T>
LogBuilder& operator()(const std::string& key, const T& value)
{
mOs << "\t" << key << ":" << value;
return *this;
}
std::ostringstream mOs;
};
class Logger
{
public:
explicit Logger(int32_t level, LogStorePtr storePtr) :
mLevel(level), mStorePtr(storePtr)
{}
template<typename ...Args>
void Log(int32_t level, Args&&... args)
{
if (level > mLevel || mStorePtr == NULL)
{
return;
}
std::ostringstream os;
LogBegin(os, level);
LogPend(os, std::forward<Args>(args)...);
LogEnd(os);
mStorePtr->Append(os.str());
}
void LogFormat(int32_t level, LogBuilder& builder)
{
if (level > mLevel || mStorePtr == NULL)
{
return;
}
std::ostringstream os;
LogBegin(os, level);
LogPendBuilder(os, builder);
LogEnd(os);
mStorePtr->Append(os.str());
}
template<typename ...Args>
void CritLog(int32_t level, const std::string& key, int32_t code, Args&&... args)
{
if (level > mLevel || mStorePtr == NULL)
{
return;
}
std::ostringstream os;
LogBegin(os, level);
LogPendCrit(os, key, code);
LogPend(os, std::forward<Args>(args)...);
LogEnd(os);
mStorePtr->Append(os.str());
}
void CritLogFormat(int32_t level, const std::string& key, int32_t code, LogBuilder& builder)
{
if (level > mLevel || mStorePtr == NULL)
{
return;
}
std::ostringstream os;
LogBegin(os, level);
LogPendCrit(os, key, code);
LogPendBuilder(os, builder);
LogEnd(os);
mStorePtr->Append(os.str());
}
private:
void LogPendCrit(std::ostringstream& os, const std::string& key, int32_t code)
{
os << " [__KEY__:" << key << "]";
os << " [__CODE__:" << code << "]";
}
template<typename ...Args>
void LogPend(std::ostringstream& os, Args&&... args)
{
os << " ";
std::initializer_list<int32_t>{ (os << args, 0)... };
}
void LogPendBuilder(std::ostringstream& os, LogBuilder& builder)
{
os << builder.mOs.str();
}
void LogBegin(std::ostringstream& os, int32_t level)
{
os << "[" << GetTimeMillisecondString() << "] ";
os << "[" << GetLogLevelDesc(level) << "] ";
os << "[" << OsHelper::Instance()->GetProcessId() << "] ";
os << "[" << OsHelper::Instance()->GetTid() << "]";
os << std::setprecision(6) << std::fixed;
}
void LogEnd(std::ostringstream& os)
{
os << std::endl;
}
private:
int32_t mLevel;
LogStorePtr mStorePtr;
};
typedef std::shared_ptr<Logger> LoggerPtr;
}
}
#endif//__UT_LOGGER_HPP__

View File

@@ -0,0 +1,52 @@
#ifndef __UT_LOG_POLICY_HPP__
#define __UT_LOG_POLICY_HPP__
#include <unitree/common/log/log_decl.hpp>
namespace unitree
{
namespace common
{
class LogPolicy
{
public:
LogPolicy();
LogPolicy(const std::string& tag, int32_t level, const std::string& store);
void Dump();
public:
std::string mTag;
int32_t mLevel;
std::string mStore;
};
typedef std::shared_ptr<LogPolicy> LogPolicyPtr;
class LogStorePolicy
{
public:
LogStorePolicy();
LogStorePolicy(const std::string& name, int32_t type, int32_t fileNumber, int64_t fileSize,
int64_t fileWriteInter = UT_LOG_WRITE_INTER, int32_t cpuId = UT_CPU_ID_NONE,
const std::string& fileName = "", const std::string& directory = "");
void Dump();
public:
std::string mName;
int32_t mType;
int32_t mFileNumber;
int64_t mFileSize;
int64_t mFileWriteInter;
int32_t mCpuId;
std::string mFileName;
std::string mDirectory;
};
typedef std::shared_ptr<LogStorePolicy> LogStorePolicyPtr;
}
}
#endif//__UT_LOG_POLICY_HPP__

View File

@@ -0,0 +1,76 @@
#ifndef __LOG_STORE_HPP__
#define __LOG_STORE_HPP__
#include <unitree/common/log/log_writer.hpp>
#include <unitree/common/log/log_keeper.hpp>
namespace unitree
{
namespace common
{
class LogStore
{
public:
explicit LogStore()
{}
virtual ~LogStore()
{
mWriterPtr.reset();
}
virtual void Append(const std::string& s) = 0;
protected:
LogWriterPtr mWriterPtr;
};
typedef std::shared_ptr<LogStore> LogStorePtr;
class LogStdoutStore : public LogStore
{
public:
explicit LogStdoutStore();
~LogStdoutStore();
void Append(const std::string& s);
};
typedef std::shared_ptr<LogStdoutStore> LogStdoutStorePtr;
class LogStderrStore : public LogStore
{
public:
explicit LogStderrStore();
~LogStderrStore();
void Append(const std::string& s);
};
typedef std::shared_ptr<LogStderrStore> LogStderrStorePtr;
class LogFileStore : public LogStore
{
public:
explicit LogFileStore(LogKeeperPtr keeperPtr);
~LogFileStore();
void Append(const std::string& s);
};
typedef std::shared_ptr<LogFileStore> LogFileStorePtr;
class LogFileAsyncStore : public LogStore
{
public:
explicit LogFileAsyncStore(LogKeeperPtr keeperPtr);
~LogFileAsyncStore();
void Append(const std::string& s);
};
typedef std::shared_ptr<LogFileAsyncStore> LogFileAsyncStorePtr;
}
}
#endif//__LOG_STORE_HPP__

View File

@@ -0,0 +1,88 @@
#ifndef __LOG_WRITER_HPP__
#define __LOG_WRITER_HPP__
#include <unitree/common/log/log_buffer.hpp>
#include <unitree/common/log/log_keeper.hpp>
namespace unitree
{
namespace common
{
class LogWriter
{
public:
virtual void Write(const std::string& s) = 0;
};
typedef std::shared_ptr<LogWriter> LogWriterPtr;
class LogDirectWriter : public LogWriter
{
public:
explicit LogDirectWriter(int32_t fd);
virtual ~LogDirectWriter();
void Write(const std::string& s);
private:
Mutex mLock;
int32_t mFd;
};
class LogStdoutWriter : public LogDirectWriter
{
public:
explicit LogStdoutWriter() :
LogDirectWriter(UT_FD_STDOUT)
{}
~LogStdoutWriter()
{}
};
class LogStderrWriter : public LogDirectWriter
{
public:
explicit LogStderrWriter() :
LogDirectWriter(UT_FD_STDERR)
{}
~LogStderrWriter()
{}
};
class LogBufferWriter : public LogWriter
{
public:
explicit LogBufferWriter(LogKeeperPtr keeperPtr);
~LogBufferWriter();
void Write(const std::string& s);
private:
LogBufferPtr mBufferPtr;
LogKeeperPtr mKeeperPtr;
Mutex mLock;
};
class LogAsyncBufferWriter : public LogWriter
{
public:
explicit LogAsyncBufferWriter(LogKeeperPtr keeperPtr);
~LogAsyncBufferWriter();
void Write(const std::string& s);
private:
void DoWrite();
private:
volatile bool mRotate;
std::string mTempBuf;
LogBufferPtr mBufferPtr;
LogKeeperPtr mKeeperPtr;
ThreadPtr mThreadPtr;
Mutex mLock;
};
}
}
#endif//__LOG_WRITER_HPP__