pstrace: initial commit

This commit is contained in:
2020-01-04 14:28:08 +04:00
parent 82c73720b1
commit 1f3a6237ac
13 changed files with 1391 additions and 0 deletions
+55
View File
@@ -0,0 +1,55 @@
#include "log.h"
#include <time.h>
#define LOG_BUFF_SIZE (1024*1024)
const char * const SC_LogBase::mSeverityStrings[] = {
" [DEBUG] : ",
" [INFO] : ",
" [WARNING]: ",
" [ERROR] : "
};
char SC_LogBase::mString[LOG_BUFF_SIZE];
uint32_t SC_LogBase::mStringSize = sizeof(SC_LogBase::mString);
SC_LogBase::SC_LogBase(uint32_t id, const char* source) : mID(id)
{
mStringLen = 0;
mCurrentSeverity = SEVERITY_DEBUG;
if(source) {
mSource = source;
}
}
SC_LogBase::~SC_LogBase()
{
Close();
}
void SC_LogBase::FormatString(const char* fmt, va_list args)
{
mStringLen += vsnprintf(mString + mStringLen, mStringSize - mStringLen, fmt, args);
}
void SC_LogBase::FormatPrefix(SC_LogSeverity severity)
{
time_t rawTime;
struct tm * timeinfo;
time(&rawTime);
timeinfo = gmtime(&rawTime);
mStringLen += strftime(mString, sizeof(mString), "%d-%m-%Y %H:%M:%S", timeinfo);
strncpy(mString + mStringLen, mSeverityStrings[(int)severity], strlen(mSeverityStrings[(int)severity]));
mStringLen += strlen(mSeverityStrings[(int)severity]);
}
void SC_LogBase::FormatPostfix()
{
if((mStringLen + 2) < mStringSize) {
mString[mStringLen++] = '\n';
mString[mStringLen++] = 0;
}
}
+148
View File
@@ -0,0 +1,148 @@
#ifndef LOG_H_
#define LOG_H_
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include "mutex.h"
using std::string;
// Log message severity
enum SC_LogSeverity {
SEVERITY_DEBUG = 0,
SEVERITY_INFO,
SEVERITY_WARNING,
SEVERITY_ERROR,
//----------------
SEVERITY_MAX
};
class SC_LogBase
{
protected:
SC_LogBase(uint32_t id, const char* source);
public:
virtual ~SC_LogBase();
//variable argument number logging
inline virtual void Log(SC_LogSeverity severity, const char* fmt, ...)
{
if (severity < mCurrentSeverity)
return;
mLock.Lock();
if(!IsOpened())
{
//print header if present
if(Open() && mHeader.size())
{
SendMessage((char*)mHeader.c_str());
}
}
mStringLen = 0;
FormatPrefix(severity);
va_list args;
va_start(args, fmt);
FormatString(fmt, args);
va_end(args);
FormatPostfix();
SendMessage(mString);
mLock.Unlock();
}
//open source for send messages
virtual bool Open() = 0;
//close source
virtual void Close(){}
//whether source already opened
virtual bool IsOpened() = 0;
//returns source path
string& GetSource()
{
return mSource;
}
//assign source path
inline void SetSource(const char* source)
{
Close();
mSource = source;
}
void SetSource(string& source)
{
SetSource(source.c_str());
}
//assign Log header
inline void SetHeader(const char* header)
{
mHeader = string(header) + '\n';
}
//return Log ID
inline uint32_t GetID() const
{
return mID;
}
//set Log ID
inline void SetID(uint32_t id)
{
mID = id;
}
//return current severity threshold applied to the messages
inline SC_LogSeverity GetCurrentSeverity() const
{
return mCurrentSeverity;
}
//set severity threshold to be applied to the messages
inline void SetCurrentSeverity(SC_LogSeverity severity)
{
mCurrentSeverity = severity;
}
protected:
//actually sends message to the source
virtual void SendMessage(const char* msg) = 0;
//format message prefix
virtual void FormatPrefix(SC_LogSeverity severity);
//format message body
virtual void FormatString(const char* fmt, va_list args);
//format message postfix
virtual void FormatPostfix();
protected:
uint32_t mID; //log ID, associated with source
string mSource; //source for store log messages (file, IP address or sometsing else)
string mHeader; //header of the Log
static char mString[]; //formatted string to be printed
static uint32_t mStringSize; //size of the string buffer
uint32_t mStringLen; //length of the string
SC_LogSeverity mCurrentSeverity; //maximum severity value to be logged
static const char * const mSeverityStrings[];
SC_Mutex mLock;
};
#endif /* LOG_H_ */
+29
View File
@@ -0,0 +1,29 @@
#ifndef SC_LOGCONSOLE_H
#define SC_LOGCONSOLE_H
#include "log.h"
class SC_LogConsole : public SC_LogBase {
public:
SC_LogConsole(uint32_t id, const char* source) : SC_LogBase(id, source) { }
//open file for writing
inline virtual bool Open() {
return true;
}
//close file opened
inline virtual void Close() { }
//whether file already opened or not
inline virtual bool IsOpened() {
return true;
}
protected:
inline virtual void SendMessage(const char* msg) {
fprintf(stderr, "%s", msg);
}
};
#endif // SC_LOGCONSOLE_H
+108
View File
@@ -0,0 +1,108 @@
#ifndef LOG_FILE_H_
#define LOG_FILE_H_
//framework
#include "log.h"
#include <stdio.h>
#include <sys/types.h>
using std::string;
//=============================================================================================
// SWI Logger to file logging class declaration
//=============================================================================================
class SC_LogFile : public SC_LogBase
{
public:
SC_LogFile(uint32_t id, const char* source) : SC_LogBase(id, source)
{
mFile = 0;
//2GByte by default
mMaxFileSize = 0x0000000080000000ULL;
mBytesCount = 0;
}
//open file for writing
inline virtual bool Open()
{
if(!mFile) {
// extract log and path names
uint32_t namelen = mSource.size();
if(namelen) {
const char* ptr = strrchr(mSource.c_str(), '/');
uint32_t pathlen = 0;
if(ptr) {
pathlen = ptr - mSource.c_str() + 1;
namelen -= pathlen + 1;
}
mLogPath.append(mSource.c_str(), pathlen);
mLogName.append((mSource.c_str() + pathlen), namelen);
//open the file finally
mFile = fopen(mSource.c_str(), "a+");
}
}
return (mFile != 0);
}
//close file opened
inline virtual void Close()
{
if(mFile) {
fclose(mFile);
}
mFile = 0;
mBytesCount = 0;
}
//whether file already opened or not
inline virtual bool IsOpened()
{
return (mFile != 0);
}
inline void SetMaxFileSize(uint64_t size)
{
mMaxFileSize = size;
}
inline uint64_t GetMaxFileSize()
{
return mMaxFileSize;
}
inline FILE* GetLogFD()
{
return mFile;
}
protected:
inline virtual void SendMessage(const char* msg)
{
if(mFile && mBytesCount < mMaxFileSize) {
if(fputs(msg, mFile) >= 0) {
fflush(mFile);
mBytesCount += mStringLen;
} else {
fclose(mFile);
mFile = 0;
}
}
}
protected:
string mLogPath; //path to the log file
string mLogName; //name of the log file
FILE* mFile; //log file descriptor
uint64_t mMaxFileSize; //maximum number bytes allowed to be written to the file
uint64_t mBytesCount; //number of bytes actually written
};
#endif /* LOG_FILE_H_ */